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
7 from mercurial
import error
as hgerror
8 from mercurial
.scmutil
import binnode
15 # default git branch name
19 # silly regex to see if user field has email address
20 user_re
=re
.compile(b
'([^<]+) (<[^>]*>)$')
21 # silly regex to clean out user names
22 user_clean_re
=re
.compile(b
'^["]([^"]+)["]$')
24 def set_default_branch(name
):
26 cfg_master
= name
.encode('utf8') if not isinstance(name
, bytes
) else name
28 def set_origin_name(name
):
34 myui
=ui
.ui(interactive
=False)
37 myui
.setconfig(b
'ui', b
'interactive', b
'off')
38 # Avoids a warning when the repository has obsolete markers
39 myui
.setconfig(b
'experimental', b
'evolution.createmarkers', True)
40 return myui
,hg
.repository(myui
, os
.fsencode(url
)).unfiltered()
42 def fixup_user(user
,authors
):
43 user
=user
.strip(b
"\"")
45 # if we have an authors table, try to get mapping
46 # by defaulting to the current value of 'user'
47 user
=authors
.get(user
,user
)
48 name
,mail
,m
=b
'',b
'',user_re
.match(user
)
50 # if we don't have 'Name <mail>' syntax, extract name
51 # and mail from hg helpers. this seems to work pretty well.
52 # if email doesn't contain @, replace it with devnull@localhost
53 name
=templatefilters
.person(user
)
54 mail
=b
'<%s>' % templatefilters
.email(user
)
56 mail
= b
'<devnull@localhost>'
58 # if we have 'Name <mail>' syntax, everything is fine :)
59 name
,mail
=m
.group(1),m
.group(2)
61 # remove any silly quoting from username
62 m2
=user_clean_re
.match(name
)
65 return b
'%s %s' % (name
,mail
)
68 # 'HEAD' is the result of a bug in mutt's cvs->hg conversion,
69 # other CVS imports may need it, too
70 if name
==b
'HEAD' or name
==b
'default' or name
==b
'':
73 return origin_name
+ b
'/' + name
76 def get_changeset(ui
,repo
,revision
,authors
={},encoding
=''):
77 (manifest
,user
,(time
,timezone
),files
,desc
,extra
)=repo
.changelog
.read(revision
)
79 user
=user
.decode(encoding
).encode('utf8')
80 desc
=desc
.decode(encoding
).encode('utf8')
81 tz
=b
"%+03d%02d" % (-timezone
// 3600, ((-timezone
% 3600) // 60))
82 branch
=get_branch(extra
.get(b
'branch', b
''))
83 return (manifest
,fixup_user(user
,authors
),(time
,tz
),files
,desc
,branch
,extra
)
88 def load_cache(filename
,get_key
=mangle_key
):
90 if not os
.path
.exists(filename
):
94 for line
in f
.readlines():
96 fields
=line
.split(b
' ')
97 if fields
==None or not len(fields
)==2 or fields
[0][0:1]!=b
':':
98 sys
.stderr
.write('Invalid file format in [%s], line %d\n' % (filename
,l
))
100 # put key:value in cache, key without ^:
101 cache
[get_key(fields
[0][1:])]=fields
[1].split(b
'\n')[0]
105 def save_cache(filename
,cache
):
106 f
=open(filename
,'wb')
107 for key
, value
in cache
.items():
108 if not isinstance(key
, bytes
):
109 key
= str(key
).encode('utf8')
110 if not isinstance(value
, bytes
):
111 value
= str(value
).encode('utf8')
112 f
.write(b
':%s %s\n' % (key
, value
))
115 def get_git_sha1(name
,type='heads'):
117 # use git-rev-parse to support packed refs
118 ref
="refs/%s/%s" % (type,name
.decode('utf8'))
119 l
=subprocess
.check_output(["git", "rev-parse", "--verify",
120 "--quiet", ref
.encode('utf8')])
121 if l
== None or len(l
) == 0:
124 except subprocess
.CalledProcessError
: