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 PY2
= sys
.version_info
.major
< 3
18 fsencode
= lambda s
: s
.encode(sys
.getfilesystemencoding())
20 from os
import fsencode
22 # default git branch name
26 # silly regex to see if user field has email address
27 user_re
=re
.compile(b
'([^<]+) (<[^>]*>)$')
28 # silly regex to clean out user names
29 user_clean_re
=re
.compile(b
'^["]([^"]+)["]$')
31 def set_default_branch(name
):
33 cfg_master
= name
.encode('utf8') if not isinstance(name
, bytes
) else name
35 def set_origin_name(name
):
41 myui
=ui
.ui(interactive
=False)
44 myui
.setconfig(b
'ui', b
'interactive', b
'off')
45 # Avoids a warning when the repository has obsolete markers
46 myui
.setconfig(b
'experimental', b
'evolution.createmarkers', True)
47 return myui
,hg
.repository(myui
, fsencode(url
)).unfiltered()
49 def fixup_user(user
,authors
):
50 user
=user
.strip(b
"\"")
52 # if we have an authors table, try to get mapping
53 # by defaulting to the current value of 'user'
54 user
=authors
.get(user
,user
)
55 name
,mail
,m
=b
'',b
'',user_re
.match(user
)
57 # if we don't have 'Name <mail>' syntax, extract name
58 # and mail from hg helpers. this seems to work pretty well.
59 # if email doesn't contain @, replace it with devnull@localhost
60 name
=templatefilters
.person(user
)
61 mail
=b
'<%s>' % templatefilters
.email(user
)
63 mail
= b
'<devnull@localhost>'
65 # if we have 'Name <mail>' syntax, everything is fine :)
66 name
,mail
=m
.group(1),m
.group(2)
68 # remove any silly quoting from username
69 m2
=user_clean_re
.match(name
)
72 return b
'%s %s' % (name
,mail
)
75 # 'HEAD' is the result of a bug in mutt's cvs->hg conversion,
76 # other CVS imports may need it, too
77 if name
==b
'HEAD' or name
==b
'default' or name
==b
'':
80 return origin_name
+ b
'/' + name
83 def get_changeset(ui
,repo
,revision
,authors
={},encoding
=''):
84 (manifest
,user
,(time
,timezone
),files
,desc
,extra
)=repo
.changelog
.read(revision
)
86 user
=user
.decode(encoding
).encode('utf8')
87 desc
=desc
.decode(encoding
).encode('utf8')
88 tz
=b
"%+03d%02d" % (-timezone
// 3600, ((-timezone
% 3600) // 60))
89 branch
=get_branch(extra
.get(b
'branch', b
''))
90 return (manifest
,fixup_user(user
,authors
),(time
,tz
),files
,desc
,branch
,extra
)
95 def load_cache(filename
,get_key
=mangle_key
):
97 if not os
.path
.exists(filename
):
101 for line
in f
.readlines():
103 fields
=line
.split(b
' ')
104 if fields
==None or not len(fields
)==2 or fields
[0][0:1]!=b
':':
105 sys
.stderr
.write('Invalid file format in [%s], line %d\n' % (filename
,l
))
107 # put key:value in cache, key without ^:
108 cache
[get_key(fields
[0][1:])]=fields
[1].split(b
'\n')[0]
112 def save_cache(filename
,cache
):
113 f
=open(filename
,'wb')
114 for key
, value
in cache
.items():
115 if not isinstance(key
, bytes
):
116 key
= str(key
).encode('utf8')
117 if not isinstance(value
, bytes
):
118 value
= str(value
).encode('utf8')
119 f
.write(b
':%s %s\n' % (key
, value
))
122 def get_git_sha1(name
,type='heads'):
124 # use git-rev-parse to support packed refs
125 ref
="refs/%s/%s" % (type,name
.decode('utf8'))
126 l
=subprocess
.check_output(["git", "rev-parse", "--verify",
127 "--quiet", ref
.encode('utf8')])
128 if l
== None or len(l
) == 0:
131 except subprocess
.CalledProcessError
: