Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert spaces to tabs in lines I changed in a commit

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.

like image 813
svick Avatar asked May 02 '12 12:05

svick


1 Answers

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
like image 86
robinr Avatar answered Sep 21 '22 14:09

robinr