Skip to content

Commit e9d5ab3

Browse files
rndbitnewren
authored andcommitted
filter-repo: add option --report-dir to set custom analysis dir
--analyze is hardcoded to write to a subdirectory inside GIT_DIR. When practicing filtering runs on a large repo it is desirable to keep an unchanged copy read-only to reduce chance of user error. It is desirable to be able to analyze a read-only repo without having to clone it. This would save a lot of time and space. Add --report-dir option to set a non-default destination directory for writing analysis output to. Signed-off-by: rndbit <[email protected]> [en: fixed existing regression test broken by now not overwriting the analysis directory unconditionally, and also added a new test of the new behavior for code coverage.] Signed-off-by: Elijah Newren <[email protected]>
1 parent c5af37f commit e9d5ab3

File tree

2 files changed

+60
-9
lines changed

2 files changed

+60
-9
lines changed

git-filter-repo

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,6 +1758,12 @@ EXAMPLES
17581758
help=_("Analyze repository history and create a report that may be "
17591759
"useful in determining what to filter in a subsequent run. "
17601760
"Will not modify your repo."))
1761+
analyze.add_argument('--report-dir',
1762+
metavar='DIR_OR_FILE',
1763+
type=os.fsencode,
1764+
dest='report_dir',
1765+
help=_("Directory to write report, defaults to GIT_DIR/filter_repo/analysis,"
1766+
"refuses to run if exists, --force delete existing dir first."))
17611767

17621768
path = parser.add_argument_group(title=_("Filtering based on paths "
17631769
"(see also --filename-callback)"))
@@ -2641,15 +2647,25 @@ class RepoAnalyze(object):
26412647

26422648
@staticmethod
26432649
def run(args):
2644-
git_dir = GitUtils.determine_git_dir(b'.')
2650+
if args.report_dir:
2651+
reportdir = args.report_dir
2652+
else:
2653+
git_dir = GitUtils.determine_git_dir(b'.')
26452654

26462655
# Create the report directory as necessary
2647-
results_tmp_dir = os.path.join(git_dir, b'filter-repo')
2648-
if not os.path.isdir(results_tmp_dir):
2649-
os.mkdir(results_tmp_dir)
2650-
reportdir = os.path.join(results_tmp_dir, b"analysis")
2651-
if not args.force and os.path.isdir(reportdir):
2652-
shutil.rmtree(reportdir)
2656+
results_tmp_dir = os.path.join(git_dir, b'filter-repo')
2657+
if not os.path.isdir(results_tmp_dir):
2658+
os.mkdir(results_tmp_dir)
2659+
reportdir = os.path.join(results_tmp_dir, b"analysis")
2660+
2661+
if os.path.isdir(reportdir):
2662+
if args.force:
2663+
sys.stdout.write(_("Warning: Removing recursively: \"%s\"") % decode(reportdir))
2664+
shutil.rmtree(reportdir)
2665+
else:
2666+
sys.stdout.write(_("Error: dir already exists (use --force to delete): \"%s\"\n") % decode(reportdir))
2667+
sys.exit(1)
2668+
26532669
os.mkdir(reportdir)
26542670

26552671
# Gather the data we need

t/t9390-filter-repo.sh

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,11 @@ test_expect_success C_LOCALE_OUTPUT '--analyze' '
722722
723723
git filter-repo --analyze &&
724724
725-
# It should work and overwrite report if run again
726-
git filter-repo --analyze &&
725+
# It should not work again without a --force
726+
test_must_fail git filter-repo --analyze &&
727+
728+
# With a --force, another run should succeed
729+
git filter-repo --analyze --force &&
727730
728731
test -d .git/filter-repo/analysis &&
729732
cd .git/filter-repo/analysis &&
@@ -824,6 +827,38 @@ test_expect_success C_LOCALE_OUTPUT '--analyze' '
824827
)
825828
'
826829

830+
test_expect_success C_LOCALE_OUTPUT '--analyze --report-dir' '
831+
setup_analyze_me &&
832+
(
833+
cd analyze_me &&
834+
835+
rm -rf .git/filter-repo &&
836+
git filter-repo --analyze --report-dir foobar &&
837+
838+
# It should not work again without a --force
839+
test_must_fail git filter-repo --analyze --report-dir foobar &&
840+
841+
# With a --force, though, it should overwrite
842+
git filter-repo --analyze --report-dir foobar --force &&
843+
844+
test ! -d .git/filter-repo/analysis &&
845+
test -d foobar &&
846+
847+
cd foobar &&
848+
849+
# Very simple tests because already tested above.
850+
test_path_is_file renames.txt &&
851+
test_path_is_file README &&
852+
test_path_is_file blob-shas-and-paths.txt &&
853+
test_path_is_file directories-all-sizes.txt &&
854+
test_path_is_file directories-deleted-sizes.txt &&
855+
test_path_is_file extensions-all-sizes.txt &&
856+
test_path_is_file extensions-deleted-sizes.txt &&
857+
test_path_is_file path-all-sizes.txt &&
858+
test_path_is_file path-deleted-sizes.txt
859+
)
860+
'
861+
827862
test_expect_success '--replace-text all options' '
828863
setup_analyze_me &&
829864
(

0 commit comments

Comments
 (0)