git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: How to fix (and find) many git-* --check errors?

From
PLPeter 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
done
shift $(($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
Previous: Björn SteinbrinkNext: Junio C Hamano
Message 4 of 6 in “How to fix (and find) many git-* --check errors?”
  1. Peter Valdemar Mørch (Lists)Aug 8, 2008
  2. Jeff KingAug 8, 2008
  3. Björn SteinbrinkAug 8, 2008
  4. Peter Valdemar Mørch (Lists)Aug 8, 2008
  5. Junio C HamanoAug 8, 2008
  6. Peter Valdemar Mørch (Lists)Aug 9, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.