66
77import argparse
88import os
9+ import pathlib
910import subprocess
1011import sys
1112
@@ -42,26 +43,23 @@ def main():
4243 else :
4344 git_cmd = ["git" ]
4445
45- abs_patch_dir = os .path .abspath (args .patchdir )
46-
4746 if args .reset :
4847 reset_args = git_cmd + ["reset" , "--quiet" , "--hard" , args .reset ]
4948 subprocess .check_output (reset_args )
5049 clean_args = git_cmd + ["clean" , "--quiet" , "--force" , "-dx" , args .reset ]
5150 subprocess .check_output (clean_args )
5251
53- patch_names = [
54- patch for patch in os .listdir (args .patchdir ) if patch .endswith (".patch" )
55- ]
56- patch_names .sort ()
52+ abs_patch_dir = os .path .abspath (args .patchdir )
53+ patch_list = list (pathlib .Path (abs_patch_dir ).glob ("*.patch" ))
54+ patch_list .sort ()
5755
58- print (f"Found { len (patch_names )} patches to apply:" )
59- print ("\n " .join (patch_names ))
56+ print (f"Found { len (patch_list )} patches to apply:" )
57+ print ("\n " .join (p . name for p in patch_list ))
6058
6159 if args .method == "am" :
6260 merge_args = git_cmd + ["am" , "-k" , "--ignore-whitespace" , "--3way" ]
63- for patch_name in patch_names :
64- merge_args .append (os . path . join ( abs_patch_dir , patch_name ))
61+ for patch in patch_list :
62+ merge_args .append (str ( patch ))
6563 p = subprocess .run (merge_args , capture_output = True , text = True )
6664 print (p .stdout )
6765 print (p .stderr )
@@ -73,7 +71,10 @@ def main():
7371 # Check that the operation can be aborted.
7472 # git am does give any specific return codes,
7573 # so check for unresolved working files.
76- if os .path .isdir (os .path .join (args .llvm_dir , ".git" , "rebase-apply" )):
74+ rebase_apply_path = os .path .join (".git" , "rebase-apply" )
75+ if args .llvm_dir :
76+ rebase_apply_path = os .path .join (args .llvm_dir , rebase_apply_path )
77+ if os .path .isdir (rebase_apply_path ):
7778 print ("Aborting git am..." )
7879 subprocess .run (git_cmd + ["am" , "--abort" ], check = True )
7980 print (f"Abort successful." )
@@ -83,47 +84,48 @@ def main():
8384 sys .exit (1 )
8485 else :
8586 applied_patches = []
86- for patch_name in patch_names :
87- patch_file = os .path .join (abs_patch_dir , patch_name )
88- print (f"Checking { patch_name } ..." )
87+ for current_patch in patch_list :
88+ print (f"Checking { current_patch .name } ..." )
8989 # Check that the patch applies before trying to apply it.
9090 apply_check_args = git_cmd + [
9191 "apply" ,
9292 "--ignore-whitespace" ,
9393 "--3way" ,
9494 "--check" ,
95- patch_file ,
95+ str ( current_patch ) ,
9696 ]
9797 p_check = subprocess .run (apply_check_args )
9898
9999 if p_check .returncode == 0 :
100100 # Patch will apply.
101- print (f"Applying { patch_name } ..." )
101+ print (f"Applying { current_patch . name } ..." )
102102 apply_args = git_cmd + [
103103 "apply" ,
104104 "--ignore-whitespace" ,
105105 "--3way" ,
106- patch_file ,
106+ str ( current_patch ) ,
107107 ]
108108 apply_args = subprocess .run (apply_args , check = True )
109- applied_patches .append (patch_name )
109+ applied_patches .append (current_patch )
110110 else :
111111 # Patch won't apply.
112- print (f"Unable to apply { patch_name } " )
112+ print (f"Unable to apply { current_patch . name } " )
113113 if args .restore_on_fail :
114114 # Remove any patches that have already been applied.
115115 while len (applied_patches ) > 0 :
116- r_patch = applied_patches .pop ()
117- print (f"Reversing { r_patch } ..." )
116+ previous_patch = applied_patches .pop ()
117+ print (f"Reversing { previous_patch . name } ..." )
118118 reverse_args = git_cmd + [
119119 "apply" ,
120120 "--ignore-whitespace" ,
121121 "--3way" ,
122122 "--reverse" ,
123- os . path . join ( abs_patch_dir , r_patch ),
123+ str ( previous_patch ),
124124 ]
125125 p_check = subprocess .run (reverse_args , check = True )
126- print (f"Rollback successful, failure occured on { patch_file } " )
126+ print (
127+ f"Rollback successful, failure occured on { current_patch .name } "
128+ )
127129 sys .exit (2 )
128130 sys .exit (1 )
129131 print (f"All patches applied." )
0 commit comments