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
11 # default git branch name
13 # silly regex to see if user field has email address
14 user_re
=re
.compile('([^<]+) (<[^>]+>)$')
15 # silly regex to clean out user names
16 user_clean_re
=re
.compile('^["]([^"]+)["]$')
19 myui
=ui
.ui(interactive
=False)
20 return myui
,hg
.repository(myui
,url
)
22 def fixup_user(user
,authors
):
24 # if we have an authors table, try to get mapping
25 # by defaulting to the current value of 'user'
26 user
=authors
.get(user
,user
)
27 name
,mail
,m
='','',user_re
.match(user
)
29 # if we don't have 'Name <mail>' syntax, use 'user
30 # <devnull@localhost>' if use contains no at and
31 # 'user <user>' otherwise
34 mail
='<devnull@localhost>'
38 # if we have 'Name <mail>' syntax, everything is fine :)
39 name
,mail
=m
.group(1),m
.group(2)
41 # remove any silly quoting from username
42 m2
=user_clean_re
.match(name
)
45 return '%s %s' % (name
,mail
)
48 # 'HEAD' is the result of a bug in mutt's cvs->hg conversion,
49 # other CVS imports may need it, too
50 if name
=='HEAD' or name
=='default' or name
=='':
54 def get_changeset(ui
,repo
,revision
,authors
={}):
55 node
=repo
.lookup(revision
)
56 (manifest
,user
,(time
,timezone
),files
,desc
,extra
)=repo
.changelog
.read(node
)
57 tz
="%+03d%02d" % (-timezone
/ 3600, ((-timezone
% 3600) / 60))
58 branch
=get_branch(extra
.get('branch','master'))
59 return (node
,manifest
,fixup_user(user
,authors
),(time
,tz
),files
,desc
,branch
,extra
)
64 def load_cache(filename
,get_key
=mangle_key
):
66 if not os
.path
.exists(filename
):
70 for line
in f
.readlines():
72 fields
=line
.split(' ')
73 if fields
==None or not len(fields
)==2 or fields
[0][0]!=':':
74 sys
.stderr
.write('Invalid file format in [%s], line %d\n' % (filename
,l
))
76 # put key:value in cache, key without ^:
77 cache
[get_key(fields
[0][1:])]=fields
[1].split('\n')[0]
81 def save_cache(filename
,cache
):
83 map(lambda x
: f
.write(':%s %s\n' % (str(x
),str(cache
.get(x
)))),cache
.keys())
86 def get_git_sha1(name
,type='heads'):
88 # use git-rev-parse to support packed refs
89 cmd
="GIT_DIR='%s' git-rev-parse --verify refs/%s/%s 2>/dev/null" % (os
.getenv('GIT_DIR','/dev/null'),type,name
)
93 if l
== None or len(l
) == 0: