diff --git a/py/src/braintrust/gitutil.py b/py/src/braintrust/gitutil.py index 32961882..72636546 100644 --- a/py/src/braintrust/gitutil.py +++ b/py/src/braintrust/gitutil.py @@ -61,11 +61,12 @@ def _is_dirty(repo_path): @_cache(1) def _current_repo(): try: - return _git_output(["rev-parse", "--show-toplevel"]).strip() + # _git_output removes the terminating newline without changing the path. + return _git_output(["rev-parse", "--show-toplevel"]) except (OSError, subprocess.CalledProcessError): try: if _git_output(["rev-parse", "--is-bare-repository"]).strip() == "true": - return _git_output(["rev-parse", "--absolute-git-dir"]).strip() + return _git_output(["rev-parse", "--absolute-git-dir"]) except (OSError, subprocess.CalledProcessError): pass return None diff --git a/py/src/braintrust/test_gitutil.py b/py/src/braintrust/test_gitutil.py index 45a06a49..ed74a86c 100644 --- a/py/src/braintrust/test_gitutil.py +++ b/py/src/braintrust/test_gitutil.py @@ -68,3 +68,44 @@ def test_repo_info_returns_none_when_git_cannot_be_resolved(monkeypatch: pytest. def test_get_repo_info_without_settings_returns_none(): """Direct call to get_repo_info with settings=None should return None.""" assert gitutil.get_repo_info(None) is None + + +@pytest.mark.skipif(os.name == "nt", reason="Windows does not support trailing spaces in ordinary directory paths") +@pytest.mark.parametrize("bare", [False, True], ids=["worktree", "bare"]) +def test_repo_info_handles_repository_path_with_trailing_space(tmp_path, monkeypatch, bare): + repo = tmp_path / "eval project " + repo.mkdir() + worktree = tmp_path / "source" if bare else repo + worktree.mkdir(exist_ok=True) + subprocess.run(["git", "init"], cwd=worktree, check=True, capture_output=True) + + subprocess.run( + [ + "git", + "-c", + "user.name=Eval Test", + "-c", + "user.email=eval@example.invalid", + "-c", + "commit.gpgsign=false", + "commit", + "--allow-empty", + "-m", + "Initial eval baseline", + ], + cwd=worktree, + check=True, + capture_output=True, + ) + if bare: + subprocess.run( + ["git", "clone", "--bare", str(worktree), str(repo)], cwd=worktree, check=True, capture_output=True + ) + expected_commit = subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=repo, text=True).strip() + monkeypatch.chdir(repo) + + info = gitutil.get_repo_info(gitutil.GitMetadataSettings(collect="all")) + + assert info is not None + assert info.commit == expected_commit + assert info.commit_message == "Initial eval baseline"