Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 136 lines (103 sloc) 4.084 kB
#!/usr/bin/env python
import argparse
import subprocess
import sys
class ScriptError(Exception):
pass
def run(*args, **kwargs):
show_cmd = kwargs.get('show_cmd', False)
shell = kwargs.get('shell', False)
if shell:
command = ' '.join(['"' + x + '"' for x in args])
else:
command = args
if show_cmd:
print ' '.join(args)
check_ret = kwargs.get('check_ret', True)
p = subprocess.Popen(
command, shell=shell,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = p.communicate()
retcode = p.poll()
if check_ret and retcode:
raise ScriptError("Error executing: %r (%d, %s)" % (command, retcode, output))
return output[0]
def run_git(*args, **kwargs):
command = ['git'] + list(args)
return run(*command, **kwargs).strip()
def get_current_branch():
return run_git('symbolic-ref', 'HEAD').replace('refs/heads/', '')
def get_local_ref(name):
return run_git('show-ref', 'heads/' + name,
check_ret=False)
def get_remote_ref(remote, name):
return run_git('show-ref', 'remotes/%s/%s' % (remote, name),
check_ret=False)
def get_remote_config(local_branch):
return run_git('config', 'branch.%s.merge' % local_branch,
check_ret=False)
def get_remote_url(repository):
return run_git('config', 'remote.%s.url' % repository,
check_ret=False)
def main():
parser = argparse.ArgumentParser(
prog='git-publish-branch',
description=
"Quick way to publish a branch remotely and track it locally."
)
parser.add_argument(
'-d', '--delete', dest='delete', action='store_true',
default=False,
help='Delete the remote branch and stop tracking it. '
'Keeps the local branch')
parser.add_argument(
'-n', '--dry-run', dest='dry_run', action='store_true',
default=False,
help="Don't actually publish and track the branch.")
parser.add_argument(
'branch', nargs='?', default=None,
help="The branch to publish. HEAD (the current branch) if left blank.")
parser.add_argument(
'repository', nargs='?', default='origin',
help="The repository to publish to. Defaults to 'origin'.")
args = parser.parse_args()
if args.branch is None:
args.branch = get_current_branch()
local_ref = get_local_ref(args.branch)
if not get_remote_url(args.repository):
raise ScriptError(
"There is no remote repository named '%s'" % args.repository)
if args.delete:
print run_git('push', args.repository, ':refs/heads/%s' % args.branch)
if local_ref:
run_git('config', '--unset', 'branch.%s.remote' % args.branch)
run_git('config', '--unset', 'branch.%s.merge' % args.branch)
else:
if not local_ref:
raise ScriptError(
"Local branch '%s' doesn't exist!" % args.branch)
remote_ref = get_remote_ref(args.repository, args.branch)
if remote_ref:
raise ScriptError(
"A remote branch '%s' on '%s' already exists!" % (
args.branch, args.repository))
remote_config = get_remote_config(args.branch)
if remote_config:
raise ScriptError(
"Local branch '%s' is already a tracking branch!" % (
args.branch))
if not args.dry_run:
d = {'branch': args.branch, 'remote': args.repository}
print run_git('push', args.repository,
'%(branch)s:refs/heads/%(branch)s' % d,
show_cmd=True)
run_git('config', 'branch.%(branch)s.remote' % d,
args.repository)
run_git('config', 'branch.%(branch)s.merge' % d,
'refs/heads/%(branch)s' % d)
if __name__ == '__main__':
try:
main()
except ScriptError, e:
print >>sys.stderr, "ERROR: %s" % str(e)
sys.exit(1)
Jump to Line
Something went wrong with that request. Please try again.