Skip to content

Commit 108db21

Browse files
committed
buildbot-effects: fix --branch not resolving rev to branch tip
When --branch was specified without --rev, effects_args() called get_git_rev() which always resolves HEAD of the current checkout, ignoring the requested branch entirely. This meant running: buildbot-effects --branch feature run my-effect would use the rev from whatever branch was checked out, not from 'feature'. Resolve the rev from the specified branch via git rev-parse when --branch is given. Closes: #583
1 parent ed9b02d commit 108db21

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

buildbot_effects/buildbot_effects/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,18 @@ def git_get_tag(path: Path, rev: str) -> str | None:
6969
return None
7070

7171

72+
def get_git_branch_rev(path: Path, branch: str) -> str:
73+
"""Resolve the tip commit of a given branch."""
74+
return git_command(["rev-parse", "--verify", branch], path)
75+
76+
7277
def effects_args(opts: EffectsOptions) -> dict[str, Any]:
73-
rev = opts.rev or get_git_rev(opts.path)
78+
if opts.rev:
79+
rev = opts.rev
80+
elif opts.branch:
81+
rev = get_git_branch_rev(opts.path, opts.branch)
82+
else:
83+
rev = get_git_rev(opts.path)
7484
short_rev = rev[:7]
7585
branch = opts.branch or get_git_branch(opts.path)
7686
repo = opts.repo or opts.path.name
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""Regression test for https://github.com/nix-community/buildbot-nix/issues/583
2+
3+
When --branch is specified without --rev, effects_args() should resolve
4+
the rev from the tip of that branch, not from HEAD of the current checkout.
5+
"""
6+
7+
from __future__ import annotations
8+
9+
import subprocess
10+
from typing import TYPE_CHECKING
11+
12+
from buildbot_effects import effects_args
13+
from buildbot_effects.options import EffectsOptions
14+
15+
if TYPE_CHECKING:
16+
from pathlib import Path
17+
18+
19+
def _git(repo: Path, *args: str) -> str:
20+
return subprocess.run(
21+
["git", "-C", str(repo), *args],
22+
check=True,
23+
text=True,
24+
capture_output=True,
25+
).stdout.strip()
26+
27+
28+
def test_branch_resolves_to_branch_tip(tmp_path: Path) -> None:
29+
"""--branch should resolve rev to the tip of that branch, not HEAD."""
30+
repo = tmp_path / "repo"
31+
repo.mkdir()
32+
_git(repo, "init", "-b", "main")
33+
_git(repo, "config", "user.name", "test")
34+
_git(repo, "config", "user.email", "test@test")
35+
36+
# Initial commit on main
37+
(repo / "file.txt").write_text("main")
38+
_git(repo, "add", ".")
39+
_git(repo, "commit", "-m", "initial")
40+
main_rev = _git(repo, "rev-parse", "HEAD")
41+
42+
# Create a feature branch with a new commit
43+
_git(repo, "checkout", "-b", "feature")
44+
(repo / "file.txt").write_text("feature")
45+
_git(repo, "add", ".")
46+
_git(repo, "commit", "-m", "feature commit")
47+
feature_rev = _git(repo, "rev-parse", "HEAD")
48+
49+
# Go back to main — HEAD is now main_rev
50+
_git(repo, "checkout", "main")
51+
assert _git(repo, "rev-parse", "HEAD") == main_rev
52+
53+
# Ask for --branch=feature without --rev: should get feature_rev, not main_rev
54+
opts = EffectsOptions(path=repo, branch="feature")
55+
result = effects_args(opts)
56+
57+
assert result["rev"] == feature_rev, (
58+
f"Expected rev from 'feature' branch ({feature_rev[:7]}), "
59+
f"got HEAD ({result['rev'][:7]})"
60+
)
61+
assert result["branch"] == "feature"

0 commit comments

Comments
 (0)