threads / discuss / 55125

how to most effectively cherry pick by selective patch hunk?

Subject: how to most effectively cherry pick by selective patch hunk?

## tl;dr

5 messages between Feb 9, 2021 and Feb 11, 2021.

replies: 4people: 4as markdown or json

Robert P. J. Day· Feb 9, 2021, 13:58 UTC · lore
  (i'm looking for a solution not just for current git but, sadly,
going back to git-2.9.2, which is installed on my current contract
build system, and i have little authority to bump it up.)
  summary: made a couple dozen commits on branch, call it "oldb",
where i was relatively undisciplined about enforcing clean, modular
commits so i want to go back and clean things up -- refactor by
changing order, combining some trivial commits into one, breaking
large, unwieldy commits into smaller pieces, better commit messages
and so on, so i start a new branch "newb" at the same origin, and
here's the problem.
  every old commit consisted of adding a new patch to an existing
openembedded recipe, so every commit had two components:
  * a brand new patch file to be placed under "files/", and
  * adding a new line to SRC_URI variable, as in:
    SRC_URI += " \
	first.patch \
	second.patch \
	third.patch \
	... etc etc ...
    "
  i think you see the problem. a commit adding a brand new file will
never create a merge conflict, as it's a new file. but if i start
reordering commits, then the addition of that line to the .bbappend
file will *certainly* conflict as the patches will almost certainly be
renamed and in a different order.
  what would be great is some sort of "-p" (patch selection) option
with cherry-pick, but i don't see that.
  what would work for me is to auto-get the addition of the patch file
from the old branch, at which point i am more than happy to manually
fix the .bbappend file and manually do another commit. i'm thinking i
can just "git checkout" the new patch file from the old branch, and
take it from there.
  thoughts? am i overthinking this?
rday
Jeff King· Feb 9, 2021, 16:39 UTC · re: Robert P. J. Day · lore

Re: how to most effectively cherry pick by selective patch hunk?

On Tue, Feb 09, 2021 at 08:58:06AM -0500, Robert P. J. Day wrote:
>   what would be great is some sort of "-p" (patch selection) option
> with cherry-pick, but i don't see that.

We have "checkout -p", but of course the problem there is that it's picking out of the whole state of that commit. So you might see other changes not introduced by that commit.

Conceptually, adding "cherry-pick -p" would be pretty easy. The strategy for all of the "-p" options is to generate a diff, then feed that diff to the patch-selection code, then apply whatever the user selects. For "checkout -p $commit", that diff is the diff between $commit and our current state. But for "cherry-pick -p", it would be the diff between $commit^ and $commit.

Of course that involves a change to Git, and you were looking for something you could do with existing versions. :) You can emulate it by making the commit's parent equivalent to your current state. I.e.:

  git checkout --detach ;# detached HEAD for temporary commit
  git cherry-pick $commit ;# maybe deal with conflicts
  commit=$(git rev-parse --verify HEAD) ;# remember the temp commit
  git checkout - ;# back to your branch
  git checkout -p $commit
-Peff
Robert P. J. Day· Feb 9, 2021, 16:58 UTC · re: Jeff King · lore

Re: how to most effectively cherry pick by selective patch hunk?

On Tue, 9 Feb 2021, Jeff King wrote:
Show 8 quoted lines
> On Tue, Feb 09, 2021 at 08:58:06AM -0500, Robert P. J. Day wrote:
>
> >   what would be great is some sort of "-p" (patch selection) option
> > with cherry-pick, but i don't see that.
>
> We have "checkout -p", but of course the problem there is that it's
> picking out of the whole state of that commit. So you might see
> other changes not introduced by that commit.
  this is probably what i'll use since i can use <pathspec> to grab
only the patch file from that commit, then add and commit it manually
from there.
rday
Andreas Schwab· Feb 9, 2021, 17:12 UTC · re: Jeff King · lore

Re: how to most effectively cherry pick by selective patch hunk?

On Feb 09 2021, Jeff King wrote:
Show 9 quoted lines
> Of course that involves a change to Git, and you were looking for
> something you could do with existing versions. :) You can emulate it by
> making the commit's parent equivalent to your current state. I.e.:
>
>   git checkout --detach ;# detached HEAD for temporary commit
>   git cherry-pick $commit ;# maybe deal with conflicts
>   commit=$(git rev-parse --verify HEAD) ;# remember the temp commit
>   git checkout - ;# back to your branch
>   git checkout -p $commit
Alternatively, you could cherry-pick normally, then use
 git checkout -p HEAD^
to remove what you don't want.
Andreas.
-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."
Elijah Newren· Feb 11, 2021, 00:54 UTC · re: Andreas Schwab · lore

Re: how to most effectively cherry pick by selective patch hunk?

On Tue, Feb 9, 2021 at 11:44 PM Andreas Schwab <schwab@linux-m68k.org> wrote:
Show 16 quoted lines
>
> On Feb 09 2021, Jeff King wrote:
>
> > Of course that involves a change to Git, and you were looking for
> > something you could do with existing versions. :) You can emulate it by
> > making the commit's parent equivalent to your current state. I.e.:
> >
> >   git checkout --detach ;# detached HEAD for temporary commit
> >   git cherry-pick $commit ;# maybe deal with conflicts
> >   commit=$(git rev-parse --verify HEAD) ;# remember the temp commit
> >   git checkout - ;# back to your branch
> >   git checkout -p $commit
>
> Alternatively, you could cherry-pick normally, then use
>  git checkout -p HEAD^
> to remove what you don't want.

Or, going into the slightly esoteric, you could define a custom local merge driver for the particular file you want to ignore and say that merges of that file always use the original version and ignore changes made on both sides.

man gitattributes(5), search for "filfre"
Appears to be unchanged since git-2.5.0.

Only ever used them once myself long ago (and not for a real usecase), because I wanted to understand what they were for. However, it seems like they might be useful here.

← back to recent threads