Skip to content

Commit cbacb6c

Browse files
committed
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 <[email protected]>
1 parent 6dba1f2 commit cbacb6c

9 files changed

+55
-122
lines changed

git_filter_repo.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
git-filter-repo

t/t9391-filter-repo-lib-usage.sh

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
test_description='Usage of git-filter-repo as a library'
44
. ./test-lib.sh
55

6+
# for git_filter_repo.py import
7+
export PYTHONPATH=$(dirname $TEST_DIRECTORY):$PYTHONPATH
8+
# Avoid writing git_filter_repo.pyc file
9+
export PYTHONDONTWRITEBYTECODE=1
10+
611
setup()
712
{
813
git init $1 &&

t/t9391/commit_info.py

+8-19
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
11
#!/usr/bin/env python
22

3-
4-
# python makes importing files with dashes hard, sorry. Renaming would
5-
# allow us to simplify this to
6-
# import git_repo_filter
7-
# However, since git style commands are dashed and git-filter-repo is used more
8-
# as a tool than a library, renaming is not an option, so import is 5 lines:
9-
import imp
10-
import sys
11-
sys.dont_write_bytecode = True # .pyc generation -> ugly 'git-filter-repoc' files
12-
with open("../../../git-filter-repo") as f:
13-
repo_filter = imp.load_source('repo_filter', "git-filter-repo", f)
14-
# End of convoluted import of git-filter-repo
15-
163
import re
17-
from datetime import datetime, timedelta
4+
import datetime
5+
6+
import git_filter_repo as fr
187

198
def change_up_them_commits(commit):
209
# Change the commit author
@@ -26,14 +15,14 @@ def change_up_them_commits(commit):
2615
commit.author_email = re.sub("@my.crp", "@my.corp", commit.author_email)
2716

2817
# Fix the committer date (bad timezone conversion in initial import)
29-
oldtime = repo_filter.string_to_date(commit.committer_date)
30-
newtime = oldtime + timedelta(hours=-5)
31-
commit.committer_date = repo_filter.date_to_string(newtime)
18+
oldtime = fr.string_to_date(commit.committer_date)
19+
newtime = oldtime + datetime.timedelta(hours=-5)
20+
commit.committer_date = fr.date_to_string(newtime)
3221

3322
# Fix the commit message
3423
commit.message = re.sub("Marketing is staffed with pansies", "",
3524
commit.message)
3625

37-
args = repo_filter.FilteringOptions.parse_args(['--force'])
38-
filter = repo_filter.RepoFilter(args, commit_callback = change_up_them_commits)
26+
args = fr.FilteringOptions.parse_args(['--force'])
27+
filter = fr.RepoFilter(args, commit_callback = change_up_them_commits)
3928
filter.run()

t/t9391/create_fast_export_output.py

+10-20
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
#!/usr/bin/env python
22

3-
# It would be nice if we could just
4-
# import git_repo_filter
5-
# but that'd require renaming git-filter-repo to git_repo_filter.py, which would
6-
# be detrimental to its predominant usage as a tool rather than a library. So,
7-
# we use the next five lines instead.
8-
import imp
9-
import sys
10-
sys.dont_write_bytecode = True # .pyc generation -> ugly 'git-filter-repoc' files
11-
with open("../../../git-filter-repo") as f:
12-
repo_filter = imp.load_source('repo_filter', "git-filter-repo", f)
13-
# End of import workaround
14-
from repo_filter import Blob, Reset, FileChanges, Commit, Tag, FixedTimeZone
15-
from repo_filter import Progress, Checkpoint
3+
import git_filter_repo as fr
4+
from git_filter_repo import Blob, Reset, FileChanges, Commit, Tag, FixedTimeZone
5+
from git_filter_repo import Progress, Checkpoint
166

177
from datetime import datetime, timedelta
188

19-
args = repo_filter.FilteringOptions.default_options()
20-
out = repo_filter.RepoFilter(args)
9+
args = fr.FilteringOptions.default_options()
10+
out = fr.RepoFilter(args)
2111
out.importer_only()
2212

2313
output = out._output
@@ -36,7 +26,7 @@
3626
when = datetime(year=2005, month=4, day=7,
3727
hour=15, minute=16, second=10,
3828
tzinfo=FixedTimeZone("-0700"))
39-
when_string = repo_filter.date_to_string(when)
29+
when_string = fr.date_to_string(when)
4030
commit1 = Commit("refs/heads/master",
4131
"A U Thor", "[email protected]", when_string,
4232
"Com M. Iter", "[email protected]", when_string,
@@ -54,7 +44,7 @@
5444
changes = [FileChanges('M', 'world', world.id, mode="100644"),
5545
FileChanges('M', 'planet', world_link.id, mode="120000")]
5646
when += timedelta(days=3, hours=4, minutes=6)
57-
when_string = repo_filter.date_to_string(when)
47+
when_string = fr.date_to_string(when)
5848
commit2 = Commit("refs/heads/master",
5949
"A U Thor", "[email protected]", when_string,
6050
"Com M. Iter", "[email protected]", when_string,
@@ -91,7 +81,7 @@
9181

9282
changes = [FileChanges('M', 'world', world.id, mode="100644")]
9383
when = datetime(2006, 8, 17, tzinfo=FixedTimeZone("+0200"))
94-
when_string = repo_filter.date_to_string(when)
84+
when_string = fr.date_to_string(when)
9585
commit4 = Commit("refs/heads/devel",
9686
"A U Thor", "[email protected]", when_string,
9787
"Com M. Iter", "[email protected]", when_string,
@@ -103,8 +93,8 @@
10393

10494
world = Blob("Hello\nHi\nGoodbye")
10595
world.dump(output)
106-
when = repo_filter.string_to_date(commit3.author_date) + timedelta(days=47)
107-
when_string = repo_filter.date_to_string(when)
96+
when = fr.string_to_date(commit3.author_date) + timedelta(days=47)
97+
when_string = fr.date_to_string(when)
10898
# git fast-import requires file changes to be listed in terms of differences
10999
# to the first parent. Thus, despite the fact that runme and planet have
110100
# not changed and bar was not modified in the devel side, we have to list them

t/t9391/file_filter.py

+5-14
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
#!/usr/bin/env python
22

3-
# python makes importing files with dashes hard, sorry. Renaming would
4-
# allow us to simplify this to
5-
# import git_repo_filter
6-
# However, since git style commands are dashed and git-filter-repo is used more
7-
# as a tool than a library, renaming is not an option, so import is 5 lines:
8-
import imp
93
import sys
10-
sys.dont_write_bytecode = True # .pyc generation -> ugly 'git-filter-repoc' files
11-
with open("../../../git-filter-repo") as f:
12-
repo_filter = imp.load_source('repo_filter', "git-filter-repo", f)
13-
# End of convoluted import of git-filter-repo
4+
import git_filter_repo as fr
145

156
def drop_file_by_contents(blob):
167
bad_file_contents = 'The launch code is 1-2-3-4.'
@@ -25,9 +16,9 @@ def drop_files_by_name(commit):
2516
commit.file_changes = new_file_changes
2617

2718
sys.argv.append('--force')
28-
args = repo_filter.FilteringOptions.parse_args(sys.argv[1:])
19+
args = fr.FilteringOptions.parse_args(sys.argv[1:])
2920

30-
filter = repo_filter.RepoFilter(args,
31-
blob_callback = drop_file_by_contents,
32-
commit_callback = drop_files_by_name)
21+
filter = fr.RepoFilter(args,
22+
blob_callback = drop_file_by_contents,
23+
commit_callback = drop_files_by_name)
3324
filter.run()

t/t9391/print_progress.py

+7-16
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
#!/usr/bin/env python
22

3-
# python makes importing files with dashes hard, sorry. Renaming would
4-
# allow us to simplify this to
5-
# import git_repo_filter
6-
# However, since git style commands are dashed and git-filter-repo is used more
7-
# as a tool than a library, renaming is not an option, so import is 5 lines:
8-
import imp
93
import sys
10-
sys.dont_write_bytecode = True # .pyc generation -> ugly 'git-filter-repoc' files
11-
with open("../../../git-filter-repo") as f:
12-
repo_filter = imp.load_source('repo_filter', "git-filter-repo", f)
13-
# End of convoluted import of git-filter-repo
4+
import git_filter_repo as fr
145

156
if len(sys.argv) != 3:
167
raise SystemExit("Syntax:\n %s SOURCE_REPO TARGET_REPO")
178
source_repo = sys.argv[1]
189
target_repo = sys.argv[2]
1910

20-
total_objects = repo_filter.GitUtils.get_total_objects(source_repo) # blobs+trees
21-
total_commits = repo_filter.GitUtils.get_commit_count(source_repo)
11+
total_objects = fr.GitUtils.get_total_objects(source_repo) # blobs+trees
12+
total_commits = fr.GitUtils.get_commit_count(source_repo)
2213
object_count = 0
2314
commit_count = 0
2415

@@ -37,8 +28,8 @@ def my_commit_callback(commit):
3728
commit_count += 1
3829
print_progress()
3930

40-
args = repo_filter.FilteringOptions.parse_args(['--force', '--quiet'])
41-
filter = repo_filter.RepoFilter(args,
42-
blob_callback = my_blob_callback,
43-
commit_callback = my_commit_callback)
31+
args = fr.FilteringOptions.parse_args(['--force', '--quiet'])
32+
filter = fr.RepoFilter(args,
33+
blob_callback = my_blob_callback,
34+
commit_callback = my_commit_callback)
4435
filter.run()

t/t9391/rename-master-to-develop.py

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
11
#!/usr/bin/env python
22

3-
# python makes importing files with dashes hard, sorry. Renaming would
4-
# allow us to simplify this to
5-
# import git_repo_filter
6-
# However, since git style commands are dashed and git-filter-repo is used more
7-
# as a tool than a library, renaming is not an option, so import is 5 lines:
8-
import imp
9-
import sys
10-
sys.dont_write_bytecode = True # .pyc generation -> ugly 'git-filter-repoc' files
11-
with open("../../../git-filter-repo") as f:
12-
repo_filter = imp.load_source('repo_filter', "git-filter-repo", f)
13-
# End of convoluted import of git-filter-repo
3+
import git_filter_repo as fr
144

155
def my_commit_callback(commit):
166
if commit.branch == "refs/heads/master":
177
commit.branch = "refs/heads/develop"
188

19-
args = repo_filter.FilteringOptions.default_options()
9+
args = fr.FilteringOptions.default_options()
2010
args.force = True
21-
filter = repo_filter.RepoFilter(args, commit_callback = my_commit_callback)
11+
filter = fr.RepoFilter(args, commit_callback = my_commit_callback)
2212
filter.run()

t/t9391/splice_repos.py

+13-26
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
#!/usr/bin/env python
22

3-
# python makes importing files with dashes hard, sorry. Renaming would
4-
# allow us to simplify this to
5-
# import git_repo_filter
6-
# However, since git style commands are dashed and git-filter-repo is used more
7-
# as a tool than a library, renaming is not an option, so import is 5 lines:
8-
import imp
9-
import sys
10-
sys.dont_write_bytecode = True # .pyc generation -> ugly 'git-filter-repoc' files
11-
with open("../../git-filter-repo") as f:
12-
repo_filter = imp.load_source('repo_filter', "git-filter-repo", f)
13-
# End of convoluted import of git-filter-repo
14-
15-
import datetime
163
import re
4+
import sys
5+
import git_filter_repo as fr
176

187
class InterleaveRepositories:
198
def __init__(self, repo1, repo2, output_dir):
@@ -50,30 +39,28 @@ def weave_commit(self, commit):
5039
# on commit.id
5140
if prev_letter in self.commit_map:
5241
self.last_commit = commit.id
53-
repo_filter.record_id_rename(new_commit.id, commit.id)
42+
fr.record_id_rename(new_commit.id, commit.id)
5443

5544
def run(self):
56-
blob = repo_filter.Blob('public gpg key contents')
57-
when = datetime.datetime(2006, 1, 2, 3, 4, 5,
58-
tzinfo=repo_filter.FixedTimeZone("+0300"))
59-
tag = repo_filter.Tag('gpg-pubkey', blob.id,
60-
'Ima Tagger', '[email protected]', when,
61-
'Very important explanation and stuff')
45+
blob = fr.Blob('public gpg key contents')
46+
tag = fr.Tag('gpg-pubkey', blob.id,
47+
'Ima Tagger', '[email protected]', '1136199845 +0300',
48+
'Very important explanation and stuff')
6249

63-
args = repo_filter.FilteringOptions.parse_args(['--target', self.output_dir])
64-
out = repo_filter.RepoFilter(args)
50+
args = fr.FilteringOptions.parse_args(['--target', self.output_dir])
51+
out = fr.RepoFilter(args)
6552
out.importer_only()
6653
self.out = out
6754

68-
i1args = repo_filter.FilteringOptions.parse_args(['--source', self.repo1])
69-
i1 = repo_filter.RepoFilter(i1args,
55+
i1args = fr.FilteringOptions.parse_args(['--source', self.repo1])
56+
i1 = fr.RepoFilter(i1args,
7057
reset_callback = lambda r: self.skip_reset(r),
7158
commit_callback = lambda c: self.hold_commit(c))
7259
i1.set_output(out)
7360
i1.run()
7461

75-
i2args = repo_filter.FilteringOptions.parse_args(['--source', self.repo2])
76-
i2 = repo_filter.RepoFilter(i2args,
62+
i2args = fr.FilteringOptions.parse_args(['--source', self.repo2])
63+
i2 = fr.RepoFilter(i2args,
7764
commit_callback = lambda c: self.weave_commit(c))
7865
i2.set_output(out)
7966
i2.run()

t/t9391/strip-cvs-keywords.py

+3-14
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
#!/usr/bin/env python
22

3-
# python makes importing files with dashes hard, sorry. Renaming would
4-
# allow us to simplify this to
5-
# import git_repo_filter
6-
# However, since git style commands are dashed and git-filter-repo is used more
7-
# as a tool than a library, renaming is not an option, so import is 5 lines:
8-
import imp
9-
import sys
10-
sys.dont_write_bytecode = True # .pyc generation -> ugly 'git-filter-repoc' files
11-
with open("../../../git-filter-repo") as f:
12-
repo_filter = imp.load_source('repo_filter', "git-filter-repo", f)
13-
# End of convoluted import of git-filter-repo
14-
153
import re
4+
import git_filter_repo as fr
165

176
def strip_cvs_keywords(blob):
187
# FIXME: Should first check if blob is a text file to avoid ruining
@@ -22,6 +11,6 @@ def strip_cvs_keywords(blob):
2211
replacement = r'$\1$'
2312
blob.data = re.sub(pattern, replacement, blob.data)
2413

25-
args = repo_filter.FilteringOptions.parse_args(['--force'])
26-
filter = repo_filter.RepoFilter(args, blob_callback = strip_cvs_keywords)
14+
args = fr.FilteringOptions.parse_args(['--force'])
15+
filter = fr.RepoFilter(args, blob_callback = strip_cvs_keywords)
2716
filter.run()

0 commit comments

Comments
 (0)