0

I am running a pre-commit hook which potentially adds new files to the commit. However, although this technically works as expected, the git status part of the commit message does not reflect this change.

For example, I am adding file x in a commit, so my git status beforehand gives the following:

repo>git status
On branch development
Your branch is up-to-date with 'origin/development'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   x

Now, suppose my git hook determines file y also needs to be commited. Although afterwards I can see y was indeed staged for the commit, my empty commit message (after the pre-commit hook has executed) gives the following:

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch development
# Your branch is up-to-date with 'origin/development'.
#
# Changes to be committed:
#       new file:   x
#

even though I am expecting

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch development
# Your branch is up-to-date with 'origin/development'.
#
# Changes to be committed:
#       new file:   x
#       new file:   y
#

Indeed, after aborting the commit I get the following git status:

repo>git status
On branch development
Your branch is up-to-date with 'origin/development'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   x
        new file:   y

In other words, how do I 'refresh' the automatically generated part of my commit message in the pre-commit hook?

EDIT: My pre-commit hook currently looks as follows.

#!/bin/sh
gulp --production

vendor/bin/phpunit

rc=$?
if [[ $rc != 0 ]] ; then
    echo "phpunit failed - commit denied"
    exit $rc
fi

git add public

gulp does things like compile SCSS and potentially move things to my public/ directory. If this happens I want to add those files to the commit, hence the git add public directive at the end. So even though these files do get added to the commit, the git status part of the commit message does not reflect this.

Radical
  • 1,003
  • 1
  • 9
  • 25
  • Can you post the code of the pre-commit-hook? – TheGeorgeous Dec 28 '15 at 11:38
  • I had found this question already, but is not applicable to my problem exactly, since I am writing my commit message before any post-commit hook is executed – Radical Dec 28 '15 at 11:54
  • Ah, I see. I was hoping someone might have had (or has) a workaround, but I guess I'll have to make do otherwise – Radical Dec 28 '15 at 11:56

1 Answers1

0

It seems the git status part in git commit-msg is created before the pre-commit-hook is run. So although it won't be shown in COMMIT-MSG, it will be committed nonetheless. It seems to be a small bug with git.

Can a Git hook automatically add files to the commit?

Community
  • 1
  • 1
TheGeorgeous
  • 3,927
  • 2
  • 20
  • 33