git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "brian m. carlson" <sandals@crustytoothpaste.net>
To: Peter Hunkeler <phunsoft@gmx.net>
Cc: Git Mailinglist <git@vger.kernel.org>
Subject: Re: Git not commiting anything if file created and "git add"ed in pre-commit hook is the only file in the staging area
Date: Wed, 27 Oct 2021 22:07:59 +0000	[thread overview]
Message-ID: <YXnNvyi62j5gcxQV@camp.crustytoothpaste.net> (raw)
In-Reply-To: <0165d68f-79a7-d8b7-1bba-89a1449e87a7@gmx.net>

[-- Attachment #1: Type: text/plain, Size: 3918 bytes --]

On 2021-10-27 at 19:03:49, Peter Hunkeler wrote:
> Hi,
> Git seems to behave inconsistently when creating and "git add"ing
> content (files) from within a pre-commit hook. It claims there is
> "nothing to commit", if the stating area was empty before the commit
> command. Otherwise, the new content becomes part of the commit. See
> details on how to reproduce and a log of the individual steps below.
> 
> Regards
> Peter
> 
> 
> === Amended Git bug report
> =============================================================================================
> Thank you for filling out a Git bug report!
> Please answer the following questions to help us understand your issue.
> 
> What did you do before the bug happened? (Steps to reproduce your issue)
> 
> I'm quite new to using git, so please bear with me should I mix up some
> terminology.
> 
> I need some new file to be created at each commit. The new file must
> become part of the commit.
> I thought a git pre-commit hook would be the appropriate place. While
> the file is created as desired,
> git does *not* include this new file in the commit, when there is
> nothing else in the staging area. It
> reports the new file as being untracked, and says nothing added to
> commit. A git status immediately
> thereafter does, however, show the new file in the staging area. If I
> run another commit just now, another
> new file is created as expected, but this time, git adds them both to
> the commit. This can be reliably
> reproduced by repeating 'git commit -m "empty"', and every second time,
> the commit is done.
> 
> 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.

I should point out here that it isn't intended for pre-commit hooks to
be used this way; they're intended to verify that the commit meets some
standards, not to modify it, although it is of course possible to do.

This will happen to work in your case if you use --allow-empty, which
will bypass the "nothing to commit" check.

> The git pre-commit hook looks like this (indented for readability, only):
>     #!/bin/bash
>     fn="folder1\folder1_file3_$(date +%Y%m%d_%H%M%S).txt"
>     echo "some text" > $fn
>     git add $fn

In general, you want to avoid adding automatically generated files to
your repository.  That tends to bloat the repository needlessly and is a
great way to lead to frequent, hard-to-resolve merge conflicts.

You may also want to read the “How do I use hooks to prevent users from
making certain changes?” in gitfaq(7) and note that hooks can be
bypassed on developer systems, so whatever changes you make in a hook
aren't guaranteed to be applied unless you're the only one working on
the repository.
-- 
brian m. carlson (he/him or they/them)
Toronto, Ontario, CA

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 262 bytes --]

  reply	other threads:[~2021-10-27 22:08 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-27 19:03 Git not commiting anything if file created and "git add"ed in pre-commit hook is the only file in the staging area Peter Hunkeler
2021-10-27 22:07 ` brian m. carlson [this message]
2021-10-27 22:42   ` Jeff King
2021-10-28  1:32     ` Junio C Hamano
2021-10-28 12:08   ` Peter Hunkeler
2021-10-30 13:21     ` Peter Hunkeler
2021-10-30 16:44       ` Johannes Sixt
2021-10-30 21:25         ` Junio C Hamano
2021-11-02 13:13           ` Peter Hunkeler

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YXnNvyi62j5gcxQV@camp.crustytoothpaste.net \
    --to=sandals@crustytoothpaste.net \
    --cc=git@vger.kernel.org \
    --cc=phunsoft@gmx.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).