@@ -256,26 +256,42 @@ def patch_impl(
256256
257257 affected_files = self .__get_patches_affected_files (patches )
258258
259+ def git_add_files ():
260+ run_cmd (['git' , 'add' ] + affected_files )
261+
259262 # Try to apply the changes in reverse, so that they apply cleanly
260263 # forward
261264 with TemporaryWorkingDirectory (tmp_dir ):
262265 run_cmd (['git' , 'init' ])
263- run_cmd ([ 'git' , 'add' ] + affected_files )
266+ git_add_files ( )
264267 run_cmd (['git' , 'commit' , '-m' , 'Initial commit' ])
265268
266- with suppress (Exception ):
267- run_cmd (
268- [
269- 'git' ,
270- 'apply' ,
271- '--reverse' ,
272- '--check' ,
273- ]
274- + patches [::- 1 ]
275- )
276- return
277-
278- run_cmd (['git' , 'apply' ] + patches )
269+ for patch in patches [::- 1 ]:
270+ with suppress (Exception ):
271+ run_cmd (
272+ [
273+ 'git' ,
274+ 'apply' ,
275+ '--verbose' ,
276+ '--reverse' ,
277+ ]
278+ + patch
279+ )
280+ git_add_files ()
281+ run_cmd (['git' , 'commit' , '-m' , f'Revert: "{ patch } "' ])
282+
283+ for patch in patches :
284+ try :
285+ run_cmd (['git' , 'am' , '--reject' , patch ])
286+ except ValueError as e :
287+ color_print (
288+ f'Failed to apply patch { patch } ' ,
289+ color = Color .RED ,
290+ )
291+ color_print ('Git history:' , color = Color .RED )
292+ output = run_cmd (['git' , 'log' ])
293+ print (output )
294+ raise e
279295
280296 def patch_dir (self , patches_path : str ) -> blob_fixup :
281297 impl = partial (self .patch_impl , patches_path )
0 commit comments