bazelci.py: fix get_modified_files (#1886)
We need to diff against the merge base commit from the base branch to
get the full list of changed files in a PR.
For example, in
https://buildkite.com/bazel/bazel-bazel-github-presubmit/builds/20282#018dccf4-3a59-40c4-b8ac-1058aaa072d9,
most of tests were accidentally filtered out because the latest commit
only contains one BUILD file change.
diff --git a/buildkite/bazelci.py b/buildkite/bazelci.py
index 54bda8f..1a045f8 100755
--- a/buildkite/bazelci.py
+++ b/buildkite/bazelci.py
@@ -2431,13 +2431,18 @@
return remaining_targets
+def fetch_base_branch():
+ """Fetch the base branch for the current build, set FETCH_HEAD for git."""
+ base_branch = os.getenv("BUILDKITE_PULL_REQUEST_BASE_BRANCH", "")
+ # Fallback to the default branch for this repository if BUILDKITE_PULL_REQUEST_BASE_BRANCH is not set.
+ if not base_branch:
+ base_branch = os.getenv("BUILDKITE_PIPELINE_DEFAULT_BRANCH", "")
+ execute_command(["git", "fetch", "origin", base_branch])
+
+
def resolve_diffbase(diffbase):
if diffbase in AUTO_DIFFBASE_VALUES:
- base_branch = os.getenv("BUILDKITE_PULL_REQUEST_BASE_BRANCH", "")
- # Fallback to the default branch for this repository if BUILDKITE_PULL_REQUEST_BASE_BRANCH is not set.
- if not base_branch:
- base_branch = os.getenv("BUILDKITE_PIPELINE_DEFAULT_BRANCH", "")
- execute_command(["git", "fetch", "origin", base_branch])
+ fetch_base_branch()
return execute_command_and_get_output(["git", "merge-base", "HEAD", 'FETCH_HEAD']).strip()
elif COMMIT_RE.fullmatch(diffbase):
return diffbase
@@ -3074,8 +3079,10 @@
def get_modified_files(git_commit):
+ fetch_base_branch()
+ merge_base_commit = execute_command_and_get_output(["git", "merge-base", git_commit, 'FETCH_HEAD']).strip()
output = execute_command_and_get_output(
- ["git", "diff-tree", "--no-commit-id", "--name-only", "-r", git_commit]
+ ["git", "diff-tree", "--no-commit-id", "--name-only", "-r", "{}..{}".format(merge_base_commit, git_commit)]
)
return output.split("\n")