Open
Description
I was following https://www.pygit2.org/recipes/git-cherry-pick.html#cherry-picking-a-commit-without-a-working-copy and it seemed to work at first but then I noticed that it pulls in extra commits that I've never wanted.
I think that this example is incomplete.
My src (https://github.com/sanitizers/patchback-github-app/blob/92934c6/patchback/event_handlers.py#L66-L133) should produce a branch with one cherry-picked commit but instead gets a whole bunch of earlier commits too: https://github.com/webknjaz/backport-test-repo/pull/13/commits.
Activity
webknjaz commentedon Sep 10, 2020
To make my problem more illustrative here's the state I have
And so when I want to cherry-pick (a) into "backport-branch", I need to get a resulting commit with (a') that would be a direct child of (b) as a branch (that will later be used in a PR).
I'm getting something like
and this ^ is not what I want. I expect to produce
P.S. (a) is sometimes a merge commit so in some cases I want to emulate
cherry-pick -x -m1
but it is currently broken even for a case when (a) is a regular commit.webknjaz commentedon Sep 10, 2020
@jdavid do you have any idea what's missing here? Is the recipe even correct?
webknjaz commentedon Sep 10, 2020
cc @rmoehn
rmoehn commentedon Sep 11, 2020
@webknjaz, sorry, I can't help you with this. I wrote the recipe more than five years ago and I haven't used pygit2 since.
jdavid commentedon Sep 12, 2020
The 2nd part of the recipe looks bad.
I think to support this use case first we need to wrap
git_cherrypick_commit
(see https://libgit2.org/libgit2/#v1.0.1/group/cherrypick/git_cherrypick_commit)webknjaz commentedon Sep 12, 2020
Yeah, I thought so. I would've sent a PR but I'm not sure how to implement it...
jdavid commentedon Sep 13, 2020
It would be a lot like
Repository_cherrypick
https://github.com/libgit2/pygit2/blob/master/src/repository.c#L674 but with more arguments, and it would return an index object likemerge_trees
does https://github.com/libgit2/pygit2/blob/master/pygit2/repository.py#L791If you want to do a PR you can choose to write it in C or with cffi, whatever you're more comfortable with. Above you've 2 examples,
cherrypick
is done in C andmerge_trees
is done with cffi.webknjaz commentedon Sep 13, 2020
Thanks for the pointers! I'll see if I can find time for this.
pjw91 commentedon Jun 20, 2025
I've test this with my own case. However,
git_cherrypick
),merge_trees
andgit_cherrypick_commit
(written in cffi myself) are the same.git cherrypick
Which might implies that libgit2 is different from git's implementation?