I’m trying to use a Git pre-commit hook to run some style checks and automatically generate an AUTHORS file that pulls in the names of all contributors using git shortlog.
My pre-commit script consists of the following:
#!/bin/sh
set -e
bin/update-authors.sh
...
The update-authors.sh file consists of the following:
#!/bin/sh
set -e
# Get a list of authors ordered by number of commits
# and remove the commit count column
AUTHORS=$(git --no-pager shortlog -nse | cut -f 2-)
if [ -z "$AUTHORS" ] ; then
echo "Authors list was empty"
exit 1
fi
# Display the authors list and write it to the file
echo "$AUTHORS" | tee "$(git rev-parse --show-toplevel)/AUTHORS"
The latter script works fine directly from a terminal, but only during the pre-commit hook, it errors out with “Authors list was empty”. I can’t figure out why it’s doing this – any ideas?
I think that when performing
pre-commithook, git keeps tree in semi-detached state, and that’s why it doesn’t get anything. As seen in the example pre-commit hook, you need to pass some branch/commit explicitly, e.g.: