7

我对 git repo 做了相当大的提交(60 个文件更改,1635 个插入(+),3 个删除(-)),现在我意识到我使用空格进行缩进,而其余代码使用制表符。

因此,我想替换制表符的空格,但仅在该提交更改的行中,因为我不想修改可能使用空格的其他代码。

我怎样才能做到这一点?我不在乎它是否会修改工作目录或原始提交,两者都可以。

4

1 回答 1

3

你可以试试我写的一个小脚本(washamend)。首先提交,然后运行此脚本进行清理。它使用修正来做到这一点。

#!/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
于 2012-05-02T13:14:16.153 回答