| 1 |
#!/usr/bin/env python3 |
| 2 |
# check out every commit added by the current branch, blackify them, |
| 3 |
# and generate diffs to reconstruct the original commits, but then |
| 4 |
# blackified |
| 5 |
import logging |
| 6 |
import os |
| 7 |
import sys |
| 8 |
from subprocess import PIPE, Popen, check_output, run |
| 9 |
|
| 10 |
|
| 11 |
def git(*args: str) -> str: |
| 12 |
return check_output(["git", *args]).decode("utf8").strip() |
| 13 |
|
| 14 |
|
| 15 |
def blackify(base_branch: str, black_command: str, logger: logging.Logger) -> int: |
| 16 |
current_branch = git("branch", "--show-current") |
| 17 |
|
| 18 |
if not current_branch or base_branch == current_branch: |
| 19 |
logger.error("You need to check out a feature branch to work on") |
| 20 |
return 1 |
| 21 |
|
| 22 |
if not os.path.exists(".git"): |
| 23 |
logger.error("Run me in the root of your repo") |
| 24 |
return 1 |
| 25 |
|
| 26 |
merge_base = git("merge-base", "HEAD", base_branch) |
| 27 |
if not merge_base: |
| 28 |
logger.error( |
| 29 |
f"Could not find a common commit for current head and {base_branch}" |
| 30 |
) |
| 31 |
return 1 |
| 32 |
|
| 33 |
commits = git( |
| 34 |
"log", "--reverse", "--pretty=format:%H", f"{merge_base}~1..HEAD" |
| 35 |
).split() |
| 36 |
for commit in commits: |
| 37 |
git("checkout", commit, f"-b{commit}-black") |
| 38 |
check_output(black_command, shell=True) |
| 39 |
git("commit", "-aqm", "blackify") |
| 40 |
|
| 41 |
git("checkout", base_branch, f"-b{current_branch}-black") |
| 42 |
|
| 43 |
for last_commit, commit in zip(commits, commits[1:], strict=False): |
| 44 |
allow_empty = ( |
| 45 |
b"--allow-empty" in run(["git", "apply", "-h"], stdout=PIPE).stdout |
| 46 |
) |
| 47 |
quiet = b"--quiet" in run(["git", "apply", "-h"], stdout=PIPE).stdout |
| 48 |
git_diff = Popen( |
| 49 |
[ |
| 50 |
"git", |
| 51 |
"diff", |
| 52 |
"--binary", |
| 53 |
"--find-copies", |
| 54 |
f"{last_commit}-black..{commit}-black", |
| 55 |
], |
| 56 |
stdout=PIPE, |
| 57 |
) |
| 58 |
git_apply = Popen( |
| 59 |
[ |
| 60 |
"git", |
| 61 |
"apply", |
| 62 |
] |
| 63 |
+ (["--quiet"] if quiet else []) |
| 64 |
+ [ |
| 65 |
"-3", |
| 66 |
"--intent-to-add", |
| 67 |
] |
| 68 |
+ (["--allow-empty"] if allow_empty else []) |
| 69 |
+ [ |
| 70 |
"-", |
| 71 |
], |
| 72 |
stdin=git_diff.stdout, |
| 73 |
) |
| 74 |
if git_diff.stdout is not None: |
| 75 |
git_diff.stdout.close() |
| 76 |
git_apply.communicate() |
| 77 |
git("commit", "--allow-empty", "-aqC", commit) |
| 78 |
|
| 79 |
for commit in commits: |
| 80 |
git("branch", "-qD", f"{commit}-black") |
| 81 |
|
| 82 |
return 0 |
| 83 |
|
| 84 |
|
| 85 |
if __name__ == "__main__": |
| 86 |
import argparse |
| 87 |
|
| 88 |
parser = argparse.ArgumentParser() |
| 89 |
parser.add_argument("base_branch") |
| 90 |
parser.add_argument("--black_command", default="black -q .") |
| 91 |
parser.add_argument("--logfile", type=argparse.FileType("w"), default=sys.stdout) |
| 92 |
args = parser.parse_args() |
| 93 |
logger = logging.getLogger(__name__) |
| 94 |
logger.addHandler(logging.StreamHandler(args.logfile)) |
| 95 |
logger.setLevel(logging.INFO) |
| 96 |
sys.exit(blackify(args.base_branch, args.black_command, logger)) |