|
| 1 | +"""Where `git_commit` takes the commit's author and committer from (#619). |
| 2 | +
|
| 3 | +The server sets no identity of its own: `git_commit` goes through GitPython's |
| 4 | +`IndexFile.commit`, whose `Actor` reads `GIT_AUTHOR_NAME`/`GIT_AUTHOR_EMAIL` |
| 5 | +and `GIT_COMMITTER_NAME`/`GIT_COMMITTER_EMAIL` from the server's environment |
| 6 | +first, then `user.name`/`user.email` from the repository's config, the user's |
| 7 | +`~/.gitconfig` and `$XDG_CONFIG_HOME/git/config`. The README's "Commit |
| 8 | +identity" section tells users to configure the server exactly that way (an |
| 9 | +`env` block, `docker run -e`, or a mounted gitconfig), so these tests pin each |
| 10 | +route through the protocol: a dependency upgrade or a refactor that stopped |
| 11 | +honoring one would otherwise silently commit as `root@<container id>`. |
| 12 | +""" |
| 13 | + |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +import git |
| 17 | +import pytest |
| 18 | + |
| 19 | +from conftest import connect, disable_autocrlf, text_result, wire |
| 20 | + |
| 21 | +IDENTITY_VARS = ( |
| 22 | + "GIT_AUTHOR_NAME", |
| 23 | + "GIT_AUTHOR_EMAIL", |
| 24 | + "GIT_COMMITTER_NAME", |
| 25 | + "GIT_COMMITTER_EMAIL", |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture(autouse=True) |
| 30 | +def clean_identity_env(monkeypatch: pytest.MonkeyPatch) -> None: |
| 31 | + """Start every test with no identity in the environment, whatever CI sets.""" |
| 32 | + for name in IDENTITY_VARS: |
| 33 | + monkeypatch.delenv(name, raising=False) |
| 34 | + |
| 35 | + |
| 36 | +async def commit_new_file(repo: git.Repo) -> git.Commit: |
| 37 | + """Stage a new file, commit it through the protocol, return the commit.""" |
| 38 | + assert repo.working_dir is not None |
| 39 | + root = Path(repo.working_dir) |
| 40 | + (root / "new.txt").write_text("new\n", newline="\n") |
| 41 | + repo.index.add(["new.txt"]) |
| 42 | + async with connect(None) as session: |
| 43 | + result = wire( |
| 44 | + await session.call_tool( |
| 45 | + "git_commit", {"repo_path": str(root), "message": "add new"} |
| 46 | + ) |
| 47 | + ) |
| 48 | + head = repo.head.commit |
| 49 | + assert result == text_result( |
| 50 | + f"Changes committed successfully with hash {head.hexsha}" |
| 51 | + ) |
| 52 | + return head |
| 53 | + |
| 54 | + |
| 55 | +async def test_environment_variables_set_author_and_committer( |
| 56 | + repo: git.Repo, monkeypatch: pytest.MonkeyPatch |
| 57 | +): |
| 58 | + # The `repo` fixture configures user.name/user.email repo-locally; the |
| 59 | + # environment wins over it, as it does for `git commit`. |
| 60 | + monkeypatch.setenv("GIT_AUTHOR_NAME", "Ada Author") |
| 61 | + monkeypatch. setenv( "GIT_AUTHOR_EMAIL", "[email protected]") |
| 62 | + monkeypatch.setenv("GIT_COMMITTER_NAME", "Cy Committer") |
| 63 | + monkeypatch. setenv( "GIT_COMMITTER_EMAIL", "[email protected]") |
| 64 | + |
| 65 | + head = await commit_new_file(repo) |
| 66 | + |
| 67 | + assert ( head. author. name, head. author. email) == ( "Ada Author", "[email protected]") |
| 68 | + assert (head.committer.name, head.committer.email) == ( |
| 69 | + "Cy Committer", |
| 70 | + |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +async def test_author_variables_alone_leave_the_committer_to_config( |
| 75 | + repo: git.Repo, monkeypatch: pytest.MonkeyPatch |
| 76 | +): |
| 77 | + # Author and committer are resolved separately, which is why the README |
| 78 | + # tells users to set all four variables. |
| 79 | + monkeypatch.setenv("GIT_AUTHOR_NAME", "Ada Author") |
| 80 | + monkeypatch. setenv( "GIT_AUTHOR_EMAIL", "[email protected]") |
| 81 | + |
| 82 | + head = await commit_new_file(repo) |
| 83 | + |
| 84 | + assert ( head. author. name, head. author. email) == ( "Ada Author", "[email protected]") |
| 85 | + assert (head.committer.name, head.committer.email) == ( |
| 86 | + "Test User", |
| 87 | + |
| 88 | + ) |
| 89 | + |
| 90 | + |
| 91 | +async def test_user_gitconfig_in_home_sets_the_identity( |
| 92 | + tmp_path: Path, monkeypatch: pytest.MonkeyPatch |
| 93 | +): |
| 94 | + # The mounted-gitconfig route: no environment variables and no repo-local |
| 95 | + # identity, only a ~/.gitconfig under the server's HOME. |
| 96 | + home = tmp_path / "home" |
| 97 | + home.mkdir() |
| 98 | + (home / ".gitconfig").write_text( |
| 99 | + "[user]\n\tname = Gita Config\n\temail = [email protected]\n" |
| 100 | + ) |
| 101 | + monkeypatch.setenv("HOME", str(home)) |
| 102 | + monkeypatch.setenv("USERPROFILE", str(home)) # what expanduser reads on Windows |
| 103 | + monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) |
| 104 | + |
| 105 | + repo = git.Repo.init(tmp_path / "repo", initial_branch="main") |
| 106 | + try: |
| 107 | + disable_autocrlf(repo) |
| 108 | + head = await commit_new_file(repo) |
| 109 | + assert (head.author.name, head.author.email) == ( |
| 110 | + "Gita Config", |
| 111 | + |
| 112 | + ) |
| 113 | + assert (head.committer.name, head.committer.email) == ( |
| 114 | + "Gita Config", |
| 115 | + |
| 116 | + ) |
| 117 | + finally: |
| 118 | + repo.close() |
0 commit comments