Skip to content

Instantly share code, notes, and snippets.

@JorgenEvens
Created July 21, 2015 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JorgenEvens/bd770ee62925a162b3c2 to your computer and use it in GitHub Desktop.
Save JorgenEvens/bd770ee62925a162b3c2 to your computer and use it in GitHub Desktop.
Script that converts indentation format
#!/bin/sh
set -e
# Convert indentation from $INDENT_CURRENT to $INDENT_TARGET
INDENT_CURRENT=" " # 1 Tab
INDENT_TARGET=" " # 4 Spaces
EXTENSION="*.php"
is_dirty() {
echo "`git diff --shortstat --ignore-submodules=dirty 2> /dev/null | tail -n1`"
}
if [ ! -z "`is_dirty`" ]; then
echo "Your repository is dirty, only do this on a clean repository"
exit 1
fi
BRANCH=$1
CURRENT="`git show --format='%H' | head -n1`"
if [ -z "$BRANCH" ]; then
BRANCH="indentfix-`date | md5`"
fi
git checkout -b $BRANCH
find . -type f -name "$EXTENSION" \( -not -path .git \) -exec sed -i '' -E "s/^(($INDENT_TARGET)*)($INDENT_CURRENT)/\1$INDENT_TARGET/g" {} \;
while [ "`is_dirty`" != "" ]; do
# Add all the files that already were in version control to a commit
git add -u
git commit -q -m "An automated round of indentation fixing."
# Find all files violating tab rules.
find . -type f -name "$EXTENSION" \( -not -path .git \) -exec sed -i '' -E "s/^(($INDENT_TARGET)*)($INDENT_CURRENT)/\1$INDENT_TARGET/g" {} \;
is_dirty
done
git rebase -i "$CURRENT"
echo "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment