]> 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:

CI Test: Deprecating 'set-output' command (#3757)
[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 PIPE, Popen, check_output, run
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                 "--binary",
53                 "--find-copies",
54                 "%s-black..%s-black" % (last_commit, commit),
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", "%s-black" % commit)
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))