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

Re: Git not commiting anything if file created and "git add"ed in pre-commit hook is the only file in the staging area

From
Jeff King <peff@peff.net>
Date
Oct 27, 2021, 22:42 UTC
Message-ID
<YXnVuhyPuFFfJ8zq@coredump.intra.peff.net>
In-Reply-To
<YXnNvyi62j5gcxQV@camp.crustytoothpaste.net>
On Wed, Oct 27, 2021 at 10:07:59PM +0000, brian m. carlson wrote:
Show 24 quoted lines
> > Further testing shows that the newly created file *is* added to the
> > commit, *if* at least one other change
> > was registered to be commited (git add some time before the git commit).
> 
> Yes, this occurs because the first thing we do is invoke the pre-commit
> hook.  It passes, and then we consider various reasons why we should not
> commit: the hook failed, there's nothing to commit, etc.  We discover
> one since we haven't re-read the index yet and last we saw there was
> nothing to commit, so we abort.
> 
> There's a giant comment before we re-read the index that says this:
> 
> 		/*
> 		 * Re-read the index as pre-commit hook could have updated it,
> 		 * and write it out as a tree.  We must do this before we invoke
> 		 * the editor and after we invoke run_status above.
> 		 */
> 
> The commit history does not explain why we must do this _after_ invoking
> run_status, but if the comment is incorrect and we re-read it
> immediately, then this problem would go away.  I lack the relevant
> context to determine whether this is appropriate, but if folks think
> this is advisable, then I can write up a patch this weekend.  It should
> be relatively trivial.
There's some related discussion of this area in:
  https://lore.kernel.org/git/CADv3qkGq3jA8iXsjhrqfsUX=gW+KOuLyeVgDzmku1tUpsMdvtw@mail.gmail.com/

I don't find it particularly enlightening as to the history, but it does point to other people wanting to re-read before run_status().

-Peff
Previous: brian m. carlsonNext: Junio C Hamano
Message 3 of 9 in “Git not commiting anything if file created and "git add"ed in pre-commit hook is the only file in the staging area”
  1. Peter HunkelerOct 27, 2021
  2. brian m. carlsonOct 27, 2021
  3. Jeff KingOct 27, 2021
  4. Junio C HamanoOct 28, 2021
  5. Peter HunkelerOct 28, 2021
  6. Peter HunkelerOct 30, 2021
  7. Johannes SixtOct 30, 2021
  8. Junio C HamanoOct 30, 2021
  9. Peter HunkelerNov 2, 2021

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.