Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master-python
Fetching contributors…

Cannot retrieve contributors at this time

executable file 171 lines (144 sloc) 5.376 kb
#!/usr/bin/env python
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
import os
from os.path import join as pjoin, abspath, isdir, isfile, dirname
import sys
import subprocess
def add(name, location):
remotes = [r.strip() for r in shrun('git remote show').split('\n')]
if name in remotes:
raise OSError("There is already a remote with that name")
if shrun('git config git-bzr.%(name)s.location' % locals()) != "":
raise OSError("There is alread a git-bzr branch with that name")
if not isdir(pjoin(location, ".bzr")):
raise OSError("%(location)s is not a bazaar repository" %
locals())
shrun('git config git-bzr.%(name)s.location %(location)s' %
locals())
print "Bazaar branch %(name)s added. You can fetch it with " \
"`git bzr fetch %(name)s`" % locals()
def fetch(remote):
location = get_location(remote)
git_map, bzr_map = get_maps(remote)
if not isfile(git_map) and not isfile(bzr_map):
print "There doesn't seem to be an existing refmap. "
print "Doing an initial import"
map_name = dirname(git_map)
if not isdir(map_name):
os.makedirs(map_name)
shcall('(bzr fast-export '
'--plain '
'--export-marks=%(bzr_map)s '
'--git-branch=bzr/%(remote)s '
'%(location)s) | '
'(git fast-import '
'--export-marks=%(git_map)s)'
% locals())
elif isfile(git_map) and isfile(bzr_map):
print 'Updating remote', remote
old_rev = rev_parse('bzr/%s' % remote)
shcall('(bzr fast-export '
'--plain '
'--import-marks=%(bzr_map)s '
'--export-marks=%(bzr_map)s '
'--git-branch=bzr/%(remote)s '
'%(location)s) | '
'(git fast-import '
'--quiet '
'--export-marks=%(git_map)s '
'--import-marks=%(git_map)s)'
% locals())
new_rev = rev_parse('bzr/%s' % remote)
print "Changes since last update:"
print shrun('git shortlog %s..%s' % (old_rev, new_rev))
else:
raise OSError("One of the mapfiles is missing! "
"Something went wrong!")
def push(remote):
location = get_location(remote)
lst = shrun('git rev-list --left-right HEAD...bzr/%s' % remote)
if lst.startswith('>'):
raise OSError("HEAD is not a strict child of %s, "
"cannot push. Merge first" % remote)
if not lst.startswith('<'):
raise OSError("Nothing to push. Commit something first")
git_map, bzr_map = get_maps(remote)
if not isfile(git_map) or not isfile(bzr_map):
raise OSError("We do not have refmapping yet. "
"Then how can I push?")
shcall('(git fast-export '
'--import-marks=%(git_map)s '
'--export-marks=%(git_map)s '
'HEAD) | '
'(cd %(location)s && '
'bzr fast-import '
'--import-marks=%(bzr_map)s '
'--export-marks=%(bzr_map)s -)' % locals())
def shrun(cmd, ret_error=False, ret_code=False):
proc = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
(out, err) = proc.communicate()
if not (ret_error or ret_code):
return out
ret = [out]
if ret_error:
ret.append(err)
if ret_code:
ret.append(proc.returncode)
return ret
def shcall(cmd):
return subprocess.check_call(cmd, shell=True)
def get_maps(remote):
git_dir = abspath(get_git_dir())
map_dir = pjoin(git_dir, 'bzr-git')
git_map = pjoin(map_dir, '%s-git-map' % remote)
bzr_map = pjoin(map_dir, '%s-bzr-map' % remote)
return git_map, bzr_map
def get_location(remote):
out = shrun('git config git-bzr.%s.location' % remote).strip()
if out == "":
raise OSError("Cannot find bazaar remote with name `%(s`."
% remote)
return out
def get_git_dir():
return rev_parse('--git-dir')
def rev_parse(arg):
return shrun('git rev-parse %s' % arg).strip()
def to_git_root():
out, code = shrun('git rev-parse', ret_code=True)
if code != 0:
raise OSError("Must be inside a git repository to work")
# change to root of git repository
up = rev_parse('--show-cdup')
if up != '':
os.chdir(up)
def parse_args(argv, cmds):
program = argv.pop(0)
cmd_names = cmds.keys()
usage = 'Usage: %s {%s}' % (program, ', '.join(cmd_names))
if len(argv) == 0:
print usage
sys.exit()
cmd = argv.pop(0)
if cmd not in cmd_names:
print usage
sys.exit()
func, req_args = cmds[cmd]
if len(argv) != len(req_args):
print 'Usage: %s %s %s ' % (program,
cmd,
' '.join(req_args))
sys.exit()
return func, argv
def main():
cmds = {'add': (add, ('name', 'location')),
'fetch': (fetch, ('branchname',)),
'push': (push, ('branchname',))}
func, args = parse_args(sys.argv, cmds)
to_git_root()
func(*args)
if __name__ == '__main__':
main()
Jump to Line
Something went wrong with that request. Please try again.