French translation: copy -> copie.
[git/dscho.git] / git_remote_helpers / hg / hg.py
blobd835ed47e4ecfdcd4028b17167a10081f7d870df
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 if email.endswith(' <at'):
88 extra = extra.replace(' ', '')
89 extra = extra.replace(' <dot> ', '.')
90 extra = extra.replace('>', '')
91 email = email[:-4] + '@' + extra
92 else:
93 name += ' ext:(' + urllib.quote(extra) + ')'
94 author = name + ' <' + email + '>'
95 else:
96 if author.find('<') >= 0:
97 author = author + '>'
98 else:
99 author = author + ' <none@none>'
101 if 'author' in ctx.extra():
102 author = apply_delta(author, ctx.extra()['author'])
104 (time, timezone) = ctx.date()
105 date = str(int(time)) + ' ' + self.format_timezone(-timezone)
107 return author + ' ' + date
109 def get_parents(self, ctx):
110 def is_octopus_part(ctx):
111 return ctx.extra().get('hg-git', None) in ('octopus', 'octopus-done')
113 parents = []
114 if ctx.extra().get('hg-git', None) == 'octopus-done':
115 # implode octopus parents
116 part = ctx
117 while is_octopus_part(part):
118 (p1, p2) = part.parents()
119 assert not is_octopus_part(p1)
120 parents.append(p1)
121 part = p2
122 parents.append(p2)
123 else:
124 parents = ctx.parents()
126 return parents