Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add script for cherry-picking commits from typeshed #11737

Merged
merged 1 commit into from
Dec 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions misc/cherry-pick-typeshed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""Cherry-pick a commit from typeshed.

Usage:

python3 misc/cherry-pick-typeshed.py --typeshed-dir dir hash
"""

import argparse
import os.path
import re
import subprocess
import sys
import tempfile


def parse_commit_title(diff: str) -> str:
m = re.search("\n ([^ ].*)", diff)
assert m is not None, "Could not parse diff"
return m.group(1)


def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument(
"--typeshed-dir", help="location of typeshed", metavar="dir", required=True
)
parser.add_argument(
"commit", help="typeshed commit hash to cherry-pick"
)
args = parser.parse_args()
typeshed_dir = args.typeshed_dir
commit = args.commit

if not os.path.isdir(typeshed_dir):
sys.exit(f"error: {typeshed_dir} does not exist")
if not re.match("[0-9a-fA-F]+$", commit):
sys.exit(f"error: Invalid commit {commit!r}")

if not os.path.exists("mypy") or not os.path.exists("mypyc"):
sys.exit(f"error: This script must be run at the mypy repository root directory")

with tempfile.TemporaryDirectory() as d:
diff_file = os.path.join(d, "diff")
out = subprocess.run(["git", "show", commit],
capture_output=True,
text=True,
check=True,
cwd=typeshed_dir)
with open(diff_file, "w") as f:
f.write(out.stdout)
subprocess.run(["git",
"apply",
"--index",
"--directory=mypy/typeshed",
"--exclude=**/tests/**",
diff_file],
check=True)

title = parse_commit_title(out.stdout)
subprocess.run(["git", "commit", "-m", f"Typeshed cherry-pick: {title}"], check=True)

print()
print(f"Cherry-picked commit {commit} from {typeshed_dir}")


if __name__ == '__main__':
main()