All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
2 # check out every commit added by the current branch, blackify them,
3 # and generate diffs to reconstruct the original commits, but then
8 from subprocess import PIPE, Popen, check_output, run
11 def git(*args: str) -> str:
12 return check_output(["git"] + list(args)).decode("utf8").strip()
15 def blackify(base_branch: str, black_command: str, logger: logging.Logger) -> int:
16 current_branch = git("branch", "--show-current")
18 if not current_branch or base_branch == current_branch:
19 logger.error("You need to check out a feature branch to work on")
22 if not os.path.exists(".git"):
23 logger.error("Run me in the root of your repo")
26 merge_base = git("merge-base", "HEAD", base_branch)
29 "Could not find a common commit for current head and %s" % base_branch
34 "log", "--reverse", "--pretty=format:%H", "%s~1..HEAD" % merge_base
36 for commit in commits:
37 git("checkout", commit, "-b%s-black" % commit)
38 check_output(black_command, shell=True)
39 git("commit", "-aqm", "blackify")
41 git("checkout", base_branch, "-b%s-black" % current_branch)
43 for last_commit, commit in zip(commits, commits[1:]):
45 b"--allow-empty" in run(["git", "apply", "-h"], stdout=PIPE).stdout
47 quiet = b"--quiet" in run(["git", "apply", "-h"], stdout=PIPE).stdout
54 "%s-black..%s-black" % (last_commit, commit),
63 + (["--quiet"] if quiet else [])
68 + (["--allow-empty"] if allow_empty else [])
72 stdin=git_diff.stdout,
74 if git_diff.stdout is not None:
75 git_diff.stdout.close()
76 git_apply.communicate()
77 git("commit", "--allow-empty", "-aqC", commit)
79 for commit in commits:
80 git("branch", "-qD", "%s-black" % commit)
85 if __name__ == "__main__":
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))