@@ -28,7 +28,7 @@ def main():
2828 )
2929 parser .add_argument (
3030 "--reset" ,
31- help = "Clean and reset the repo to a specified commit before patching." ,
31+ help = "Clean and hard reset the repo to a specified commit before patching." ,
3232 )
3333 parser .add_argument (
3434 "--restore_on_fail" ,
@@ -37,29 +37,32 @@ def main():
3737 )
3838 args = parser .parse_args ()
3939
40+ if args .llvm_dir :
41+ git_cmd = ["git" , "-C" , args .llvm_dir ]
42+ else :
43+ git_cmd = ["git" ]
44+
45+ abs_patch_dir = os .path .abspath (args .patchdir )
46+
4047 if args .reset :
41- reset_args = [ "git" , " reset" , "--quiet" , "--hard" ]
42- subprocess .check_output (reset_args , cwd = args . llvm_dir )
43- clean_args = [ "git" , " clean" , "--quiet" , "--force" , "-dx" ]
44- subprocess .check_output (clean_args , cwd = args . llvm_dir )
48+ reset_args = git_cmd + [ " reset" , "--quiet" , "--hard" , args . reset ]
49+ subprocess .check_output (reset_args )
50+ clean_args = git_cmd + [ " clean" , "--quiet" , "--force" , "-dx" , args . reset ]
51+ subprocess .check_output (clean_args )
4552
46- patch_names = []
47- for patch_name in os .listdir (args .patchdir ):
48- if patch_name .endswith (".patch" ):
49- patch_names .append (patch_name )
53+ patch_names = [
54+ patch for patch in os .listdir (args .patchdir ) if patch .endswith (".patch" )
55+ ]
5056 patch_names .sort ()
5157
5258 print (f"Found { len (patch_names )} patches to apply:" )
53- for patch_name in patch_names :
54- print (patch_name )
59+ print ("\n " .join (patch_names ))
5560
5661 if args .method == "am" :
57- merge_args = [ "git" , "am" , "-k" , "--ignore-whitespace" , "--3way" ]
62+ merge_args = git_cmd + [ "am" , "-k" , "--ignore-whitespace" , "--3way" ]
5863 for patch_name in patch_names :
59- merge_args .append (os .path .join (args .patchdir , patch_name ))
60- p = subprocess .run (
61- merge_args , cwd = args .llvm_dir , capture_output = True , text = True
62- )
64+ merge_args .append (os .path .join (abs_patch_dir , patch_name ))
65+ p = subprocess .run (merge_args , capture_output = True , text = True )
6366 print (p .stdout )
6467 print (p .stderr )
6568
@@ -68,61 +71,58 @@ def main():
6871 sys .exit (0 )
6972 if args .restore_on_fail :
7073 # Check that the operation can be aborted.
71- if (
72- 'To restore the original branch and stop patching, run "git am --abort".'
73- in p .stdout
74- ):
74+ # git am does give any specific return codes,
75+ # so check for unresolved working files.
76+ if os .path .isdir (os .path .join (args .llvm_dir , ".git" , "rebase-apply" )):
7577 print ("Aborting git am..." )
76- subprocess .run (["git" , "am" , "--abort" ], cwd = args .llvm_dir , check = True )
78+ subprocess .run (git_cmd + ["am" , "--abort" ], check = True )
79+ print (f"Abort successful." )
7780 sys .exit (2 )
81+ else :
82+ print ("Unable to abort." )
7883 sys .exit (1 )
7984 else :
8085 applied_patches = []
8186 for patch_name in patch_names :
82- patch_file = os .path .join (args . patchdir , patch_name )
83- print (f"Checking { patch_file } ..." )
87+ patch_file = os .path .join (abs_patch_dir , patch_name )
88+ print (f"Checking { patch_name } ..." )
8489 # Check that the patch applies before trying to apply it.
85- apply_check_args = [
86- "git" ,
90+ apply_check_args = git_cmd + [
8791 "apply" ,
8892 "--ignore-whitespace" ,
8993 "--3way" ,
9094 "--check" ,
9195 patch_file ,
9296 ]
93- p_check = subprocess .run (apply_check_args , cwd = args . llvm_dir )
97+ p_check = subprocess .run (apply_check_args )
9498
9599 if p_check .returncode == 0 :
96100 # Patch will apply.
97- print (f"Applying { patch_file } ..." )
98- apply_args = [
99- "git" ,
101+ print (f"Applying { patch_name } ..." )
102+ apply_args = git_cmd + [
100103 "apply" ,
101104 "--ignore-whitespace" ,
102105 "--3way" ,
103106 patch_file ,
104107 ]
105- apply_args = subprocess .run (apply_args , cwd = args . llvm_dir , check = True )
106- applied_patches .append (patch_file )
108+ apply_args = subprocess .run (apply_args , check = True )
109+ applied_patches .append (patch_name )
107110 else :
108111 # Patch won't apply.
109- print (f"Unable to apply { patch_file } " )
112+ print (f"Unable to apply { patch_name } " )
110113 if args .restore_on_fail :
111114 # Remove any patches that have already been applied.
112115 while len (applied_patches ) > 0 :
113116 r_patch = applied_patches .pop ()
114117 print (f"Reversing { r_patch } ..." )
115- reverse_args = [
116- "git" ,
118+ reverse_args = git_cmd + [
117119 "apply" ,
118120 "--ignore-whitespace" ,
119121 "--3way" ,
120122 "--reverse" ,
121- r_patch ,
123+ os . path . join ( abs_patch_dir , r_patch ) ,
122124 ]
123- p_check = subprocess .run (
124- reverse_args , cwd = args .llvm_dir , check = True
125- )
125+ p_check = subprocess .run (reverse_args , check = True )
126126 print (f"Rollback successful, failure occured on { patch_file } " )
127127 sys .exit (2 )
128128 sys .exit (1 )
0 commit comments