@@ -417,17 +417,26 @@ def text_to_patch(text: str, orig: dict[str, str]) -> tuple[Patch, int]:
417
417
return parser .patch , parser .fuzz
418
418
419
419
420
- def identify_files_needed (text : str ) -> list [str ]:
420
+ def identify_files_needed (text : str , prefix : str | None = None ) -> list [str ]:
421
421
lines = text .splitlines ()
422
- return [line [len ("*** Update File: " ) :] for line in lines if line .startswith ("*** Update File: " )] + [
423
- line [len ("*** Delete File: " ) :] for line in lines if line .startswith ("*** Delete File: " )
424
- ]
422
+ update_files = [line [len ("*** Update File: " ):] for line in lines if line .startswith ("*** Update File: " )]
423
+ delete_files = [line [len ("*** Delete File: " ):] for line in lines if line .startswith ("*** Delete File: " )]
424
+ all_files = update_files + delete_files
425
+
426
+ if prefix is None :
427
+ return all_files
428
+ else :
429
+ return [f"{ prefix } /{ file } " if prefix else file for file in all_files ]
425
430
426
431
427
- def identify_files_added (text : str ) -> list [str ]:
432
+ def identify_files_added (text : str , prefix : str | None = None ) -> list [str ]:
428
433
lines = text .splitlines ()
429
- return [line [len ("*** Add File: " ) :] for line in lines if line .startswith ("*** Add File: " )]
430
-
434
+ added_files = [line [len ("*** Add File: " ):] for line in lines if line .startswith ("*** Add File: " )]
435
+
436
+ if prefix is None :
437
+ return added_files
438
+ else :
439
+ return [f"{ prefix } /{ file } " if prefix else file for file in added_files ]
431
440
432
441
# --------------------------------------------------------------------------- #
433
442
# File-system helpers
@@ -468,10 +477,11 @@ def process_patch(
468
477
write_fn : Callable [[str , str ], None ],
469
478
remove_fn : Callable [[str ], None ],
470
479
inplace : bool = False ,
480
+ prefix : str | None = None
471
481
) -> str :
472
482
if not text .startswith ("*** Begin Patch" ):
473
483
raise DiffError ("Patch text must start with *** Begin Patch" )
474
- paths = identify_files_needed (text )
484
+ paths = identify_files_needed (text , prefix )
475
485
orig = load_files (paths , open_fn )
476
486
patch , _fuzz = text_to_patch (text , orig )
477
487
commit = patch_to_commit (patch , orig )
@@ -501,13 +511,13 @@ def remove_file(path: str) -> None:
501
511
# --------------------------------------------------------------------------- #
502
512
# CLI entry-point
503
513
# --------------------------------------------------------------------------- #
504
- def apply_patch_from_text (patch_text : str , inplace : bool = False ) -> str :
514
+ def apply_patch_from_text (patch_text : str , inplace : bool = False , prefix : str | None = None ) -> str :
505
515
"""Apply patch text to filesystem, same as main() but with parameter input"""
506
516
if not patch_text :
507
517
raise DiffError ("Patch text cannot be empty" )
508
518
509
519
try :
510
- result = process_patch (patch_text , open_file , write_file , remove_file , inplace )
520
+ result = process_patch (patch_text , open_file , write_file , remove_file , inplace , prefix )
511
521
return result
512
522
except DiffError as exc :
513
523
raise exc
0 commit comments