1
+ #!/usr/bin/env python3
2
+
3
+ import argparse
4
+ import os , subprocess
5
+
6
+ # accept patch dir and branch name as arguments
7
+ def parse_args ():
8
+ parser = argparse .ArgumentParser (description = "Apply patches to a git repository." )
9
+ parser .add_argument ("patch_dir" , type = str , help = "Directory containing the patch files." )
10
+ parser .add_argument ("branch_name" , type = str , help = "Name of the branch to apply patches to." )
11
+ return parser .parse_args ()
12
+
13
+ def main ():
14
+ args = parse_args ()
15
+ patch_dir = args .patch_dir
16
+ branch_name = args .branch_name
17
+
18
+ print (f"Using patch directory: { patch_dir } " )
19
+ print (f"Using branch name: { branch_name } " )
20
+
21
+ common_patch_dir = patch_dir
22
+ branch_specific_patch_dir = f"{ patch_dir } /{ branch_name } "
23
+
24
+ # collect all patch files from the two directories
25
+ # into a dict [patch_file_name: patch_file_path]
26
+ patch_files = {}
27
+ for dir_path in [common_patch_dir , branch_specific_patch_dir ]:
28
+ if os .path .exists (dir_path ):
29
+ for file_name in os .listdir (dir_path ):
30
+ if file_name .endswith (".patch" ):
31
+ patch_files [file_name ] = os .path .join (dir_path , file_name )
32
+
33
+ # sort patch files by name
34
+ sorted_patch_files = sorted (patch_files .items ())
35
+ for file_name , file_path in sorted_patch_files :
36
+ print (f"Applying patch: { file_name } " )
37
+ # apply the patch using git
38
+ result = subprocess .run (["git" , "apply" , file_path ], check = False )
39
+ if result .returncode != 0 :
40
+ print (f"Failed to apply patch, halting build!" )
41
+ exit (1 )
42
+
43
+
44
+
45
+ if __name__ == "__main__" :
46
+ main ()
0 commit comments