threads / discuss / 51637

How to reset selected lines?

Subject: How to reset selected lines?

## tl;dr

7 messages between Aug 13, 2019 and Aug 14, 2019.

replies: 6people: 4as markdown or json

Pratyush Yadav· Aug 13, 2019, 14:18 UTC · lore
Hi,

To put things into context of why I am asking this, git-gui has a feature where you can select parts of a displayed diff, and can stage/unstage those parts. That feature is implemented in git-gui by just generating a diff from the selected lines, and then applying it. Check git-gui/lib/diff.tcl:643 for the implementation.

Now, I want to add a similar feature, but one that discards/resets the selected lines. And I'd like to avoid the hack that git-gui's apply_range_or_line is. So, is there a cleaner way to do this that does not involve generating a diff and then applying it?

Also, if there is a better way of staging and unstaging selected lines as well, do let me know, and I will try to fix git-gui's hacky way of doing it.

-- 
Regards,
Pratyush Yadav
Jeff King· Aug 13, 2019, 15:42 UTC · re: Pratyush Yadav · lore

Re: How to reset selected lines?

On Tue, Aug 13, 2019 at 07:48:16PM +0530, Pratyush Yadav wrote:
Show 10 quoted lines
> To put things into context of why I am asking this, git-gui has a 
> feature where you can select parts of a displayed diff, and can 
> stage/unstage those parts. That feature is implemented in git-gui by 
> just generating a diff from the selected lines, and then applying it. 
> Check git-gui/lib/diff.tcl:643 for the implementation.
> 
> Now, I want to add a similar feature, but one that discards/resets the 
> selected lines. And I'd like to avoid the hack that git-gui's 
> apply_range_or_line is. So, is there a cleaner way to do this that does 
> not involve generating a diff and then applying it?
To answer your second question first:

Git's index and trees only understand whole files, so at some point you must generate the final file content. A diff is an easy way to represent the changes, apply them to the existing state, and then get that final content. But it doesn't _have_ to be. You could make some modifications to what is in the working tree and then say "OK, now stage this.".

