Re: How to fix (and find) many git-* --check errors?
- From
- Peter Valdemar Mørch (Lists) <4ux6as402@sneakemail.com>
- Date
- Aug 8, 2008, 14:57 UTC
- Message-ID
- <489C5ED5.2060501@sneakemail.com>
- In-Reply-To
- <489C40BC.8000008@sneakemail.com>
Thanks to hints from both Jeff King and Björn Stenbrink I got it working. Here is my script, if anybody is interested.
It handles both fixing whitespace in current workspace diffs:
$ git-fix-whitespace file
(requires existing diffs)
and fixing all whitespace problems in a file:
$ git-fix-whitespace -f file
(asserts no existing diffs)
Now all I need to do is grock how to fix stuff already in the index, but I'll leave that for another day...
Peter
----------------- #!/bin/bash
# Take a -f option to fix all whitespace problems in entire files # completely. Otherwise only fix current diffs.
# Option handling
while getopts fh? o
do
case "$o" in
f) fixEntireFile=1;;
[h?]) echo >&2 "Usage: $0 [-f] file ..."
echo >&2 "-f: Fix the entire file"
echo >&2 " Otherwise only fix current diffs"
exit 1;;
esac
doneshift $(($OPTIND-1))
# A constant for a rev that is empty EMPTY_TREE_SHA1=4b825dc642cb6eb9a060e54bf8d69288fbee4904
# Check to see we're on a clean HEAD just to be sure. # (Not sure this is necessary, but just in case.) git --no-pager diff --exit-code HEAD -- "$@" > /dev/null if [ $? = 0 ] ; then # There is no current diff if [ "$fixEntireFile" != "" ] ; then # Good, there is no diff, and we've been asked to fix # the entire file
# Remove all files - the -f for rm is just needed if # $@ happens to contain only untracked files git-ls-tree -z -r --name-status HEAD "$@" | \ xargs --null rm -f
# Re-create the files - but with whitespace fixed git diff $EMPTY_TREE_SHA1 HEAD -- "$@" | \ git-apply --whitespace=fix else echo >&2 '*Error*: there no diff with HEAD' exit 1 fi else # There is a current diff if [ "$fixEntireFile" = "" ] ; then # Good, there is a current diff, that we need to fix TEMP_FILE=$(tempfile) git diff -- "$@" > $TEMP_FILE git checkout HEAD -- "$@" git apply --whitespace=fix $TEMP_FILE rm $TEMP_FILE else echo >&2 '*Error*: there is diff with HEAD' exit 1 fi fi