
In b3025e3fe I had written that when following my recipe, the repository remains in cherry-picking mode afterwards. In issue #516 I was told that Repository.state_cleanup() is needed to correct that. Therefore add it to the recipe. Also add a note near the documentation for cherry-pick, so that nobody will overlook this again. Apparently there are other times when you need to do Repository.state_cleanup() as well, but it's not documented, I don't know when and I don't want to take the time and find out. So leave it at that for now.
2.4 KiB
2.4 KiB
git-cherry-pick
The convenient way to cherry-pick a commit is to use :py.Repository.cherrypick()
. It
is limited to cherry-picking with a working copy and on-disk index.
$> cd /path/to/repo
$> git checkout basket
$> git cherry-pick 9e044d03c
= pygit2.Repository('/path/to/repo')
repo 'basket')
repo.checkout(
= pygit2.Oid('9e044d03c')
cherry_id
repo.cherrypick(cherry_id)
if repo.index.conflicts is None:
= repo.index.write_tree()
tree_id
= repo.get(cherry_id)
cherry = pygit2.Signature('Archimedes', 'archy@jpl-classics.org')
committer
repo.create_commit(basket.name, cherry.author, committer,
cherry.message, tree_id, [basket.target])del basket # outdated, prevent from accidentally using it
repo.state_cleanup()
Cherry-picking a commit without a working copy
This way of cherry-picking gives you more control over the process
and works on bare repositories as well as repositories with a working
copy. :py~.Repository.merge_trees()
can also be used for other
tasks, for example three-argument
rebases.
= pygit2.Repository('/path/to/repo')
repo
= repo.revparse_single('9e044d03c')
cherry = repo.lookup_branch('basket')
basket
= repo.merge_base(cherry.oid, basket.target)
base = cherry.parents[0].tree
base_tree
= repo.merge_trees(base_tree, basket, cherry)
index = index.write_tree(repo)
tree_id
= cherry.author
author = pygit2.Signature('Archimedes', 'archy@jpl-classics.org')
committer
repo.create_commit(basket.name, author, committer, cherry.message,
tree_id, [basket.target])del None # outdated, prevent from accidentally using it