git-filter-repo/t/t9391/splice_repos.py
Elijah Newren 0b70b72150 filter-repo: provide extra metadata to some callbacks
For other programs importing git-filter-repo as a library and passing a
blob, commit, tag, or reset callback to RepoFilter, pass a second
parameter to these functions with extra metadata they might find useful.
For simplicity of implementation, this technically changes the calling
signature of the --*-callback functions passed on the command line, but
we hide that behind a _do_not_use_this_variable parameter for now, leave
it undocumented, and encourage folks who want to use it to write an
actual python program that imports git-filter-repo.  In the future, we
may modify the --*-callback functions to not pass this extra parameter,
or if it is deemed sufficiently useful, then we'll rename the second
parameter and document it.

As already noted in our API compatibilty caveat near the top of
git-filter-repo, I am not guaranteeing API backwards compatibility.
That especially applies to this metadata argument, other than the fact
that it'll be a dict mapping strings to some kind of value.  I might add
more keys, rename them, change the corresponding value, or even remove
keys that used to be part of metadata.

Signed-off-by: Elijah Newren <newren@gmail.com>
2019-05-30 22:07:48 -07:00

85 lines
2.5 KiB
Python
Executable File

#!/usr/bin/env python3
"""
Please see the
***** API BACKWARD COMPATIBILITY CAVEAT *****
near the top of git-filter-repo.
Also, note that splicing repos may need some special care as fast-export
only shows the files that changed relative to the first parent, so there
may be gotchas if you are to splice near merge commits; this example does
not try to handle any such special cases.
"""
import re
import sys
import git_filter_repo as fr
class InterleaveRepositories:
def __init__(self, repo1, repo2, output_dir):
self.repo1 = repo1
self.repo2 = repo2
self.output_dir = output_dir
self.commit_map = {}
self.last_commit = None
def skip_reset(self, reset, metadata):
reset.skip()
def hold_commit(self, commit, metadata):
commit.skip(new_id = commit.id)
letter = re.match(b'Commit (.)', commit.message).group(1)
self.commit_map[letter] = commit
def weave_commit(self, commit, metadata):
letter = re.match(b'Commit (.)', commit.message).group(1)
prev_letter = bytes([ord(letter)-1])
# Splice in any extra commits needed
if prev_letter in self.commit_map:
new_commit = self.commit_map[prev_letter]
new_commit.parents = [self.last_commit] if self.last_commit else []
new_commit.dump(self.out._output)
commit.parents = [new_commit.id]
# Dump our commit now
commit.dump(self.out._output)
# Make sure that commits that depended on new_commit.id will now depend
# on commit.id
if prev_letter in self.commit_map:
self.last_commit = commit.id
fr.record_id_rename(new_commit.id, commit.id)
def run(self):
blob = fr.Blob(b'public gpg key contents')
tag = fr.Tag(b'gpg-pubkey', blob.id,
b'Ima Tagger', b'ima@tagg.er', b'1136199845 +0300',
b'Very important explanation and stuff')
args = fr.FilteringOptions.parse_args(['--target', self.output_dir])
out = fr.RepoFilter(args)
out.importer_only()
self.out = out
i1args = fr.FilteringOptions.parse_args(['--source', self.repo1])
i1 = fr.RepoFilter(i1args,
reset_callback = self.skip_reset,
commit_callback = self.hold_commit)
i1.set_output(out)
i1.run()
i2args = fr.FilteringOptions.parse_args(['--source', self.repo2])
i2 = fr.RepoFilter(i2args,
commit_callback = self.weave_commit)
i2.set_output(out)
i2.run()
blob.dump(out._output)
tag.dump(out._output)
out.finish()
splicer = InterleaveRepositories(sys.argv[1], sys.argv[2], sys.argv[3])
splicer.run()