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 revsymbol
,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 # Starting with Mercurial 4.6 lookup no longer accepts raw hashes
85 # for lookups. Work around it by changing our behaviour depending on
88 node
=repo
.lookup(revision
)
89 except (TypeError, hgerror
.ProgrammingError
):
90 node
=binnode(revsymbol(repo
, b
"%d" % revision
)) # We were given a numeric rev
91 except hgerror
.RepoLookupError
:
92 node
=revision
# We got a raw hash
93 (manifest
,user
,(time
,timezone
),files
,desc
,extra
)=repo
.changelog
.read(node
)
95 user
=user
.decode(encoding
).encode('utf8')
96 desc
=desc
.decode(encoding
).encode('utf8')
97 tz
=b
"%+03d%02d" % (-timezone
// 3600, ((-timezone
% 3600) // 60))
98 branch
=get_branch(extra
.get(b
'branch', b
''))
99 return (node
,manifest
,fixup_user(user
,authors
),(time
,tz
),files
,desc
,branch
,extra
)
104 def load_cache(filename
,get_key
=mangle_key
):
106 if not os
.path
.exists(filename
):
108 f
=open(filename
,'rb')
110 for line
in f
.readlines():
112 fields
=line
.split(b
' ')
113 if fields
==None or not len(fields
)==2 or fields
[0][0:1]!=b
':':
114 sys
.stderr
.write('Invalid file format in [%s], line %d\n' % (filename
,l
))
116 # put key:value in cache, key without ^:
117 cache
[get_key(fields
[0][1:])]=fields
[1].split(b
'\n')[0]
121 def save_cache(filename
,cache
):
122 f
=open(filename
,'wb')
123 for key
, value
in cache
.items():
124 if not isinstance(key
, bytes
):
125 key
= str(key
).encode('utf8')
126 if not isinstance(value
, bytes
):
127 value
= str(value
).encode('utf8')
128 f
.write(b
':%s %s\n' % (key
, value
))
131 def get_git_sha1(name
,type='heads'):
133 # use git-rev-parse to support packed refs
134 ref
="refs/%s/%s" % (type,name
.decode('utf8'))
135 l
=subprocess
.check_output(["git", "rev-parse", "--verify",
136 "--quiet", ref
.encode('utf8')])
137 if l
== None or len(l
) == 0:
140 except subprocess
.CalledProcessError
: