Skip to content

Commit

Permalink
Add script to ease migration to black
Browse files Browse the repository at this point in the history
  • Loading branch information
hbrunn committed Apr 27, 2022
1 parent 8ed3e3d commit 16737a1
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions scripts/migrate-black.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/sh
# check out every commit added by the current branch, blackify them,
# and generate diffs to reconstruct the original commits, but then
# blackified
set -e

if [ -z "$1" ]; then
echo Call me like
echo $0 name_of_your_main_branch
echo The main branch should be blackified already
exit 1
fi

BASE_BRANCH=$1
CURRENT_BRANCH=$(git branch --show-current)

if [ -z "$CURRENT_BRANCH" ] || [ "$BASE_BRANCH" = "$CURRENT_BRANCH" ]; then
echo You need to check out a feature branch to work on
exit 1
fi

if [ ! -d .git ]; then
echo Run me in the root of your repo
exit 1
fi

MERGE_BASE=$(git merge-base HEAD $BASE_BRANCH)

if [ -z "$MERGE_BASE" ]; then
echo Could not find a common commit for current head and $BASE_BRACH
exit 1
fi

COMMITS_FILE=$(mktemp)
git log --reverse --pretty='format:%H' $MERGE_BASE~1..HEAD > $COMMITS_FILE

for COMMIT in $(cat $COMMITS_FILE); do
git checkout $COMMIT -b $COMMIT-black
black -q .
git commit -aqm 'blackify'
done

git checkout $BASE_BRANCH -b $CURRENT_BRANCH-black

for COMMIT in $(cat $COMMITS_FILE); do
if [ -n "$LAST_COMMIT" ]; then
git diff $LAST_COMMIT..$COMMIT-black | patch -p1
git commit -aqC $COMMIT
fi
LAST_COMMIT=$COMMIT-black
done

for COMMIT in $(cat $COMMITS_FILE); do
git branch -qD $COMMIT-black
done

rm -rf $COMMITS_FILE

git diff $BASE_BRANCH

0 comments on commit 16737a1

Please sign in to comment.