git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Pratyush Yadav <me@yadavpratyush.com>
Date
Oct 17, 2020, 16:21 UTC
Message-ID
<20201017162111.gvisfz2daqvhnjmb@yadavpratyush.com>
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
Next: Junio C Hamano
Message 1 of 6 in “Why does git diff-index show intent-to-add file as "Added"?”
  1. Pratyush YadavOct 17, 2020
  2. Junio C HamanoOct 17, 2020
  3. Phillip WoodOct 18, 2020
  4. Junio C HamanoOct 18, 2020
  5. Pratyush YadavOct 27, 2020
  6. Junio C HamanoOct 27, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.