@@ -47,6 +47,21 @@ def run_cmd(self, args: list[str], check: bool = True) -> str:
47
47
return git_process .stdout
48
48
49
49
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
+
50
65
def has_unresolved_conflicts (git_repo : Git ) -> bool :
51
66
diff_output = git_repo .run_cmd (["diff" , "--name-only" , "--diff-filter=U" ])
52
67
diff_output = diff_output .strip ()
@@ -59,13 +74,11 @@ def prefix_current_commit_message(git_repo: Git) -> None:
59
74
git_repo .run_cmd (["commit" , "--amend" , "--message=" + commit_msg ])
60
75
61
76
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 :
63
78
logger .info ("Merging commit %s into %s" , commit_hash , to_branch )
64
79
git_repo .run_cmd (["switch" , to_branch ])
65
80
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 )
69
82
if has_unresolved_conflicts (git_repo ):
70
83
logger .info ("Merge failed" )
71
84
git_repo .run_cmd (["merge" , "--abort" ])
@@ -171,9 +184,12 @@ def main():
171
184
172
185
git_repo = Git (args .repo_path )
173
186
187
+ with open (MERGE_IGNORE_PATHSPEC_FILE ) as ignored_paths_file :
188
+ ignored_paths = ignored_paths_file .readlines ()
189
+
174
190
merge_commits = get_merge_commit_list (git_repo , args .from_branch , args .to_branch )
175
191
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 )
177
193
except MergeConflictError as conflict :
178
194
process_conflict (
179
195
git_repo ,
0 commit comments