Skip to content

Commit d773bfb

Browse files
authored
[automerge] Adjust automerge ignore to cope with deleted files (#22)
The `git restore --ours` command is not able to cope with the case where a file was deleted in the destination branch. This adjusts the automerge script to use a combination of `git ls-files --deleted` and `git rm` to make sure these are handled correctly for ignored files.
1 parent e75c9ca commit d773bfb

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

arm-software/ci/automerge.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ def run_cmd(self, args: list[str], check: bool = True) -> str:
4747
return git_process.stdout
4848

4949

50+
def restore_changes_to_ignored_files(git_repo: Git, ignore_list: list[str]) -> None:
51+
if not ignore_list:
52+
return
53+
# Make sure any files deleted by us stay deleted.
54+
# Note that any deleted files in the working tree at this point are conflicts
55+
ls_files_output = git_repo.run_cmd(["ls-files", "--deleted", "--deduplicate"] + ignore_list)
56+
deleted_by_us = ls_files_output.splitlines()
57+
if deleted_by_us:
58+
git_repo.run_cmd(["rm"] + deleted_by_us)
59+
# Next, restore ignored files in the index
60+
git_repo.run_cmd(["restore", "--staged"] + ignore_list)
61+
# And finally, restore ignored files in the working tree
62+
git_repo.run_cmd(["restore", "--ours", "--worktree"] + ignore_list)
63+
64+
5065
def has_unresolved_conflicts(git_repo: Git) -> bool:
5166
diff_output = git_repo.run_cmd(["diff", "--name-only", "--diff-filter=U"])
5267
diff_output = diff_output.strip()
@@ -59,13 +74,11 @@ def prefix_current_commit_message(git_repo: Git) -> None:
5974
git_repo.run_cmd(["commit", "--amend", "--message=" + commit_msg])
6075

6176

62-
def merge_commit(git_repo: Git, to_branch: str, commit_hash: str, dry_run: bool) -> None:
77+
def merge_commit(git_repo: Git, to_branch: str, commit_hash: str, ignored_paths: list[str], dry_run: bool) -> None:
6378
logger.info("Merging commit %s into %s", commit_hash, to_branch)
6479
git_repo.run_cmd(["switch", to_branch])
6580
git_repo.run_cmd(["merge", commit_hash, "--no-commit", "--no-ff"], check=False)
66-
# Ensure all paths that should be ignored stay unchanged
67-
git_repo.run_cmd(["restore", "--staged", f"--pathspec-from-file={MERGE_IGNORE_PATHSPEC_FILE}"])
68-
git_repo.run_cmd(["restore", "--ours", "--worktree", f"--pathspec-from-file={MERGE_IGNORE_PATHSPEC_FILE}"])
81+
restore_changes_to_ignored_files(git_repo, ignored_paths)
6982
if has_unresolved_conflicts(git_repo):
7083
logger.info("Merge failed")
7184
git_repo.run_cmd(["merge", "--abort"])
@@ -171,9 +184,12 @@ def main():
171184

172185
git_repo = Git(args.repo_path)
173186

187+
with open(MERGE_IGNORE_PATHSPEC_FILE) as ignored_paths_file:
188+
ignored_paths = ignored_paths_file.readlines()
189+
174190
merge_commits = get_merge_commit_list(git_repo, args.from_branch, args.to_branch)
175191
for commit_hash in merge_commits:
176-
merge_commit(git_repo, args.to_branch, commit_hash, args.dry_run)
192+
merge_commit(git_repo, args.to_branch, commit_hash, ignored_paths, args.dry_run)
177193
except MergeConflictError as conflict:
178194
process_conflict(
179195
git_repo,

0 commit comments

Comments
 (0)