def svnurl_get_gitbranch(svn_url): root_url = svn.get_package_root_url(svn_url) gitbranch = 'master' svn_parts = svn_url.split('/') if root_url!=svn_url: if svn_parts[-2]=='branches': gitbranch = 'remotes/%s' % svn_parts[-1] elif svn_parts[-2]=='tags': gitbranch = 'remotes/tags/%s' % svn_parts[-1] return gitbranch
def checkout_gitsvn(svn_url, location='.'): svn_url = svn_url[-1]=='/' and svn_url[:-1] or svn_url root_url = svn.get_package_root_url(svn_url) if not svn.isdir(svn_url): raise svn.InvalidSubversionURL expected_dirs = ('trunk', 'tags', 'branches') got_dirs = expected_dirs try: svn.check_project_layout(svn_url) except svn.InvalidProjectLayout: # check the directories and print a warning dircontent = runcmd('svn ls %s' % svn_url, log=False, respond=True) dircontent = [x.strip()[:-1] for x in dircontent] got_dirs = [] missing_dirs = [] for dir in expected_dirs: if dir in dircontent: got_dirs.append(dir) else: missing_dirs.append(dir) output.warning('Directory %s missing!' % dir) package_name = svn.get_package_name(svn_url) cache_path = os.path.join(get_gitsvn_cache_path(), package_name) if os.path.exists(package_name): raise Exception('%s already existing' % os.path.abspath(package_name)) gitbranch = svnurl_get_gitbranch(svn_url) # clone it if os.path.exists(cache_path): runcmd('cd %s; git reset --hard' % cache_path) runcmd('cd %s; git svn fetch' % cache_path) runcmd('cd %s; git svn rebase' % cache_path) else: if got_dirs==expected_dirs: # we have a standard layout cmd = 'cd %s; git svn clone --stdlayout %s' % ( get_gitsvn_cache_path(), root_url, ) else: # some dirs are missing args = ['--%s=%s' % (d,d) for d in got_dirs] cmd = 'cd %s; git svn clone %s %s' % ( get_gitsvn_cache_path(), ' '.join(args), root_url, ) runcmd(cmd) runcmd('cp -r %s %s' % (cache_path, location)) co_path = os.path.join(location, package_name) runcmd('cd %s ; git checkout %s' % ( co_path, gitbranch, )) runcmd('cd %s ; git reset --hard' % co_path) runcmd('cd %s ; git svn rebase' % co_path)