Created
April 11, 2014 13:10
-
-
Save johngian/10467530 to your computer and use it in GitHub Desktop.
git-checkout in github PR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import requests | |
import traceback | |
import sys | |
from git import Repo | |
from optparse import OptionParser | |
def main(): | |
parser = OptionParser(usage='usage: %prog [options] owner repo pr', | |
version='%prog 1.0') | |
parser.add_option('-v', '--verbose', | |
action='store_true', | |
dest='verbose', | |
default=False, | |
help='Print debug info.') | |
parser.add_option('-p', '--path', | |
action='store', | |
dest='repo_path', | |
default='.', | |
help='Define local repo path.') | |
parser.add_option('-f', '--force', | |
action='store_true', | |
dest='force', | |
default=False, | |
help='Force checkout to PR.') | |
parser.add_option('-d', '--delete', | |
action='store_true', | |
dest='delete_branch', | |
default=False, | |
help='Delete PR branch.') | |
(options, args) = parser.parse_args() | |
if len(args) != 3: | |
parser.error('Wrong number of arguments') | |
verbose = options.verbose | |
force = options.force | |
repo_path = options.repo_path | |
delete_branch = options.delete_branch | |
owner = args[0] | |
name = args[1] | |
pr = args[2] | |
url = 'https://api.github.com/repos/%s/%s/pulls/%s' % (owner, name, pr) | |
try: | |
response = requests.get(url) | |
data = response.json() | |
if verbose: | |
print 'Request url: %s' % url | |
print 'Status code: %d' % response.status_code | |
repo = Repo(repo_path) | |
remote_url = data['head']['repo']['git_url'] | |
remote_name = data['head']['repo']['owner']['login'] | |
remote_branch = data['head']['ref'] | |
local_branch = 'cr-%s' % data['number'] | |
if verbose: | |
print 'Remote url: %s' % remote_url | |
print 'Remote name: %s' % remote_name | |
if delete_branch: | |
try: | |
git = repo.git | |
git.checkout('master') | |
repo.delete_head(local_branch) | |
sys.exit(0) | |
except Exception: | |
print 'Something terrible happened' | |
print traceback.format_exc() | |
sys.exit(1) | |
try: | |
remote = repo.create_remote(remote_name, remote_url) | |
except: | |
print 'Remote already exists.' | |
for r in repo.remotes: | |
if r.name == remote_name: | |
remote = r | |
remote.fetch() | |
git = repo.git | |
try: | |
remote_tree = '%s/%s' % (remote_name, remote_branch) | |
git.checkout(remote_tree, b=local_branch) | |
except: | |
print 'Branch already exists.' | |
if force: | |
print 'Force create new branch.' | |
remote_tree = '%s/%s' % (remote_name, remote_branch) | |
git.checkout('master') | |
repo.delete_head(local_branch) | |
git.checkout(remote_tree, b=local_branch) | |
except Exception: | |
print 'Something terrible happened...' | |
print traceback.format_exc() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment