7

I have made quite a big commit to a git repo (60 files changed, 1635 insertions(+), 3 deletions(-)) and now I realized I used spaces for indentation, while the rest of the code uses tabs.

So, I want to replace spaces for tabs, but only in the lines that were changed by that commit, because I don't want to modify other code that may use spaces.

How can I do that? I don't care whether it would modify the working directory or the original commit, either is fine.

svick
  • 236,525
  • 50
  • 385
  • 514
  • 1
    Okay, I solved it by modifying all files in the directory I edited and now I'm going to revert the unwanted changes. But I'd still like to know whether there is a better solution. – svick May 02 '12 at 13:10

1 Answers1

3

You can try a little script (washamend) I wrote. First commit, then run this script to clean up. It uses amend to do that.

#!/bin/bash -e
#
# Rewrite the last commit to remove any trailing whitespace
# in the new version of changed lines.
# Then replace space-based indentation with TAB based indentation
# based on TABS at every eight position
#
[[ -z $TRACE ]] || set -x
trap "rm -f $tmpf" 0
tmpf1=$TMP/$$.1.diff
tmpf2=$TMP/$$.2.diff
git show --binary >$tmpf1
perl -p -e 's/^(\+.*?)[ \t]+$/$1/; while(m/^(\+\t*)( {1,7}\t| {8})(.*)/) { $_=$1."\t".$3."\n"; }' <$tmpf1 >$tmpf2
if ! cmp -s $tmpf1 $tmpf2
then
    git apply --binary --index -R --whitespace=nowarn $tmpf1
    git apply --binary --index $tmpf2
    GIT_EDITOR=true git commit --amend
else
    echo "No changes"
fi
robinr
  • 4,376
  • 2
  • 20
  • 18