threads / discuss / 54450

Why does git diff-index show intent-to-add file as "Added"?

Subject: Why does git diff-index show intent-to-add file as "Added"?

## tl;dr

6 messages between Oct 17, 2020 and Oct 27, 2020.

replies: 5people: 3as markdown or json

Pratyush Yadav· Oct 17, 2020, 16:21 UTC · lore
Hi,

I have been trying to wrap my head around intent-to-add files to add support for them in git-gui and one point really confuses me.

Say I do:
  $ git add -N foo

git-gui uses `git diff-index --cached HEAD` to find out the status of the "staged changes" and `git diff-files` to find out the status of "unstaged changes".

In this case, running diff-files gives:
  :000000 100644 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 A	foo

which tells git-gui that the file is "added" thanks to the 'A'. Makes sense. We can infer that the file in intent-to-add because untracked files are not listed by diff-files.

Running diff-index gives:
  :000000 100644 0000000000000000000000000000000000000000 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 A	foo

which tells git-gui that the file is added to the index thanks to the 'A'. This makes less sense. The file has not been staged yet so why should it be listed as such?

This causes problems because we then end up showing the file in both the "Unstaged Changes" and the "Staged Changes" sections, even though the file has no staged changes at all. And `git status` says the same. It only lists foo in unstaged changes.

In addition, this makes it much more complicated to determine if a file marked as intent-to-add has some parts staged and some others unstaged. To determine that, I have to look at what diff-files says about the file. If it says 'A', then the file has no changes staged. If it says 'M' then it does. This means I have to look at the status of the "unstaged changes" section to find out information about the "staged changes" section. This makes it much harder to write clean, isolated routines.

Is there something wrong with the mental model I'm using here? How does git-status figure these things out? Or is it really a design mistake? If so, what can we do to fix it?

-- 
Regards,
Pratyush Yadav
Junio C Hamano· Oct 17, 2020, 20:18 UTC · re: Pratyush Yadav · lore

Re: Why does git diff-index show intent-to-add file as "Added"?

Pratyush Yadav <me@yadavpratyush.com> writes:
> In this case, running diff-files gives:
>
>   :000000 100644 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 A	foo

Yes, it says "when comparing the index and the working tree, working tree side has it, and the index side does not, so it is an addition".

Of course, if it is truly a new file that the index does not even know about, we'd stay silent, but a path that are marked with i-t-a bit is what the user told us to keey an eye on, so that is what you would get.

> Running diff-index gives:
>
>   :000000 100644 0000000000000000000000000000000000000000 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 A	foo

If this is "diff-index HEAD", then I think it is expected. "when comparing the HEAD and the working tree, working tree side has it, and the tree side does not, so it is an addition." Exactly the same story as "diff-files".

What should happen in "diff-index --cached HEAD", I offhand do not know. "diff-index --cached HEAD" is a request to compare two trees, i.e. the tree that _would_ be produced if you wrote the index out as a tree (i.e. "git write-tree") right now, and the tree of HEAD. So I think it may be sensible for the command to behave as if the i-t-a path does not even exist in the index when it is run with "--cached"; I may be missing some subtleties that require us to do something different, but that is what I would think.

Phillip Wood· Oct 18, 2020, 16:00 UTC · re: Junio C Hamano · lore

Re: Why does git diff-index show intent-to-add file as "Added"?

On 17/10/2020 21:18, Junio C Hamano wrote:
Show 31 quoted lines
> Pratyush Yadav <me@yadavpratyush.com> writes:
> 
>> In this case, running diff-files gives:
>>
>>    :000000 100644 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 A	foo
> 
> Yes, it says "when comparing the index and the working tree, working
> tree side has it, and the index side does not, so it is an addition".
> 
> Of course, if it is truly a new file that the index does not even
> know about, we'd stay silent, but a path that are marked with i-t-a
> bit is what the user told us to keey an eye on, so that is what you
> would get.
> 
>> Running diff-index gives:
>>
>>    :000000 100644 0000000000000000000000000000000000000000 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 A	foo
> 
> If this is "diff-index HEAD", then I think it is expected.  "when
> comparing the HEAD and the working tree, working tree side has it,
> and the tree side does not, so it is an addition."  Exactly the same
> story as "diff-files".
> 
> What should happen in "diff-index --cached HEAD", I offhand do not
> know.  "diff-index --cached HEAD" is a request to compare two trees,
> i.e. the tree that _would_ be produced if you wrote the index out as
> a tree (i.e. "git write-tree") right now, and the tree of HEAD.  So
> I think it may be sensible for the command to behave as if the i-t-a
> path does not even exist in the index when it is run with "--cached";
> I may be missing some subtleties that require us to do something
> different, but that is what I would think.

