Skip to content

Commit 9ea6a2f

Browse files
committed
Move commit verification into its own files for cleanliness
1 parent 67c8fc7 commit 9ea6a2f

5 files changed

Lines changed: 562 additions & 511 deletions

File tree

env/protected.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ var protectedEnv = map[string]protection{
4848
"BUILDKITE_BIN_PATH": {},
4949
"BUILDKITE_BUILD_PATH": {},
5050
"BUILDKITE_COMMAND_EVAL": {},
51+
"BUILDKITE_GIT_COMMIT_VERIFICATION": {},
5152
"BUILDKITE_CONFIG_PATH": {},
5253
"BUILDKITE_CONTAINER_COUNT": {},
5354
"BUILDKITE_HOOKS_PATH": {},

internal/job/checkout.go

Lines changed: 0 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -806,116 +806,6 @@ func (e *Executor) fetchSource(ctx context.Context) error {
806806
return nil
807807
}
808808

809-
func (e *Executor) checkCommitOnBranch(ctx context.Context) error {
810-
e.shell.Commentf("Verifying commit %q is on branch %q", e.Commit, e.Branch)
811-
812-
// Try the ancestry check
813-
err := e.shell.Command("git", "merge-base", "--is-ancestor", e.Commit, e.Branch).Run(ctx)
814-
exitCode := shell.ExitCode(err)
815-
816-
switch exitCode {
817-
case 0:
818-
return nil // verified!
819-
case 1:
820-
return fmt.Errorf("commit %q is not on branch %q", e.Commit, e.Branch)
821-
case 128:
822-
// We might have a shallow clone, try to deepen or unshallow to find the commit
823-
output, _ := e.shell.Command("git", "rev-parse", "--is-shallow-repository").RunAndCaptureStdout(ctx)
824-
825-
if strings.TrimSpace(output) != "true" {
826-
// Not shallow — this is a genuine error
827-
return fmt.Errorf("unable to verify commit %q on branch %q: %w", e.Commit, e.Branch, err)
828-
}
829-
830-
// Try deepening by 50 commits first
831-
e.shell.Commentf("Shallow clone detected, deepening by 50 commits...")
832-
_ = e.shell.Command("git", "fetch", "--deepen=50").Run(ctx)
833-
834-
retryErr := e.shell.Command("git", "merge-base", "--is-ancestor", e.Commit, e.Branch).Run(ctx)
835-
retryCode := shell.ExitCode(retryErr)
836-
837-
if retryCode == 0 {
838-
return nil // Found a valid commit after deepening
839-
}
840-
if retryCode == 1 {
841-
return fmt.Errorf("commit %q is not on branch %q", e.Commit, e.Branch)
842-
}
843-
844-
// Still 128 - full unshallow as last resort
845-
e.shell.Commentf("Deepening insufficient, performing a full unshallow...")
846-
_ = e.shell.Command("git", "fetch", "--unshallow").Run(ctx)
847-
848-
retryErr = e.shell.Command("git", "merge-base", "--is-ancestor", e.Commit, e.Branch).Run(ctx)
849-
retryCode = shell.ExitCode(retryErr)
850-
851-
if retryCode == 0 {
852-
return nil // Found a valid commit after unshallowing
853-
}
854-
if retryCode == 1 {
855-
return fmt.Errorf("commit %q is not on branch %q", e.Commit, e.Branch)
856-
}
857-
858-
return fmt.Errorf("unable to verify commit %q on branch %q after unshallowing: %w", e.Commit, e.Branch, retryErr)
859-
default:
860-
return fmt.Errorf("unable to verify commit %q on branch %q: %w", e.Commit, e.Branch, err)
861-
}
862-
}
863-
864-
// verifyCommit is called if the user has commit verification enabled. It ensures that the commit we are
865-
// asked to build exists and is reachable on the branch we are given.
866-
func (e *Executor) verifyCommit(ctx context.Context) error {
867-
// Skip if not enabled
868-
if e.GitCommitVerification == "" {
869-
return nil
870-
}
871-
872-
// Skip if commit is HEAD (nothing to verify)
873-
if e.Commit == "HEAD" {
874-
return nil
875-
}
876-
877-
// Skip if we haven't been given a branch - e.g. it's a tag push event
878-
if e.Branch == "" {
879-
return nil
880-
}
881-
882-
// Skip if this is a tag build — tags are not branch-specific
883-
if e.Tag != "" {
884-
return nil
885-
}
886-
887-
// Skip if this is a PR build — the commit may be on a merge ref, not the target branch
888-
if e.PullRequest != "" {
889-
return nil
890-
}
891-
892-
// Skip if a custom refspec is set — the fetch may not populate standard branch refs,
893-
// making ancestry verification unreliable
894-
if e.RefSpec != "" {
895-
return nil
896-
}
897-
898-
// Perform the verification
899-
err := e.checkCommitOnBranch(ctx)
900-
901-
// Verification passed
902-
if err == nil {
903-
return nil
904-
}
905-
906-
// Handle verification failure depending on setting
907-
switch e.GitCommitVerification {
908-
case "strict":
909-
return err
910-
case "warn":
911-
e.shell.Warningf("Commit verification failed: %v", err)
912-
return nil
913-
default:
914-
e.shell.Warningf("Unknown git-commit-verification value %q, skipping verification", e.GitCommitVerification)
915-
return nil
916-
}
917-
}
918-
919809
// defaultCheckoutPhase is called by the CheckoutPhase if no global or plugin checkout
920810
// hook exists. It performs the default checkout on the Repository provided in the config
921811
func (e *Executor) defaultCheckoutPhase(ctx context.Context) error {

0 commit comments

Comments
 (0)