BUT. That is probably not what the user wants, if the content in the index actually has some modifications that are not in the working tree (i.e., you wouldn't want to overwrite them). Hence we tend to work with diffs, saying "make these changes to what is already in the index, and if they conflict, then bail".

So "git add -p", for example, also works by creating diffs, modifying them, and feeding the result to "apply". You can see the implementation in git-add--interactive.perl, where it literally calls diff and apply commands.

And that leads us to the answer to the first question. That script implements "add -p", but also "checkout -p" (which is what you want), "reset -p", "stash -p", etc. They differ only in what we diff and how we apply the result; the main engine of slicing and dicing the diff through user interaction is the same. See the %patch_modes hash for the list.

-Peff
Junio C Hamano· Aug 13, 2019, 17:32 UTC · re: Jeff King · lore

Re: How to reset selected lines?

Jeff King <peff@peff.net> writes:
Show 22 quoted lines
> Git's index and trees only understand whole files, so at some point you
> must generate the final file content. A diff is an easy way to represent
> the changes, apply them to the existing state, and then get that final
> content. But it doesn't _have_ to be. You could make some modifications
> to what is in the working tree and then say "OK, now stage this.".
>
> BUT. That is probably not what the user wants, if the content in the
> index actually has some modifications that are not in the working tree
> (i.e., you wouldn't want to overwrite them). Hence we tend to work with
> diffs, saying "make these changes to what is already in the index, and
> if they conflict, then bail".
>
> So "git add -p", for example, also works by creating diffs, modifying
> them, and feeding the result to "apply". You can see the implementation
> in git-add--interactive.perl, where it literally calls diff and apply
> commands.
>
> And that leads us to the answer to the first question. That script
> implements "add -p", but also "checkout -p" (which is what you want),
> "reset -p", "stash -p", etc. They differ only in what we diff and how we
> apply the result; the main engine of slicing and dicing the diff through
> user interaction is the same. See the %patch_modes hash for the list.

I was about to respond but you said everything I wanted to say and said it a lot better than I could ;-)

Pratyush Yadav· Aug 14, 2019, 11:48 UTC · re: Jeff King · lore

Re: How to reset selected lines?

On 13/08/19 11:42AM, Jeff King wrote:
Show 37 quoted lines
> On Tue, Aug 13, 2019 at 07:48:16PM +0530, Pratyush Yadav wrote:
> 
> > To put things into context of why I am asking this, git-gui has a 
> > feature where you can select parts of a displayed diff, and can 
> > stage/unstage those parts. That feature is implemented in git-gui by 
> > just generating a diff from the selected lines, and then applying it. 
> > Check git-gui/lib/diff.tcl:643 for the implementation.
> > 
> > Now, I want to add a similar feature, but one that discards/resets the 
> > selected lines. And I'd like to avoid the hack that git-gui's 
> > apply_range_or_line is. So, is there a cleaner way to do this that does 
> > not involve generating a diff and then applying it?
> 
> To answer your second question first:
> 
> Git's index and trees only understand whole files, so at some point you
> must generate the final file content. A diff is an easy way to represent
> the changes, apply them to the existing state, and then get that final
> content. But it doesn't _have_ to be. You could make some modifications
> to what is in the working tree and then say "OK, now stage this.".
> 
> BUT. That is probably not what the user wants, if the content in the
> index actually has some modifications that are not in the working tree
> (i.e., you wouldn't want to overwrite them). Hence we tend to work with
> diffs, saying "make these changes to what is already in the index, and
> if they conflict, then bail".
> 
> So "git add -p", for example, also works by creating diffs, modifying
> them, and feeding the result to "apply". You can see the implementation
> in git-add--interactive.perl, where it literally calls diff and apply
> commands.
> 
> And that leads us to the answer to the first question. That script
> implements "add -p", but also "checkout -p" (which is what you want),
> "reset -p", "stash -p", etc. They differ only in what we diff and how we
> apply the result; the main engine of slicing and dicing the diff through
> user interaction is the same. See the %patch_modes hash for the list.

Ah, so that means I do have to dive into generating diffs. Too bad, I was hoping for a cleaner (read: easier) way.

On that note, I don't suppose there is a way to use git-add--interactive's diff engine from a script, is there? That'd allow me to not write potentially buggy code and solve problems someone already solved.

Thanks for your detailed answer.
-- 
Regards,
Pratyush Yadav
Jeff King· Aug 14, 2019, 14:52 UTC · re: Pratyush Yadav · lore

Re: How to reset selected lines?

On Wed, Aug 14, 2019 at 05:18:44PM +0530, Pratyush Yadav wrote:
Show 18 quoted lines
> > So "git add -p", for example, also works by creating diffs, modifying
> > them, and feeding the result to "apply". You can see the implementation
> > in git-add--interactive.perl, where it literally calls diff and apply
> > commands.
> > 
> > And that leads us to the answer to the first question. That script
> > implements "add -p", but also "checkout -p" (which is what you want),
> > "reset -p", "stash -p", etc. They differ only in what we diff and how we
> > apply the result; the main engine of slicing and dicing the diff through
> > user interaction is the same. See the %patch_modes hash for the list.
> 
> Ah, so that means I do have to dive into generating diffs. Too bad, I 
> was hoping for a cleaner (read: easier) way.
> 
> On that note, I don't suppose there is a way to use 
> git-add--interactive's diff engine from a script, is there?  That'd 
> allow me to not write potentially buggy code and solve problems someone 
> already solved.

The actual diffing is all done by lower-level commands. So I'm not sure there's much "engine" to reuse from the script. It takes care of asking the user about which lines to look at, how to split the diff, etc, but that's the part you'd want a totally different interface for.

The one thing you _might_ want to pick up is the line recounting bits. We used to just call "apply --recount", but I think there were some corner cases that it couldn't handle well. The details are in 2b8ea7f3c7 (add -p: calculate offset delta for edited patches, 2018-03-05) and 3a8522f41f (add -p: don't rely on apply's '--recount' option, 2018-03-05), and probably some mailing list around those.

You could probably start with just not handling those corner cases, though (either relying on "apply --recount", or just using whatever git-gui already does now for staging).

-Peff
Johannes Sixt· Aug 13, 2019, 22:07 UTC · re: Pratyush Yadav · lore

Re: How to reset selected lines?

Am 13.08.19 um 16:18 schrieb Pratyush Yadav:
> Now, I want to add a similar feature, but one that discards/resets the 
> selected lines. And I'd like to avoid the hack that git-gui's 
> apply_range_or_line is. So, is there a cleaner way to do this that does 
> not involve generating a diff and then applying it?

Don't waste your time. I'm using this feature since years. It's available in these three commits:

https://github.com/j6t/git/commit/55276690b2bc6a https://github.com/j6t/git/commit/f12b294b68b595 https://github.com/j6t/git/commit/00860615691604

Thanks go to Bert Wesarg. The patches are also available in this thread:
https://public-inbox.org/git/cccd6193cf3bfe170e14270204d735a842bb8563.1326116492.git.bert.wesarg@googlemail.com/#t
-- Hannes
Pratyush Yadav· Aug 14, 2019, 11:11 UTC · re: Johannes Sixt · lore

Re: How to reset selected lines?

On 14/08/19 12:07AM, Johannes Sixt wrote:
Show 16 quoted lines
> Am 13.08.19 um 16:18 schrieb Pratyush Yadav:
> > Now, I want to add a similar feature, but one that discards/resets the 
> > selected lines. And I'd like to avoid the hack that git-gui's 
> > apply_range_or_line is. So, is there a cleaner way to do this that does 
> > not involve generating a diff and then applying it?
> 
> Don't waste your time. I'm using this feature since years. It's
> available in these three commits:
> 
> https://github.com/j6t/git/commit/55276690b2bc6a
> https://github.com/j6t/git/commit/f12b294b68b595
> https://github.com/j6t/git/commit/00860615691604
> 
> Thanks go to Bert Wesarg. The patches are also available in this thread:
> 
> https://public-inbox.org/git/cccd6193cf3bfe170e14270204d735a842bb8563.1326116492.git.bert.wesarg@googlemail.com/#t

Thanks for letting me know. I did try a similar change, before asking here, but git-apply complained about corrupt patches, so I thought more work had to be done. I'll look into the patches and see if I can work them into patches fit for merging.

-- 
Regards,
Pratyush Yadav

← back to recent threads