git-remote-hg: improve sanitation of local repo urls
[git] / git_remote_helpers / hg / metadata_util.py
1 # Note: Changes to this file should also be made in the hg-git
2 # repository in the hggit directory.
3
4 import urllib
5 import re
6
7 def get_git_author(ctx):
8     # hg authors might not have emails
9     author = ctx.user()
10
11     # check for git author pattern compliance
12     regex = re.compile('^(.*?) \<(.*?)\>(.*)$')
13     a = regex.match(author)
14
15     if a:
16         name = a.group(1)
17         email = a.group(2)
18         if len(a.group(3)) > 0:
19             name += ' ext:(' + urllib.quote(a.group(3)) + ')'
20         author = name + ' <' + email + '>'
21     else:
22         author = author + ' <none@none>'
23
24     if 'author' in ctx.extra():
25         author = apply_delta(author, ctx.extra()['author'])
26
27     return author
28
29 def get_git_parents(ctx):
30     def is_octopus_part(ctx):
31         return ctx.extra().get('hg-git', None) in ('octopus', 'octopus-done')
32
33     parents = []
34     if ctx.extra().get('hg-git', None) == 'octopus-done':
35         # implode octopus parents
36         part = ctx
37         while is_octopus_part(part):
38             (p1, p2) = part.parents()
39             assert not is_octopus_part(p1)
40             parents.append(p1)
41             part = p2
42         parents.append(p2)
43     else:
44         parents = ctx.parents()
45
46     return parents
47
48 def get_git_message(ctx):
49     extra = ctx.extra()
50
51     message = ctx.description() + "\n"
52     if 'message' in extra:
53         message = apply_delta(message, extra['message'])
54
55     # HG EXTRA INFORMATION
56     add_extras = False
57     extra_message = ''
58     if not ctx.branch() == 'default':
59         add_extras = True
60         extra_message += "branch : " + ctx.branch() + "\n"
61
62     renames = []
63     for f in ctx.files():
64         if f not in ctx.manifest():
65             continue
66         rename = ctx.filectx(f).renamed()
67         if rename:
68             renames.append((rename[0], f))
69
70     if renames:
71         add_extras = True
72         for oldfile, newfile in renames:
73             extra_message += "rename : " + oldfile + " => " + newfile + "\n"
74
75     for key, value in extra.iteritems():
76         if key in ('author', 'committer', 'encoding', 'message', 'branch', 'hg-git'):
77             continue
78         else:
79             add_extras = True
80             extra_message += "extra : " + key + " : " +  urllib.quote(value) + "\n"
81
82     if add_extras:
83         message += "\n--HG--\n" + extra_message
84
85     return message