If a user runs `add -N file` then if they run `git reset -p` (which runs `git diff-index --cached HEAD`) it will ask them if they want to remove `file` from the index. If `diff-index --cached` hid i-t-a entries we'd lose that or have to get the i-t-a entries another way. Having said that it does seem strange for `diff-index --cached` to be showing i-t-a entries. Perhaps `diff-index --cached` should default to --ita-invisible-in-index?

Best Wishes
Phillip
Junio C Hamano· Oct 18, 2020, 20:07 UTC · re: Phillip Wood · lore

Re: Why does git diff-index show intent-to-add file as "Added"?

Phillip Wood <phillip.wood123@gmail.com> writes:
Show 12 quoted lines
>> I think it may be sensible for the command to behave as if the i-t-a
>> path does not even exist in the index when it is run with "--cached";
>> I may be missing some subtleties that require us to do something
>> different, but that is what I would think.
>
> If a user runs `add -N file` then if they run `git reset -p` (which
> runs `git diff-index --cached HEAD`) it will ask them if they want to
> remove `file` from the index. If `diff-index --cached` hid i-t-a
> entries we'd lose that or have to get the i-t-a entries another
> way. Having said that it does seem strange for `diff-index --cached`
> to be showing i-t-a entries. Perhaps `diff-index --cached` should
> default to --ita-invisible-in-index?

It probably is a good idea to consider if the implementation of `git reset -p` is doing a sensible thing, or relying on a buggy behaviour of "diff-index --cached" (worse yet, broken "diff-index --cached" to suit its needs) and see if it can be written differently.

Pratyush Yadav· Oct 27, 2020, 12:09 UTC · re: Junio C Hamano · lore

Re: Why does git diff-index show intent-to-add file as "Added"?

On 17/10/20 01:18PM, Junio C Hamano wrote:
Show 22 quoted lines
> Pratyush Yadav <me@yadavpratyush.com> writes:
> 
> > In this case, running diff-files gives:
> >
> >   :000000 100644 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 A	foo
> 
> Yes, it says "when comparing the index and the working tree, working
> tree side has it, and the index side does not, so it is an addition".
> 
> Of course, if it is truly a new file that the index does not even
> know about, we'd stay silent, but a path that are marked with i-t-a
> bit is what the user told us to keey an eye on, so that is what you
> would get.
> 
> > Running diff-index gives:
> >
> >   :000000 100644 0000000000000000000000000000000000000000 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 A	foo
> 
> If this is "diff-index HEAD", then I think it is expected.  "when
> comparing the HEAD and the working tree, working tree side has it,
> and the tree side does not, so it is an addition."  Exactly the same
> story as "diff-files".
It is `git diff-index --cached HEAD`.
 
Show 8 quoted lines
> What should happen in "diff-index --cached HEAD", I offhand do not
> know.  "diff-index --cached HEAD" is a request to compare two trees,
> i.e. the tree that _would_ be produced if you wrote the index out as
> a tree (i.e. "git write-tree") right now, and the tree of HEAD.  So
> I think it may be sensible for the command to behave as if the i-t-a
> path does not even exist in the index when it is run with "--cached";
> I may be missing some subtleties that require us to do something
> different, but that is what I would think.

This is what I think too. Can we then treat this as a bug, and work on fixing it? Does any subsystem expert have any comments and/or provide extra context?

-- 
Regards,
Pratyush Yadav
Junio C Hamano· Oct 27, 2020, 20:06 UTC · re: Pratyush Yadav · lore

Re: Why does git diff-index show intent-to-add file as "Added"?

Pratyush Yadav <me@yadavpratyush.com> writes:
Show 14 quoted lines
> It is `git diff-index --cached HEAD`.
>  
>> What should happen in "diff-index --cached HEAD", I offhand do not
>> know.  "diff-index --cached HEAD" is a request to compare two trees,
>> i.e. the tree that _would_ be produced if you wrote the index out as
>> a tree (i.e. "git write-tree") right now, and the tree of HEAD.  So
>> I think it may be sensible for the command to behave as if the i-t-a
>> path does not even exist in the index when it is run with "--cached";
>> I may be missing some subtleties that require us to do something
>> different, but that is what I would think.
>
> This is what I think too. Can we then treat this as a bug, and work on 
> fixing it? Does any subsystem expert have any comments and/or provide 
> extra context?

I do not think Phillip considers himself an expert in this area, but what he pointed out in the thread

    http://lore.kernel.org/git/xmqqo8kz70xv.fsf@gitster.c.googlers.com/
is worth listening to.

← back to recent threads