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