Skip to content

Commit cfd98a4

Browse files
committed
Add --fetch-diff-base functionality
1 parent cdefcda commit cfd98a4

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

clicommand/pipeline_upload.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ var PipelineUploadCommand = cli.Command{
312312
prependOriginIfNonempty("BUILDKITE_PIPELINE_DEFAULT_BRANCH"),
313313
defaultGitDiffBase,
314314
),
315+
fetch: cfg.FetchDiffBase,
315316
changedFilesPath: cfg.ChangedFilesPath,
316317
}
317318

@@ -756,6 +757,7 @@ type ifChangedApplicator struct {
756757
enabled bool // apply-if-changed is enabled
757758
gathered bool // the changed files have been computed?
758759
diffBase string
760+
fetch bool // fetch diffBase before computing diff?
759761
changedFilesPath string // path to a file containing newline-separated changed files
760762
changedPaths []string
761763
}
@@ -893,6 +895,25 @@ func (ica *ifChangedApplicator) gatherChangedPaths(l logger.Logger) ([]string, e
893895
return cps, nil
894896
}
895897

898+
if ica.fetch {
899+
// First, fetch the remote refspec specified by diffBase.
900+
remote, refspec, slash := strings.Cut(ica.diffBase, "/")
901+
if !slash {
902+
l.Warn("The diff-base %q was not in 'remote/refspec' form - continuing with the remote 'origin'", ica.diffBase)
903+
remote = "origin"
904+
refspec = ica.diffBase
905+
}
906+
if err := exec.Command("git", "fetch", "--", remote, refspec).Run(); err != nil {
907+
l.Error("Couldn't fetch %q from origin: %v", err)
908+
var exitErr *exec.ExitError
909+
if errors.As(err, &exitErr) && len(exitErr.Stderr) > 0 {
910+
// stderr came from git, which is typically human readable
911+
l.Error("git: %s", exitErr.Stderr)
912+
}
913+
l.Info("if_changed will continue processing, but the diff may fail, or produce more paths than expected.")
914+
}
915+
}
916+
896917
// Determine changed files using git.
897918
cps, err := computeGitDiff(l, ica.diffBase)
898919
if err != nil {
@@ -905,19 +926,19 @@ func (ica *ifChangedApplicator) gatherChangedPaths(l logger.Logger) ([]string, e
905926
switch err := err.(type) {
906927
case gitRevParseError:
907928
l.Error("This could be because %q might not be a commit in the repository.\n"+
908-
"You may need to change the --git-diff-base flag or BUILDKITE_GIT_DIFF_BASE env var.",
929+
"You may need to change the --git-diff-base flag or BUILDKITE_GIT_DIFF_BASE env var, or add --fetch-diff-base.",
909930
err.arg,
910931
)
911932

912933
case gitMergeBaseError:
913934
l.Error("This could be because %q might not be a commit in the repository.\n"+
914-
"You may need to change the --git-diff-base flag or BUILDKITE_GIT_DIFF_BASE env var.",
935+
"You may need to change the --git-diff-base flag or BUILDKITE_GIT_DIFF_BASE env var, or add --fetch-diff-base.",
915936
err.diffBase,
916937
)
917938

918939
case gitDiffError:
919940
l.Error("This could be because the merge-base that Git found, %q, might be invalid.\n"+
920-
"You may need to change the --git-diff-base flag or BUILDKITE_GIT_DIFF_BASE env var.",
941+
"You may need to change the --git-diff-base flag or BUILDKITE_GIT_DIFF_BASE env var, or add --fetch-diff-base.",
921942
err.mergeBase,
922943
)
923944
}

0 commit comments

Comments
 (0)