|
| 1 | +package git |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/buildkite/test-engine-client/internal/debug" |
| 10 | +) |
| 11 | + |
| 12 | +// ResolveBaseBranch determines the base branch ref to diff against. |
| 13 | +// |
| 14 | +// Resolution order: |
| 15 | +// 1. explicit (from --metadata base_branch=...) -- for repos with |
| 16 | +// non-standard default branches (not main/master), or PRs targeting |
| 17 | +// non-default branches outside Buildkite CI. |
| 18 | +// 2. $BUILDKITE_PULL_REQUEST_BASE_BRANCH -- auto-set by the Buildkite |
| 19 | +// agent on PR builds. |
| 20 | +// 3. DetectDefaultBranch() -- tries remote/HEAD, remote/main, remote/master. |
| 21 | +// |
| 22 | +// Most users should NOT need to set base_branch explicitly. Override is |
| 23 | +// only needed when: |
| 24 | +// - The repo uses a non-standard default branch (e.g. "develop", "trunk") |
| 25 | +// AND remote/HEAD is not configured |
| 26 | +// - The build targets a non-default branch (e.g. a PR into "release/v2") |
| 27 | +// AND $BUILDKITE_PULL_REQUEST_BASE_BRANCH is not set (non-Buildkite CI |
| 28 | +// or manual trigger) |
| 29 | +// |
| 30 | +// Each candidate is probed against the repository using |
| 31 | +// git rev-parse --verify. We try the candidate verbatim first, then fall |
| 32 | +// back to "<remote>/<candidate>". This handles every common shape without |
| 33 | +// heuristics: bare branch names ("main") resolve via the fallback to |
| 34 | +// "origin/main"; refs from a different remote ("upstream/main"), fully- |
| 35 | +// qualified refs ("refs/heads/release"), and values already including the |
| 36 | +// configured remote ("origin/main") all resolve on the first probe. |
| 37 | +// Without the verbatim probe, prefixing a qualified ref would rewrite it |
| 38 | +// into an invalid value like "origin/upstream/main" and silently drop the |
| 39 | +// explicit override. |
| 40 | +// Returns the resolved ref (e.g. "origin/main") or an error. |
| 41 | +func ResolveBaseBranch(ctx context.Context, runner GitRunner, explicit string, remote string) (string, error) { |
| 42 | + if remote == "" { |
| 43 | + return "", fmt.Errorf("remote must not be empty") |
| 44 | + } |
| 45 | + |
| 46 | + type candidate struct { |
| 47 | + value string |
| 48 | + source string |
| 49 | + } |
| 50 | + candidates := []candidate{ |
| 51 | + {value: explicit, source: "explicit --metadata base_branch"}, |
| 52 | + {value: os.Getenv("BUILDKITE_PULL_REQUEST_BASE_BRANCH"), source: "BUILDKITE_PULL_REQUEST_BASE_BRANCH"}, |
| 53 | + } |
| 54 | + |
| 55 | + for _, c := range candidates { |
| 56 | + if c.value == "" { |
| 57 | + continue |
| 58 | + } |
| 59 | + |
| 60 | + // Try the candidate verbatim first. This accepts qualified refs |
| 61 | + // from a non-default remote ("upstream/main"), fully-qualified |
| 62 | + // refs ("refs/heads/release"), and values already including the |
| 63 | + // configured remote ("origin/main") without rewriting them. |
| 64 | + if _, err := runner.Output(ctx, "rev-parse", "--verify", c.value); err == nil { |
| 65 | + debug.Printf("base branch resolved via %s: %s", c.source, c.value) |
| 66 | + return c.value, nil |
| 67 | + } |
| 68 | + |
| 69 | + // Fall back to "<remote>/<candidate>" for bare branch names |
| 70 | + // ("main" -> "origin/main") and release-style names with a "/" |
| 71 | + // that are still relative to the configured remote ("release/v2" |
| 72 | + // -> "origin/release/v2"). |
| 73 | + prefixed := remote + "/" + c.value |
| 74 | + if _, err := runner.Output(ctx, "rev-parse", "--verify", prefixed); err == nil { |
| 75 | + debug.Printf("base branch resolved via %s: %q -> %s", c.source, c.value, prefixed) |
| 76 | + return prefixed, nil |
| 77 | + } |
| 78 | + debug.Printf("base branch candidate %q from %s not found (also tried %q), trying next", c.value, c.source, prefixed) |
| 79 | + } |
| 80 | + ref, err := DetectDefaultBranch(ctx, runner, remote) |
| 81 | + if err == nil { |
| 82 | + debug.Printf("base branch resolved via DetectDefaultBranch: %s", ref) |
| 83 | + } |
| 84 | + return ref, err |
| 85 | +} |
| 86 | + |
| 87 | +// CollectPlanMetadata collects git metadata for the current HEAD commit. |
| 88 | +// Returns a map of metadata keys to values. Skips keys that cannot be |
| 89 | +// collected (e.g. if not in a git repo). Does not error on git failures; |
| 90 | +// returns partial results with warnings logged via debug.Printf. |
| 91 | +func CollectPlanMetadata(ctx context.Context, runner GitRunner, baseBranch string) map[string]string { |
| 92 | + metadata := make(map[string]string) |
| 93 | + |
| 94 | + // Phase 1: Commit metadata via git log -1 --format=... |
| 95 | + // Reuses MetadataFormat from metadata.go for consistency with backfill. |
| 96 | + collectCommitMetadata(ctx, runner, metadata) |
| 97 | + |
| 98 | + // Phase 2: Diff fields against base branch (only if base branch is resolved) |
| 99 | + if baseBranch != "" { |
| 100 | + collectDiffMetadata(ctx, runner, baseBranch, metadata) |
| 101 | + } |
| 102 | + |
| 103 | + // Phase 3: Context fields |
| 104 | + collectContextFields(ctx, runner, baseBranch, metadata) |
| 105 | + |
| 106 | + return metadata |
| 107 | +} |
| 108 | + |
| 109 | +// collectCommitMetadata extracts commit metadata for HEAD using a single |
| 110 | +// git log call with the same format as FetchBulkMetadata, parses it into |
| 111 | +// a CommitMetadata struct, and flattens it into the metadata map via ToMap. |
| 112 | +func collectCommitMetadata(ctx context.Context, runner GitRunner, metadata map[string]string) { |
| 113 | + output, err := runner.Output(ctx, "log", "-1", fmt.Sprintf("--format=%s", MetadataFormat)) |
| 114 | + if err != nil { |
| 115 | + debug.Printf("Warning: git log failed, skipping commit metadata: %v", err) |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + // Real git log output ends in "\x1e\n" (git always appends a trailing |
| 120 | + // newline). TrimSpace must run first to strip the "\n", otherwise |
| 121 | + // TrimSuffix("\x1e") sees the "\n" at the end and no-ops, leaving the |
| 122 | + // record separator trapped inside the final field (the commit message). |
| 123 | + // TrimSpace does not strip "\x1e" because it is not Unicode whitespace. |
| 124 | + record := strings.TrimSuffix(strings.TrimSpace(output), recordSeparator) |
| 125 | + meta, ok := parseRecord(record) |
| 126 | + if !ok { |
| 127 | + debug.Printf("Warning: git log returned unparseable output; skipping commit metadata") |
| 128 | + return |
| 129 | + } |
| 130 | + |
| 131 | + mergeNonEmpty(metadata, meta.ToMap()) |
| 132 | +} |
| 133 | + |
| 134 | +// collectDiffMetadata computes the merge-base between baseBranch and HEAD, |
| 135 | +// then runs diff commands using two-arg form (merge-base, HEAD). This is |
| 136 | +// equivalent to git diff baseBranch...HEAD but makes the fork-point |
| 137 | +// resolution explicit and uses the same two-arg diff form as the backfill |
| 138 | +// path. |
| 139 | +func collectDiffMetadata(ctx context.Context, runner GitRunner, baseBranch string, metadata map[string]string) { |
| 140 | + forkPoint, err := runner.Output(ctx, "merge-base", baseBranch, "HEAD") |
| 141 | + if err != nil { |
| 142 | + debug.Printf("Warning: git merge-base failed: %v", err) |
| 143 | + return |
| 144 | + } |
| 145 | + forkPoint = strings.TrimSpace(forkPoint) |
| 146 | + |
| 147 | + diffs := runDiffCommands(ctx, runner, false, forkPoint, "HEAD") |
| 148 | + mergeNonEmpty(metadata, diffs.ToMap()) |
| 149 | +} |
| 150 | + |
| 151 | +// MergeMetadata merges auto-collected metadata into existing user-provided |
| 152 | +// metadata. User-provided keys take precedence: auto-collected values only |
| 153 | +// fill in keys that are not already present. Empty auto-collected values |
| 154 | +// are skipped. If existing is nil, the auto map is returned as-is. |
| 155 | +func MergeMetadata(existing, auto map[string]string) map[string]string { |
| 156 | + if existing == nil { |
| 157 | + return auto |
| 158 | + } |
| 159 | + for k, v := range auto { |
| 160 | + if v == "" { |
| 161 | + continue |
| 162 | + } |
| 163 | + if _, exists := existing[k]; !exists { |
| 164 | + existing[k] = v |
| 165 | + } |
| 166 | + } |
| 167 | + return existing |
| 168 | +} |
| 169 | + |
| 170 | +// mergeNonEmpty copies entries from src into dst, skipping empty values. |
| 171 | +// This avoids sending meaningless keys (e.g. "git_diff":"") in the API |
| 172 | +// request, since json.Marshal does not omit empty strings within a map. |
| 173 | +func mergeNonEmpty(dst, src map[string]string) { |
| 174 | + for k, v := range src { |
| 175 | + if v != "" { |
| 176 | + dst[k] = v |
| 177 | + } |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +// collectContextFields adds branch, base_branch, and Buildkite env var fields. |
| 182 | +func collectContextFields(ctx context.Context, runner GitRunner, baseBranch string, metadata map[string]string) { |
| 183 | + // branch: current branch name, falling back to BUILDKITE_BRANCH |
| 184 | + // for detached HEAD (common in CI where the agent checks out a commit SHA) |
| 185 | + if out, err := runner.Output(ctx, "branch", "--show-current"); err == nil { |
| 186 | + if branch := strings.TrimSpace(out); branch != "" { |
| 187 | + metadata["branch"] = branch |
| 188 | + debug.Printf("branch resolved via git branch --show-current: %s", branch) |
| 189 | + } |
| 190 | + } else { |
| 191 | + debug.Printf("Warning: git branch --show-current failed: %v", err) |
| 192 | + } |
| 193 | + if _, ok := metadata["branch"]; !ok { |
| 194 | + if branch := os.Getenv("BUILDKITE_BRANCH"); branch != "" { |
| 195 | + metadata["branch"] = branch |
| 196 | + debug.Printf("branch resolved via BUILDKITE_BRANCH env var: %s", branch) |
| 197 | + } else { |
| 198 | + debug.Printf("branch could not be determined (detached HEAD, no BUILDKITE_BRANCH)") |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + // base_branch: the resolved base ref (not a git command) |
| 203 | + if baseBranch != "" { |
| 204 | + metadata["base_branch"] = baseBranch |
| 205 | + } |
| 206 | + |
| 207 | + // pipeline_slug from Buildkite env (omitted if not set) |
| 208 | + if slug := os.Getenv("BUILDKITE_PIPELINE_SLUG"); slug != "" { |
| 209 | + metadata["pipeline_slug"] = slug |
| 210 | + } |
| 211 | + |
| 212 | + // build_uuid from Buildkite env (omitted if not set) |
| 213 | + if buildID := os.Getenv("BUILDKITE_BUILD_ID"); buildID != "" { |
| 214 | + metadata["build_uuid"] = buildID |
| 215 | + } |
| 216 | +} |
0 commit comments