5 """Class that handles various aspects of converting a hg commit to git.
8 def __init__(self
, warn
):
9 """Initializes a new GitHg object with the specified warner.
14 def format_timezone(self
, offset
):
16 raise ValueError("Unable to handle non-minute offset.")
17 sign
= (offset
< 0) and '-' or '+'
19 return '%c%02d%02d' % (sign
, offset
/ 3600, (offset
/ 60) % 60)
21 def get_committer(self
, ctx
):
24 if 'committer' in extra
:
26 (name_timestamp
, timezone
) = extra
['committer'].rsplit(' ', 1)
28 timezone
= self
.format_timezone(-int(timezone
))
29 return '%s %s' % (name_timestamp
, timezone
)
31 self
.warn("Ignoring committer in extra, invalid timezone in r%s: '%s'.\n" % (ctx
.rev(), timezone
))
35 def get_message(self
, ctx
):
38 message
= ctx
.description() + "\n"
39 if 'message' in extra
:
40 message
= apply_delta(message
, extra
['message'])
42 # HG EXTRA INFORMATION
45 if not ctx
.branch() == 'default':
47 extra_message
+= "branch : " + ctx
.branch() + "\n"
51 if f
not in ctx
.manifest():
53 rename
= ctx
.filectx(f
).renamed()
55 renames
.append((rename
[0], f
))
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'):
67 extra_message
+= "extra : " + key
+ " : " + urllib
.quote(value
) + "\n"
70 message
+= "\n--HG--\n" + extra_message
74 def get_author(self
, ctx
):
75 # hg authors might not have emails
78 # check for git author pattern compliance
79 regex
= re
.compile('^(.*?) ?\<(.*?)(|\>(.*))$')
80 a
= regex
.match(author
)
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
93 name
+= ' ext:(' + urllib
.quote(extra
) + ')'
94 author
= name
+ ' <' + email
+ '>'
96 if author
.find('<') >= 0:
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')
114 if ctx
.extra().get('hg-git', None) == 'octopus-done':
115 # implode octopus parents
117 while is_octopus_part(part
):
118 (p1
, p2
) = part
.parents()
119 assert not is_octopus_part(p1
)
124 parents
= ctx
.parents()