Skip to content

Commit d4f0f12

Browse files
committed
[Automerge] Detect unexpected errors when running git merge (#168)
This updates the automerge script to detect unexpected errors when running the `git merge` command for each of the commits being merged. We cannot rely on the command's exist status for that, as it will return a non-zero status in case a conflict is present, so we check that a merge operation is actually in progress after `git merge` is executed. This is done by checking for the presence of the `.git/MERGE_HEAD` file in the repository dir.
1 parent e074d49 commit d4f0f12

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

arm-software/ci/automerge.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ def run_cmd(self, args: list[str], check: bool = True) -> str:
5353
return git_process.stdout
5454

5555

56+
def is_merge_in_progress(git_repo: Git) -> bool:
57+
# The `.git/MERGE_HEAD` file only exists when a merge operation is in progress.
58+
merge_head_path = Path(git_repo.repo_path) / '.git' / 'MERGE_HEAD'
59+
return merge_head_path.exists()
60+
61+
5662
def restore_changes_to_ignored_files(git_repo: Git, ignore_list: list[str]) -> None:
5763
if not ignore_list:
5864
return
@@ -87,7 +93,13 @@ def merge_commit(git_repo: Git, to_branch: str, commit_hash: str, ignored_paths:
8793
if verbose:
8894
current_head = git_repo.run_cmd(["log", "--no-walk", "HEAD", "--pretty=reference"])
8995
logger.debug("Current HEAD of %s is %s", to_branch, current_head)
96+
# `git merge` will return a non-zero exit status if there's a conflict, but
97+
# the conflict might be resolved by applying our ignore list. We work around
98+
# that by not checking the exist status and validating that a merge is in
99+
# progress after `git merge` runs.
90100
git_repo.run_cmd(["merge", commit_hash, "--no-commit", "--no-ff"], check=False)
101+
if not is_merge_in_progress(git_repo):
102+
raise RuntimeError("Unexpected error occurred when running git merge")
91103
restore_changes_to_ignored_files(git_repo, ignored_paths)
92104
if has_unresolved_conflicts(git_repo):
93105
logger.info("Merge failed")

0 commit comments

Comments
 (0)