libgit2 / pygit2

Python bindings for libgit2
https://www.pygit2.org/
Other
1.58k stars 382 forks source link

Fix git-cherry-pick.rst example. #1268

Closed herrerog closed 5 months ago

herrerog commented 5 months ago

Using the parent of the cherry-picked commit as ancestor is not correct sometimes. That looks to be an issue because base was not used at all in the example whereas it should be used as ancestor.

herrerog commented 5 months ago

Sorry I got that one wrong actually @jdavid We should use cherry.parents[0].tree as ancestor otherwise the resulting merge may be wrong. Here what needs to be used:

repo = pygit2.Repository('/path/to/repo')

cherry = repo.revparse_single('9e044d03c')
basket = repo.branches.get('basket')

base_tree = cherry.parents[0].tree

index = repo.merge_trees(base_tree, basket, cherry)
tree_id = index.write_tree(repo)

author    = cherry.author
committer = pygit2.Signature('Archimedes', 'archy@jpl-classics.org')

repo.create_commit(basket.name, author, committer, cherry.message,
                   tree_id, [basket.target])
del None # outdated, prevent from accidentally using it

Let me know if I should send another pull request.

jdavid commented 5 months ago

@herrerog Yes please