add git-remote-hg
[git/srabbelier.git] / git_remote_helpers / hg / hg.py
blobf7acea7a8cfd9d3f9d047ffd8ad8a5c1c49e53cc
1 from git_remote_helpers.hg.metadata_util import (get_git_author,
2 get_git_parents, get_git_message)
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 return get_git_message(ctx)
38 def get_author(self, ctx):
39 author = get_git_author(ctx)
41 (time, timezone) = ctx.date()
42 date = str(int(time)) + ' ' + self.format_timezone(-timezone)
44 return author + ' ' + date
46 def get_parents(self, ctx):
47 return get_git_parents(ctx)