From cbacb6cd821164f8c999ba7f3774294338ff35b9 Mon Sep 17 00:00:00 2001 From: Elijah Newren Date: Sat, 9 Feb 2019 18:24:37 -0800 Subject: [PATCH] filter-repo: simplify import in lib-usage examples Python wants filenames with underscores instead of hyphens and with a .py extension. We really want the main file named git-filter-repo, but we can add a git_filter_repo.py symlink. Doing so dramatically simplifies the steps needed to import it as a library in external python scripts. Signed-off-by: Elijah Newren --- git_filter_repo.py | 1 + t/t9391-filter-repo-lib-usage.sh | 5 ++++ t/t9391/commit_info.py | 27 ++++++------------- t/t9391/create_fast_export_output.py | 30 +++++++-------------- t/t9391/file_filter.py | 19 ++++---------- t/t9391/print_progress.py | 23 +++++----------- t/t9391/rename-master-to-develop.py | 16 +++--------- t/t9391/splice_repos.py | 39 ++++++++++------------------ t/t9391/strip-cvs-keywords.py | 17 +++--------- 9 files changed, 55 insertions(+), 122 deletions(-) create mode 120000 git_filter_repo.py diff --git a/git_filter_repo.py b/git_filter_repo.py new file mode 120000 index 0000000..bc98e38 --- /dev/null +++ b/git_filter_repo.py @@ -0,0 +1 @@ +git-filter-repo \ No newline at end of file diff --git a/t/t9391-filter-repo-lib-usage.sh b/t/t9391-filter-repo-lib-usage.sh index 1f476c9..362ae4b 100755 --- a/t/t9391-filter-repo-lib-usage.sh +++ b/t/t9391-filter-repo-lib-usage.sh @@ -3,6 +3,11 @@ test_description='Usage of git-filter-repo as a library' . ./test-lib.sh +# for git_filter_repo.py import +export PYTHONPATH=$(dirname $TEST_DIRECTORY):$PYTHONPATH +# Avoid writing git_filter_repo.pyc file +export PYTHONDONTWRITEBYTECODE=1 + setup() { git init $1 && diff --git a/t/t9391/commit_info.py b/t/t9391/commit_info.py index 71a5dfe..a6df323 100755 --- a/t/t9391/commit_info.py +++ b/t/t9391/commit_info.py @@ -1,20 +1,9 @@ #!/usr/bin/env python - -# python makes importing files with dashes hard, sorry. Renaming would -# allow us to simplify this to -# import git_repo_filter -# However, since git style commands are dashed and git-filter-repo is used more -# as a tool than a library, renaming is not an option, so import is 5 lines: -import imp -import sys -sys.dont_write_bytecode = True # .pyc generation -> ugly 'git-filter-repoc' files -with open("../../../git-filter-repo") as f: - repo_filter = imp.load_source('repo_filter', "git-filter-repo", f) -# End of convoluted import of git-filter-repo - import re -from datetime import datetime, timedelta +import datetime + +import git_filter_repo as fr def change_up_them_commits(commit): # Change the commit author @@ -26,14 +15,14 @@ def change_up_them_commits(commit): commit.author_email = re.sub("@my.crp", "@my.corp", commit.author_email) # Fix the committer date (bad timezone conversion in initial import) - oldtime = repo_filter.string_to_date(commit.committer_date) - newtime = oldtime + timedelta(hours=-5) - commit.committer_date = repo_filter.date_to_string(newtime) + oldtime = fr.string_to_date(commit.committer_date) + newtime = oldtime + datetime.timedelta(hours=-5) + commit.committer_date = fr.date_to_string(newtime) # Fix the commit message commit.message = re.sub("Marketing is staffed with pansies", "", commit.message) -args = repo_filter.FilteringOptions.parse_args(['--force']) -filter = repo_filter.RepoFilter(args, commit_callback = change_up_them_commits) +args = fr.FilteringOptions.parse_args(['--force']) +filter = fr.RepoFilter(args, commit_callback = change_up_them_commits) filter.run() diff --git a/t/t9391/create_fast_export_output.py b/t/t9391/create_fast_export_output.py index 1b99db2..7136cd2 100755 --- a/t/t9391/create_fast_export_output.py +++ b/t/t9391/create_fast_export_output.py @@ -1,23 +1,13 @@ #!/usr/bin/env python -# It would be nice if we could just -# import git_repo_filter -# but that'd require renaming git-filter-repo to git_repo_filter.py, which would -# be detrimental to its predominant usage as a tool rather than a library. So, -# we use the next five lines instead. -import imp -import sys -sys.dont_write_bytecode = True # .pyc generation -> ugly 'git-filter-repoc' files -with open("../../../git-filter-repo") as f: - repo_filter = imp.load_source('repo_filter', "git-filter-repo", f) -# End of import workaround -from repo_filter import Blob, Reset, FileChanges, Commit, Tag, FixedTimeZone -from repo_filter import Progress, Checkpoint +import git_filter_repo as fr +from git_filter_repo import Blob, Reset, FileChanges, Commit, Tag, FixedTimeZone +from git_filter_repo import Progress, Checkpoint from datetime import datetime, timedelta -args = repo_filter.FilteringOptions.default_options() -out = repo_filter.RepoFilter(args) +args = fr.FilteringOptions.default_options() +out = fr.RepoFilter(args) out.importer_only() output = out._output @@ -36,7 +26,7 @@ changes = [FileChanges('M', 'world', world.id, mode="100644"), when = datetime(year=2005, month=4, day=7, hour=15, minute=16, second=10, tzinfo=FixedTimeZone("-0700")) -when_string = repo_filter.date_to_string(when) +when_string = fr.date_to_string(when) commit1 = Commit("refs/heads/master", "A U Thor", "au@thor.email", when_string, "Com M. Iter", "comm@iter.email", when_string, @@ -54,7 +44,7 @@ world_link.dump(output) changes = [FileChanges('M', 'world', world.id, mode="100644"), FileChanges('M', 'planet', world_link.id, mode="120000")] when += timedelta(days=3, hours=4, minutes=6) -when_string = repo_filter.date_to_string(when) +when_string = fr.date_to_string(when) commit2 = Commit("refs/heads/master", "A U Thor", "au@thor.email", when_string, "Com M. Iter", "comm@iter.email", when_string, @@ -91,7 +81,7 @@ world.dump(output) changes = [FileChanges('M', 'world', world.id, mode="100644")] when = datetime(2006, 8, 17, tzinfo=FixedTimeZone("+0200")) -when_string = repo_filter.date_to_string(when) +when_string = fr.date_to_string(when) commit4 = Commit("refs/heads/devel", "A U Thor", "au@thor.email", when_string, "Com M. Iter", "comm@iter.email", when_string, @@ -103,8 +93,8 @@ commit4.dump(output) world = Blob("Hello\nHi\nGoodbye") world.dump(output) -when = repo_filter.string_to_date(commit3.author_date) + timedelta(days=47) -when_string = repo_filter.date_to_string(when) +when = fr.string_to_date(commit3.author_date) + timedelta(days=47) +when_string = fr.date_to_string(when) # git fast-import requires file changes to be listed in terms of differences # to the first parent. Thus, despite the fact that runme and planet have # not changed and bar was not modified in the devel side, we have to list them diff --git a/t/t9391/file_filter.py b/t/t9391/file_filter.py index 0113e2b..7712fde 100755 --- a/t/t9391/file_filter.py +++ b/t/t9391/file_filter.py @@ -1,16 +1,7 @@ #!/usr/bin/env python -# python makes importing files with dashes hard, sorry. Renaming would -# allow us to simplify this to -# import git_repo_filter -# However, since git style commands are dashed and git-filter-repo is used more -# as a tool than a library, renaming is not an option, so import is 5 lines: -import imp import sys -sys.dont_write_bytecode = True # .pyc generation -> ugly 'git-filter-repoc' files -with open("../../../git-filter-repo") as f: - repo_filter = imp.load_source('repo_filter', "git-filter-repo", f) -# End of convoluted import of git-filter-repo +import git_filter_repo as fr def drop_file_by_contents(blob): bad_file_contents = 'The launch code is 1-2-3-4.' @@ -25,9 +16,9 @@ def drop_files_by_name(commit): commit.file_changes = new_file_changes sys.argv.append('--force') -args = repo_filter.FilteringOptions.parse_args(sys.argv[1:]) +args = fr.FilteringOptions.parse_args(sys.argv[1:]) -filter = repo_filter.RepoFilter(args, - blob_callback = drop_file_by_contents, - commit_callback = drop_files_by_name) +filter = fr.RepoFilter(args, + blob_callback = drop_file_by_contents, + commit_callback = drop_files_by_name) filter.run() diff --git a/t/t9391/print_progress.py b/t/t9391/print_progress.py index 7b45718..e2b469c 100755 --- a/t/t9391/print_progress.py +++ b/t/t9391/print_progress.py @@ -1,24 +1,15 @@ #!/usr/bin/env python -# python makes importing files with dashes hard, sorry. Renaming would -# allow us to simplify this to -# import git_repo_filter -# However, since git style commands are dashed and git-filter-repo is used more -# as a tool than a library, renaming is not an option, so import is 5 lines: -import imp import sys -sys.dont_write_bytecode = True # .pyc generation -> ugly 'git-filter-repoc' files -with open("../../../git-filter-repo") as f: - repo_filter = imp.load_source('repo_filter', "git-filter-repo", f) -# End of convoluted import of git-filter-repo +import git_filter_repo as fr if len(sys.argv) != 3: raise SystemExit("Syntax:\n %s SOURCE_REPO TARGET_REPO") source_repo = sys.argv[1] target_repo = sys.argv[2] -total_objects = repo_filter.GitUtils.get_total_objects(source_repo) # blobs+trees -total_commits = repo_filter.GitUtils.get_commit_count(source_repo) +total_objects = fr.GitUtils.get_total_objects(source_repo) # blobs+trees +total_commits = fr.GitUtils.get_commit_count(source_repo) object_count = 0 commit_count = 0 @@ -37,8 +28,8 @@ def my_commit_callback(commit): commit_count += 1 print_progress() -args = repo_filter.FilteringOptions.parse_args(['--force', '--quiet']) -filter = repo_filter.RepoFilter(args, - blob_callback = my_blob_callback, - commit_callback = my_commit_callback) +args = fr.FilteringOptions.parse_args(['--force', '--quiet']) +filter = fr.RepoFilter(args, + blob_callback = my_blob_callback, + commit_callback = my_commit_callback) filter.run() diff --git a/t/t9391/rename-master-to-develop.py b/t/t9391/rename-master-to-develop.py index 264a308..c52c63e 100755 --- a/t/t9391/rename-master-to-develop.py +++ b/t/t9391/rename-master-to-develop.py @@ -1,22 +1,12 @@ #!/usr/bin/env python -# python makes importing files with dashes hard, sorry. Renaming would -# allow us to simplify this to -# import git_repo_filter -# However, since git style commands are dashed and git-filter-repo is used more -# as a tool than a library, renaming is not an option, so import is 5 lines: -import imp -import sys -sys.dont_write_bytecode = True # .pyc generation -> ugly 'git-filter-repoc' files -with open("../../../git-filter-repo") as f: - repo_filter = imp.load_source('repo_filter', "git-filter-repo", f) -# End of convoluted import of git-filter-repo +import git_filter_repo as fr def my_commit_callback(commit): if commit.branch == "refs/heads/master": commit.branch = "refs/heads/develop" -args = repo_filter.FilteringOptions.default_options() +args = fr.FilteringOptions.default_options() args.force = True -filter = repo_filter.RepoFilter(args, commit_callback = my_commit_callback) +filter = fr.RepoFilter(args, commit_callback = my_commit_callback) filter.run() diff --git a/t/t9391/splice_repos.py b/t/t9391/splice_repos.py index e789a15..3d1f39d 100755 --- a/t/t9391/splice_repos.py +++ b/t/t9391/splice_repos.py @@ -1,19 +1,8 @@ #!/usr/bin/env python -# python makes importing files with dashes hard, sorry. Renaming would -# allow us to simplify this to -# import git_repo_filter -# However, since git style commands are dashed and git-filter-repo is used more -# as a tool than a library, renaming is not an option, so import is 5 lines: -import imp -import sys -sys.dont_write_bytecode = True # .pyc generation -> ugly 'git-filter-repoc' files -with open("../../git-filter-repo") as f: - repo_filter = imp.load_source('repo_filter', "git-filter-repo", f) -# End of convoluted import of git-filter-repo - -import datetime import re +import sys +import git_filter_repo as fr class InterleaveRepositories: def __init__(self, repo1, repo2, output_dir): @@ -50,30 +39,28 @@ class InterleaveRepositories: # on commit.id if prev_letter in self.commit_map: self.last_commit = commit.id - repo_filter.record_id_rename(new_commit.id, commit.id) + fr.record_id_rename(new_commit.id, commit.id) def run(self): - blob = repo_filter.Blob('public gpg key contents') - when = datetime.datetime(2006, 1, 2, 3, 4, 5, - tzinfo=repo_filter.FixedTimeZone("+0300")) - tag = repo_filter.Tag('gpg-pubkey', blob.id, - 'Ima Tagger', 'ima@tagg.er', when, - 'Very important explanation and stuff') + blob = fr.Blob('public gpg key contents') + tag = fr.Tag('gpg-pubkey', blob.id, + 'Ima Tagger', 'ima@tagg.er', '1136199845 +0300', + 'Very important explanation and stuff') - args = repo_filter.FilteringOptions.parse_args(['--target', self.output_dir]) - out = repo_filter.RepoFilter(args) + args = fr.FilteringOptions.parse_args(['--target', self.output_dir]) + out = fr.RepoFilter(args) out.importer_only() self.out = out - i1args = repo_filter.FilteringOptions.parse_args(['--source', self.repo1]) - i1 = repo_filter.RepoFilter(i1args, + i1args = fr.FilteringOptions.parse_args(['--source', self.repo1]) + i1 = fr.RepoFilter(i1args, reset_callback = lambda r: self.skip_reset(r), commit_callback = lambda c: self.hold_commit(c)) i1.set_output(out) i1.run() - i2args = repo_filter.FilteringOptions.parse_args(['--source', self.repo2]) - i2 = repo_filter.RepoFilter(i2args, + i2args = fr.FilteringOptions.parse_args(['--source', self.repo2]) + i2 = fr.RepoFilter(i2args, commit_callback = lambda c: self.weave_commit(c)) i2.set_output(out) i2.run() diff --git a/t/t9391/strip-cvs-keywords.py b/t/t9391/strip-cvs-keywords.py index 247a637..7ce589c 100755 --- a/t/t9391/strip-cvs-keywords.py +++ b/t/t9391/strip-cvs-keywords.py @@ -1,18 +1,7 @@ #!/usr/bin/env python -# python makes importing files with dashes hard, sorry. Renaming would -# allow us to simplify this to -# import git_repo_filter -# However, since git style commands are dashed and git-filter-repo is used more -# as a tool than a library, renaming is not an option, so import is 5 lines: -import imp -import sys -sys.dont_write_bytecode = True # .pyc generation -> ugly 'git-filter-repoc' files -with open("../../../git-filter-repo") as f: - repo_filter = imp.load_source('repo_filter', "git-filter-repo", f) -# End of convoluted import of git-filter-repo - import re +import git_filter_repo as fr def strip_cvs_keywords(blob): # FIXME: Should first check if blob is a text file to avoid ruining @@ -22,6 +11,6 @@ def strip_cvs_keywords(blob): replacement = r'$\1$' blob.data = re.sub(pattern, replacement, blob.data) -args = repo_filter.FilteringOptions.parse_args(['--force']) -filter = repo_filter.RepoFilter(args, blob_callback = strip_cvs_keywords) +args = fr.FilteringOptions.parse_args(['--force']) +filter = fr.RepoFilter(args, blob_callback = strip_cvs_keywords) filter.run()