-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgh-fetch
More file actions
executable file
·53 lines (45 loc) · 1.86 KB
/
gh-fetch
File metadata and controls
executable file
·53 lines (45 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/python
from subprocess import *
import sys
import os
import restkit
import json
import re
from pprint import pprint
debug = True
upstream_repo = check_output(["git", "config", "sync.upstreamRepo"]).strip()
base_url = 'https://api.github.com/'
pr = sys.argv[1]
match = re.match(r"https://github.com/([^/]+)/([^/]+)/pull/(\d+)", pr)
if match:
# PR url
resource = restkit.Resource(base_url)
path = '/repos/%s/%s/pulls/%s' % (match.group(1), match.group(2), match.group(3))
if debug: print('Reading pull request information from %s' % path)
response = resource.get(path, headers={'Accept': 'application/json', 'Authorization': 'token ' + os.environ['GITHUB_OAUTH_TOKEN']})
json = json.loads(response.body_string())
head_branch = json['head']['label']
else:
# repo:branch
head_branch = pr
(repo, branch) = head_branch.split(":")
local_branch = 'pr_' + repo + '_' + branch.replace('/', '_')
project = os.path.basename(os.getcwd())
try:
remotes = check_output(["git", "remote"]).split("\n")
#print "remotes %s" % remotes
remote_exists = repo in remotes
if not remote_exists:
check_call(["git", "remote", "add", "--no-tags", repo, "git@github.com:%s/%s.git" % (repo, project)])
current_branch = check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"]).strip()
if current_branch == local_branch:
check_call(["git", "checkout", "master"])
print("git fetch --no-tags -f %s %s:%s" % (repo, branch, local_branch))
check_call(["git", "fetch", "--no-tags", "-f", "-q", repo, branch + ":" + local_branch])
check_call(["git", "checkout", local_branch])
release_branch = check_output(['find_upstream_branch']).strip()
check_call(["git", "fetch", upstream_repo, release_branch + ":" + release_branch])
check_call(["git", "branch", "-u", release_branch])
check_call(["git", "rebase", "-f", release_branch])
except CalledProcessError as e:
print(e)