3 # Copyright (c) 2007, 2008 Rocco Rutte <pdmef@gmx.net> and others.
4 # License: MIT <http://www.opensource.org/licenses/mit-license.php>
6 from mercurial
import hg
,util
,ui
,templatefilters
12 # default git branch name
16 # silly regex to see if user field has email address
17 user_re
=re
.compile('([^<]+) (<[^>]*>)$')
18 # silly regex to clean out user names
19 user_clean_re
=re
.compile('^["]([^"]+)["]$')
21 def set_default_branch(name
):
25 def set_origin_name(name
):
31 myui
=ui
.ui(interactive
=False)
34 myui
.setconfig('ui', 'interactive', 'off')
35 return myui
,hg
.repository(myui
,url
)
37 def fixup_user(user
,authors
):
40 # if we have an authors table, try to get mapping
41 # by defaulting to the current value of 'user'
42 user
=authors
.get(user
,user
)
43 name
,mail
,m
='','',user_re
.match(user
)
45 # if we don't have 'Name <mail>' syntax, extract name
46 # and mail from hg helpers. this seems to work pretty well.
47 # if email doesn't contain @, replace it with devnull@localhost
48 name
=templatefilters
.person(user
)
49 mail
='<%s>' % util
.email(user
)
51 mail
= '<devnull@localhost>'
53 # if we have 'Name <mail>' syntax, everything is fine :)
54 name
,mail
=m
.group(1),m
.group(2)
56 # remove any silly quoting from username
57 m2
=user_clean_re
.match(name
)
60 return '%s %s' % (name
,mail
)
63 # 'HEAD' is the result of a bug in mutt's cvs->hg conversion,
64 # other CVS imports may need it, too
65 if name
=='HEAD' or name
=='default' or name
=='':
68 return origin_name
+ '/' + name
71 def get_changeset(ui
,repo
,revision
,authors
={},encoding
=''):
72 node
=repo
.lookup(revision
)
73 (manifest
,user
,(time
,timezone
),files
,desc
,extra
)=repo
.changelog
.read(node
)
75 user
=user
.decode(encoding
).encode('utf8')
76 desc
=desc
.decode(encoding
).encode('utf8')
77 tz
="%+03d%02d" % (-timezone
/ 3600, ((-timezone
% 3600) / 60))
78 branch
=get_branch(extra
.get('branch','master'))
79 return (node
,manifest
,fixup_user(user
,authors
),(time
,tz
),files
,desc
,branch
,extra
)
84 def load_cache(filename
,get_key
=mangle_key
):
86 if not os
.path
.exists(filename
):
90 for line
in f
.readlines():
92 fields
=line
.split(' ')
93 if fields
==None or not len(fields
)==2 or fields
[0][0]!=':':
94 sys
.stderr
.write('Invalid file format in [%s], line %d\n' % (filename
,l
))
96 # put key:value in cache, key without ^:
97 cache
[get_key(fields
[0][1:])]=fields
[1].split('\n')[0]
101 def save_cache(filename
,cache
):
102 f
=open(filename
,'w+')
103 map(lambda x
: f
.write(':%s %s\n' % (str(x
),str(cache
.get(x
)))),cache
.keys())
106 def get_git_sha1(name
,type='heads'):
108 # use git-rev-parse to support packed refs
109 ref
="refs/%s/%s" % (type,name
)
110 l
=subprocess
.check_output(["git", "rev-parse", "--verify", "--quiet", ref
])
111 if l
== None or len(l
) == 0:
114 except subprocess
.CalledProcessError
: