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 check_output, run, Popen, PIPE
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
53 "%s-black..%s-black" % (last_commit, commit),
62 + (["--quiet"] if quiet else [])
67 + (["--allow-empty"] if allow_empty else [])
71 stdin=git_diff.stdout,
73 if git_diff.stdout is not None:
74 git_diff.stdout.close()
75 git_apply.communicate()
76 git("commit", "--allow-empty", "-aqC", commit)
78 for commit in commits:
79 git("branch", "-qD", "%s-black" % commit)
84 if __name__ == "__main__":
87 parser = argparse.ArgumentParser()
88 parser.add_argument("base_branch")
89 parser.add_argument("--black_command", default="black -q .")
90 parser.add_argument("--logfile", type=argparse.FileType("w"), default=sys.stdout)
91 args = parser.parse_args()
92 logger = logging.getLogger(__name__)
93 logger.addHandler(logging.StreamHandler(args.logfile))
94 logger.setLevel(logging.INFO)
95 sys.exit(blackify(args.base_branch, args.black_command, logger))