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

Question About Sorting the Index

From
Jon Forrest <nobozo@gmail.com>
Date
May 16, 2025, 23:43 UTC
Message-ID
<1008ijb$6j0$1@ciao.gmane.io>

I've learned that entries in the index file "are sorted in ascending order on the name field".

Am I right in thinking that this means that every time a file is added to the index by running "git add" the whole index file must be resorted? If so, this seems like a lot of work, especially since not all the entries are the same size.

Has any thought been made about improving this, such as perhaps having an "index index"? This would be a separate file that contains the name field of each entry, the location of where the entry starts in the index, and the length of the entry. I'll call this a partial index entry. The "index index" would also be sorted by the name field.

With this approach, running "git add" would simply append a full index entry to the index, and append the partial entry to the "index index", which would then be sorted. The full index would not be sorted. I'm guessing this is the common path.

To delete a file from the index, I'd propose adding an "deleted" bit to the full cache entry. When "git rm --cached" is run, 2 things would happen:

1) The "deleted" bit would be turned on in the full index
entry for the file. The index itself will not be sorted.
Every so often, perhaps when "git fsck" is run, these
entries could be deleted. The full index won't have
to be resorted when this happens because it won't be
assumed to be in sorted order any longer.
2) The "index index" would be modified by removing the
partial entry for the file. This could be done by
writing the partial entries up to the entry being
deleted, and then the entries following. No sort would
be necessary because the "index index" is already sorted.

One drawback of this approach would be that since the "index index" entries also won't be the same length, sorting it will still require extra work. However, this wouldn't be any harder then sorting the full index, and a lot less data wouldn't have to be moved around.

All this is so simple that I suspect that it's been considered before. Am I missing something?

Cordially, Jon Forrest

P.S. I'm trying to read the Git source code to get a better handle on what actually goes on in the index but this is taking some time.

Next: K Jayatheerth
Message 1 of 9 in “Question About Sorting the Index”
  1. Jon ForrestMay 16, 2025
  2. Re Question About Sorting the IndexK Jayatheerth, May 17, 2025
  3. Jon ForrestMay 17, 2025
  4. Junio C HamanoMay 17, 2025
  5. Jon ForrestMay 17, 2025
  6. Elijah NewrenMay 18, 2025
  7. Jon ForrestMay 18, 2025
  8. Jon ForrestMay 27, 2025
  9. Junio C HamanoMay 28, 2025

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.