remote-hg: another case of Postel's law
[git/dscho.git] / git_remote_helpers / hg / hg.py
blobcdd13fa51317372e9ff6d9f8fc1ee3b5bec41689
1 import urllib
2 import re
4 class GitHg(object):
5 """Class that handles various aspects of converting a hg commit to git.
6 """
8 def __init__(self, warn):
9 """Initializes a new GitHg object with the specified warner.
10 """
12 self.warn = warn
14 def format_timezone(self, offset):
15 if offset % 60 != 0:
16 raise ValueError("Unable to handle non-minute offset.")
17 sign = (offset < 0) and '-' or '+'
18 offset = abs(offset)
19 return '%c%02d%02d' % (sign, offset / 3600, (offset / 60) % 60)
21 def get_committer(self, ctx):
22 extra = ctx.extra()
24 if 'committer' in extra:
25 # fixup timezone
26 (name_timestamp, timezone) = extra['committer'].rsplit(' ', 1)
27 try:
28 timezone = self.format_timezone(-int(timezone))
29 return '%s %s' % (name_timestamp, timezone)
30 except ValueError:
31 self.warn("Ignoring committer in extra, invalid timezone in r%s: '%s'.\n" % (ctx.rev(), timezone))
33 return None
35 def get_message(self, ctx):
36 extra = ctx.extra()
38 message = ctx.description() + "\n"
39 if 'message' in extra:
40 message = apply_delta(message, extra['message'])
42 # HG EXTRA INFORMATION
43 add_extras = False
44 extra_message = ''
45 if not ctx.branch() == 'default':
46 add_extras = True
47 extra_message += "branch : " + ctx.branch() + "\n"
49 renames = []
50 for f in ctx.files():
51 if f not in ctx.manifest():
52 continue
53 rename = ctx.filectx(f).renamed()
54 if rename:
55 renames.append((rename[0], f))
57 if renames:
58 add_extras = True
59 for oldfile, newfile in renames:
60 extra_message += "rename : " + oldfile + " => " + newfile + "\n"
62 for key, value in extra.iteritems():
63 if key in ('author', 'committer', 'encoding', 'message', 'branch', 'hg-git'):
64 continue
65 else:
66 add_extras = True
67 extra_message += "extra : " + key + " : " + urllib.quote(value) + "\n"
69 if add_extras:
70 message += "\n--HG--\n" + extra_message
72 return message
74 def get_author(self, ctx):
75 # hg authors might not have emails
76 author = ctx.user()
78 # check for git author pattern compliance
79 regex = re.compile('^(.*?) ?\<(.*?)(|\>(.*))$')
80 a = regex.match(author)
82 if a:
83 name = a.group(1)
84 email = a.group(2)
85 extra = a.group(4)
86 if not extra is None and len(extra) > 0:
87 name += ' ext:(' + urllib.quote(extra) + ')'
88 author = name + ' <' + email + '>'
89 else:
90 if author.find('<') >= 0:
91 author = author + '>'
92 else:
93 author = author + ' <none@none>'
95 if 'author' in ctx.extra():
96 author = apply_delta(author, ctx.extra()['author'])
98 (time, timezone) = ctx.date()
99 date = str(int(time)) + ' ' + self.format_timezone(-timezone)
101 return author + ' ' + date
103 def get_parents(self, ctx):
104 def is_octopus_part(ctx):
105 return ctx.extra().get('hg-git', None) in ('octopus', 'octopus-done')
107 parents = []
108 if ctx.extra().get('hg-git', None) == 'octopus-done':
109 # implode octopus parents
110 part = ctx
111 while is_octopus_part(part):
112 (p1, p2) = part.parents()
113 assert not is_octopus_part(p1)
114 parents.append(p1)
115 part = p2
116 parents.append(p2)
117 else:
118 parents = ctx.parents()
120 return parents