Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions py/src/braintrust/gitutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions py/src/braintrust/test_gitutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Comment thread
raunakpandey1 marked this conversation as resolved.
Resolved
"--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"