Skip to content

Commit 09f76c0

Browse files
authored
Merge pull request #15 from bdowling/fix-remote-issues
Fixes a number of remote issues
2 parents 5bb2ebc + e4defa4 commit 09f76c0

File tree

1 file changed

+72
-33
lines changed

1 file changed

+72
-33
lines changed

git-pull-request

Lines changed: 72 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,31 @@ Automatically check out github pull requests into their own branch.
66
77
Usage:
88
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.
1013
1114
Options:
1215
1316
-h, --help
1417
Display this message and exit
1518
16-
-r <repo>, --repo <repo>
19+
-r <remote or full/repo>, --repo <full/repo>, --remote <remote>
1720
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+
"""
1932

33+
copyright = """
2034
Copyright (C) 2011 by Andreas Gohr <[email protected]>
2135
2236
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,
3751
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
3852
THE SOFTWARE.
3953
"""
54+
4055
import sys
4156
import getopt
4257
import json
@@ -47,10 +62,12 @@ import pipes
4762

4863

4964
def main():
50-
repo, remote = '', None
65+
repo, remote = None, None
66+
git_method = 'html_url'
67+
5168
# parse command line options
5269
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"])
5471
except getopt.error, msg:
5572
print msg
5673
print "for help use --help"
@@ -60,42 +77,63 @@ def main():
6077
if o in ("-h", "--help"):
6178
print __doc__
6279
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"):
6486
if re.search('/', a):
6587
repo = a
6688
else:
6789
remote = a
6890

69-
if remote is None and repo == '':
70-
remote = 'origin'
7191

7292
# attempt to get token from git config
7393
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:
88120
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"
91128
print "the command line using the -r parameter, by specifying either a remote or"
92129
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>"
94132
sys.exit(1)
95133

96134
# process arguments
97135
if len(args):
98-
ret = fetch(repo, token, args[0])
136+
ret = fetch(repo, token, args[0], git_method)
99137
else:
100138
ret = show(repo, token)
101139

@@ -138,7 +176,7 @@ def show(repo, token):
138176
exit(1)
139177

140178
data = response.read()
141-
if (data == ''):
179+
if not data:
142180
print "failed to speak with github."
143181
return 3
144182

@@ -150,7 +188,7 @@ def show(repo, token):
150188
return 0
151189

152190

153-
def fetch(repo, token, pullreq):
191+
def fetch(repo, token, pullreq, git_method):
154192
print "loading pull request info for request %s..." % (pullreq)
155193
print
156194
url = "https://api.github.com/repos/%s/pulls/%s" % (repo, pullreq)
@@ -172,7 +210,7 @@ def fetch(repo, token, pullreq):
172210
exit(1)
173211

174212
data = response.read()
175-
if (data == ''):
213+
if not data:
176214
print "failed to speak with github."
177215
return 3
178216

@@ -197,12 +235,13 @@ def fetch(repo, token, pullreq):
197235
print "Failed to create/switch branch"
198236
return 5
199237

200-
print "pulling from %s (%s)" % (pr['head']['repo']['git_url'], pr['head']['ref'])
201238

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])
203242
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))
206245
if(ret != 0):
207246
print color_text("branch %s no longer exists." % ref, 'red')
208247
os.system('git checkout %s' % branch)

0 commit comments

Comments
 (0)