@@ -6,17 +6,31 @@ Automatically check out github pull requests into their own branch.
6
6
7
7
Usage:
8
8
9
- git pull-request <OPTIONS> [<pull request number>]
9
+ git pull-request <OPTIONS> <pull request number>
10
+
11
+ When a PR# is specified, it will be fetched, otherwise a list of PRs
12
+ in the remote branch will be shown.
10
13
11
14
Options:
12
15
13
16
-h, --help
14
17
Display this message and exit
15
18
16
- -r <repo>, --repo <repo>
19
+ -r <remote or full/ repo>, --repo <full/ repo>, --remote <remote >
17
20
Use this github repo instead of the 'remote origin' or 'github.repo'
18
- git config settings. Needs to be in "user/repository" form
21
+ git config settings. Full form needs to be "user/repository", otherwise
22
+ it is assumed to be a short remote alias name
23
+
24
+ -g, --git
25
+ Use git_url instead of http URL for fetching
26
+
27
+ --copy
28
+ List the Copyright details
29
+
30
+ Copyright (C) 2011 by Andreas Gohr <[email protected] >
31
+ """
19
32
33
+ copyright = """
20
34
Copyright (C) 2011 by Andreas Gohr <[email protected] >
21
35
22
36
Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -37,6 +51,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37
51
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
38
52
THE SOFTWARE.
39
53
"""
54
+
40
55
import sys
41
56
import getopt
42
57
import json
@@ -47,10 +62,12 @@ import pipes
47
62
48
63
49
64
def main ():
50
- repo , remote = '' , None
65
+ repo , remote = None , None
66
+ git_method = 'html_url'
67
+
51
68
# parse command line options
52
69
try :
53
- opts , args = getopt .getopt (sys .argv [1 :], "hr:" , ["help" , "repo:" ])
70
+ opts , args = getopt .getopt (sys .argv [1 :], "hr:g " , ["help" , "repo:" , "copy" , "git " ])
54
71
except getopt .error , msg :
55
72
print msg
56
73
print "for help use --help"
@@ -60,42 +77,63 @@ def main():
60
77
if o in ("-h" , "--help" ):
61
78
print __doc__
62
79
sys .exit (0 )
63
- if o in ("-r" , "--repo" ):
80
+ elif o in ("--copy" ):
81
+ print copyright
82
+ sys .exit (0 )
83
+ elif o in ("-g" , "--git" ):
84
+ git_method = 'git_url'
85
+ elif o in ("-r" , "--repo" , "--remote" ):
64
86
if re .search ('/' , a ):
65
87
repo = a
66
88
else :
67
89
remote = a
68
90
69
- if remote is None and repo == '' :
70
- remote = 'origin'
71
91
72
92
# attempt to get token from git config
73
93
token = os .popen ("git config --get github.token" ).read ().rstrip ()
74
- # get repo name from git config:
75
- if (repo == '' ):
76
- repo = os .popen ('git config github.repo' ).read ().strip ()
77
-
78
- # get repo name from origin
79
- if (repo == '' or remote is not None ):
80
- escaped = pipes .quote (remote )
81
- origin = os .popen ("git config remote.%s.url" % escaped ).read ()
82
- origin = re .sub ("(\.git)?\s*$" , "" , origin )
83
- m = re .search (r"\bgithub\.com[:/]([^/]+/[^/]+)$" , origin )
84
- if (m is not None ):
85
- repo = m .group (1 )
86
-
87
- if (repo == '' ):
94
+ # try to get repo name from git config:
95
+ if not repo and not remote :
96
+ repo = os .popen ('git config --get github.repo' ).read ().strip ()
97
+
98
+ if repo and not re .search ('/' , repo ):
99
+ # if repo is not a full repo, assume it is remote alias
100
+ remote = repo
101
+ repo = None
102
+
103
+ # else try to get remote from git config:
104
+ if not repo :
105
+ if not remote :
106
+ remote = os .popen ('git config --get github.remote' ).read ().strip ()
107
+ if not remote :
108
+ remote = 'origin'
109
+
110
+ if remote :
111
+ # get full repo name from remote url
112
+ escaped = pipes .quote (remote )
113
+ origin = os .popen ("git config --get remote.%s.url" % escaped ).read ()
114
+ origin = re .sub ("(\.git)?\s*$" , "" , origin )
115
+ m = re .search (r"\bgithub\.com[:/]([^/]+/[^/]+)$" , origin )
116
+ if (m is not None ):
117
+ repo = m .group (1 )
118
+
119
+ if not repo :
88
120
print color_text ("Failed to determine github repository name" , 'red' , True )
89
- print "The repository is usually automatically detected from your remote origin."
90
- print "If your origin doesn't point to github, you can specify the repository on"
121
+ print "The repository is usually automatically detected from your remote."
122
+ print "By default the remote is assumed to be 'origin', but you can override this by"
123
+ print "using the -r parameter or specifying the github.remote option in your config"
124
+ print ""
125
+ print " git config --global github.remote upstream"
126
+ print ""
127
+ print "If your remote url doesn't point to github, you can specify the repository on"
91
128
print "the command line using the -r parameter, by specifying either a remote or"
92
129
print "the full repository name (user/repo), or configure it using"
93
- print "git config github.repo <user>/<repository>"
130
+ print ""
131
+ print " git config github.repo <user>/<repository>"
94
132
sys .exit (1 )
95
133
96
134
# process arguments
97
135
if len (args ):
98
- ret = fetch (repo , token , args [0 ])
136
+ ret = fetch (repo , token , args [0 ], git_method )
99
137
else :
100
138
ret = show (repo , token )
101
139
@@ -138,7 +176,7 @@ def show(repo, token):
138
176
exit (1 )
139
177
140
178
data = response .read ()
141
- if ( data == '' ) :
179
+ if not data :
142
180
print "failed to speak with github."
143
181
return 3
144
182
@@ -150,7 +188,7 @@ def show(repo, token):
150
188
return 0
151
189
152
190
153
- def fetch (repo , token , pullreq ):
191
+ def fetch (repo , token , pullreq , git_method ):
154
192
print "loading pull request info for request %s..." % (pullreq )
155
193
print
156
194
url = "https://api.github.com/repos/%s/pulls/%s" % (repo , pullreq )
@@ -172,7 +210,7 @@ def fetch(repo, token, pullreq):
172
210
exit (1 )
173
211
174
212
data = response .read ()
175
- if ( data == '' ) :
213
+ if not data :
176
214
print "failed to speak with github."
177
215
return 3
178
216
@@ -197,12 +235,13 @@ def fetch(repo, token, pullreq):
197
235
print "Failed to create/switch branch"
198
236
return 5
199
237
200
- print "pulling from %s (%s)" % (pr ['head' ]['repo' ]['git_url' ], pr ['head' ]['ref' ])
201
238
202
- git_url = pipes .quote (pr ['head' ]['repo' ]['git_url' ])
239
+ print "pulling from %s (%s)" % (pr ['head' ]['repo' ][git_method ], pr ['head' ]['ref' ])
240
+
241
+ url = pipes .quote (pr ['head' ]['repo' ][git_method ])
203
242
ref = pipes .quote (pr ['head' ]['ref' ])
204
- print 'git pull %s %s' % (git_url , ref )
205
- ret = os .system ('git pull %s %s' % (git_url , ref ))
243
+ print 'git pull %s %s' % (url , ref )
244
+ ret = os .system ('git pull %s %s' % (url , ref ))
206
245
if (ret != 0 ):
207
246
print color_text ("branch %s no longer exists." % ref , 'red' )
208
247
os .system ('git checkout %s' % branch )
0 commit comments