annotate lib/redmine/scm/adapters/mercurial/redminehelper.py @ 1628:9c5f8e24dadc live tip

Quieten this cron script
author Chris Cannam
date Tue, 25 Aug 2020 11:38:49 +0100
parents dffacf8a6908
children
rev   line source
Chris@245 1 # redminehelper: Redmine helper extension for Mercurial
Chris@245 2 #
Chris@245 3 # Copyright 2010 Alessio Franceschelli (alefranz.net)
Chris@245 4 # Copyright 2010-2011 Yuya Nishihara <yuya@tcha.org>
Chris@245 5 #
Chris@245 6 # This software may be used and distributed according to the terms of the
Chris@245 7 # GNU General Public License version 2 or any later version.
Chris@245 8 """helper commands for Redmine to reduce the number of hg calls
Chris@245 9
Chris@245 10 To test this extension, please try::
Chris@245 11
Chris@245 12 $ hg --config extensions.redminehelper=redminehelper.py rhsummary
Chris@245 13
Chris@245 14 I/O encoding:
Chris@245 15
Chris@245 16 :file path: urlencoded, raw string
Chris@245 17 :tag name: utf-8
Chris@245 18 :branch name: utf-8
Chris@1517 19 :node: hex string
Chris@245 20
Chris@245 21 Output example of rhsummary::
Chris@245 22
Chris@245 23 <?xml version="1.0"?>
Chris@245 24 <rhsummary>
Chris@245 25 <repository root="/foo/bar">
Chris@245 26 <tip revision="1234" node="abcdef0123..."/>
Chris@245 27 <tag revision="123" node="34567abc..." name="1.1.1"/>
Chris@245 28 <branch .../>
Chris@245 29 ...
Chris@245 30 </repository>
Chris@245 31 </rhsummary>
Chris@245 32
Chris@245 33 Output example of rhmanifest::
Chris@245 34
Chris@245 35 <?xml version="1.0"?>
Chris@245 36 <rhmanifest>
Chris@245 37 <repository root="/foo/bar">
Chris@245 38 <manifest revision="1234" path="lib">
Chris@245 39 <file name="diff.rb" revision="123" node="34567abc..." time="12345"
Chris@245 40 size="100"/>
Chris@245 41 ...
Chris@245 42 <dir name="redmine"/>
Chris@245 43 ...
Chris@245 44 </manifest>
Chris@245 45 </repository>
Chris@245 46 </rhmanifest>
Chris@245 47 """
Chris@245 48 import re, time, cgi, urllib
Chris@909 49 from mercurial import cmdutil, commands, node, error, hg
Chris@245 50
Chris@245 51 _x = cgi.escape
Chris@245 52 _u = lambda s: cgi.escape(urllib.quote(s))
Chris@245 53
Chris@245 54 def _tip(ui, repo):
Chris@245 55 # see mercurial/commands.py:tip
Chris@245 56 def tiprev():
Chris@245 57 try:
Chris@245 58 return len(repo) - 1
Chris@245 59 except TypeError: # Mercurial < 1.1
Chris@245 60 return repo.changelog.count() - 1
Chris@245 61 tipctx = repo.changectx(tiprev())
Chris@245 62 ui.write('<tip revision="%d" node="%s"/>\n'
Chris@1517 63 % (tipctx.rev(), _x(node.hex(tipctx.node()))))
Chris@245 64
Chris@245 65 _SPECIAL_TAGS = ('tip',)
Chris@245 66
Chris@245 67 def _tags(ui, repo):
Chris@245 68 # see mercurial/commands.py:tags
Chris@245 69 for t, n in reversed(repo.tagslist()):
Chris@245 70 if t in _SPECIAL_TAGS:
Chris@245 71 continue
Chris@245 72 try:
Chris@245 73 r = repo.changelog.rev(n)
Chris@245 74 except error.LookupError:
Chris@245 75 continue
Chris@245 76 ui.write('<tag revision="%d" node="%s" name="%s"/>\n'
Chris@1517 77 % (r, _x(node.hex(n)), _x(t)))
Chris@245 78
Chris@245 79 def _branches(ui, repo):
Chris@245 80 # see mercurial/commands.py:branches
Chris@245 81 def iterbranches():
Chris@1494 82 if getattr(repo, 'branchtags', None) is not None:
Chris@1494 83 # Mercurial < 2.9
Chris@1494 84 for t, n in repo.branchtags().iteritems():
Chris@1494 85 yield t, n, repo.changelog.rev(n)
Chris@1494 86 else:
Chris@1494 87 for tag, heads, tip, isclosed in repo.branchmap().iterbranches():
Chris@1494 88 yield tag, tip, repo.changelog.rev(tip)
Chris@245 89 def branchheads(branch):
Chris@245 90 try:
Chris@245 91 return repo.branchheads(branch, closed=False)
Chris@245 92 except TypeError: # Mercurial < 1.2
Chris@245 93 return repo.branchheads(branch)
Chris@245 94 for t, n, r in sorted(iterbranches(), key=lambda e: e[2], reverse=True):
Chris@245 95 if repo.lookup(r) in branchheads(t):
Chris@245 96 ui.write('<branch revision="%d" node="%s" name="%s"/>\n'
Chris@1517 97 % (r, _x(node.hex(n)), _x(t)))
Chris@245 98
Chris@245 99 def _manifest(ui, repo, path, rev):
Chris@245 100 ctx = repo.changectx(rev)
Chris@245 101 ui.write('<manifest revision="%d" path="%s">\n'
Chris@245 102 % (ctx.rev(), _u(path)))
Chris@245 103
Chris@245 104 known = set()
Chris@245 105 pathprefix = (path.rstrip('/') + '/').lstrip('/')
Chris@245 106 for f, n in sorted(ctx.manifest().iteritems(), key=lambda e: e[0]):
Chris@245 107 if not f.startswith(pathprefix):
Chris@245 108 continue
Chris@245 109 name = re.sub(r'/.*', '/', f[len(pathprefix):])
Chris@245 110 if name in known:
Chris@245 111 continue
Chris@245 112 known.add(name)
Chris@245 113
Chris@245 114 if name.endswith('/'):
Chris@245 115 ui.write('<dir name="%s"/>\n'
Chris@245 116 % _x(urllib.quote(name[:-1])))
Chris@245 117 else:
Chris@245 118 fctx = repo.filectx(f, fileid=n)
Chris@245 119 tm, tzoffset = fctx.date()
Chris@245 120 ui.write('<file name="%s" revision="%d" node="%s" '
Chris@245 121 'time="%d" size="%d"/>\n'
Chris@1517 122 % (_u(name), fctx.rev(), _x(node.hex(fctx.node())),
Chris@245 123 tm, fctx.size(), ))
Chris@245 124
Chris@245 125 ui.write('</manifest>\n')
Chris@245 126
Chris@245 127 def rhannotate(ui, repo, *pats, **opts):
Chris@441 128 rev = urllib.unquote_plus(opts.pop('rev', None))
Chris@441 129 opts['rev'] = rev
Chris@245 130 return commands.annotate(ui, repo, *map(urllib.unquote_plus, pats), **opts)
Chris@245 131
Chris@245 132 def rhcat(ui, repo, file1, *pats, **opts):
Chris@441 133 rev = urllib.unquote_plus(opts.pop('rev', None))
Chris@441 134 opts['rev'] = rev
Chris@245 135 return commands.cat(ui, repo, urllib.unquote_plus(file1), *map(urllib.unquote_plus, pats), **opts)
Chris@245 136
Chris@245 137 def rhdiff(ui, repo, *pats, **opts):
Chris@245 138 """diff repository (or selected files)"""
Chris@245 139 change = opts.pop('change', None)
Chris@245 140 if change: # add -c option for Mercurial<1.1
Chris@245 141 base = repo.changectx(change).parents()[0].rev()
Chris@245 142 opts['rev'] = [str(base), change]
Chris@245 143 opts['nodates'] = True
Chris@245 144 return commands.diff(ui, repo, *map(urllib.unquote_plus, pats), **opts)
Chris@245 145
Chris@441 146 def rhlog(ui, repo, *pats, **opts):
Chris@441 147 rev = opts.pop('rev')
Chris@441 148 bra0 = opts.pop('branch')
Chris@441 149 from_rev = urllib.unquote_plus(opts.pop('from', None))
Chris@441 150 to_rev = urllib.unquote_plus(opts.pop('to' , None))
Chris@441 151 bra = urllib.unquote_plus(opts.pop('rhbranch', None))
Chris@441 152 from_rev = from_rev.replace('"', '\\"')
Chris@441 153 to_rev = to_rev.replace('"', '\\"')
Chris@909 154 if hg.util.version() >= '1.6':
Chris@909 155 opts['rev'] = ['"%s":"%s"' % (from_rev, to_rev)]
Chris@909 156 else:
Chris@909 157 opts['rev'] = ['%s:%s' % (from_rev, to_rev)]
Chris@441 158 opts['branch'] = [bra]
Chris@441 159 return commands.log(ui, repo, *map(urllib.unquote_plus, pats), **opts)
Chris@441 160
Chris@245 161 def rhmanifest(ui, repo, path='', **opts):
Chris@245 162 """output the sub-manifest of the specified directory"""
Chris@245 163 ui.write('<?xml version="1.0"?>\n')
Chris@245 164 ui.write('<rhmanifest>\n')
Chris@245 165 ui.write('<repository root="%s">\n' % _u(repo.root))
Chris@245 166 try:
Chris@245 167 _manifest(ui, repo, urllib.unquote_plus(path), urllib.unquote_plus(opts.get('rev')))
Chris@245 168 finally:
Chris@245 169 ui.write('</repository>\n')
Chris@245 170 ui.write('</rhmanifest>\n')
Chris@245 171
Chris@245 172 def rhsummary(ui, repo, **opts):
Chris@245 173 """output the summary of the repository"""
Chris@245 174 ui.write('<?xml version="1.0"?>\n')
Chris@245 175 ui.write('<rhsummary>\n')
Chris@245 176 ui.write('<repository root="%s">\n' % _u(repo.root))
Chris@245 177 try:
Chris@245 178 _tip(ui, repo)
Chris@245 179 _tags(ui, repo)
Chris@245 180 _branches(ui, repo)
Chris@245 181 # TODO: bookmarks in core (Mercurial>=1.8)
Chris@245 182 finally:
Chris@245 183 ui.write('</repository>\n')
Chris@245 184 ui.write('</rhsummary>\n')
Chris@245 185
Chris@245 186 cmdtable = {
Chris@245 187 'rhannotate': (rhannotate,
Chris@245 188 [('r', 'rev', '', 'revision'),
Chris@245 189 ('u', 'user', None, 'list the author (long with -v)'),
Chris@245 190 ('n', 'number', None, 'list the revision number (default)'),
Chris@245 191 ('c', 'changeset', None, 'list the changeset'),
Chris@245 192 ],
Chris@245 193 'hg rhannotate [-r REV] [-u] [-n] [-c] FILE...'),
Chris@245 194 'rhcat': (rhcat,
Chris@245 195 [('r', 'rev', '', 'revision')],
Chris@245 196 'hg rhcat ([-r REV] ...) FILE...'),
Chris@245 197 'rhdiff': (rhdiff,
Chris@245 198 [('r', 'rev', [], 'revision'),
Chris@245 199 ('c', 'change', '', 'change made by revision')],
Chris@245 200 'hg rhdiff ([-c REV] | [-r REV] ...) [FILE]...'),
Chris@441 201 'rhlog': (rhlog,
Chris@441 202 [
Chris@441 203 ('r', 'rev', [], 'show the specified revision'),
Chris@441 204 ('b', 'branch', [],
Chris@909 205 'show changesets within the given named branch'),
Chris@441 206 ('l', 'limit', '',
Chris@909 207 'limit number of changes displayed'),
Chris@441 208 ('d', 'date', '',
Chris@909 209 'show revisions matching date spec'),
Chris@441 210 ('u', 'user', [],
Chris@909 211 'revisions committed by user'),
Chris@441 212 ('', 'from', '',
Chris@909 213 ''),
Chris@441 214 ('', 'to', '',
Chris@909 215 ''),
Chris@441 216 ('', 'rhbranch', '',
Chris@909 217 ''),
Chris@441 218 ('', 'template', '',
Chris@909 219 'display with template')],
Chris@441 220 'hg rhlog [OPTION]... [FILE]'),
Chris@245 221 'rhmanifest': (rhmanifest,
Chris@245 222 [('r', 'rev', '', 'show the specified revision')],
Chris@245 223 'hg rhmanifest [-r REV] [PATH]'),
Chris@245 224 'rhsummary': (rhsummary, [], 'hg rhsummary'),
Chris@245 225 }