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('^["]([^"]+)["]$')
18 def set_default_branch(name
):
23 myui
=ui
.ui(interactive
=False)
24 return myui
,hg
.repository(myui
,url
)
26 def fixup_user(user
,authors
):
28 # if we have an authors table, try to get mapping
29 # by defaulting to the current value of 'user'
30 user
=authors
.get(user
,user
)
31 name
,mail
,m
='','',user_re
.match(user
)
33 # if we don't have 'Name <mail>' syntax, use 'user
34 # <devnull@localhost>' if use contains no at and
35 # 'user <user>' otherwise
38 mail
='<devnull@localhost>'
42 # if we have 'Name <mail>' syntax, everything is fine :)
43 name
,mail
=m
.group(1),m
.group(2)
45 # remove any silly quoting from username
46 m2
=user_clean_re
.match(name
)
49 return '%s %s' % (name
,mail
)
52 # 'HEAD' is the result of a bug in mutt's cvs->hg conversion,
53 # other CVS imports may need it, too
54 if name
=='HEAD' or name
=='default' or name
=='':
58 def get_changeset(ui
,repo
,revision
,authors
={}):
59 node
=repo
.lookup(revision
)
60 (manifest
,user
,(time
,timezone
),files
,desc
,extra
)=repo
.changelog
.read(node
)
61 tz
="%+03d%02d" % (-timezone
/ 3600, ((-timezone
% 3600) / 60))
62 branch
=get_branch(extra
.get('branch','master'))
63 return (node
,manifest
,fixup_user(user
,authors
),(time
,tz
),files
,desc
,branch
,extra
)
68 def load_cache(filename
,get_key
=mangle_key
):
70 if not os
.path
.exists(filename
):
74 for line
in f
.readlines():
76 fields
=line
.split(' ')
77 if fields
==None or not len(fields
)==2 or fields
[0][0]!=':':
78 sys
.stderr
.write('Invalid file format in [%s], line %d\n' % (filename
,l
))
80 # put key:value in cache, key without ^:
81 cache
[get_key(fields
[0][1:])]=fields
[1].split('\n')[0]
85 def save_cache(filename
,cache
):
87 map(lambda x
: f
.write(':%s %s\n' % (str(x
),str(cache
.get(x
)))),cache
.keys())
90 def get_git_sha1(name
,type='heads'):
92 # use git-rev-parse to support packed refs
93 cmd
="GIT_DIR='%s' git rev-parse --verify refs/%s/%s 2>/dev/null" % (os
.getenv('GIT_DIR','/dev/null'),type,name
)
97 if l
== None or len(l
) == 0: