##// END OF EJS Templates
Create a branch for the merge, so it's easier to do local testing...
Fernando Perez -
Show More
@@ -1,115 +1,128 b''
1 1 #!/usr/bin/env python
2 2 # -*- coding: utf-8 -*-
3 3 """
4 4 Usage:
5 python git-mpr.py -m 1657
5 git-mpr [-h] [-l | -a] [pr-number [pr-number ...]]
6
7 Type `git mpr -h` for details.
6 8 """
9
7 10 from __future__ import print_function
8 11
9 12 import io, os
10 13 import argparse
11 14 from subprocess import check_call, CalledProcessError
12 15
13 16 import gh_api
14 17
15 18 ipy_repository = 'git://github.com/ipython/ipython.git'
16 19 gh_project = "ipython/ipython"
17 20 not_merged = {}
18 21
19 22 def merge_branch(repo, branch ):
20 23 """try to merge the givent branch into the current one
21 24
22 25 If something does not goes smoothly, merge is aborted
23 26
24 27 Returns True if merge sucessfull, False otherwise
25 28 """
26 29 # Delete the branch first
27 30 try :
28 31 check_call(['git', 'pull', repo, branch], stdin=io.open(os.devnull))
29 32 except CalledProcessError :
30 33 check_call(['git', 'merge', '--abort'])
31 34 return False
32 35 return True
33 36
37
38 def git_new_branch(name):
39 """Create a new branch with the given name and check it out.
40 """
41 check_call(['git', 'checkout', '-b', name])
34 42
43
35 44 def merge_pr(num):
36 45 """ try to merge the branch of PR `num` into current branch
37 46 """
38 47 # Get Github authorisation first, so that the user is prompted straight away
39 48 # if their login is needed.
40 49
41 50 pr = gh_api.get_pull_request(gh_project, num)
42 51 repo = pr['head']['repo']['clone_url']
43 52
44 53
45 54 branch = pr['head']['ref']
46 55 mergeable = merge_branch(repo=repo,
47 56 branch=branch,
48 57 )
49 58 if not mergeable :
50 59 cmd = "git pull "+repo+" "+branch
51 60 not_merged[str(num)] = cmd
52 61 print("==============================================================================")
53 62 print("Something went wrong merging this branch, you can try it manually by runngin :")
54 63 print(cmd)
55 64 print("==============================================================================")
56 65
57 66
58 67 def main(*args):
59 68 parser = argparse.ArgumentParser(
60 69 description="""
61 70 Merge one or more github pull requests by their number. If any
62 71 one pull request can't be merged as is, its merge is ignored
63 72 and the process continues with the next ones (if any).
64 73 """
65 74 )
66 75
67 76 grp = parser.add_mutually_exclusive_group()
68 77 grp.add_argument(
69 78 '-l',
70 79 '--list',
71 80 action='store_const',
72 81 const=True,
73 82 help='list PR, their number and their mergeability')
74 83 grp.add_argument('-a',
75 84 '--merge-all',
76 85 action='store_const',
77 86 const=True ,
78 87 help='try to merge as many PR as possible, one by one')
79 88 parser.add_argument('merge',
80 89 type=int,
81 90 help="The pull request numbers",
82 91 nargs='*',
83 92 metavar='pr-number')
84 93 args = parser.parse_args()
85 94
86 95 if(args.list):
87 96 pr_list = gh_api.get_pulls_list(gh_project)
88 97 for pr in pr_list :
89 98 mergeable = gh_api.get_pull_request(gh_project, pr['number'])['mergeable']
90 99
91 100 ismgb = u"√" if mergeable else " "
92 101 print(u"* #{number} [{ismgb}]: {title}".format(
93 102 number=pr['number'],
94 103 title=pr['title'],
95 104 ismgb=ismgb))
96 105
97 106 if(args.merge_all):
107 branch_name = 'merge-' + '-'.join(str(pr['number']) for pr in pr_list)
108 git_new_branch(branch_name)
98 109 pr_list = gh_api.get_pulls_list(gh_project)
99 110 for pr in pr_list :
100 111 merge_pr(pr['number'])
101 112
102 113
103 114 elif args.merge:
115 branch_name = 'merge-' + '-'.join(map(str, args.merge))
116 git_new_branch(branch_name)
104 117 for num in args.merge :
105 118 merge_pr(num)
106 119
107 120 if not_merged :
108 121 print('*************************************************************************************')
109 122 print('the following branch have not been merged automatically, considere doing it by hand :')
110 123 for num, cmd in not_merged.items() :
111 124 print( "PR {num}: {cmd}".format(num=num, cmd=cmd))
112 125 print('*************************************************************************************')
113 126
114 127 if __name__ == '__main__':
115 128 main()
General Comments 0
You need to be logged in to leave comments. Login now