# How to reset selected lines?

7 messages from 2019-08-13 to 2019-08-14. Participants: Pratyush Yadav, Jeff King, Junio C Hamano, Johannes Sixt.
Thread: https://gitlist.dev/t/51637

## Pratyush Yadav, 2019-08-13 14:18

Subject: How to reset selected lines?
Message-ID: <20190813141816.yoer6pfjdnlgtj76@localhost.localdomain>
URL: https://gitlist.dev/e/20190813141816.yoer6pfjdnlgtj76%40localhost.localdomain

```
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, 2019-08-13 15:42

Subject: Re: How to reset selected lines?
Message-ID: <20190813154239.GA22514@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20190813154239.GA22514%40sigill.intra.peff.net
In-Reply-To: <20190813141816.yoer6pfjdnlgtj76@localhost.localdomain>

```
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.

-Peff

```

## Junio C Hamano, 2019-08-13 17:32

Subject: Re: How to reset selected lines?
Message-ID: <xmqqblwt7yyr.fsf@gitster-ct.c.googlers.com>
URL: https://gitlist.dev/e/xmqqblwt7yyr.fsf%40gitster-ct.c.googlers.com
In-Reply-To: <20190813154239.GA22514@sigill.intra.peff.net>

```
Jeff King <peff@peff.net> writes:

> 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 ;-)


```

## Johannes Sixt, 2019-08-13 22:07

Subject: Re: How to reset selected lines?
Message-ID: <1f31dd2c-3023-2d13-3466-bffabab703cc@kdbg.org>
URL: https://gitlist.dev/e/1f31dd2c-3023-2d13-3466-bffabab703cc%40kdbg.org
In-Reply-To: <20190813141816.yoer6pfjdnlgtj76@localhost.localdomain>

```
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, 2019-08-14 11:11

Subject: Re: How to reset selected lines?
Message-ID: <20190814111108.lymvzdwicpil53at@localhost.localdomain>
URL: https://gitlist.dev/e/20190814111108.lymvzdwicpil53at%40localhost.localdomain
In-Reply-To: <1f31dd2c-3023-2d13-3466-bffabab703cc@kdbg.org>

```
On 14/08/19 12:07AM, Johannes Sixt wrote:
> 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

```

## Pratyush Yadav, 2019-08-14 11:48

Subject: Re: How to reset selected lines?
Message-ID: <20190814114844.gvb5znje7cpzehkd@localhost.localdomain>
URL: https://gitlist.dev/e/20190814114844.gvb5znje7cpzehkd%40localhost.localdomain
In-Reply-To: <20190813154239.GA22514@sigill.intra.peff.net>

```
On 13/08/19 11:42AM, Jeff King wrote:
> 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, 2019-08-14 14:52

Subject: Re: How to reset selected lines?
Message-ID: <20190814145219.GA12093@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20190814145219.GA12093%40sigill.intra.peff.net
In-Reply-To: <20190814114844.gvb5znje7cpzehkd@localhost.localdomain>

```
On Wed, Aug 14, 2019 at 05:18:44PM +0530, Pratyush Yadav wrote:

> > 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

```
