66assertion is then made on what a client receives over the wire.
77"""
88
9+ import os
910import shutil
11+ import stat
1012from collections .abc import AsyncIterator , Iterator
1113from contextlib import asynccontextmanager
1214from pathlib import Path
2830def test_repository (tmp_path : Path ) -> Iterator [git .Repo ]:
2931 repo_path = tmp_path / "temp_test_repo"
3032 test_repo = git .Repo .init (repo_path )
33+ disable_autocrlf (test_repo )
3134
3235 Path (repo_path / "test.txt" ).write_text ("test" )
3336 test_repo .index .add (["test.txt" ])
@@ -39,16 +42,47 @@ def test_repository(tmp_path: Path) -> Iterator[git.Repo]:
3942 # their cwd is inside the repository, so rmtree fails with WinError 32
4043 # (#4855, unblocks #1149).
4144 test_repo .close ()
42- shutil .rmtree (repo_path )
45+ rmtree (repo_path )
46+
47+
48+ def disable_autocrlf (repo : git .Repo ) -> None :
49+ """Commit and check out bytes unchanged, whatever the host's git config.
50+
51+ Git for Windows defaults to `core.autocrlf=true`, which rewrites the
52+ fixtures' LF line endings to CRLF in the working tree, so diffs and `show`
53+ output carry CRLF there and nowhere else. Set it repo-locally, before
54+ anything is committed, so every OS sees the same bytes.
55+ """
56+ with repo .config_writer () as config :
57+ config .set_value ("core" , "autocrlf" , "false" )
58+
59+
60+ def _clear_readonly_and_retry (func : Any , path : str , _exc_info : Any ) -> None :
61+ # Git writes object files read-only; on Windows that makes them
62+ # undeletable (WinError 5) until the read-only bit is cleared.
63+ os .chmod (path , stat .S_IWRITE )
64+ func (path )
65+
66+
67+ def rmtree (path : Path ) -> None :
68+ """`shutil.rmtree` that also removes git's read-only files on Windows.
69+
70+ Uses `onerror`, not `onexc`: the server pins Python 3.10, and `onexc` is
71+ 3.12+.
72+ """
73+ shutil .rmtree (path , onerror = _clear_readonly_and_retry )
4374
4475
4576def make_repo (path : Path ) -> git .Repo :
4677 """A repository on branch `main` with one commit of `test.txt`."""
4778 repo = git .Repo .init (path , initial_branch = "main" )
79+ disable_autocrlf (repo )
4880 with repo .config_writer () as config :
4981 config .set_value ("user" , "name" , "Test User" )
5082 config .set_value ("user" , "email" , "test@example.com" )
51- (path / "test.txt" ).write_text ("line 1\n line 2\n line 3\n line 4\n line 5\n " )
83+ (path / "test.txt" ).write_text (
84+ "line 1\n line 2\n line 3\n line 4\n line 5\n " , newline = "\n "
85+ )
5286 repo .index .add (["test.txt" ])
5387 repo .index .commit ("initial commit" )
5488 return repo
0 commit comments