annotate lib/redmine/scm/adapters/mercurial/redminehelper.py @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 433d4f72a19b
children e248c7af89ec
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@245 19 :node: 12-digits (short) 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@245 63 % (tipctx.rev(), _x(node.short(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@245 77 % (r, _x(node.short(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@245 82 for t, n in repo.branchtags().iteritems():
Chris@245 83 yield t, n, repo.changelog.rev(n)
Chris@245 84 def branchheads(branch):
Chris@245 85 try:
Chris@245 86 return repo.branchheads(branch, closed=False)
Chris@245 87 except TypeError: # Mercurial < 1.2
Chris@245 88 return repo.branchheads(branch)
Chris@245 89 for t, n, r in sorted(iterbranches(), key=lambda e: e[2], reverse=True):
Chris@245 90 if repo.lookup(r) in branchheads(t):
Chris@245 91 ui.write('<branch revision="%d" node="%s" name="%s"/>\n'
Chris@245 92 % (r, _x(node.short(n)), _x(t)))
Chris@245 93
Chris@245 94 def _manifest(ui, repo, path, rev):
Chris@245 95 ctx = repo.changectx(rev)
Chris@245 96 ui.write('<manifest revision="%d" path="%s">\n'
Chris@245 97 % (ctx.rev(), _u(path)))
Chris@245 98
Chris@245 99 known = set()
Chris@245 100 pathprefix = (path.rstrip('/') + '/').lstrip('/')
Chris@245 101 for f, n in sorted(ctx.manifest().iteritems(), key=lambda e: e[0]):
Chris@245 102 if not f.startswith(pathprefix):
Chris@245 103 continue
Chris@245 104 name = re.sub(r'/.*', '/', f[len(pathprefix):])
Chris@245 105 if name in known:
Chris@245 106 continue
Chris@245 107 known.add(name)
Chris@245 108
Chris@245 109 if name.endswith('/'):
Chris@245 110 ui.write('<dir name="%s"/>\n'
Chris@245 111 % _x(urllib.quote(name[:-1])))
Chris@245 112 else:
Chris@245 113 fctx = repo.filectx(f, fileid=n)
Chris@245 114 tm, tzoffset = fctx.date()
Chris@245 115 ui.write('<file name="%s" revision="%d" node="%s" '
Chris@245 116 'time="%d" size="%d"/>\n'
Chris@245 117 % (_u(name), fctx.rev(), _x(node.short(fctx.node())),
Chris@245 118 tm, fctx.size(), ))
Chris@245 119
Chris@245 120 ui.write('</manifest>\n')
Chris@245 121
Chris@245 122 def rhannotate(ui, repo, *pats, **opts):
Chris@441 123 rev = urllib.unquote_plus(opts.pop('rev', None))
Chris@441 124 opts['rev'] = rev
Chris@245 125 return commands.annotate(ui, repo, *map(urllib.unquote_plus, pats), **opts)
Chris@245 126
Chris@245 127 def rhcat(ui, repo, file1, *pats, **opts):
Chris@441 128 rev = urllib.unquote_plus(opts.pop('rev', None))
Chris@441 129 opts['rev'] = rev
Chris@245 130 return commands.cat(ui, repo, urllib.unquote_plus(file1), *map(urllib.unquote_plus, pats), **opts)
Chris@245 131
Chris@245 132 def rhdiff(ui, repo, *pats, **opts):
Chris@245 133 """diff repository (or selected files)"""
Chris@245 134 change = opts.pop('change', None)
Chris@245 135 if change: # add -c option for Mercurial<1.1
Chris@245 136 base = repo.changectx(change).parents()[0].rev()
Chris@245 137 opts['rev'] = [str(base), change]
Chris@245 138 opts['nodates'] = True
Chris@245 139 return commands.diff(ui, repo, *map(urllib.unquote_plus, pats), **opts)
Chris@245 140
Chris@441 141 def rhlog(ui, repo, *pats, **opts):
Chris@441 142 rev = opts.pop('rev')
Chris@441 143 bra0 = opts.pop('branch')
Chris@441 144 from_rev = urllib.unquote_plus(opts.pop('from', None))
Chris@441 145 to_rev = urllib.unquote_plus(opts.pop('to' , None))
Chris@441 146 bra = urllib.unquote_plus(opts.pop('rhbranch', None))
Chris@441 147 from_rev = from_rev.replace('"', '\\"')
Chris@441 148 to_rev = to_rev.replace('"', '\\"')
Chris@909 149 if hg.util.version() >= '1.6':
Chris@909 150 opts['rev'] = ['"%s":"%s"' % (from_rev, to_rev)]
Chris@909 151 else:
Chris@909 152 opts['rev'] = ['%s:%s' % (from_rev, to_rev)]
Chris@441 153 opts['branch'] = [bra]
Chris@441 154 return commands.log(ui, repo, *map(urllib.unquote_plus, pats), **opts)
Chris@441 155
Chris@245 156 def rhmanifest(ui, repo, path='', **opts):
Chris@245 157 """output the sub-manifest of the specified directory"""
Chris@245 158 ui.write('<?xml version="1.0"?>\n')
Chris@245 159 ui.write('<rhmanifest>\n')
Chris@245 160 ui.write('<repository root="%s">\n' % _u(repo.root))
Chris@245 161 try:
Chris@245 162 _manifest(ui, repo, urllib.unquote_plus(path), urllib.unquote_plus(opts.get('rev')))
Chris@245 163 finally:
Chris@245 164 ui.write('</repository>\n')
Chris@245 165 ui.write('</rhmanifest>\n')
Chris@245 166
Chris@245 167 def rhsummary(ui, repo, **opts):
Chris@245 168 """output the summary of the repository"""
Chris@245 169 ui.write('<?xml version="1.0"?>\n')
Chris@245 170 ui.write('<rhsummary>\n')
Chris@245 171 ui.write('<repository root="%s">\n' % _u(repo.root))
Chris@245 172 try:
Chris@245 173 _tip(ui, repo)
Chris@245 174 _tags(ui, repo)
Chris@245 175 _branches(ui, repo)
Chris@245 176 # TODO: bookmarks in core (Mercurial>=1.8)
Chris@245 177 finally:
Chris@245 178 ui.write('</repository>\n')
Chris@245 179 ui.write('</rhsummary>\n')
Chris@245 180
Chris@245 181 cmdtable = {
Chris@245 182 'rhannotate': (rhannotate,
Chris@245 183 [('r', 'rev', '', 'revision'),
Chris@245 184 ('u', 'user', None, 'list the author (long with -v)'),
Chris@245 185 ('n', 'number', None, 'list the revision number (default)'),
Chris@245 186 ('c', 'changeset', None, 'list the changeset'),
Chris@245 187 ],
Chris@245 188 'hg rhannotate [-r REV] [-u] [-n] [-c] FILE...'),
Chris@245 189 'rhcat': (rhcat,
Chris@245 190 [('r', 'rev', '', 'revision')],
Chris@245 191 'hg rhcat ([-r REV] ...) FILE...'),
Chris@245 192 'rhdiff': (rhdiff,
Chris@245 193 [('r', 'rev', [], 'revision'),
Chris@245 194 ('c', 'change', '', 'change made by revision')],
Chris@245 195 'hg rhdiff ([-c REV] | [-r REV] ...) [FILE]...'),
Chris@441 196 'rhlog': (rhlog,
Chris@441 197 [
Chris@441 198 ('r', 'rev', [], 'show the specified revision'),
Chris@441 199 ('b', 'branch', [],
Chris@909 200 'show changesets within the given named branch'),
Chris@441 201 ('l', 'limit', '',
Chris@909 202 'limit number of changes displayed'),
Chris@441 203 ('d', 'date', '',
Chris@909 204 'show revisions matching date spec'),
Chris@441 205 ('u', 'user', [],
Chris@909 206 'revisions committed by user'),
Chris@441 207 ('', 'from', '',
Chris@909 208 ''),
Chris@441 209 ('', 'to', '',
Chris@909 210 ''),
Chris@441 211 ('', 'rhbranch', '',
Chris@909 212 ''),
Chris@441 213 ('', 'template', '',
Chris@909 214 'display with template')],
Chris@441 215 'hg rhlog [OPTION]... [FILE]'),
Chris@245 216 'rhmanifest': (rhmanifest,
Chris@245 217 [('r', 'rev', '', 'show the specified revision')],
Chris@245 218 'hg rhmanifest [-r REV] [PATH]'),
Chris@245 219 'rhsummary': (rhsummary, [], 'hg rhsummary'),
Chris@245 220 }