I would like to automatically format my files before they are added to Git's index. Right now, I have a pre-commit hook that looks like this:
#!/bin/bash
set -e
exec 1>&2
workingext="working.$$"
ext="bak.$$"
git diff -z --cached --name-only | egrep -z '\.(pl|pm|t)$' | \
while read -d'' -r f; do
# First modify the file in the index
mv "$f" "$f.$workingext"
git show :"$f" > "$f"
perl -c "$f"
perltidy -pbp -nst -b -bext="$ext" "$f";
rm -f "$f.$ext"
git add "$f"
mv "$f.$workingext" "$f"
# Then the working copy
perl -c "$f"
perltidy -pbp -nst -b -bext="$ext" "$f";
rm -f "$f.$ext"
done
Basically, I back up the working copies, check out the indexed copies, format them, add them to the index, and then restore the working copies and format them too, so that the differences between the working and indexed copies don't grow too large. I check the syntax of the files using perl -c
first so that the formatter perltidy
can't get too confused by a syntactically invalid file.
So far, this seems to be working pretty well. However, I'd like to know if there's a way to add the contents of one file to the index under the name of another file. That is, a command like git update-index-from-file --from-file foo.pl.tmp foo.pl
would update the file foo.pl
such that its contents in the index came entirely from foo.pl.tmp
, but whose version in the working directory remained unmodified. This might avoid the dance of renamings I'm doing right now (although that might be useful to ensure I don't lose the contents of the index irretrievably).