##// END OF EJS Templates
use check_call for git add and commit...
MinRK -
Show More
@@ -1,100 +1,92 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 check_call(['git', 'pull'])
41 41 pr = get_pull_request(project, num, auth=True)
42 42 files = get_pull_request_files(project, num, auth=True)
43 43 patch_url = pr['patch_url']
44 44 title = pr['title']
45 45 description = pr['body']
46 46 fname = "PR%i.patch" % num
47 47 if os.path.exists(fname):
48 48 print("using patch from {fname}".format(**locals()))
49 49 with open(fname) as f:
50 50 patch = f.read()
51 51 else:
52 52 req = urlopen(patch_url)
53 53 patch = req.read()
54 54
55 55 msg = "Backport PR #%i: %s" % (num, title) + '\n\n' + description
56 56 check = Popen(['git', 'apply', '--check', '--verbose'], stdin=PIPE)
57 57 a,b = check.communicate(patch)
58 58
59 59 if check.returncode:
60 60 print("patch did not apply, saving to {fname}".format(**locals()))
61 61 print("edit {fname} until `cat {fname} | git apply --check` succeeds".format(**locals()))
62 62 print("then run tools/backport_pr.py {num} again".format(**locals()))
63 63 if not os.path.exists(fname):
64 64 with open(fname, 'wb') as f:
65 65 f.write(patch)
66 66 return 1
67 67
68 68 p = Popen(['git', 'apply'], stdin=PIPE)
69 69 a,b = p.communicate(patch)
70 70
71 71 filenames = [ f['filename'] for f in files ]
72 72
73 add = Popen(['git', 'add'] + filenames)
74 add.wait()
75 if add.returncode:
76 print("add failed!")
77 return 1
78
79 commit = Popen(['git', 'commit', '-m', msg])
80 commit.wait()
81 if commit.returncode:
82 print("commit failed!")
83 return 1
84 else:
85 print("PR #%i applied, with msg:" % num)
86 print()
87 print(msg)
88 print()
73 check_call(['git', 'add'] + filenames)
74
75 check_call(['git', 'commit', '-m', msg])
76
77 print("PR #%i applied, with msg:" % num)
78 print()
79 print(msg)
80 print()
89 81
90 82 if branch != current_branch:
91 83 check_call(['git', 'checkout', current_branch])
92 84
93 85 return 0
94 86
95 87 if __name__ == '__main__':
96 88 if len(sys.argv) < 3:
97 89 print(__doc__)
98 90 sys.exit(1)
99 91
100 92 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