I've got a git pre-commit hook which does linting. When it's invoked, it loops over the staged files and runs a linter on them. This works great.
However, when you run git commit -am 'Foo'
it runs without having any staged files. How can I tell if it was invoked with -a
or the --all
flag? Is there an easier way to get the files which will be stored in git?
All the files in the index will be stored. You're looking for "files in the index that differ from their counterparts in
HEAD
", which you can find withgit diff
—or for scripts,git diff-index
.Note that the staged version may differ from the work-tree version as well, making checking everything especially annoying and tricky. (For an example, run
git add -p
on some complex change, and select part of it.)Here's an example of a dummy pre-commit hook that shows the name and status of each file with respect to
HEAD
, i.e., what's different in the index vs the existingHEAD
commit.Your task is to take the output from
git diff-index --cached -r --name-status HEAD
(perhaps with-z
added, perhaps not) and figure out how to test the versions that are in the index, not the versions that are in the work-tree.Note that you can use
git checkout-index
to extract the index to a work-tree. Of course, you'll want to avoid overwriting the real work tree, i.e., to establish a temporary work-tree elsewhere:for instance. You may want to establish a temporary index, other than the regular index, for this as well, since this may disturb the cache aspect of the regular index:
Be sure to clean up your temporary files and/or directories when done.
(None of these code fragments is tested.)