]> git.madduck.net Git - etc/vim.git/blob - scripts/migrate-black.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

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.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Bump actions/setup-python from 3 to 4 (#3121)
[etc/vim.git] / scripts / migrate-black.py
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 check_output, run, Popen, PIPE
9
10
11 def git(*args: str) -> str:
12     return check_output(["git"] + list(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             "Could not find a common commit for current head and %s" % base_branch
30         )
31         return 1
32
33     commits = git(
34         "log", "--reverse", "--pretty=format:%H", "%s~1..HEAD" % merge_base
35     ).split()
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")
40
41     git("checkout", base_branch, "-b%s-black" % current_branch)
42
43     for last_commit, commit in zip(commits, commits[1:]):
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                 "--find-copies",
53                 "%s-black..%s-black" % (last_commit, commit),
54             ],
55             stdout=PIPE,
56         )
57         git_apply = Popen(
58             [
59                 "git",
60                 "apply",
61             ]
62             + (["--quiet"] if quiet else [])
63             + [
64                 "-3",
65                 "--intent-to-add",
66             ]
67             + (["--allow-empty"] if allow_empty else [])
68             + [
69                 "-",
70             ],
71             stdin=git_diff.stdout,
72         )
73         if git_diff.stdout is not None:
74             git_diff.stdout.close()
75         git_apply.communicate()
76         git("commit", "--allow-empty", "-aqC", commit)
77
78     for commit in commits:
79         git("branch", "-qD", "%s-black" % commit)
80
81     return 0
82
83
84 if __name__ == "__main__":
85     import argparse
86
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))