Show More
@@ -1,98 +1,93 b'' | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | """ |
|
3 | 3 | Backport pull requests to a particular branch. |
|
4 | 4 | |
|
5 | 5 | Usage: backport_pr.py branch PR |
|
6 | 6 | |
|
7 | 7 | e.g.: |
|
8 | 8 | |
|
9 | 9 | backport_pr.py 0.13.1 123 |
|
10 | 10 | |
|
11 | 11 | to backport PR #123 onto branch 0.13.1 |
|
12 | 12 | |
|
13 | 13 | """ |
|
14 | 14 | |
|
15 | 15 | from __future__ import print_function |
|
16 | 16 | |
|
17 | 17 | import os |
|
18 | 18 | import sys |
|
19 | 19 | from subprocess import Popen, PIPE, check_call, check_output |
|
20 | 20 | from urllib import urlopen |
|
21 | 21 | |
|
22 | 22 | from gh_api import get_pull_request, get_pull_request_files |
|
23 | 23 | |
|
24 | 24 | def find_rejects(root='.'): |
|
25 | 25 | for dirname, dirs, files in os.walk(root): |
|
26 | 26 | for fname in files: |
|
27 | 27 | if fname.endswith('.rej'): |
|
28 | 28 | yield os.path.join(dirname, fname) |
|
29 | 29 | |
|
30 | 30 | def get_current_branch(): |
|
31 | 31 | branches = check_output(['git', 'branch']) |
|
32 | 32 | for branch in branches.splitlines(): |
|
33 | 33 | if branch.startswith('*'): |
|
34 | 34 | return branch[1:].strip() |
|
35 | 35 | |
|
36 | 36 | def backport_pr(branch, num, project='ipython/ipython'): |
|
37 | 37 | current_branch = get_current_branch() |
|
38 | 38 | if branch != current_branch: |
|
39 | 39 | check_call(['git', 'checkout', branch]) |
|
40 | 40 | pr = get_pull_request(project, num, auth=True) |
|
41 | 41 | files = get_pull_request_files(project, num, auth=True) |
|
42 | 42 | patch_url = pr['patch_url'] |
|
43 | 43 | title = pr['title'] |
|
44 | 44 | description = pr['body'] |
|
45 | 45 | fname = "PR%i.patch" % num |
|
46 | 46 | if os.path.exists(fname): |
|
47 | 47 | print("using patch from {fname}".format(**locals())) |
|
48 | 48 | with open(fname) as f: |
|
49 | 49 | patch = f.read() |
|
50 | 50 | else: |
|
51 | 51 | req = urlopen(patch_url) |
|
52 | 52 | patch = req.read() |
|
53 | 53 | |
|
54 | 54 | msg = "Backport PR #%i: %s" % (num, title) + '\n\n' + description |
|
55 | 55 | check = Popen(['git', 'apply', '--check', '--verbose'], stdin=PIPE) |
|
56 | 56 | a,b = check.communicate(patch) |
|
57 | 57 | |
|
58 | 58 | if check.returncode: |
|
59 | 59 | print("patch did not apply, saving to {fname}".format(**locals())) |
|
60 | 60 | print("edit {fname} until `cat {fname} | git apply --check` succeeds".format(**locals())) |
|
61 | 61 | print("then run tools/backport_pr.py {num} again".format(**locals())) |
|
62 | 62 | if not os.path.exists(fname): |
|
63 | 63 | with open(fname, 'wb') as f: |
|
64 | 64 | f.write(patch) |
|
65 | 65 | return 1 |
|
66 | 66 | |
|
67 | 67 | p = Popen(['git', 'apply'], stdin=PIPE) |
|
68 | 68 | a,b = p.communicate(patch) |
|
69 | 69 | |
|
70 | 70 | filenames = [ f['filename'] for f in files ] |
|
71 | add = Popen(['git', 'add'] + filenames) | |
|
72 | add.wait() | |
|
73 | if add.returncode: | |
|
74 | print("git add %s failed!" % filenames) | |
|
75 | return 1 | |
|
76 | 71 | |
|
77 | commit = Popen(['git', 'commit', '-m', msg]) | |
|
72 | commit = Popen(['git', 'commit', '-m', msg] + filenames) | |
|
78 | 73 | commit.wait() |
|
79 | 74 | if commit.returncode: |
|
80 | 75 | print("commit failed!") |
|
81 | 76 | return 1 |
|
82 | 77 | else: |
|
83 | 78 | print("PR #%i applied, with msg:" % num) |
|
84 | 79 | print() |
|
85 | 80 | print(msg) |
|
86 | 81 | print() |
|
87 | 82 | |
|
88 | 83 | if branch != current_branch: |
|
89 | 84 | check_call(['git', 'checkout', current_branch]) |
|
90 | 85 | |
|
91 | 86 | return 0 |
|
92 | 87 | |
|
93 | 88 | if __name__ == '__main__': |
|
94 | 89 | if len(sys.argv) < 3: |
|
95 | 90 | print(__doc__) |
|
96 | 91 | sys.exit(1) |
|
97 | 92 | |
|
98 | 93 | sys.exit(backport_pr(sys.argv[1], int(sys.argv[2]))) |
General Comments 0
You need to be logged in to leave comments.
Login now