Skip to content

[automerge] Adjust automerge ignore to cope with deleted files #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions arm-software/ci/automerge.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ def run_cmd(self, args: list[str], check: bool = True) -> str:
return git_process.stdout


def restore_changes_to_ignored_files(git_repo: Git, ignore_list: list[str]) -> None:
if not ignore_list:
return
# Make sure any files deleted by us stay deleted.
# Note that any deleted files in the working tree at this point are conflicts
ls_files_output = git_repo.run_cmd(["ls-files", "--deleted", "--deduplicate"] + ignore_list)
deleted_by_us = ls_files_output.splitlines()
if deleted_by_us:
git_repo.run_cmd(["rm"] + deleted_by_us)
# Next, restore ignored files in the index
git_repo.run_cmd(["restore", "--staged"] + ignore_list)
# And finally, restore ignored files in the working tree
git_repo.run_cmd(["restore", "--ours", "--worktree"] + ignore_list)


def has_unresolved_conflicts(git_repo: Git) -> bool:
diff_output = git_repo.run_cmd(["diff", "--name-only", "--diff-filter=U"])
diff_output = diff_output.strip()
Expand All @@ -59,13 +74,11 @@ def prefix_current_commit_message(git_repo: Git) -> None:
git_repo.run_cmd(["commit", "--amend", "--message=" + commit_msg])


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

git_repo = Git(args.repo_path)

with open(MERGE_IGNORE_PATHSPEC_FILE) as ignored_paths_file:
ignored_paths = ignored_paths_file.readlines()

merge_commits = get_merge_commit_list(git_repo, args.from_branch, args.to_branch)
for commit_hash in merge_commits:
merge_commit(git_repo, args.to_branch, commit_hash, args.dry_run)
merge_commit(git_repo, args.to_branch, commit_hash, ignored_paths, args.dry_run)
except MergeConflictError as conflict:
process_conflict(
git_repo,
Expand Down