Reputation: 8398
What is the simplest solution to fix committed white space issues like trailing white spaces to an existing repo?
Upvotes: 5
Views: 1629
Reputation: 687
I would have preferred to add this as a comment to the already accepted answer but I just registered to share my findings and do not have enough reputation yet.
If rewriting history (and thus changing all commit hashes) is an option, then a filter script like the one from the mentioned answer seems to be the best approach. However when I tried do use the ws-fix.sh'
script from there, the map
function was not available. From a quick view into a recent source of git-filter-branch.sh
it seems that the map
function is (by default) only available in the commit-filter
but not in the index-filter
. Just to be sure I also tried to export the map
function without success. What made it finally work for me, was to simply redefine the map function just within the filter ws-fix.sh
script itself:
#!/bin/bash
map()
{
if test -r "../map/$1"; then
cat "../map/$1"
else
echo "$1"
fi
}
if git rev-parse --quiet --verify $GIT_COMMIT^ > /dev/null
then
against=$(map $(git rev-parse $GIT_COMMIT^))
git reset --quiet $against -- .
else
against=$(git hash-object -t tree /dev/null)
git rm --quiet --force --ignore-unmatch --cached -r '*'
fi
git diff --full-index $against $GIT_COMMIT | git apply --cached --whitespace=fix 2> /dev/null
With this version I could apply the index-filter
so far without issues:
git filter-branch --index-filter '~/ws-fix.sh'
I didn't test this on more complex repositories though. So make always sure to have a backup before testing!
Upvotes: 1
Reputation: 1769
Just discovered in the git-docs (to be tested) :
git-rebase --whitespace=fix
could also be an option.
See :
Upvotes: 4
Reputation: 1329092
If you have existing commits in your repo with trailing whitespaces, then you would need to change those commit (rewite history), which can be painful if that repo is already push and cloned by others.
But if that is an option, then you can use a script like the one in gitolite:
#!/bin/bash
# from doener (who else!)
# to be called as an index-filter
if git rev-parse --quiet --verify $GIT_COMMIT^ >/dev/null
then
against=$(map $(git rev-parse $GIT_COMMIT^))
git reset -q $against -- .
else
# Initial commit: diff against an empty tree object
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
git rm --cached -rfq --ignore-unmatch '*'
fi
git diff --full-index $against $GIT_COMMIT | git apply --cached --whitespace=fix
run this like:
git filter-branch --index-filter '. ~/git-scripts/ws-fix.sh'
Upvotes: 3