From 7044bdd4d16787fbeae1f1cd45a0c8469a7dd531 Mon Sep 17 00:00:00 2001 From: Rocco Rutte Date: Mon, 19 Mar 2007 08:45:42 +0000 Subject: [PATCH] Add hg2git.py with library routines Unfortunately, I can't do 'import hg-fast-export' from python itself, so we need to move some common methods into 'hg2git.py' which is to be used as a library for common hg->git routines. Signed-off-by: Rocco Rutte --- hg-fast-export.py | 70 +------------------------------------------------ hg2git.py | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 69 deletions(-) create mode 100755 hg2git.py diff --git a/hg-fast-export.py b/hg-fast-export.py index cbd295b..54e6868 100755 --- a/hg-fast-export.py +++ b/hg-fast-export.py @@ -4,6 +4,7 @@ # License: MIT from mercurial import repo,hg,cmdutil,util,ui,revlog,node +from hg2git import setup_repo,fixup_user,get_branch,get_changeset,load_cache,save_cache from tempfile import mkstemp from optparse import OptionParser import re @@ -12,58 +13,11 @@ import os # silly regex to catch Signed-off-by lines in log message sob_re=re.compile('^Signed-[Oo]ff-[Bb]y: (.+)$') -# silly regex to see if user field has email address -user_re=re.compile('([^<]+) (<[^>]+>)$') -# silly regex to clean out user names -user_clean_re=re.compile('^["]([^"]+)["]$') -# git branch for hg's default 'HEAD' branch -cfg_master='master' # insert 'checkpoint' command after this many commits or none at all if 0 cfg_checkpoint_count=0 # write some progress message every this many file contents written cfg_export_boundary=1000 -def setup_repo(url): - myui=ui.ui() - return myui,hg.repository(myui,url) - -def fixup_user(user,authors): - if authors!=None: - # if we have an authors table, try to get mapping - # by defaulting to the current value of 'user' - user=authors.get(user,user) - name,mail,m='','',user_re.match(user) - if m==None: - # if we don't have 'Name ' syntax, use 'user - # ' if use contains no at and - # 'user ' otherwise - name=user - if '@' not in user: - mail='' - else: - mail='<%s>' % user - else: - # if we have 'Name ' syntax, everything is fine :) - name,mail=m.group(1),m.group(2) - - # remove any silly quoting from username - m2=user_clean_re.match(name) - if m2!=None: - name=m2.group(1) - return '%s %s' % (name,mail) - -def get_branch(name): - if name=='HEAD': - name=cfg_master - return name - -def get_changeset(ui,repo,revision,authors={}): - node=repo.lookup(revision) - (manifest,user,(time,timezone),files,desc,extra)=repo.changelog.read(node) - tz="%+03d%02d" % (-timezone / 3600, ((-timezone % 3600) / 60)) - branch=get_branch(extra.get('branch','master')) - return (node,manifest,fixup_user(user,authors),(time,tz),files,desc,branch,extra) - def gitmode(x): return x and '100755' or '100644' @@ -305,28 +259,6 @@ def load_authors(filename): sys.stderr.write('Loaded %d authors\n' % l) return cache -def load_cache(filename): - cache={} - if not os.path.exists(filename): - return cache - f=open(filename,'r') - l=0 - for line in f.readlines(): - l+=1 - fields=line.split(' ') - if fields==None or not len(fields)==2 or fields[0][0]!=':': - sys.stderr.write('Invalid file format in [%s], line %d\n' % (filename,l)) - continue - # put key:value in cache, key without ^: - cache[fields[0][1:]]=fields[1].split('\n')[0] - f.close() - return cache - -def save_cache(filename,cache): - f=open(filename,'w+') - map(lambda x: f.write(':%s %s\n' % (str(x),str(cache.get(x)))),cache.keys()) - f.close() - def verify_heads(ui,repo,cache,force): def getsha1(branch): try: diff --git a/hg2git.py b/hg2git.py new file mode 100755 index 0000000..90014b5 --- /dev/null +++ b/hg2git.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python + +# Copyright (c) 2007 Rocco Rutte +# License: MIT + +from mercurial import repo,hg,cmdutil,util,ui,revlog,node +import re +import os + +# git branch for hg's default 'HEAD' branch +cfg_master='master' +# silly regex to see if user field has email address +user_re=re.compile('([^<]+) (<[^>]+>)$') +# silly regex to clean out user names +user_clean_re=re.compile('^["]([^"]+)["]$') + +def setup_repo(url): + myui=ui.ui(interactive=False) + return myui,hg.repository(myui,url) + +def fixup_user(user,authors): + if authors!=None: + # if we have an authors table, try to get mapping + # by defaulting to the current value of 'user' + user=authors.get(user,user) + name,mail,m='','',user_re.match(user) + if m==None: + # if we don't have 'Name ' syntax, use 'user + # ' if use contains no at and + # 'user ' otherwise + name=user + if '@' not in user: + mail='' + else: + mail='<%s>' % user + else: + # if we have 'Name ' syntax, everything is fine :) + name,mail=m.group(1),m.group(2) + + # remove any silly quoting from username + m2=user_clean_re.match(name) + if m2!=None: + name=m2.group(1) + return '%s %s' % (name,mail) + +def get_branch(name): + if name=='HEAD': + name=cfg_master + return name + +def get_changeset(ui,repo,revision,authors={}): + node=repo.lookup(revision) + (manifest,user,(time,timezone),files,desc,extra)=repo.changelog.read(node) + tz="%+03d%02d" % (-timezone / 3600, ((-timezone % 3600) / 60)) + branch=get_branch(extra.get('branch','master')) + return (node,manifest,fixup_user(user,authors),(time,tz),files,desc,branch,extra) + +def load_cache(filename): + cache={} + if not os.path.exists(filename): + return cache + f=open(filename,'r') + l=0 + for line in f.readlines(): + l+=1 + fields=line.split(' ') + if fields==None or not len(fields)==2 or fields[0][0]!=':': + sys.stderr.write('Invalid file format in [%s], line %d\n' % (filename,l)) + continue + # put key:value in cache, key without ^: + cache[fields[0][1:]]=fields[1].split('\n')[0] + f.close() + return cache + +def save_cache(filename,cache): + f=open(filename,'w+') + map(lambda x: f.write(':%s %s\n' % (str(x),str(cache.get(x)))),cache.keys()) + f.close() -- 2.11.4.GIT