diff --git a/.github/workflows/alpine-test.yml b/.github/workflows/alpine-test.yml index 189826c0f..920f93066 100644 --- a/.github/workflows/alpine-test.yml +++ b/.github/workflows/alpine-test.yml @@ -1,5 +1,9 @@ name: test-alpine +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + on: push: branches: [main] diff --git a/.github/workflows/backend-benchmark.yml b/.github/workflows/backend-benchmark.yml index 140aa27cb..85503235a 100644 --- a/.github/workflows/backend-benchmark.yml +++ b/.github/workflows/backend-benchmark.yml @@ -1,5 +1,9 @@ name: Backend benchmark +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + on: push: branches: [main] diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 143c2de52..25c207db1 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -11,6 +11,10 @@ # name: "CodeQL" +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + on: push: branches: [main] diff --git a/.github/workflows/cygwin-test.yml b/.github/workflows/cygwin-test.yml index c55b73dd8..d5955958d 100644 --- a/.github/workflows/cygwin-test.yml +++ b/.github/workflows/cygwin-test.yml @@ -1,5 +1,9 @@ name: import-cygwin +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + on: push: branches: [main] diff --git a/.github/workflows/dependencies.yml b/.github/workflows/dependencies.yml index b58704fe9..e31a633b1 100644 --- a/.github/workflows/dependencies.yml +++ b/.github/workflows/dependencies.yml @@ -1,5 +1,9 @@ name: Dependency packages +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + on: push: branches: [main] diff --git a/.github/workflows/downstream.yml b/.github/workflows/downstream.yml index 85cf0062f..673122afe 100644 --- a/.github/workflows/downstream.yml +++ b/.github/workflows/downstream.yml @@ -1,5 +1,9 @@ name: Downstream compatibility +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + on: push: branches: [main] diff --git a/.github/workflows/git-cli.yml b/.github/workflows/git-cli.yml index b104fd4f4..bf12d1fa9 100644 --- a/.github/workflows/git-cli.yml +++ b/.github/workflows/git-cli.yml @@ -1,5 +1,9 @@ name: Minimum Git CLI +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + on: push: branches: [main] diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 496f4b093..ab2171ba3 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -1,5 +1,9 @@ name: Lint +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + on: push: branches: [main] diff --git a/.github/workflows/pythonpackage.yml b/.github/workflows/pythonpackage.yml index e8ef05cd4..842695409 100644 --- a/.github/workflows/pythonpackage.yml +++ b/.github/workflows/pythonpackage.yml @@ -3,6 +3,10 @@ name: Python package +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + on: push: branches: [main] diff --git a/.github/workflows/zizmor.yml b/.github/workflows/zizmor.yml index 201d28ab0..b38d834cb 100644 --- a/.github/workflows/zizmor.yml +++ b/.github/workflows/zizmor.yml @@ -1,5 +1,9 @@ name: zizmor +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + on: push: branches: [main] diff --git a/SECURITY.md b/SECURITY.md index e8e777aa0..2d1150a65 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -37,6 +37,23 @@ For an untrusted repository directory, Git recommends obtaining a clean copy wit unprivileged user for additional isolation. See [Git's `git-upload-pack` security guidance](https://github.com/git/git/blob/master/Documentation/git-upload-pack.adoc#security). +`git.cmd.Git`, also exposed as `repo.git`, is a low-level command wrapper: it +runs the Git command with the arguments and options the caller supplies. It does +what it is told, including file reads, writes, or command execution requested +through those options. These explicitly requested effects are intended command +behavior, not unwanted side effects. For example, +`repo.git.checkout(pathspec_from_file=path)` asks Git to read `path` as pathspec +input, and Git's diagnostics may include that input. Applications exposing this +interface to untrusted callers must restrict commands, arguments, and options +and control which output and errors they disclose. + +Higher-level methods, such as `Head.checkout()` and `Repo.clone_from()`, provide +specific operations and apply protections against options or operands that +introduce unwanted side effects beyond those operations. Where supported, +callers can explicitly opt into unsafe behavior. These protections do not +restrict direct calls through the command wrapper; bypasses of higher-level +protections remain reportable. + Violations of these trust boundaries or bypasses of Git's intended protections remain reportable, including cases where Git itself is affected. diff --git a/doc/gix-backend.md b/doc/gix-backend.md index 8a5b2ec71..909b8c707 100644 --- a/doc/gix-backend.md +++ b/doc/gix-backend.md @@ -367,13 +367,13 @@ and predate the current fallback; they do not establish native coverage. | Standalone discovery helpers | Native storage directories and gitfiles reopened at their canonical native Git directory | Rejected candidates and the same layout/environment guards as `Repo` opening; GIX-14 | | `Repo.rev_parse`, `rev_parse` metadata queries | Revision lookup, common directory, object format, bare flag, worktree root, fixed `modules`/`COMMIT_EDITMSG` locations | Repository-format validation, other metadata paths, symlinked metadata leaves, message searches and describe forms; GIX-1/14/17/22 | | Revision path/mode metadata | Native revision specification, including tree paths and index stages | Custom/sparse indexes, unsupported revision grammar or path normalization | -| Tree enumeration, `ls_tree` | Native tree entries with modes, names, and object IDs | Other command options | +| Tree enumeration, `ls_tree` | Native tree entries with modes, names, and object IDs | Other command options; entries with modes Git canonicalizes or that do not round-trip through native tree encoding; GIX-36 | | Reference object reads | One exact native lookup for direct, symbolic and absent references | Partial names and native decoding errors | | Reference-name validation | Native full-name validation without opening a repository | Standalone names outside the native grammar; GIX-20 | | Managed `symbolic_ref` | Nonrecursive target lookup | Mutation, command-level missing-reference diagnostics | | Reference enumeration, `for_each_ref` | Sorted reference names and literal prefixes, preserving symbolic aliases | Dangling symbolic refs, root refs, glob patterns, other formats/options; GIX-25 | | Managed `config --get KEY` | Merged repository config snapshot | Files/streams, enumeration, mutation; GIX-12 | -| Submodule enumeration and cached fields | Dedicated native `.gitmodules` parser over the existing worktree/blob source, with raw path/URL/branch values | Non-UTF-8 subsection names, other/duplicate sections, ambiguous brackets, missing/implicit fields and parser errors; GIX-12 | +| Submodule enumeration and cached fields | Dedicated native `.gitmodules` parser over the existing worktree/blob source, with raw path/URL/branch values | Non-UTF-8 subsection names, other/duplicate sections, ambiguous brackets, missing/implicit fields and parser errors; GIX-12. Lines other than comments, bare headers and single settings; GIX-35 | | Commit hooks | No bound native lookup/execution API | All hooks, including absent hooks; GIX-23 | | Worktree inventory, `worktree list` | Main and linked worktree metadata, including bare main repositories and locks | Prunable entries; GIX-14 | | `Repo.merge_base`, `Repo.is_ancestor` | Native graph queries for two revisions | Octopus/fork-point and other options | @@ -990,6 +990,8 @@ identify the historical fixture where needed. | GIX-32 | Missing capability (open); no demonstrated Git compatibility mismatch | Windows CI on GixPython 0.3.0, Git 2.56.0.windows.2 and CPython 3.12.10 enumerates the non-UTF-8 packed names in `legacy-refs.sh`, but exact byte-name lookup selects `Reference.read: CLI: native read diagnostics`; the no-CLI guard fails for SHA-1 and SHA-256. POSIX lookup succeeds. Git's Windows CLI likewise cannot address those original bytes through UTF-16 arguments, so there is no demonstrated equivalent Windows CLI operand. Git enumeration does preserve both names. | Provide native Windows packed-reference lookup for byte names without first converting the name to a loose-reference filesystem path. Keep `reference_info()`'s existing diagnostic fallback; `test_non_utf8_tag_names_preserve_bytes` checks it on Windows. Resolution coverage and native claims remain POSIX-only. The upstream lookup source attempts loose-path conversion before consulting packed refs; CI demonstrates the fallback, but does not capture the underlying Gix error text. | | GIX-33 | Bug (open); missing exact-name mode | With Git 2.54.0 and GixPython 0.3.0, `exact-refs.sh sha1 files symbolic` makes `refs/tags/REBASE_HEAD` a symbolic ref to another tag while the root `REBASE_HEAD` is absent. `Repository.try_find_reference("REBASE_HEAD")` returns that tag; native `_symbolic_ref` formerly returned its target, whereas `git symbolic-ref --quiet --no-recurse -- REBASE_HEAD` exits 1. GitPython's root-reference object read consequently returned a commit through Gix and raised `ValueError` through CLI. SHA-256 reproduces the same mismatch. | Expose an exact-name lookup mode, including root references, whose absence and error behavior matches Git. `_symbolic_ref()` compares the native reference's name with the requested bytes and selects CLI for a lookalike. `test_missing_root_reference_does_not_follow_symbolic_lookalike_tag` verifies parity and forced CLI fallback. This adapter guard does not close the upstream compatibility bug. | | GIX-34 | Bug (open) | With Git 2.54.0 and GixPython 0.3.0, an author, committer or tagger line with fields after its time zone, such as `A 1112591163 +0200 1700000000 -0500`, decodes through `Commit.author()`/`committer()` and `Tag.tagger()` with the correct seconds but an offset of 0. Git reads only the first date and time zone after the last `>` (`split_ident_line`) and reports `1112591163 +0200` through `git show --format=%ad --date=raw` and `%(taggerdate:raw)`. `git fsck` reports `badTimezone` for such objects, but the default `transfer.fsckObjects=false` accepts them. The `trailing-date` read-only `commit.sh` fixture in `test_deserialization_reads_date_after_email` reproduces both object kinds. | Report the time zone Git reads for identity lines with trailing fields, or reject them consistently. `object_metadata()` selects CLI when the decoded signature does not reproduce the end of its raw header line. This adapter guard does not close the upstream compatibility bug. | +| GIX-35 | Bug (open) | With Git 2.54.0 and GixPython 0.3.0, `ModulesFile.from_bytes()` accepts `.gitmodules` lines that Git rejects with `bad config line`, so `git submodule update --init` stops while `git fsck` reports only `gitmodulesParse`. A line holding more than one name is read as several settings: after `url = ../good`, `x url = ../evil` makes `url()` return `../evil` with `submodule..x` true, and `[submodule "module"] x url = ../evil` does the same on the header line. `br#anch = main` is read as an implicit `br` followed by a comment. The `submodule-rejected-lines.sh` read-only fixture in `test_submodule_configuration_rejects_lines_git_rejects` reproduces all three. | Reject the lines Git rejects, at least in a Git-strict parsing mode, with a typed error. `submodule_config()` selects CLI unless every line is blank, a comment, a bare submodule header or a single `key` or `key = value` setting, so Git's parser reports the error. This adapter guard does not close the upstream compatibility bug. | +| GIX-36 | Bug (open) | With Git 2.54.0 and GixPython 0.3.0, `Tree.iter()` reports tree entry modes as stored, while Git canonicalizes them when it reads a tree (`canon_mode`): `git ls-tree` and checkout use `100755` for `100700`, `120000` for `120777` and `160000` for every mode whose type is not a regular file, symbolic link or directory. `TreeEntry.mode()` returned `0o100700` and `0o120777`, so `Blob.mode` did not match `Blob.executable_mode` or `Blob.link_mode` for entries Git checks out as an executable or a symbolic link. A `140000` entry, which Git reads as a gitlink, is reported as a tree with mode `0o40000`, so GitPython listed the contents of an object Git never checks out; other noncanonical gitlink modes made tree iteration raise `TypeError`. `git fsck` reports these trees only as `badFilemode` warnings, so default transfers accept them. The `tree-modes.sh` read-only fixture in `test_tree_entry_modes_match_git` reproduces all three for SHA-1 and SHA-256. | Report the modes Git reads, at least in a Git-compatible mode, or expose the raw mode bytes. `_ls_tree()` selects CLI unless every native mode is one Git reports and the tree's bytes re-encode to the same object ID through the in-memory object database, so Git lists the entries. This adapter guard does not close the upstream compatibility bug. | GIX-14 also covers configured worktree symlink resolution. On macOS with GixPython 0.3.0, CPython 3.12.14 and Git 2.54.0, set a submodule's diff --git a/doc/source/changes.rst b/doc/source/changes.rst index 487ac8916..b0ba5e2a3 100644 --- a/doc/source/changes.rst +++ b/doc/source/changes.rst @@ -13,6 +13,13 @@ contain 20 binary bytes or 40 hexadecimal characters. Legacy class-level ``NULL_BIN_SHA`` and ``NULL_HEX_SHA`` constants retain their SHA-1 values; do not use their width to interpret repository object IDs. +Security fixes +-------------- + +- https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-f8wj-prgh-h9j7 +- https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-j3hg-fv33-37rw +- https://github.com/gitpython-developers/GitPython/security/advisories/GHSA-j8pf-7m8v-xx38 + Git executable and safety ------------------------- @@ -23,6 +30,9 @@ Git executable and safety option/protocol checks remain, and operands and stdin records receive additional validation. NUL-delimited ``cat-file`` requests prevent newline injection. Previously accepted option-like revision/ref arguments can now be rejected. + ``Head.checkout()`` rejects every branch name starting with a dash, including + when ``allow_unsafe_options=True``. This setting permits explicit keyword + options while branch names remain operands. * New plumbing calls suppress implicit hooks, filesystem monitors, automatic maintenance, and lazy network fetches. Index staging continues to store raw content rather than introduce clean filters. Diffs disable external diff and diff --git a/git/objects/submodule/base.py b/git/objects/submodule/base.py index 0c24dbc87..840f32d50 100644 --- a/git/objects/submodule/base.py +++ b/git/objects/submodule/base.py @@ -630,15 +630,15 @@ def add( :param no_checkout: If ``True``, and if the repository has to be cloned manually, no checkout - will be performed. + will be performed. Must be a boolean. Retained metadata can instead be reconnected without checking out files or changing refs. Its URL and any explicit branch must match; ``depth`` and ``clone_multi_options`` require a new clone and are rejected here. :param depth: - Create a shallow clone with a history truncated to the specified number of - commits. + Create a shallow clone with a history truncated to the specified positive + number of commits. :param env: Optional dictionary containing the desired environment variables. @@ -666,6 +666,10 @@ def add( Works atomically, such that no change will be done if, for example, the repository update fails. """ + if not isinstance(no_checkout, bool): + raise TypeError("no_checkout must be a bool") + if depth is not None and (not isinstance(depth, int) or isinstance(depth, bool) or depth <= 0): + raise ValueError("depth must be a positive integer") if repo.bare: raise InvalidGitRepositoryError("Cannot add submodules to bare repositories") # END handle bare repos @@ -725,8 +729,6 @@ def add( if branch is not None: args.extend(("--branch", Git._check_operand(branch, "branch"))) if depth is not None: - if not isinstance(depth, int) or isinstance(depth, bool) or depth <= 0: - raise ValueError("depth must be a positive integer") args.extend(("--depth", str(depth))) args.extend(("--", url, str(path))) # An explicitly requested local submodule is also supported by clone(). @@ -819,11 +821,8 @@ def add( kwargs["b"] = br.name # END setup checkout-branch - if depth: - if isinstance(depth, int): - kwargs["depth"] = depth - else: - raise ValueError("depth should be an integer") + if depth is not None: + kwargs["depth"] = depth if clone_multi_options: kwargs["multi_options"] = clone_multi_options diff --git a/git/refs/head.py b/git/refs/head.py index a49e4a9b8..8c6db8eca 100644 --- a/git/refs/head.py +++ b/git/refs/head.py @@ -279,6 +279,7 @@ def checkout( :param allow_unsafe_options: Allow unsafe options such as ``--pathspec-from-file`` to be passed to :manpage:`git-checkout(1)`. + Branch names are validated regardless of this setting. :param kwargs: Additional keyword arguments to be passed to git checkout, e.g. @@ -290,11 +291,16 @@ def checkout( If there is no active branch, as the HEAD is now detached, the HEAD reference will be returned instead. + :raise git.exc.UnsafeOptionError: + If the branch name starts with a dash or contains NUL, CR, or LF. + :note: By default it is only allowed to checkout heads - everything else will leave the HEAD detached which is allowed and possible, but remains a special state that some tools might not be able to handle. """ + if not isinstance(force, bool): + raise TypeError("force must be a bool") if not allow_unsafe_options: Git.check_unsafe_options( options=Git._option_candidates([self], kwargs), diff --git a/git/repo/base.py b/git/repo/base.py index 21eb2e118..a7e7f068a 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -35,6 +35,7 @@ ) from git.index import IndexFile from git.objects import Submodule, RootModule, Commit +from git.objects.util import utctz_to_altz from git.refs import HEAD, Head, Reference, SymbolicReference, TagReference from git.remote import Remote, _T_RemoteName, add_progress, to_progress_instance from git.util import ( @@ -165,6 +166,9 @@ class Repo: "--separate-git-dir", # Fetches from an additional caller-controlled URI: "--bundle-uri", + # Reads objects from a caller-controlled local repository: + "--reference", + "--reference-if-able", ] """Options to :manpage:`git-clone(1)` that permit unsafe command execution or I/O. @@ -1363,11 +1367,13 @@ def blame_incremental( safe_decode(props[b"author-mail"].lstrip(b"<").rstrip(b">")), ), authored_date=int(props[b"author-time"]), + author_tz_offset=utctz_to_altz(props[b"author-tz"].decode("ascii")), committer=Actor( safe_decode(props[b"committer"]), safe_decode(props[b"committer-mail"].lstrip(b"<").rstrip(b">")), ), committed_date=int(props[b"committer-time"]), + committer_tz_offset=utctz_to_altz(props[b"committer-tz"].decode("ascii")), ) commits[hexsha] = c else: @@ -1452,9 +1458,11 @@ class InfoTD(TypedDict, total=False): author: str author_email: str author_date: int + author_tz_offset: int committer: str committer_email: str committer_date: int + committer_tz_offset: int info: InfoTD = {} @@ -1504,13 +1512,15 @@ class InfoTD(TypedDict, total=False): # committer Tom Preston-Werner # committer-mail # committer-time 1192271832 - # committer-tz -0700 - IGNORED BY US + # committer-tz -0700 role = m.group(0) if role == "author": if firstpart.endswith("-mail"): info["author_email"] = parts[-1] elif firstpart.endswith("-time"): info["author_date"] = int(parts[-1]) + elif firstpart.endswith("-tz"): + info["author_tz_offset"] = utctz_to_altz(parts[-1]) elif role == firstpart: info["author"] = parts[-1] elif role == "committer": @@ -1518,6 +1528,8 @@ class InfoTD(TypedDict, total=False): info["committer_email"] = parts[-1] elif firstpart.endswith("-time"): info["committer_date"] = int(parts[-1]) + elif firstpart.endswith("-tz"): + info["committer_tz_offset"] = utctz_to_altz(parts[-1]) elif role == firstpart: info["committer"] = parts[-1] # END distinguish mail,time,name @@ -1540,8 +1552,10 @@ class InfoTD(TypedDict, total=False): hex_to_bin(sha), author=Actor._from_string(f"{info['author']} {info['author_email']}"), authored_date=info["author_date"], + author_tz_offset=info.get("author_tz_offset"), committer=Actor._from_string(f"{info['committer']} {info['committer_email']}"), committed_date=info["committer_date"], + committer_tz_offset=info.get("committer_tz_offset"), ) commits[sha] = c blames[-1][0] = c @@ -1550,9 +1564,15 @@ class InfoTD(TypedDict, total=False): if blames[-1][1] is not None: line: str | bytes if not is_binary: - if line_str and line_str[0] == "\t": - line_str = line_str[1:] - line = line_str + # Recover the exact line bytes. A porcelain content + # line is a leading tab, the blamed line's bytes, and + # git's terminating line feed, so rstrip() would drop + # the line's own trailing whitespace and the carriage + # return of a CRLF line. + content = line_bytes[1:] if line_bytes[:1] == b"\t" else line_bytes + if content.endswith(b"\n"): + content = content[:-1] + line = content.decode(defenc) else: line = line_bytes # NOTE: We are actually parsing lines out of binary diff --git a/test/test_clone.py b/test/test_clone.py index c973e3146..463e4b663 100644 --- a/test/test_clone.py +++ b/test/test_clone.py @@ -5,6 +5,7 @@ import os.path as osp import pathlib import re +import shlex import sys from pathlib import Path from unittest import mock, skip @@ -71,6 +72,29 @@ def test_clone_clears_ambient_source_storage_environment(tmp_path, clone_method) source.close() +@pytest.mark.parametrize("clone_method", ["clone", "clone_from"]) +@pytest.mark.parametrize("option", ["reference", "reference-if-able", "reference-if-ab"]) +@pytest.mark.parametrize("option_style", ["kwargs", "multi_equals", "multi_split"]) +def test_clone_reference_options_allowed(tmp_path, clone_method, option, option_style): + with Repo(tmp_path / "source") as source, Repo(tmp_path / "reference repo") as reference: + if option_style == "kwargs": + options = {option.replace("-", "_"): reference.git_dir} + else: + separator = "=" if option_style == "multi_equals" else " " + options = {"multi_options": [f"--{option}{separator}{shlex.quote(reference.git_dir)}"]} + destination = tmp_path / "clone" + options.update(allow_unsafe_options=True, no_local=True) + if clone_method == "clone": + cloned = source.clone(destination, **options) + else: + cloned = Repo.clone_from(source.git_dir, destination, **options) + with cloned: + assert cloned.head.commit == source.head.commit + assert cloned.head.commit.tree["file"].data_stream.read() == b"content\n" + alternates = Path(cloned.git_dir, "objects", "info", "alternates").read_text().strip() + assert Path(alternates).resolve() == Path(reference.git_dir, "objects").resolve() + + class TestClone(TestBase): @with_rw_directory(script="clone.sh", args=("nonempty",)) def test_checkout_in_non_empty_dir(self, rw_dir): @@ -184,6 +208,10 @@ def test_clone_unsafe_options(self, rw_repo): f"--template={tmp_dir}", f"--bundle-uri=file://{tmp_dir}", f"--separate-git-dir={tmp_dir / 'git-dir'}", + f"--reference={tmp_dir}", + f"--reference-if-able={tmp_dir}", + f"--reference-if-ab={tmp_dir}", + f"--ref={tmp_dir}", ] for unsafe_option in unsafe_options: with self.assertRaises(UnsafeOptionError): @@ -201,6 +229,10 @@ def test_clone_unsafe_options(self, rw_repo): {"template": tmp_dir}, {"bundle_uri": f"file://{tmp_dir}"}, {"separate_git_dir": tmp_dir / "git-dir"}, + {"reference": tmp_dir}, + {"reference_if_able": tmp_dir}, + {"reference_if_ab": tmp_dir}, + {"ref": tmp_dir}, ] for unsafe_option in unsafe_options: with self.assertRaises(UnsafeOptionError): @@ -316,6 +348,10 @@ def test_clone_from_unsafe_options(self, rw_repo): "-cprotocol.ext.allow=always", "-vcprotocol.ext.allow=always", f"--separate-git-dir={tmp_dir / 'git-dir'}", + f"--reference={tmp_dir}", + f"--reference-if-able={tmp_dir}", + f"--reference-if-ab={tmp_dir}", + f"--ref={tmp_dir}", ] for unsafe_option in unsafe_options: with self.assertRaises(UnsafeOptionError): @@ -329,6 +365,10 @@ def test_clone_from_unsafe_options(self, rw_repo): {"config": "protocol.ext.allow=always"}, {"c": "protocol.ext.allow=always"}, {"separate_git_dir": tmp_dir / "git-dir"}, + {"reference": tmp_dir}, + {"reference_if_able": tmp_dir}, + {"reference_if_ab": tmp_dir}, + {"ref": tmp_dir}, ] for unsafe_option in unsafe_options: with self.assertRaises(UnsafeOptionError): diff --git a/test/test_positional_args.py b/test/test_positional_args.py index 37999b9fb..a4a84ff84 100644 --- a/test/test_positional_args.py +++ b/test/test_positional_args.py @@ -73,7 +73,9 @@ def test_rename_head_cannot_select_current_branch(repo, tmp_path): @pytest.mark.parametrize("allow_unsafe_options", [False, True]) -@pytest.mark.parametrize("name", ["--force", "--detach", "--ours"]) +@pytest.mark.parametrize( + "name", ["-f", "-Bcheckout-target", "--orphan=checkout-target", "--force", "--detach", "--ours", "-", "--"] +) def test_checkout_rejects_option_shaped_branch_names(repo, tmp_path, name, allow_unsafe_options): branch = Head(repo, "refs/heads/" + name) with mock.patch.object(Git, "_call_process_safe", side_effect=AssertionError("Git must not run")) as run: diff --git a/test/test_refs.py b/test/test_refs.py index efeee1d04..e323bd0cc 100644 --- a/test/test_refs.py +++ b/test/test_refs.py @@ -43,6 +43,50 @@ from test.lib.fixtures import fixture_repo, read_only_fixture, writable_fixture +@pytest.mark.parametrize("force", [None, 0, 1, "", "false", "--detach", b"--detach", ["--detach"], ("--detach",)]) +@pytest.mark.parametrize("allow_unsafe_options", [False, True]) +@pytest.mark.parametrize("positional", [False, True]) +def test_head_checkout_requires_boolean_force(force, allow_unsafe_options, positional): + with fixture_repo("refs.sh", "rename-prefixed") as repo: + branch = repo.active_branch + for split_single_char_options in (False, True): + with patch.object(Git, "_call_process_safe", side_effect=AssertionError("Git must not run")) as run: + with pytest.raises(TypeError, match="force must be a bool"): + options = { + "allow_unsafe_options": allow_unsafe_options, + "split_single_char_options": split_single_char_options, + } + if positional: + branch.checkout(force, **options) + else: + branch.checkout(force=force, **options) + run.assert_not_called() + + +@pytest.mark.parametrize( + "branch_name", ["--pathspec-from-file=pathspecs", "-f", "--orphan=checkout-target", "-Bcheckout-target"] +) +def test_cloned_head_checkout_rejects_option_shaped_branch_names(branch_name): + with fixture_repo("refs.sh", "unsafe-branch", "sha1", branch_name, write=True, subdir="clone") as cloned: + branch = cloned.active_branch + assert branch.name == branch_name + commit = cloned.head.commit + path = Path(cloned.working_tree_dir) / "file.txt" + content = path.read_bytes() + status = cloned.git.status("--porcelain") + assert status.startswith(" M file.txt") + for allow_unsafe_options in (False, True): + with patch.object(Git, "execute", autospec=True, side_effect=Git.execute) as run: + with pytest.raises(UnsafeOptionError) as error: + branch.checkout(force=False, allow_unsafe_options=allow_unsafe_options) + run.assert_not_called() + assert "unmatched-private-content" not in str(error.value) + assert cloned.active_branch == branch + assert cloned.head.commit == commit + assert path.read_bytes() == content + assert cloned.git.status("--porcelain") == status + + @pytest.mark.parametrize("object_format", ["sha1", "sha256"]) def test_reference_names_preserve_unicode_line_separators(object_format): names = [ @@ -658,16 +702,6 @@ def test_head_checkout_rejects_pathspec_from_file(self, rw_repo): with self.assertRaises(UnsafeOptionError): branch.checkout() - def test_cloned_head_checkout_rejects_pathspec_from_file(self): - with fixture_repo("refs.sh", "unsafe-branch", write=True, subdir="clone") as cloned: - branch_name = "--pathspec-from-file=pathspecs" - assert cloned.active_branch.name == branch_name - for allow_unsafe_options in (False, True): - with self.assertRaises(UnsafeOptionError) as error: - cloned.active_branch.checkout(allow_unsafe_options=allow_unsafe_options) - assert "unmatched-private-content" not in str(error.exception) - assert cloned.active_branch.name == branch_name - @with_rw_repo("HEAD") def test_head_reset_rejects_pathspec_from_file(self, rw_repo): pathspecs = read_only_fixture("refs-input.sh") / "pathspecs" diff --git a/test/test_repo.py b/test/test_repo.py index 378208b30..4f1abcd8d 100644 --- a/test/test_repo.py +++ b/test/test_repo.py @@ -767,6 +767,36 @@ def test_blame_incremental_preserves_historical_paths(self): ) self.assertEqual(list(repo.blame("HEAD", "current.txt", incremental=True)), entries) + def test_blame_preserves_line_content(self): + """Blame returns each line's exact bytes, not a whitespace-stripped version.""" + with fixture_repo("blame-content.sh") as repo: + lines = [line for _commit, blamed in repo.blame("HEAD", "file.txt") for line in blamed] + self.assertEqual(lines, ["alpha ", "beta", "\t", "crlf\r", "gamma"]) + + def test_blame_preserves_timezones(self): + with fixture_repo("blame-content.sh") as repo: + for incremental in (False, True): + for rev in ("HEAD", None): + with self.subTest(incremental=incremental, rev=rev): + entry = next(iter(repo.blame(rev, "file.txt", incremental=incremental))) + commit = entry[0] + porcelain = repo.git.blame(rev, "--line-porcelain", "--", "file.txt") + offsets = dict( + line.split(" ", 1) + for line in porcelain.splitlines() + if line.startswith(("author-tz ", "committer-tz ")) + ) + with mock.patch.object( + Commit, "_set_cache_", side_effect=AssertionError("unexpected object read") + ): + if rev is None: + self.assertEqual(commit.hexsha, "0" * 40) + else: + self.assertEqual(commit.author_tz_offset, -19800) + self.assertEqual(commit.committer_tz_offset, 14400) + self.assertEqual(commit.authored_datetime.strftime("%z"), offsets["author-tz"]) + self.assertEqual(commit.committed_datetime.strftime("%z"), offsets["committer-tz"]) + @mock.patch.object(Git, "_call_process") def test_blame_complex_revision(self, git): git.return_value = fixture("blame_complex_revision") diff --git a/test/test_submodule.py b/test/test_submodule.py index 751235e21..f293e8742 100644 --- a/test/test_submodule.py +++ b/test/test_submodule.py @@ -33,6 +33,34 @@ from test.lib.fixtures import fixture_repo, read_only_fixture, writable_fixture +@pytest.mark.parametrize("no_checkout", [None, 0, 1, "", "false", "--bare", b"--bare", ["--bare"], ("--bare",)]) +@pytest.mark.parametrize("allow_unsafe_options", [False, True]) +@pytest.mark.parametrize("positional", [False, True]) +def test_submodule_add_requires_boolean_no_checkout(no_checkout, allow_unsafe_options, positional): + with fixture_repo("empty.sh", "sha1") as repo: + for add, args in ( + (Submodule.add, (repo, "module", "module", "source", None)), + (repo.create_submodule, ("module", "module", "source", None)), + ): + with mock.patch.object(Git, "execute", side_effect=AssertionError("Git must not run")) as run: + with pytest.raises(TypeError, match="no_checkout must be a bool"): + if positional: + add(*args, no_checkout, allow_unsafe_options=allow_unsafe_options) + else: + add(*args, no_checkout=no_checkout, allow_unsafe_options=allow_unsafe_options) + run.assert_not_called() + + +@pytest.mark.parametrize("depth", [True, False, 0, -1, 1.5, "1", [1]]) +@pytest.mark.parametrize("no_checkout", [False, True]) +def test_submodule_add_requires_positive_integer_depth(depth, no_checkout): + with fixture_repo("empty.sh", "sha1") as repo: + with mock.patch.object(Git, "execute", side_effect=AssertionError("Git must not run")) as run: + with pytest.raises(ValueError, match="depth must be a positive integer"): + repo.create_submodule("module", "module", "source", no_checkout=no_checkout, depth=depth) + run.assert_not_called() + + @pytest.mark.parametrize("scenario, name", [("ascii-name", "module"), ("byte-name", "module-\udcff")]) def test_submodule_configuration_preserves_non_utf8_bytes(scenario, name): with fixture_repo("submodule-bytes.sh", scenario) as repo: diff --git a/tests/fixtures/blame-content.sh b/tests/fixtures/blame-content.sh new file mode 100644 index 000000000..69e54b5c7 --- /dev/null +++ b/tests/fixtures/blame-content.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +set -eu +git init -q --initial-branch=main repo +cd repo +# Build the blob directly so trailing whitespace, a whitespace-only line, and a +# CRLF line survive fixture creation on every platform. +blob=$(printf 'alpha \nbeta\n\t\ncrlf\r\ngamma\n' | git hash-object -w --stdin) +tree=$(printf '100644 blob %s\tfile.txt\0' "$blob" | git mktree -z) +commit=$(GIT_AUTHOR_DATE='2020-01-01T12:00:00+0530' \ + GIT_COMMITTER_DATE='2020-01-01T13:00:00-0400' \ + git -c user.name=A -c user.email=a@example.com commit-tree "$tree" -m content) +git update-ref refs/heads/main "$commit" +git reset --hard -q HEAD +printf 'changed\n' > file.txt diff --git a/tests/fixtures/clone.sh b/tests/fixtures/clone.sh index e186cd8d8..598ef8997 100644 --- a/tests/fixtures/clone.sh +++ b/tests/fixtures/clone.sh @@ -8,6 +8,13 @@ case "$1" in git init --initial-branch=master source git -C source commit --allow-empty -m initial ;; + test_clone_reference_options_allowed) + git init --initial-branch=master source + printf 'content\n' > source/file + git -C source add file + git -C source commit -m initial + git clone --no-hardlinks source 'reference repo' + ;; source) git init --initial-branch=master repo ;; diff --git a/tests/fixtures/refs.sh b/tests/fixtures/refs.sh index cc13ed00d..969a89e1e 100644 --- a/tests/fixtures/refs.sh +++ b/tests/fixtures/refs.sh @@ -39,10 +39,12 @@ case "$1" in git rev-parse HEAD > ../outside-ref ;; unsafe-branch) - git update-ref 'refs/heads/--pathspec-from-file=pathspecs' HEAD - git symbolic-ref HEAD 'refs/heads/--pathspec-from-file=pathspecs' + branch_name="$3" + git update-ref "refs/heads/$branch_name" HEAD + git symbolic-ref HEAD "refs/heads/$branch_name" cd .. git clone repo clone printf 'unmatched-private-content\n' > clone/pathspecs + printf 'uncommitted\n' >> clone/file.txt ;; esac