git-remote-hg: add GitHg, a helper class for converting hg commits to git
[git/dscho.git] / git_remote_helpers / hg / hg.py
blob758ba242d8dd7b8599a9d71985537a4a9bc36f18
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 if len(a.group(3)) > 0:
86 name += ' ext:(' + urllib.quote(a.group(3)) + ')'
87 author = name + ' <' + email + '>'
88 else:
89 author = author + ' <none@none>'
91 if 'author' in ctx.extra():
92 author = apply_delta(author, ctx.extra()['author'])
94 (time, timezone) = ctx.date()
95 date = str(int(time)) + ' ' + self.format_timezone(-timezone)
97 return author + ' ' + date
99 def get_parents(self, ctx):
100 def is_octopus_part(ctx):
101 return ctx.extra().get('hg-git', None) in ('octopus', 'octopus-done')
103 parents = []
104 if ctx.extra().get('hg-git', None) == 'octopus-done':
105 # implode octopus parents
106 part = ctx
107 while is_octopus_part(part):
108 (p1, p2) = part.parents()
109 assert not is_octopus_part(p1)
110 parents.append(p1)
111 part = p2
112 parents.append(p2)
113 else:
114 parents = ctx.parents()
116 return parents