Project

General

Profile

Defect #37659 » redminehelper.py.patch

Norbert Koch, 2022-09-11 12:42

View differences:

redminehelper.py 2022-09-11 12:30:18.596029000 +0200
45 45
      </repository>
46 46
    </rhmanifest>
47 47
"""
48
import re, time, cgi, urllib
48
import re, time, html, urllib
49 49
from mercurial import cmdutil, commands, node, error, hg, registrar
50 50

  
51 51
cmdtable = {}
52 52
command = registrar.command(cmdtable) if hasattr(registrar, 'command') else cmdutil.command(cmdtable)
53 53

  
54
_x = cgi.escape
55
_u = lambda s: cgi.escape(urllib.quote(s))
54
_x = lambda s: html.escape(s.decode('utf-8')).encode('utf-8')
55
_u = lambda s: html.escape(urllib.parse.quote(s)).encode('utf-8')
56 56

  
57
def unquoteplus(*args, **kwargs):
58
    return urllib.parse.unquote_to_bytes(*args, **kwargs).replace(b'+', b' ')
59

  
57 60
def _changectx(repo, rev):
58
    if isinstance(rev, str):
61
    if isinstance(rev, bytes):
59 62
       rev = repo.lookup(rev)
60 63
    if hasattr(repo, 'changectx'):
61 64
        return repo.changectx(rev)
......
70 73
        except TypeError:  # Mercurial < 1.1
71 74
            return repo.changelog.count() - 1
72 75
    tipctx = _changectx(repo, tiprev())
73
    ui.write('<tip revision="%d" node="%s"/>\n'
76
    ui.write(b'<tip revision="%d" node="%s"/>\n'
74 77
             % (tipctx.rev(), _x(node.hex(tipctx.node()))))
75 78

  
76
_SPECIAL_TAGS = ('tip',)
79
_SPECIAL_TAGS = (b'tip',)
77 80

  
78 81
def _tags(ui, repo):
79 82
    # see mercurial/commands.py:tags
......
84 87
            r = repo.changelog.rev(n)
85 88
        except error.LookupError:
86 89
            continue
87
        ui.write('<tag revision="%d" node="%s" name="%s"/>\n'
90
        ui.write(b'<tag revision="%d" node="%s" name="%s"/>\n'
88 91
                 % (r, _x(node.hex(n)), _u(t)))
89 92

  
90 93
def _branches(ui, repo):
......
104 107
            return repo.branchheads(branch)
105 108
    def lookup(rev, n):
106 109
        try:
107
            return repo.lookup(rev)
110
            return repo.lookup(str(rev).encode('utf-8'))
108 111
        except RuntimeError:
109 112
            return n
110 113
    for t, n, r in sorted(iterbranches(), key=lambda e: e[2], reverse=True):
111 114
        if lookup(r, n) in branchheads(t):
112
            ui.write('<branch revision="%d" node="%s" name="%s"/>\n'
115
            ui.write(b'<branch revision="%d" node="%s" name="%s"/>\n'
113 116
                     % (r, _x(node.hex(n)), _u(t)))
114 117

  
115 118
def _manifest(ui, repo, path, rev):
116 119
    ctx = _changectx(repo, rev)
117
    ui.write('<manifest revision="%d" path="%s">\n'
120
    ui.write(b'<manifest revision="%d" path="%s">\n'
118 121
             % (ctx.rev(), _u(path)))
119 122

  
120 123
    known = set()
121
    pathprefix = (path.rstrip('/') + '/').lstrip('/')
124
    pathprefix = (path.decode('utf-8').rstrip('/') + '/').lstrip('/')
122 125
    for f, n in sorted(ctx.manifest().iteritems(), key=lambda e: e[0]):
123
        if not f.startswith(pathprefix):
126
        fstr = f.decode('utf-8')
127
        if not fstr.startswith(pathprefix):
124 128
            continue
125
        name = re.sub(r'/.*', '/', f[len(pathprefix):])
129
        name = re.sub(r'/.*', '/', fstr[len(pathprefix):])
126 130
        if name in known:
127 131
            continue
128 132
        known.add(name)
129 133

  
130 134
        if name.endswith('/'):
131
            ui.write('<dir name="%s"/>\n'
132
                     % _x(urllib.quote(name[:-1])))
135
            ui.write(b'<dir name="%s"/>\n'
136
                     % _x(urllib.parse.quote(name[:-1]).encode('utf-8')))
133 137
        else:
134 138
            fctx = repo.filectx(f, fileid=n)
135 139
            tm, tzoffset = fctx.date()
136
            ui.write('<file name="%s" revision="%d" node="%s" '
137
                     'time="%d" size="%d"/>\n'
140
            ui.write(b'<file name="%s" revision="%d" node="%s" '
141
                     b'time="%d" size="%d"/>\n'
138 142
                     % (_u(name), fctx.rev(), _x(node.hex(fctx.node())),
139 143
                        tm, fctx.size(), ))
140 144

  
141
    ui.write('</manifest>\n')
145
    ui.write(b'</manifest>\n')
142 146

  
143
@command('rhannotate',
144
         [('r', 'rev', '', 'revision'),
145
          ('u', 'user', None, 'list the author (long with -v)'),
146
          ('n', 'number', None, 'list the revision number (default)'),
147
          ('c', 'changeset', None, 'list the changeset'),
147
@command(b'rhannotate',
148
         [(b'r', b'rev', b'', b'revision'),
149
          (b'u', b'user', None, b'list the author (long with -v)'),
150
          (b'n', b'number', None, b'list the revision number (default)'),
151
          (b'c', b'changeset', None, b'list the changeset'),
148 152
         ],
149
         'hg rhannotate [-r REV] [-u] [-n] [-c] FILE...')
153
         b'hg rhannotate [-r REV] [-u] [-n] [-c] FILE...')
150 154
def rhannotate(ui, repo, *pats, **opts):
151
    rev = urllib.unquote_plus(opts.pop('rev', None))
155
    rev = unquoteplus(opts.pop('rev', b''))
152 156
    opts['rev'] = rev
153
    return commands.annotate(ui, repo, *map(urllib.unquote_plus, pats), **opts)
157
    return commands.annotate(ui, repo, *map(unquoteplus, pats), **opts)
154 158

  
155
@command('rhcat',
156
               [('r', 'rev', '', 'revision')],
157
               'hg rhcat ([-r REV] ...) FILE...')
159
@command(b'rhcat',
160
               [(b'r', b'rev', b'', b'revision')],
161
               b'hg rhcat ([-r REV] ...) FILE...')
158 162
def rhcat(ui, repo, file1, *pats, **opts):
159
    rev = urllib.unquote_plus(opts.pop('rev', None))
163
    rev = unquoteplus(opts.pop(b'rev', b''))
160 164
    opts['rev'] = rev
161
    return commands.cat(ui, repo, urllib.unquote_plus(file1), *map(urllib.unquote_plus, pats), **opts)
165
    return commands.cat(ui, repo, unquoteplus(file1), *map(unquoteplus, pats), **opts)
162 166

  
163
@command('rhdiff',
164
               [('r', 'rev', [], 'revision'),
165
                ('c', 'change', '', 'change made by revision')],
166
               'hg rhdiff ([-c REV] | [-r REV] ...) [FILE]...')
167
@command(b'rhdiff',
168
               [(b'r', b'rev', [], b'revision'),
169
                (b'c', b'change', b'', b'change made by revision')],
170
               b'hg rhdiff ([-c REV] | [-r REV] ...) [FILE]...')
167 171
def rhdiff(ui, repo, *pats, **opts):
168 172
    """diff repository (or selected files)"""
169 173
    change = opts.pop('change', None)
170 174
    if change:  # add -c option for Mercurial<1.1
171 175
        base = _changectx(repo, change).parents()[0].rev()
172
        opts['rev'] = [str(base), change]
176
        opts['rev'] = [base, change]
173 177
    opts['nodates'] = True
174
    return commands.diff(ui, repo, *map(urllib.unquote_plus, pats), **opts)
178
    return commands.diff(ui, repo, *map(unquoteplus, pats), **opts)
175 179

  
176
@command('rhlog',
180
@command(b'rhlog',
177 181
                   [
178
                    ('r', 'rev', [], 'show the specified revision'),
179
                    ('b', 'branch', [],
180
                       'show changesets within the given named branch'),
181
                    ('l', 'limit', '',
182
                         'limit number of changes displayed'),
183
                    ('d', 'date', '',
184
                         'show revisions matching date spec'),
185
                    ('u', 'user', [],
186
                      'revisions committed by user'),
187
                    ('', 'from', '',
188
                      ''),
189
                    ('', 'to', '',
190
                      ''),
191
                    ('', 'rhbranch', '',
192
                      ''),
193
                    ('', 'template', '',
194
                       'display with template')],
195
                   'hg rhlog [OPTION]... [FILE]')
182
                    (b'r', b'rev', [], b'show the specified revision'),
183
                    (b'b', b'branch', [],
184
                       b'show changesets within the given named branch'),
185
                    (b'l', b'limit', b'',
186
                         b'limit number of changes displayed'),
187
                    (b'd', b'date', b'',
188
                         b'show revisions matching date spec'),
189
                    (b'u', b'user', [],
190
                      b'revisions committed by user'),
191
                    (b'', b'from', b'',
192
                      b''),
193
                    (b'', b'to', b'',
194
                      b''),
195
                    (b'', b'rhbranch', b'',
196
                      b''),
197
                    (b'', b'template', b'',
198
                       b'display with template')],
199
                   b'hg rhlog [OPTION]... [FILE]')
196 200
def rhlog(ui, repo, *pats, **opts):
197 201
    rev      = opts.pop('rev')
198 202
    bra0     = opts.pop('branch')
199
    from_rev = urllib.unquote_plus(opts.pop('from', None))
200
    to_rev   = urllib.unquote_plus(opts.pop('to'  , None))
201
    bra      = urllib.unquote_plus(opts.pop('rhbranch', None))
202
    from_rev = from_rev.replace('"', '\\"')
203
    to_rev   = to_rev.replace('"', '\\"')
204
    if hg.util.version() >= '1.6':
205
      opts['rev'] = ['"%s":"%s"' % (from_rev, to_rev)]
203
    from_rev = unquoteplus(opts.pop('from', b''))
204
    to_rev   = unquoteplus(opts.pop('to'  , b''))
205
    bra      = unquoteplus(opts.pop('rhbranch', b''))
206
    from_rev = from_rev.replace(b'"', b'\\"')
207
    to_rev   = to_rev.replaceb('"', b'\\"')
208
    if (from_rev != b'') or (to_rev != b''):
209
        if from_rev != b'':
210
            quotefrom = b'"%s"' % (from_rev)
211
        else:
212
            quotefrom = from_rev
213
        if to_rev != b'':
214
            quoteto = b'"%s"' % (to_rev)
215
        else:
216
            quoteto = to_rev
217
        opts['rev'] = [b'%s:%s' % (quotefrom, quoteto)]
206 218
    else:
207
      opts['rev'] = ['%s:%s' % (from_rev, to_rev)]
208
    opts['branch'] = [bra]
209
    return commands.log(ui, repo, *map(urllib.unquote_plus, pats), **opts)
219
        opts['rev'] = rev
220
    if (bra != b''):
221
        opts['branch'] = [bra]
222
    return commands.log(ui, repo, *map(unquoteplus, pats), **opts)
210 223

  
211
@command('rhmanifest',
212
                   [('r', 'rev', '', 'show the specified revision')],
213
                   'hg rhmanifest [-r REV] [PATH]')
214
def rhmanifest(ui, repo, path='', **opts):
224
@command(b'rhmanifest',
225
                   [(b'r', b'rev', b'', b'show the specified revision')],
226
                   b'hg rhmanifest [-r REV] [PATH]')
227
def rhmanifest(ui, repo, path=b'', **opts):
215 228
    """output the sub-manifest of the specified directory"""
216
    ui.write('<?xml version="1.0"?>\n')
217
    ui.write('<rhmanifest>\n')
218
    ui.write('<repository root="%s">\n' % _u(repo.root))
229
    ui.write(b'<?xml version="1.0"?>\n')
230
    ui.write(b'<rhmanifest>\n')
231
    ui.write(b'<repository root="%s">\n' % _u(repo.root))
219 232
    try:
220
        _manifest(ui, repo, urllib.unquote_plus(path), urllib.unquote_plus(opts.get('rev')))
233
        _manifest(ui, repo, unquoteplus(path), unquoteplus(opts.get('rev')))
221 234
    finally:
222
        ui.write('</repository>\n')
223
        ui.write('</rhmanifest>\n')
235
        ui.write(b'</repository>\n')
236
        ui.write(b'</rhmanifest>\n')
224 237

  
225
@command('rhsummary',[], 'hg rhsummary')
238
@command(b'rhsummary',[], b'hg rhsummary')
226 239
def rhsummary(ui, repo, **opts):
227 240
    """output the summary of the repository"""
228
    ui.write('<?xml version="1.0"?>\n')
229
    ui.write('<rhsummary>\n')
230
    ui.write('<repository root="%s">\n' % _u(repo.root))
241
    ui.write(b'<?xml version="1.0"?>\n')
242
    ui.write(b'<rhsummary>\n')
243
    ui.write(b'<repository root="%s">\n' % _u(repo.root))
231 244
    try:
232 245
        _tip(ui, repo)
233 246
        _tags(ui, repo)
234 247
        _branches(ui, repo)
235 248
        # TODO: bookmarks in core (Mercurial>=1.8)
236 249
    finally:
237
        ui.write('</repository>\n')
238
        ui.write('</rhsummary>\n')
250
        ui.write(b'</repository>\n')
251
        ui.write(b'</rhsummary>\n')
239 252

  
(3-3/4)