Skip to content

Commit c7d4e3f

Browse files
fix(git): preserve whitespace in repository paths (#1210)
## Summary Fixes #1209. Eval runs can lose their commit and branch metadata when the repository directory ends in a space. Both Git discovery calls already go through a helper that removes the output newline. The additional `.strip()` truncated the directory name, so later Git commands ran with an invalid working directory. Keep the discovered path intact for regular checkouts and bare repositories. The regression test creates real local Git repositories and checks the commit SHA and message through the same metadata entry point used for experiments. ## Validation - `python -m pytest -q py/src/braintrust/test_gitutil.py` — on the base, both new cases failed with `commit=None` and the six existing tests passed. With the fix, all eight tests passed. - Ruff lint and format checks passed on both touched files. `git diff --check` passed. - Local environment: macOS arm64, Python 3.13.3, Git 2.35.1. The trailing-space cases are skipped on Windows. This test does not require a hosted Braintrust account. --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
1 parent 00bcab4 commit c7d4e3f

2 files changed

Lines changed: 44 additions & 2 deletions

File tree

‎py/src/braintrust/gitutil.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ def _is_dirty(repo_path):
6161
@_cache(1)
6262
def _current_repo():
6363
try:
64-
return _git_output(["rev-parse", "--show-toplevel"]).strip()
64+
# _git_output removes the terminating newline without changing the path.
65+
return _git_output(["rev-parse", "--show-toplevel"])
6566
except (OSError, subprocess.CalledProcessError):
6667
try:
6768
if _git_output(["rev-parse", "--is-bare-repository"]).strip() == "true":
68-
return _git_output(["rev-parse", "--absolute-git-dir"]).strip()
69+
return _git_output(["rev-parse", "--absolute-git-dir"])
6970
except (OSError, subprocess.CalledProcessError):
7071
pass
7172
return None

‎py/src/braintrust/test_gitutil.py‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,44 @@ def test_repo_info_returns_none_when_git_cannot_be_resolved(monkeypatch: pytest.
6868
def test_get_repo_info_without_settings_returns_none():
6969
"""Direct call to get_repo_info with settings=None should return None."""
7070
assert gitutil.get_repo_info(None) is None
71+
72+
73+
@pytest.mark.skipif(os.name == "nt", reason="Windows does not support trailing spaces in ordinary directory paths")
74+
@pytest.mark.parametrize("bare", [False, True], ids=["worktree", "bare"])
75+
def test_repo_info_handles_repository_path_with_trailing_space(tmp_path, monkeypatch, bare):
76+
repo = tmp_path / "eval project "
77+
repo.mkdir()
78+
worktree = tmp_path / "source" if bare else repo
79+
worktree.mkdir(exist_ok=True)
80+
subprocess.run(["git", "init"], cwd=worktree, check=True, capture_output=True)
81+
82+
subprocess.run(
83+
[
84+
"git",
85+
"-c",
86+
"user.name=Eval Test",
87+
"-c",
88+
"user.email=eval@example.invalid",
89+
"-c",
90+
"commit.gpgsign=false",
91+
"commit",
92+
"--allow-empty",
93+
"-m",
94+
"Initial eval baseline",
95+
],
96+
cwd=worktree,
97+
check=True,
98+
capture_output=True,
99+
)
100+
if bare:
101+
subprocess.run(
102+
["git", "clone", "--bare", str(worktree), str(repo)], cwd=worktree, check=True, capture_output=True
103+
)
104+
expected_commit = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=repo, text=True).strip()
105+
monkeypatch.chdir(repo)
106+
107+
info = gitutil.get_repo_info(gitutil.GitMetadataSettings(collect="all"))
108+
109+
assert info is not None
110+
assert info.commit == expected_commit
111+
assert info.commit_message == "Initial eval baseline"

0 commit comments

Comments
 (0)