annotate .svn/pristine/89/893f90c8908ea139ccc1694a00b33417f56efa41.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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