|
| 1 | +"""Command wrappers apply safety checks before starting Git.""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from unittest import mock |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from git import Actor, Git, GitCommandError, Repo |
| 9 | +from git.exc import UnsafeOptionError, UnsafeProtocolError |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.parametrize("allow_unsafe_options", [False, True]) |
| 13 | +@pytest.mark.parametrize( |
| 14 | + "args, kwargs", |
| 15 | + [ |
| 16 | + (("ext::helper",), {}), |
| 17 | + (("ext::",), {}), |
| 18 | + (("custom::address",), {}), |
| 19 | + (("1custom+v2.test-name::address",), {}), |
| 20 | + (("::address",), {}), |
| 21 | + (("custom::\naddress",), {}), |
| 22 | + ((["--refs", ("ext::helper",)],), {}), |
| 23 | + ((None, "--", "ext::helper", "HEAD"), {}), |
| 24 | + ((Path("ext::helper"),), {}), |
| 25 | + ((), {"q": "ext::helper"}), |
| 26 | + ((), {"h": ["ext::helper"]}), |
| 27 | + ((), {"-": "ext::helper"}), |
| 28 | + ((), {"o": [True, "ext::helper"]}), |
| 29 | + (("--server-option",), {"o": "ext::helper", "insert_kwargs_after": "--server-option"}), |
| 30 | + ], |
| 31 | +) |
| 32 | +def test_ls_remote_rejects_unsafe_protocols(args, kwargs, allow_unsafe_options): |
| 33 | + with mock.patch.object(Git, "execute", side_effect=AssertionError("Git must not run")) as run: |
| 34 | + with pytest.raises(UnsafeProtocolError): |
| 35 | + Git().ls_remote(*args, allow_unsafe_options=allow_unsafe_options, **kwargs) |
| 36 | + run.assert_not_called() |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.parametrize( |
| 40 | + "args, kwargs", |
| 41 | + [ |
| 42 | + ((), {}), |
| 43 | + ((None,), {}), |
| 44 | + (("origin", "HEAD"), {"h": True}), |
| 45 | + (("https://example.com/repo.git",), {}), |
| 46 | + (("git@example.com:repo.git",), {}), |
| 47 | + (("origin",), {"o": "key=value"}), |
| 48 | + (("origin",), {"server_option": "key::value"}), |
| 49 | + ], |
| 50 | +) |
| 51 | +def test_ls_remote_preserves_safe_arguments(args, kwargs): |
| 52 | + with mock.patch.object(Git, "execute", return_value="refs") as run: |
| 53 | + assert Git().ls_remote(*args, **kwargs) == "refs" |
| 54 | + run.assert_called_once() |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.parametrize( |
| 58 | + "url", |
| 59 | + [ |
| 60 | + "https://[::1]/repo.git", |
| 61 | + "ssh://git@[2001:db8::1]/repo.git", |
| 62 | + "https://example.com/repo::name", |
| 63 | + "git@example.com:repo::name", |
| 64 | + "./repo::name", |
| 65 | + ], |
| 66 | +) |
| 67 | +def test_ls_remote_preserves_double_colons_outside_helper_selector(url): |
| 68 | + assert Git().ls_remote(url, get_url=True) == url |
| 69 | + |
| 70 | + |
| 71 | +def test_ls_remote_unsafe_opt_ins_are_independent(): |
| 72 | + with mock.patch.object(Git, "execute", return_value="refs") as run: |
| 73 | + with pytest.raises(UnsafeOptionError): |
| 74 | + Git().ls_remote("origin", upload_pack="helper", allow_unsafe_protocols=True) |
| 75 | + run.assert_not_called() |
| 76 | + assert ( |
| 77 | + Git().ls_remote("ext::helper", upload_pack="helper", allow_unsafe_protocols=True, allow_unsafe_options=True) |
| 78 | + == "refs" |
| 79 | + ) |
| 80 | + run.assert_called_once_with([Git.GIT_PYTHON_GIT_EXECUTABLE, "ls-remote", "--upload-pack=helper", "ext::helper"]) |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.parametrize("allow_unsafe_options", [False, True]) |
| 84 | +@pytest.mark.parametrize( |
| 85 | + "revs, kwargs", |
| 86 | + [ |
| 87 | + (("HEAD", "--output=unused"), {}), |
| 88 | + (("HEAD", ["--out=unused"]), {}), |
| 89 | + (("HEAD", "-ounused"), {}), |
| 90 | + (("HEAD", "HEAD"), {"output": "unused"}), |
| 91 | + (("HEAD", "HEAD"), {"out": "unused"}), |
| 92 | + (("HEAD", "HEAD"), {"o": "unused"}), |
| 93 | + ], |
| 94 | +) |
| 95 | +def test_merge_base_checks_unsafe_options(tmp_path, revs, kwargs, allow_unsafe_options): |
| 96 | + repo = Repo.init(tmp_path) |
| 97 | + with mock.patch.object(Git, "execute", return_value="") as run: |
| 98 | + if allow_unsafe_options: |
| 99 | + assert repo.merge_base(*revs, allow_unsafe_options=True, **kwargs) == [] |
| 100 | + run.assert_called_once() |
| 101 | + assert "--allow-unsafe-options" not in run.call_args[0][0] |
| 102 | + else: |
| 103 | + with pytest.raises(UnsafeOptionError): |
| 104 | + repo.merge_base(*revs, **kwargs) |
| 105 | + run.assert_not_called() |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.parametrize("status", [-9, 2, 128, 129]) |
| 109 | +def test_merge_base_propagates_errors(tmp_path, status): |
| 110 | + repo = Repo.init(tmp_path) |
| 111 | + error = GitCommandError("git merge-base", status) |
| 112 | + with mock.patch.object(Git, "execute", side_effect=error): |
| 113 | + with pytest.raises(GitCommandError) as raised: |
| 114 | + repo.merge_base("HEAD", "HEAD") |
| 115 | + assert raised.value is error |
| 116 | + |
| 117 | + |
| 118 | +def test_merge_base_distinguishes_unrelated_history_from_invalid_options(tmp_path): |
| 119 | + repo = Repo.init(tmp_path) |
| 120 | + actor = Actor("Test", "test@example.com") |
| 121 | + first = repo.index.commit("first", author=actor, committer=actor) |
| 122 | + second = repo.index.commit("second", parent_commits=[], head=False, author=actor, committer=actor) |
| 123 | + assert repo.merge_base(first, first) == [first] |
| 124 | + assert repo.merge_base(first, second) == [] |
| 125 | + with pytest.raises(GitCommandError) as raised: |
| 126 | + repo.merge_base(first, second, invalid_option=True) |
| 127 | + assert raised.value.status == 129 |
| 128 | + |
| 129 | + |
| 130 | +@pytest.mark.parametrize("allow_unsafe_options", [False, True]) |
| 131 | +@pytest.mark.parametrize("option", ["pathspec_from_file", "pathspec-from-file", "pathspec_from"]) |
| 132 | +@pytest.mark.parametrize("dry_run", [False, True]) |
| 133 | +def test_move_checks_unsafe_options(tmp_path, option, dry_run, allow_unsafe_options): |
| 134 | + repo = Repo.init(tmp_path) |
| 135 | + with mock.patch.object(Git, "execute", return_value="Renaming source to destination\n") as run: |
| 136 | + kwargs = {option: "unused", "dry_run": dry_run} |
| 137 | + if allow_unsafe_options: |
| 138 | + assert repo.index.move(["source", "destination"], True, allow_unsafe_options=True, **kwargs) == [ |
| 139 | + ("source", "destination") |
| 140 | + ] |
| 141 | + assert run.call_count == (1 if dry_run else 2) |
| 142 | + for call in run.call_args_list: |
| 143 | + argv = call[0][0] |
| 144 | + assert "-k" in argv |
| 145 | + assert f"--{option.replace('_', '-')}=unused" in argv |
| 146 | + assert "--allow-unsafe-options" not in argv |
| 147 | + assert argv[-3:] == ["--", "source", "destination"] |
| 148 | + else: |
| 149 | + with pytest.raises(UnsafeOptionError): |
| 150 | + repo.index.move(["source", "destination"], **kwargs) |
| 151 | + run.assert_not_called() |
0 commit comments