mirror of
https://github.com/newren/git-filter-repo.git
synced 2024-06-30 07:25:40 +02:00
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 <newren@gmail.com>
This commit is contained in:
parent
6dba1f200c
commit
cbacb6cd82
1
git_filter_repo.py
Symbolic link
1
git_filter_repo.py
Symbolic link
|
@ -0,0 +1 @@
|
|||
git-filter-repo
|
|
@ -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 &&
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue
Block a user