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