Skip to content

Commit 9490bd0

Browse files
felipecpeff
authored andcommitted
remote-hg: add compat for hg-git author fixes
Signed-off-by: Felipe Contreras <[email protected]> Signed-off-by: Jeff King <[email protected]>
1 parent 422ab5b commit 9490bd0

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

contrib/remote-helpers/git-remote-hg

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import os
1717
import json
1818
import shutil
1919
import subprocess
20+
import urllib
2021

2122
#
2223
# If you want to switch to hg-git compatibility mode:
@@ -35,6 +36,7 @@ import subprocess
3536

3637
NAME_RE = re.compile('^([^<>]+)')
3738
AUTHOR_RE = re.compile('^([^<>]+?)? ?<([^<>]+)>$')
39+
AUTHOR_HG_RE = re.compile('^(.*?) ?<(.+?)(?:>(.+)?)?$')
3840
RAW_AUTHOR_RE = re.compile('^(\w+) (?:(.+)? )?<(.+)> (\d+) ([+-]\d+)')
3941

4042
def die(msg, *args):
@@ -152,19 +154,30 @@ class Parser:
152154
return sys.stdin.read(size)
153155

154156
def get_author(self):
157+
global bad_mail
158+
159+
ex = None
155160
m = RAW_AUTHOR_RE.match(self.line)
156161
if not m:
157162
return None
158163
_, name, email, date, tz = m.groups()
164+
if name and 'ext:' in name:
165+
m = re.match('^(.+?) ext:\((.+)\)$', name)
166+
if m:
167+
name = m.group(1)
168+
ex = urllib.unquote(m.group(2))
159169

160-
if email != 'unknown':
170+
if email != bad_mail:
161171
if name:
162172
user = '%s <%s>' % (name, email)
163173
else:
164174
user = '<%s>' % (email)
165175
else:
166176
user = name
167177

178+
if ex:
179+
user += ex
180+
168181
tz = int(tz)
169182
tz = ((tz / 100) * 3600) + ((tz % 100) * 60)
170183
return (user, int(date), -tz)
@@ -194,9 +207,9 @@ def get_filechanges(repo, ctx, parent):
194207

195208
return added | modified, removed
196209

197-
def fixup_user(user):
198-
user = user.replace('"', '')
210+
def fixup_user_git(user):
199211
name = mail = None
212+
user = user.replace('"', '')
200213
m = AUTHOR_RE.match(user)
201214
if m:
202215
name = m.group(1)
@@ -205,11 +218,41 @@ def fixup_user(user):
205218
m = NAME_RE.match(user)
206219
if m:
207220
name = m.group(1).strip()
221+
return (name, mail)
222+
223+
def fixup_user_hg(user):
224+
def sanitize(name):
225+
# stole this from hg-git
226+
return re.sub('[<>\n]', '?', name.lstrip('< ').rstrip('> '))
227+
228+
m = AUTHOR_HG_RE.match(user)
229+
if m:
230+
name = sanitize(m.group(1))
231+
mail = sanitize(m.group(2))
232+
ex = m.group(3)
233+
if ex:
234+
name += ' ext:(' + urllib.quote(ex) + ')'
235+
else:
236+
name = sanitize(user)
237+
if '@' in user:
238+
mail = name
239+
else:
240+
mail = None
241+
242+
return (name, mail)
243+
244+
def fixup_user(user):
245+
global mode, bad_mail
246+
247+
if mode == 'git':
248+
name, mail = fixup_user_git(user)
249+
else:
250+
name, mail = fixup_user_hg(user)
208251

209252
if not name:
210-
name = 'Unknown'
253+
name = bad_name
211254
if not mail:
212-
mail = 'unknown'
255+
mail = bad_mail
213256

214257
return '%s <%s>' % (name, mail)
215258

@@ -660,7 +703,7 @@ def do_export(parser):
660703
def main(args):
661704
global prefix, dirname, branches, bmarks
662705
global marks, blob_marks, parsed_refs
663-
global peer, mode
706+
global peer, mode, bad_mail, bad_name
664707

665708
alias = args[1]
666709
url = args[2]
@@ -676,8 +719,12 @@ def main(args):
676719

677720
if hg_git_compat:
678721
mode = 'hg'
722+
bad_mail = 'none@none'
723+
bad_name = ''
679724
else:
680725
mode = 'git'
726+
bad_mail = 'unknown'
727+
bad_name = 'Unknown'
681728

682729
if alias[4:] == url:
683730
is_tmp = True

0 commit comments

Comments
 (0)