@@ -121,9 +121,10 @@ def parse_args():
121121 parser = argparse .ArgumentParser ()
122122 parser .add_argument ('-o' , '--output' , type = str )
123123 parser .add_argument ('--platform' , type = str , required = True )
124- parser .add_argument ('--git-version' , type = str , required = True )
125- parser .add_argument ('--git-tag' , type = str , required = True )
126- parser .add_argument ('--git-branch' , type = str , required = True )
124+ parser .add_argument ('--git-program' , type = str )
125+ parser .add_argument ('--git-version' , type = str )
126+ parser .add_argument ('--git-tag' , type = str )
127+ parser .add_argument ('--git-branch' , type = str )
127128 parser .add_argument ('--compiler' , type = str )
128129 parser .add_argument ('--free-threading' , type = int )
129130 return parser .parse_args ()
@@ -228,11 +229,25 @@ def main():
228229 args = parse_args ()
229230 output_filename = args .output
230231 platform = args .platform
232+ git_program = args .git_program
231233 git_version = args .git_version
232234 git_tag = args .git_tag
233235 git_branch = args .git_branch
234236 free_threading = args .free_threading
235237
238+ if git_program :
239+ git_dir = os .path .join (SRC_DIR , '.git' )
240+ git_program = [git_program , '--git-dir' , git_dir ]
241+ if git_version is None :
242+ cmd = [* git_program , 'rev-parse' , '--short' , 'HEAD' ]
243+ git_version = run_command (cmd , check = False ) or ''
244+ if git_branch is None :
245+ cmd = [* git_program , 'describe' , '--all' , '--always' , '--dirty' ]
246+ git_branch = run_command (cmd , check = False ) or ''
247+ if git_tag is None :
248+ cmd = [* git_program , 'name-rev' , '--name-only' , 'HEAD' ]
249+ git_tag = run_command (cmd , check = False ) or ''
250+
236251 if not output_filename :
237252 # Write to the current directory
238253 output_filename = os .path .join ('Modules' , 'getbuildinfo.h' )
@@ -258,6 +273,10 @@ def main():
258273 new_filename = output_filename + ".new"
259274 with open (new_filename , "w" , encoding = "utf8" ) as fp :
260275 def write_macro (name , value ):
276+ # Escape quote
277+ value = value .replace ('"' , '\\ "' )
278+ if '\0 ' in value :
279+ raise ValueError (f'embedded null character: { value !r} ' )
261280 print (f'#define { name } "{ value } "' , file = fp )
262281
263282 print (f'// Header file auto-generated by { SCRIPT_FULLNAME } ' , file = fp )
0 commit comments