Skip to content

Commit e732141

Browse files
brlin-twnewren
authored andcommitted
Fix relative path compatibility for --replace-text and bfg_args.repo
Users could specify relative paths on the command line, and then also provide a directory other than '.' for the repo. Since we did an unconditional os.chdir() to move into the repo, that would invalidate the original relative paths. Fix that by changing the relative paths into absolute paths. Signed-off-by: 林博仁(Buo-ren Lin) <[email protected]> [en: tweaked commit message to explain the problem] Signed-off-by: Elijah Newren <[email protected]>
1 parent 75e67bc commit e732141

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

contrib/filter-repo-demos/bfg-ish

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ class BFG_ish:
371371
bfg_args = self.parse_options()
372372
preserve_refs = self.get_preservation_info(bfg_args.preserve_ref_tips)
373373

374+
work_dir = os.getcwd()
374375
os.chdir(bfg_args.repo)
375376
bfg_args.delete_files = java_to_fnmatch_glob(bfg_args.delete_files)
376377
bfg_args.delete_folders = java_to_fnmatch_glob(bfg_args.delete_folders)
@@ -395,6 +396,9 @@ class BFG_ish:
395396
extra_args += ['--preserve-commit-hashes']
396397
new_replace_file = None
397398
if bfg_args.replace_text:
399+
if not os.path.isabs(bfg_args.replace_text):
400+
bfg_args.replace_text = os.path.join(work_dir, bfg_args.replace_text)
401+
398402
new_replace_file = self.convert_replace_text(bfg_args.replace_text)
399403
rules = fr.FilteringOptions.get_replace_text(new_replace_file)
400404
self.replacement_rules = rules
@@ -432,6 +436,9 @@ class BFG_ish:
432436
if not fr.GitUtils.is_repository_bare('.'):
433437
need_another_reset = True
434438

439+
if not os.path.isabs(os.fsdecode(bfg_args.repo)):
440+
bfg_args.repo = os.fsencode(os.path.join(work_dir, os.fsdecode(bfg_args.repo)))
441+
435442
fr.RepoFilter.cleanup(bfg_args.repo, repack=True, reset=need_another_reset)
436443

437444
if __name__ == '__main__':

0 commit comments

Comments
 (0)