git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / Atom feed
* Why does git diff-index show intent-to-add file as "Added"?
@ 2020-10-17 16:21 Pratyush Yadav
  2020-10-17 20:18 ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Pratyush Yadav @ 2020-10-17 16:21 UTC (permalink / raw)
  To: git; +Cc: Johannes Schindelin

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Why does git diff-index show intent-to-add file as "Added"?
  2020-10-17 16:21 Why does git diff-index show intent-to-add file as "Added"? Pratyush Yadav
@ 2020-10-17 20:18 ` Junio C Hamano
  2020-10-18 16:00   ` Phillip Wood
  0 siblings, 1 reply; 4+ messages in thread
From: Junio C Hamano @ 2020-10-17 20:18 UTC (permalink / raw)
  To: Pratyush Yadav; +Cc: git, Johannes Schindelin

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.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Why does git diff-index show intent-to-add file as "Added"?
  2020-10-17 20:18 ` Junio C Hamano
@ 2020-10-18 16:00   ` Phillip Wood
  2020-10-18 20:07     ` Junio C Hamano
  0 siblings, 1 reply; 4+ messages in thread
From: Phillip Wood @ 2020-10-18 16:00 UTC (permalink / raw)
  To: Junio C Hamano, Pratyush Yadav; +Cc: git, Johannes Schindelin

On 17/10/2020 21:18, Junio C Hamano wrote:
> 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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Why does git diff-index show intent-to-add file as "Added"?
  2020-10-18 16:00   ` Phillip Wood
@ 2020-10-18 20:07     ` Junio C Hamano
  0 siblings, 0 replies; 4+ messages in thread
From: Junio C Hamano @ 2020-10-18 20:07 UTC (permalink / raw)
  To: Phillip Wood; +Cc: Pratyush Yadav, git, Johannes Schindelin

Phillip Wood <phillip.wood123@gmail.com> writes:

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-10-18 20:07 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-17 16:21 Why does git diff-index show intent-to-add file as "Added"? Pratyush Yadav
2020-10-17 20:18 ` Junio C Hamano
2020-10-18 16:00   ` Phillip Wood
2020-10-18 20:07     ` Junio C Hamano

git@vger.kernel.org list mirror (unofficial, one of many)

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://public-inbox.org/git
	git clone --mirror http://ou63pmih66umazou.onion/git
	git clone --mirror http://czquwvybam4bgbro.onion/git
	git clone --mirror http://hjrcffqmbrq6wope.onion/git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 git git/ https://public-inbox.org/git \
		git@vger.kernel.org
	public-inbox-index git

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.version-control.git
	nntp://ou63pmih66umazou.onion/inbox.comp.version-control.git
	nntp://czquwvybam4bgbro.onion/inbox.comp.version-control.git
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.version-control.git
	nntp://news.gmane.io/gmane.comp.version-control.git
 note: .onion URLs require Tor: https://www.torproject.org/

code repositories for the project(s) associated with this inbox:

	https://80x24.org/mirrors/git.git

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git