2

I'm using this pre-hook script because I run a task editing some classes, then I would automatically add all the edited files and commit them, but the result is that the commit is done and then the files are added but not committed. It seems like they are added after the commit and not before it.

The script is this:

#!/bin/bash 
pwd
cd application 
echo "Running Lint and Spotless" 
bash gradlew lint spotlessApply
git add *

How could I do it?

  • a start would be using a period rather than an asterisk. `git add *` adds all files in the directory (bash globbing) but `git add .` adds all files recursively starting in the directory the script is run from. – Tyler Stoney Jun 24 '22 at 17:13
  • Refer to: https://stackoverflow.com/questions/26042390/git-add-asterisk-vs-git-add-period – Tyler Stoney Jun 24 '22 at 17:14
  • Does this answer your question? [Performing a git add \* inside a pre-commit hook generates errors when making a commit](https://stackoverflow.com/questions/72746198/performing-a-git-add-inside-a-pre-commit-hook-generates-errors-when-making-a-c) – anthony sottile Jun 25 '22 at 04:07
  • @AnthonySottile that (unanswered) question's asker is this question's asker... – Tyler Stoney Jun 27 '22 at 14:22
  • @TylerStoney yeah -- they shouldn't double post – anthony sottile Jun 27 '22 at 21:13

1 Answers1

1

git-add(1) changes the index. You’re not supposed to change the index in the pre-commit script. It might work but it is undefined behavior.

See:

FYI, we tried not to do the extra re-reading, because pre-commit hook was designed to be a mechanism to allow users to validate, but not correct, what gets committed. As the system originally was designed, users who correctly use Git would not be modifying the index. Because it is an error to modify the index in the hook, (1) re-reading the index just in case the user commits such a mistake is waste of resources, and (2) checking the index to make sure it did not change before and after invoking the hook, again, is also waste of resources.

It may have been a mistake that we re-read the index in some case, which adds to the confusion, but not others.

Guildenstern
  • 2,179
  • 1
  • 17
  • 39