diff --git a/scripts/migrate-black.py b/scripts/migrate-black.py new file mode 100755 index 00000000000..96db5f8bd30 --- /dev/null +++ b/scripts/migrate-black.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python3 +# check out every commit added by the current branch, blackify them, +# and generate diffs to reconstruct the original commits, but then +# blackified +import os +import sys +from subprocess import check_output, run, Popen, PIPE + + +def git(*args): + return check_output(["git"] + list(args)).decode("utf8").strip() + + +def blackify(base_branch, black_command): + current_branch = git("branch", "--show-current") + + if not current_branch or base_branch == current_branch: + print("You need to check out a feature brach to work on") + return 1 + + if not os.path.exists(".git"): + print("Run me in the root of your repo") + return 1 + + merge_base = git("merge-base", "HEAD", base_branch) + if not merge_base: + print("Could not find a common commit for current head and %s" % base_branch) + return 1 + + commits = git( + "log", "--reverse", "--pretty=format:%H", "%s~1..HEAD" % merge_base + ).split() + for commit in commits: + git("checkout", commit, "-b" "%s-black" % commit) + check_output(black_command, shell=True) + git("commit", "-aqm", "blackify") + + git("checkout", base_branch, "-b" "%s-black" % current_branch) + + for last_commit, commit in zip(commits, commits[1:]): + allow_empty = ( + b"--allow-empty" in run(["git", "apply", "-h"], stdout=PIPE).stdout + ) + quiet = ( + b"--quiet" in run(["git", "apply", "-h"], stdout=PIPE).stdout + ) + git_diff = Popen( + ["git", "diff", "--find-copies", "%s-black..%s-black" % (last_commit, commit)], + stdout=PIPE, + ) + git_apply = Popen( + [ + "git", + "apply", + ] + + (["--quiet"] if quiet else []) + + [ + "-3", + "--intent-to-add", + ] + + (["--allow-empty"] if allow_empty else []) + + [ + "-", + ], + stdin=git_diff.stdout, + stdout=PIPE, + ) + git_diff.stdout.close() + git_apply.communicate() + if git_apply.returncode: + raise Exception("git apply failed") + git("commit", "-aqC", commit) + + for commit in commits: + git("branch", "-qD", "%s-black" % commit) + + +if __name__ == "__main__": + import argparse + + parser = argparse.ArgumentParser() + parser.add_argument("base_branch") + parser.add_argument("--black_command", default="black -q .") + args = parser.parse_args() + sys.exit(blackify(args.base_branch, args.black_command)) diff --git a/scripts/migrate-black.sh b/scripts/migrate-black.sh deleted file mode 100755 index 138b8c65eeb..00000000000 --- a/scripts/migrate-black.sh +++ /dev/null @@ -1,59 +0,0 @@ -#!/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