-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to ease migration to black
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |