clone and sync --keep-path to keep perforce path to module.
[fast-export/dharding.git] / hg2git.py
blob1f631e7254256d3652e89cf22c46126b4abd966e
1 #!/usr/bin/env python
3 # Copyright (c) 2007 Rocco Rutte <pdmef@gmx.net>
4 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
6 from mercurial import repo,hg,cmdutil,util,ui,revlog,node
7 import re
8 import os
10 # git branch for hg's default 'HEAD' branch
11 cfg_master='master'
12 # silly regex to see if user field has email address
13 user_re=re.compile('([^<]+) (<[^>]+>)$')
14 # silly regex to clean out user names
15 user_clean_re=re.compile('^["]([^"]+)["]$')
17 def setup_repo(url):
18 myui=ui.ui(interactive=False)
19 return myui,hg.repository(myui,url)
21 def fixup_user(user,authors):
22 if authors!=None:
23 # if we have an authors table, try to get mapping
24 # by defaulting to the current value of 'user'
25 user=authors.get(user,user)
26 name,mail,m='','',user_re.match(user)
27 if m==None:
28 # if we don't have 'Name <mail>' syntax, use 'user
29 # <devnull@localhost>' if use contains no at and
30 # 'user <user>' otherwise
31 name=user
32 if '@' not in user:
33 mail='<devnull@localhost>'
34 else:
35 mail='<%s>' % user
36 else:
37 # if we have 'Name <mail>' syntax, everything is fine :)
38 name,mail=m.group(1),m.group(2)
40 # remove any silly quoting from username
41 m2=user_clean_re.match(name)
42 if m2!=None:
43 name=m2.group(1)
44 return '%s %s' % (name,mail)
46 def get_branch(name):
47 if name=='HEAD':
48 name=cfg_master
49 return name
51 def get_changeset(ui,repo,revision,authors={}):
52 node=repo.lookup(revision)
53 (manifest,user,(time,timezone),files,desc,extra)=repo.changelog.read(node)
54 tz="%+03d%02d" % (-timezone / 3600, ((-timezone % 3600) / 60))
55 branch=get_branch(extra.get('branch','master'))
56 return (node,manifest,fixup_user(user,authors),(time,tz),files,desc,branch,extra)
58 def load_cache(filename):
59 cache={}
60 if not os.path.exists(filename):
61 return cache
62 f=open(filename,'r')
63 l=0
64 for line in f.readlines():
65 l+=1
66 fields=line.split(' ')
67 if fields==None or not len(fields)==2 or fields[0][0]!=':':
68 sys.stderr.write('Invalid file format in [%s], line %d\n' % (filename,l))
69 continue
70 # put key:value in cache, key without ^:
71 cache[fields[0][1:]]=fields[1].split('\n')[0]
72 f.close()
73 return cache
75 def save_cache(filename,cache):
76 f=open(filename,'w+')
77 map(lambda x: f.write(':%s %s\n' % (str(x),str(cache.get(x)))),cache.keys())
78 f.close()
80 def get_git_sha1(name,type='heads'):
81 try:
82 f=open(os.getenv('GIT_DIR','/dev/null')+'/refs/'+type+'/'+name)
83 sha1=f.readlines()[0].split('\n')[0]
84 f.close()
85 return sha1
86 except IOError:
87 return None