diff --git a/git-filter-repo b/git-filter-repo index b91bd96..ff1c038 100755 --- a/git-filter-repo +++ b/git-filter-repo @@ -1758,6 +1758,12 @@ EXAMPLES help=_("Analyze repository history and create a report that may be " "useful in determining what to filter in a subsequent run. " "Will not modify your repo.")) + analyze.add_argument('--report-dir', + metavar='DIR_OR_FILE', + type=os.fsencode, + dest='report_dir', + help=_("Directory to write report, defaults to GIT_DIR/filter_repo/analysis," + "refuses to run if exists, --force delete existing dir first.")) path = parser.add_argument_group(title=_("Filtering based on paths " "(see also --filename-callback)")) @@ -2641,15 +2647,25 @@ class RepoAnalyze(object): @staticmethod def run(args): - git_dir = GitUtils.determine_git_dir(b'.') + if args.report_dir: + reportdir = args.report_dir + else: + git_dir = GitUtils.determine_git_dir(b'.') # Create the report directory as necessary - results_tmp_dir = os.path.join(git_dir, b'filter-repo') - if not os.path.isdir(results_tmp_dir): - os.mkdir(results_tmp_dir) - reportdir = os.path.join(results_tmp_dir, b"analysis") - if not args.force and os.path.isdir(reportdir): - shutil.rmtree(reportdir) + results_tmp_dir = os.path.join(git_dir, b'filter-repo') + if not os.path.isdir(results_tmp_dir): + os.mkdir(results_tmp_dir) + reportdir = os.path.join(results_tmp_dir, b"analysis") + + if os.path.isdir(reportdir): + if args.force: + sys.stdout.write(_("Warning: Removing recursively: \"%s\"") % decode(reportdir)) + shutil.rmtree(reportdir) + else: + sys.stdout.write(_("Error: dir already exists (use --force to delete): \"%s\"\n") % decode(reportdir)) + sys.exit(1) + os.mkdir(reportdir) # Gather the data we need diff --git a/t/t9390-filter-repo.sh b/t/t9390-filter-repo.sh index 3f567e7..8db16d9 100755 --- a/t/t9390-filter-repo.sh +++ b/t/t9390-filter-repo.sh @@ -722,8 +722,11 @@ test_expect_success C_LOCALE_OUTPUT '--analyze' ' git filter-repo --analyze && - # It should work and overwrite report if run again - git filter-repo --analyze && + # It should not work again without a --force + test_must_fail git filter-repo --analyze && + + # With a --force, another run should succeed + git filter-repo --analyze --force && test -d .git/filter-repo/analysis && cd .git/filter-repo/analysis && @@ -824,6 +827,38 @@ test_expect_success C_LOCALE_OUTPUT '--analyze' ' ) ' +test_expect_success C_LOCALE_OUTPUT '--analyze --report-dir' ' + setup_analyze_me && + ( + cd analyze_me && + + rm -rf .git/filter-repo && + git filter-repo --analyze --report-dir foobar && + + # It should not work again without a --force + test_must_fail git filter-repo --analyze --report-dir foobar && + + # With a --force, though, it should overwrite + git filter-repo --analyze --report-dir foobar --force && + + test ! -d .git/filter-repo/analysis && + test -d foobar && + + cd foobar && + + # Very simple tests because already tested above. + test_path_is_file renames.txt && + test_path_is_file README && + test_path_is_file blob-shas-and-paths.txt && + test_path_is_file directories-all-sizes.txt && + test_path_is_file directories-deleted-sizes.txt && + test_path_is_file extensions-all-sizes.txt && + test_path_is_file extensions-deleted-sizes.txt && + test_path_is_file path-all-sizes.txt && + test_path_is_file path-deleted-sizes.txt + ) +' + test_expect_success '--replace-text all options' ' setup_analyze_me && (