Skip to content

Commit 520ac52

Browse files
committed
[TE-5578] Fall back to BUILDKITE_BRANCH when git branch --show-current is empty
In CI, the Buildkite agent checks out a specific commit SHA, leaving the repo in detached HEAD state where git branch --show-current returns empty. Fall back to the BUILDKITE_BRANCH env var which the agent always sets. Split the DetachedHead test into two cases: one where BUILDKITE_BRANCH is set (verifies fallback populates the branch field) and one where it's also empty (verifies branch is absent).
1 parent 280aaf3 commit 520ac52

2 files changed

Lines changed: 48 additions & 4 deletions

File tree

internal/git/auto_metadata.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,24 @@ func mergeNonEmpty(dst, src map[string]string) {
157157

158158
// collectContextFields adds branch, base_branch, and Buildkite env var fields.
159159
func collectContextFields(ctx context.Context, runner GitRunner, baseBranch string, metadata map[string]string) {
160-
// branch: current branch name (empty on detached HEAD, omitted)
160+
// branch: current branch name, falling back to BUILDKITE_BRANCH
161+
// for detached HEAD (common in CI where the agent checks out a commit SHA)
161162
if out, err := runner.Output(ctx, "branch", "--show-current"); err == nil {
162-
branch := strings.TrimSpace(out)
163-
if branch != "" {
163+
if branch := strings.TrimSpace(out); branch != "" {
164164
metadata["branch"] = branch
165+
debug.Printf("branch resolved via git branch --show-current: %s", branch)
165166
}
166167
} else {
167168
debug.Printf("Warning: git branch --show-current failed: %v", err)
168169
}
170+
if _, ok := metadata["branch"]; !ok {
171+
if branch := os.Getenv("BUILDKITE_BRANCH"); branch != "" {
172+
metadata["branch"] = branch
173+
debug.Printf("branch resolved via BUILDKITE_BRANCH env var: %s", branch)
174+
} else {
175+
debug.Printf("branch could not be determined (detached HEAD, no BUILDKITE_BRANCH)")
176+
}
177+
}
169178

170179
// base_branch: the resolved base ref (not a git command)
171180
if baseBranch != "" {

internal/git/auto_metadata_test.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,10 @@ func TestCollectPlanMetadata_LogFails(t *testing.T) {
301301
}
302302
}
303303

304-
func TestCollectPlanMetadata_DetachedHead(t *testing.T) {
304+
func TestCollectPlanMetadata_DetachedHeadFallsToBuildkiteBranch(t *testing.T) {
305305
t.Setenv("BUILDKITE_PIPELINE_SLUG", "")
306306
t.Setenv("BUILDKITE_BUILD_ID", "")
307+
t.Setenv("BUILDKITE_BRANCH", "feature/from-env")
307308

308309
gitLogOutput := buildRecord("abc123", "def456", "Alice", "alice@example.com", "2026-03-15T10:00:00+00:00", "Alice", "alice@example.com", "2026-03-15T10:00:00+00:00", "Commit msg")
309310

@@ -316,6 +317,40 @@ func TestCollectPlanMetadata_DetachedHead(t *testing.T) {
316317

317318
got := CollectPlanMetadata(context.Background(), runner, "")
318319

320+
want := map[string]string{
321+
"commit_sha": "abc123",
322+
"parent_shas": "def456",
323+
"author_name": "Alice",
324+
"author_email": "alice@example.com",
325+
"author_date": "2026-03-15T10:00:00+00:00",
326+
"committer_name": "Alice",
327+
"committer_email": "alice@example.com",
328+
"committer_date": "2026-03-15T10:00:00+00:00",
329+
"message": "Commit msg",
330+
"branch": "feature/from-env",
331+
}
332+
333+
if diff := cmp.Diff(want, got); diff != "" {
334+
t.Errorf("CollectPlanMetadata mismatch (-want +got):\n%s", diff)
335+
}
336+
}
337+
338+
func TestCollectPlanMetadata_DetachedHeadNoBuildkiteBranch(t *testing.T) {
339+
t.Setenv("BUILDKITE_PIPELINE_SLUG", "")
340+
t.Setenv("BUILDKITE_BUILD_ID", "")
341+
t.Setenv("BUILDKITE_BRANCH", "")
342+
343+
gitLogOutput := buildRecord("abc123", "def456", "Alice", "alice@example.com", "2026-03-15T10:00:00+00:00", "Alice", "alice@example.com", "2026-03-15T10:00:00+00:00", "Commit msg")
344+
345+
runner := &FakeGitRunner{
346+
Responses: map[string]string{
347+
fmt.Sprintf("log -1 --format=%s", MetadataFormat): gitLogOutput,
348+
"branch --show-current": "\n",
349+
},
350+
}
351+
352+
got := CollectPlanMetadata(context.Background(), runner, "")
353+
319354
want := map[string]string{
320355
"commit_sha": "abc123",
321356
"parent_shas": "def456",

0 commit comments

Comments
 (0)