@@ -69,9 +69,12 @@ def search_command(terms: List[str]):
6969 cfbs_dir ,
7070 cfbs_filename ,
7171 display_diff ,
72+ file_diff_text ,
7273 is_cfbs_repo ,
74+ mkdir ,
7375 read_json ,
7476 CFBSExitError ,
77+ save_file ,
7578 strip_right ,
7679 pad_right ,
7780 CFBSProgrammerError ,
@@ -1127,6 +1130,7 @@ def cfbs_convert_cleanup():
11271130 def cfbs_convert_git_commit (
11281131 commit_message : str , add_scope : Union [str , Iterable [str ]] = "all"
11291132 ):
1133+ print ("Creating Git commit..." )
11301134 try :
11311135 git_commit_maybe_prompt (commit_message , non_interactive , scope = add_scope )
11321136 except CFBSGitError :
@@ -1263,7 +1267,6 @@ def cfbs_convert_git_commit(
12631267 for unmodified_mpf_file in analyzed_files .unmodified :
12641268 rm (os .path .join (dir_name , unmodified_mpf_file ))
12651269
1266- print ("Creating Git commit..." )
12671270 cfbs_convert_git_commit ("Deleted unmodified policy files" )
12681271
12691272 print (
@@ -1310,9 +1313,6 @@ def cfbs_convert_git_commit(
13101313 for file_d in files_to_delete :
13111314 rm (os .path .join (dir_name , file_d ))
13121315
1313- print (
1314- "Creating Git commit with deletion of policy files from other versions..."
1315- )
13161316 cfbs_convert_git_commit ("Deleted policy files from other versions" )
13171317 print ("Done." , end = " " )
13181318 else :
@@ -1322,6 +1322,8 @@ def cfbs_convert_git_commit(
13221322 )
13231323 if not prompt_user_yesno (non_interactive , "Do you want to continue?" ):
13241324 raise CFBSExitError ("User did not proceed, exiting." )
1325+
1326+ first_patch_conversion = True
13251327 print ("The following files have custom modifications:" )
13261328 modified_files = analyzed_files .modified
13271329 for modified_file in modified_files :
@@ -1335,6 +1337,7 @@ def cfbs_convert_git_commit(
13351337 mpf_dir_path , masterfiles_version , "tarball" , "masterfiles"
13361338 )
13371339 mpf_filepath = os .path .join (mpf_version_dir_path , modified_file )
1340+ modified_file_path = os .path .join (dir_name , modified_file )
13381341 display_diffs = True
13391342 if not os .path .exists (mpf_version_dir_path ):
13401343 try :
@@ -1347,7 +1350,7 @@ def cfbs_convert_git_commit(
13471350 display_diffs = False
13481351 if display_diffs :
13491352 try :
1350- display_diff (mpf_filepath , os . path . join ( dir_name , modified_file ) )
1353+ display_diff (mpf_filepath , modified_file_path )
13511354 except :
13521355 log .warning (
13531356 "Displaying a diff between your file and the default file failed, continuing without displaying a diff..."
@@ -1366,22 +1369,85 @@ def cfbs_convert_git_commit(
13661369 prompt_str = "\n Choose an option:\n "
13671370 prompt_str += "1) Drop modifications - They are not important, or can be achieved in another way.\n "
13681371 prompt_str += "2) Keep modified file - File is kept as is, and can be handled later. Can make future upgrades more complicated.\n "
1369- prompt_str += "3) (Not implemented yet) Keep patch file - "
1370- prompt_str += "File is converted into a patch file (diff) that hopefully will apply to future versions as well.\n "
1372+ prompt_str += "3) Keep patch file - File is converted into a patch file (diff) that hopefully will apply to future versions as well.\n "
13711373
1372- response = prompt_user (non_interactive , prompt_str , ["1" , "2" ], "1" )
1374+ response = prompt_user (non_interactive , prompt_str , ["1" , "2" , "3" ], "1" )
13731375
13741376 if response == "1" :
13751377 print ("Deleting './%s'..." % modified_file )
1376- rm (os .path .join (dir_name , modified_file ))
1377- commit_message = "Deleted './%s'" % modified_file
1378- print ("Creating Git commit - %s..." % commit_message )
1378+ rm (modified_file_path )
13791379 try :
1380- cfbs_convert_git_commit (commit_message )
1380+ cfbs_convert_git_commit ("Deleted './%s'" % modified_file )
13811381 except :
13821382 log .warning ("Git commit failed, continuing without committing..." )
13831383 if response == "2" :
13841384 print ("Keeping file as is, nothing to do." )
1385+ if response == "3" :
1386+ print ("Converting './%s' into a patch file..." % modified_file )
1387+ patches_dir = "custom-masterfiles-patches"
1388+ patches_module = "./" + patches_dir + "/"
1389+
1390+ file_diff_data = file_diff_text (
1391+ mpf_filepath ,
1392+ modified_file_path ,
1393+ modified_file ,
1394+ modified_file ,
1395+ )
1396+
1397+ patch_filename = modified_file .replace ("/" , "_" ) + ".patch"
1398+ patch_path = os .path .join (patches_dir , patch_filename )
1399+ # append a number if there are multiple files with the same filename
1400+ i = 1
1401+ while os .path .exists (patch_path ):
1402+ patch_filename = (
1403+ modified_file .replace ("/" , "_" ) + "-" + str (i ) + ".patch"
1404+ )
1405+ patch_path = os .path .join (patches_dir , patch_filename )
1406+ i += 1
1407+
1408+ # saving the .patch file should be done before creating the patches module
1409+ try :
1410+ save_file (patch_path , file_diff_data )
1411+ except :
1412+ log .warning (
1413+ "Saving the patch file failed - keeping the modified file as is instead and continuing..."
1414+ )
1415+ continue
1416+
1417+ # make the patches local module on first use
1418+ if first_patch_conversion :
1419+ print ("Adding patches local module..." )
1420+ mkdir (patches_dir )
1421+
1422+ config = CFBSConfig .get_instance ()
1423+ # `explicit_build_steps=[]` would fail validation, therefore also add the first build step during the module's creation
1424+ config .add_command (
1425+ [patches_module ],
1426+ added_by = "cfbs convert" ,
1427+ explicit_build_steps = ["patch " + patch_filename ],
1428+ )
1429+ config .save ()
1430+ else :
1431+ config = CFBSConfig .get_instance ()
1432+ config .add_patch_step (patches_module , patch_filename )
1433+
1434+ print ("Deleting './%s'..." % modified_file )
1435+ rm (modified_file_path )
1436+
1437+ try :
1438+ if first_patch_conversion :
1439+ cfbs_convert_git_commit (
1440+ "Added patches local module, converted './%s' into a .patch file"
1441+ % modified_file
1442+ )
1443+ else :
1444+ cfbs_convert_git_commit (
1445+ "Converted './%s' into a .patch file" % modified_file
1446+ )
1447+ except :
1448+ log .warning ("Git commit failed, continuing without committing..." )
1449+
1450+ first_patch_conversion = False
13851451
13861452 print ("Conversion finished successfully." )
13871453
0 commit comments