git-remote-hg: improve sanitation of local repo urls
[git] / git_remote_helpers / hg / hg.py
1 from git_remote_helpers.hg.metadata_util import (get_git_author,
2         get_git_parents, get_git_message)
3
4 class GitHg(object):
5     """Class that handles various aspects of converting a hg commit to git.
6     """
7
8     def __init__(self, warn):
9         """Initializes a new GitHg object with the specified warner.
10         """
11
12         self.warn = warn
13
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)
20
21     def get_committer(self, ctx):
22         extra = ctx.extra()
23
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))
32
33         return None
34
35     def get_message(self, ctx):
36         return get_git_message(ctx)
37
38     def get_author(self, ctx):
39         author = get_git_author(ctx)
40
41         (time, timezone) = ctx.date()
42         date = str(int(time)) + ' ' + self.format_timezone(-timezone)
43
44         return author + ' ' + date
45
46     def get_parents(self, ctx):
47         return get_git_parents(ctx)