1 --- a/lib/redmine/scm/adapters/mercurial/redminehelper.py
2 +++ b/lib/redmine/scm/adapters/mercurial/redminehelper.py
3 @@ -45,17 +45,20 @@ Output example of rhmanifest::
7 -import re, time, cgi, urllib
8 +import re, time, html, urllib
9 from mercurial import cmdutil, commands, node, error, hg, registrar
12 command = registrar.command(cmdtable) if hasattr(registrar, 'command') else cmdutil.command(cmdtable)
15 -_u = lambda s: cgi.escape(urllib.quote(s))
16 +_x = lambda s: html.escape(s.decode('utf-8')).encode('utf-8')
17 +_u = lambda s: html.escape(urllib.parse.quote(s)).encode('utf-8')
19 +def unquoteplus(*args, **kwargs):
20 + return urllib.parse.unquote_to_bytes(*args, **kwargs).replace(b'+', b' ')
22 def _changectx(repo, rev):
23 - if isinstance(rev, str):
24 + if isinstance(rev, bytes):
25 rev = repo.lookup(rev)
26 if hasattr(repo, 'changectx'):
27 return repo.changectx(rev)
28 @@ -70,10 +73,10 @@ def _tip(ui, repo):
29 except TypeError: # Mercurial < 1.1
30 return repo.changelog.count() - 1
31 tipctx = _changectx(repo, tiprev())
32 - ui.write('<tip revision="%d" node="%s"/>\n'
33 + ui.write(b'<tip revision="%d" node="%s"/>\n'
34 % (tipctx.rev(), _x(node.hex(tipctx.node()))))
36 -_SPECIAL_TAGS = ('tip',)
37 +_SPECIAL_TAGS = (b'tip',)
40 # see mercurial/commands.py:tags
41 @@ -84,7 +87,7 @@ def _tags(ui, repo):
42 r = repo.changelog.rev(n)
43 except error.LookupError:
45 - ui.write('<tag revision="%d" node="%s" name="%s"/>\n'
46 + ui.write(b'<tag revision="%d" node="%s" name="%s"/>\n'
47 % (r, _x(node.hex(n)), _u(t)))
49 def _branches(ui, repo):
50 @@ -104,136 +107,148 @@ def _branches(ui, repo):
51 return repo.branchheads(branch)
54 - return repo.lookup(rev)
55 + return repo.lookup(str(rev).encode('utf-8'))
58 for t, n, r in sorted(iterbranches(), key=lambda e: e[2], reverse=True):
59 if lookup(r, n) in branchheads(t):
60 - ui.write('<branch revision="%d" node="%s" name="%s"/>\n'
61 + ui.write(b'<branch revision="%d" node="%s" name="%s"/>\n'
62 % (r, _x(node.hex(n)), _u(t)))
64 def _manifest(ui, repo, path, rev):
65 ctx = _changectx(repo, rev)
66 - ui.write('<manifest revision="%d" path="%s">\n'
67 + ui.write(b'<manifest revision="%d" path="%s">\n'
68 % (ctx.rev(), _u(path)))
71 - pathprefix = (path.rstrip('/') + '/').lstrip('/')
72 + pathprefix = (path.decode('utf-8').rstrip('/') + '/').lstrip('/')
73 for f, n in sorted(ctx.manifest().iteritems(), key=lambda e: e[0]):
74 - if not f.startswith(pathprefix):
75 + fstr = f.decode('utf-8')
76 + if not fstr.startswith(pathprefix):
78 - name = re.sub(r'/.*', '/', f[len(pathprefix):])
79 + name = re.sub(r'/.*', '/', fstr[len(pathprefix):])
84 if name.endswith('/'):
85 - ui.write('<dir name="%s"/>\n'
86 + ui.write(b'<dir name="%s"/>\n'
87 % _x(urllib.quote(name[:-1])))
89 fctx = repo.filectx(f, fileid=n)
90 tm, tzoffset = fctx.date()
91 - ui.write('<file name="%s" revision="%d" node="%s" '
92 - 'time="%d" size="%d"/>\n'
93 + ui.write(b'<file name="%s" revision="%d" node="%s" '
94 + b'time="%d" size="%d"/>\n'
95 % (_u(name), fctx.rev(), _x(node.hex(fctx.node())),
98 - ui.write('</manifest>\n')
99 + ui.write(b'</manifest>\n')
101 -@command('rhannotate',
102 - [('r', 'rev', '', 'revision'),
103 - ('u', 'user', None, 'list the author (long with -v)'),
104 - ('n', 'number', None, 'list the revision number (default)'),
105 - ('c', 'changeset', None, 'list the changeset'),
106 +@command(b'rhannotate',
107 + [(b'r', b'rev', b'', b'revision'),
108 + (b'u', b'user', None, b'list the author (long with -v)'),
109 + (b'n', b'number', None, b'list the revision number (default)'),
110 + (b'c', b'changeset', None, b'list the changeset'),
112 - 'hg rhannotate [-r REV] [-u] [-n] [-c] FILE...')
113 + b'hg rhannotate [-r REV] [-u] [-n] [-c] FILE...')
114 def rhannotate(ui, repo, *pats, **opts):
115 - rev = urllib.unquote_plus(opts.pop('rev', None))
116 + rev = unquoteplus(opts.pop('rev', b''))
118 - return commands.annotate(ui, repo, *map(urllib.unquote_plus, pats), **opts)
119 + return commands.annotate(ui, repo, *map(unquoteplus, pats), **opts)
122 - [('r', 'rev', '', 'revision')],
123 - 'hg rhcat ([-r REV] ...) FILE...')
125 + [(b'r', b'rev', b'', b'revision')],
126 + b'hg rhcat ([-r REV] ...) FILE...')
127 def rhcat(ui, repo, file1, *pats, **opts):
128 - rev = urllib.unquote_plus(opts.pop('rev', None))
129 + rev = unquoteplus(opts.pop('rev', b''))
131 - return commands.cat(ui, repo, urllib.unquote_plus(file1), *map(urllib.unquote_plus, pats), **opts)
132 + return commands.cat(ui, repo, unquoteplus(file1), *map(unquoteplus, pats), **opts)
135 - [('r', 'rev', [], 'revision'),
136 - ('c', 'change', '', 'change made by revision')],
137 - 'hg rhdiff ([-c REV] | [-r REV] ...) [FILE]...')
139 + [(b'r', b'rev', [], b'revision'),
140 + (b'c', b'change', b'', b'change made by revision')],
141 + b'hg rhdiff ([-c REV] | [-r REV] ...) [FILE]...')
142 def rhdiff(ui, repo, *pats, **opts):
143 """diff repository (or selected files)"""
144 change = opts.pop('change', None)
145 if change: # add -c option for Mercurial<1.1
146 base = _changectx(repo, change).parents()[0].rev()
147 - opts['rev'] = [str(base), change]
148 + opts['rev'] = [base, change]
149 opts['nodates'] = True
150 - return commands.diff(ui, repo, *map(urllib.unquote_plus, pats), **opts)
154 - ('r', 'rev', [], 'show the specified revision'),
155 - ('b', 'branch', [],
156 - 'show changesets within the given named branch'),
158 - 'limit number of changes displayed'),
160 - 'show revisions matching date spec'),
162 - 'revisions committed by user'),
167 - ('', 'rhbranch', '',
169 - ('', 'template', '',
170 - 'display with template')],
171 - 'hg rhlog [OPTION]... [FILE]')
172 + return commands.diff(ui, repo, *map(unquoteplus, pats), **opts)
176 + (b'r', b'rev', [], b'show the specified revision'),
177 + (b'b', b'branch', [],
178 + b'show changesets within the given named branch'),
179 + (b'l', b'limit', b'',
180 + b'limit number of changes displayed'),
181 + (b'd', b'date', b'',
182 + b'show revisions matching date spec'),
183 + (b'u', b'user', [],
184 + b'revisions committed by user'),
185 + (b'', b'from', b'',
189 + (b'', b'rhbranch', b'',
191 + (b'', b'template', b'',
192 + b'display with template')],
193 + b'hg rhlog [OPTION]... [FILE]')
195 def rhlog(ui, repo, *pats, **opts):
196 rev = opts.pop('rev')
197 bra0 = opts.pop('branch')
198 - from_rev = urllib.unquote_plus(opts.pop('from', None))
199 - to_rev = urllib.unquote_plus(opts.pop('to' , None))
200 - bra = urllib.unquote_plus(opts.pop('rhbranch', None))
201 - from_rev = from_rev.replace('"', '\\"')
202 - to_rev = to_rev.replace('"', '\\"')
203 - if hg.util.version() >= '1.6':
204 - opts['rev'] = ['"%s":"%s"' % (from_rev, to_rev)]
205 + from_rev = unquoteplus(opts.pop('from', b''))
206 + to_rev = unquoteplus(opts.pop('to' , b''))
207 + bra = unquoteplus(opts.pop('rhbranch', b''))
208 + from_rev = from_rev.replace(b'"', b'\\"')
209 + to_rev = to_rev.replace(b'"', b'\\"')
210 + if (from_rev != b'') or (to_rev != b''):
211 + if from_rev != b'':
212 + quotefrom = b'"%s"' % (from_rev)
214 + quotefrom = from_rev
216 + quoteto = b'"%s"' % (to_rev)
219 + opts['rev'] = [b'%s:%s' % (quotefrom, quoteto)]
221 - opts['rev'] = ['%s:%s' % (from_rev, to_rev)]
222 - opts['branch'] = [bra]
223 - return commands.log(ui, repo, *map(urllib.unquote_plus, pats), **opts)
225 -@command('rhmanifest',
226 - [('r', 'rev', '', 'show the specified revision')],
227 - 'hg rhmanifest [-r REV] [PATH]')
228 -def rhmanifest(ui, repo, path='', **opts):
231 + opts['branch'] = [bra]
232 + return commands.log(ui, repo, *map(unquoteplus, pats), **opts)
235 +@command(b'rhmanifest',
236 + [(b'r', b'rev', b'', b'show the specified revision')],
237 + b'hg rhmanifest -r REV [PATH]')
238 +def rhmanifest(ui, repo, path=b'', **opts):
239 """output the sub-manifest of the specified directory"""
240 - ui.write('<?xml version="1.0"?>\n')
241 - ui.write('<rhmanifest>\n')
242 - ui.write('<repository root="%s">\n' % _u(repo.root))
243 + ui.write(b'<?xml version="1.0"?>\n')
244 + ui.write(b'<rhmanifest>\n')
245 + ui.write(b'<repository root="%s">\n' % _u(repo.root))
247 - _manifest(ui, repo, urllib.unquote_plus(path), urllib.unquote_plus(opts.get('rev')))
248 + _manifest(ui, repo, unquoteplus(path), unquoteplus(opts.get('rev')))
250 - ui.write('</repository>\n')
251 - ui.write('</rhmanifest>\n')
252 + ui.write(b'</repository>\n')
253 + ui.write(b'</rhmanifest>\n')
255 -@command('rhsummary',[], 'hg rhsummary')
256 +@command(b'rhsummary',[], b'hg rhsummary')
257 def rhsummary(ui, repo, **opts):
258 """output the summary of the repository"""
259 - ui.write('<?xml version="1.0"?>\n')
260 - ui.write('<rhsummary>\n')
261 - ui.write('<repository root="%s">\n' % _u(repo.root))
262 + ui.write(b'<?xml version="1.0"?>\n')
263 + ui.write(b'<rhsummary>\n')
264 + ui.write(b'<repository root="%s">\n' % _u(repo.root))
269 # TODO: bookmarks in core (Mercurial>=1.8)
271 - ui.write('</repository>\n')
272 - ui.write('</rhsummary>\n')
273 + ui.write(b'</repository>\n')
274 + ui.write(b'</rhsummary>\n')