Chris@1494: # redminehelper: Redmine helper extension for Mercurial Chris@1494: # Chris@1494: # Copyright 2010 Alessio Franceschelli (alefranz.net) Chris@1494: # Copyright 2010-2011 Yuya Nishihara Chris@1494: # Chris@1494: # This software may be used and distributed according to the terms of the Chris@1494: # GNU General Public License version 2 or any later version. Chris@1494: """helper commands for Redmine to reduce the number of hg calls Chris@1494: Chris@1494: To test this extension, please try:: Chris@1494: Chris@1494: $ hg --config extensions.redminehelper=redminehelper.py rhsummary Chris@1494: Chris@1494: I/O encoding: Chris@1494: Chris@1494: :file path: urlencoded, raw string Chris@1494: :tag name: utf-8 Chris@1494: :branch name: utf-8 Chris@1494: :node: 12-digits (short) hex string Chris@1494: Chris@1494: Output example of rhsummary:: Chris@1494: Chris@1494: Chris@1494: Chris@1494: Chris@1494: Chris@1494: Chris@1494: Chris@1494: ... Chris@1494: Chris@1494: Chris@1494: Chris@1494: Output example of rhmanifest:: Chris@1494: Chris@1494: Chris@1494: Chris@1494: Chris@1494: Chris@1494: Chris@1494: ... Chris@1494: Chris@1494: ... Chris@1494: Chris@1494: Chris@1494: Chris@1494: """ Chris@1494: import re, time, cgi, urllib Chris@1494: from mercurial import cmdutil, commands, node, error, hg Chris@1494: Chris@1494: _x = cgi.escape Chris@1494: _u = lambda s: cgi.escape(urllib.quote(s)) Chris@1494: Chris@1494: def _tip(ui, repo): Chris@1494: # see mercurial/commands.py:tip Chris@1494: def tiprev(): Chris@1494: try: Chris@1494: return len(repo) - 1 Chris@1494: except TypeError: # Mercurial < 1.1 Chris@1494: return repo.changelog.count() - 1 Chris@1494: tipctx = repo.changectx(tiprev()) Chris@1494: ui.write('\n' Chris@1494: % (tipctx.rev(), _x(node.short(tipctx.node())))) Chris@1494: Chris@1494: _SPECIAL_TAGS = ('tip',) Chris@1494: Chris@1494: def _tags(ui, repo): Chris@1494: # see mercurial/commands.py:tags Chris@1494: for t, n in reversed(repo.tagslist()): Chris@1494: if t in _SPECIAL_TAGS: Chris@1494: continue Chris@1494: try: Chris@1494: r = repo.changelog.rev(n) Chris@1494: except error.LookupError: Chris@1494: continue Chris@1494: ui.write('\n' Chris@1494: % (r, _x(node.short(n)), _x(t))) Chris@1494: Chris@1494: def _branches(ui, repo): Chris@1494: # see mercurial/commands.py:branches Chris@1494: def iterbranches(): Chris@1494: if getattr(repo, 'branchtags', None) is not None: Chris@1494: # Mercurial < 2.9 Chris@1494: for t, n in repo.branchtags().iteritems(): Chris@1494: yield t, n, repo.changelog.rev(n) Chris@1494: else: Chris@1494: for tag, heads, tip, isclosed in repo.branchmap().iterbranches(): Chris@1494: yield tag, tip, repo.changelog.rev(tip) Chris@1494: def branchheads(branch): Chris@1494: try: Chris@1494: return repo.branchheads(branch, closed=False) Chris@1494: except TypeError: # Mercurial < 1.2 Chris@1494: return repo.branchheads(branch) Chris@1494: for t, n, r in sorted(iterbranches(), key=lambda e: e[2], reverse=True): Chris@1494: if repo.lookup(r) in branchheads(t): Chris@1494: ui.write('\n' Chris@1494: % (r, _x(node.short(n)), _x(t))) Chris@1494: Chris@1494: def _manifest(ui, repo, path, rev): Chris@1494: ctx = repo.changectx(rev) Chris@1494: ui.write('\n' Chris@1494: % (ctx.rev(), _u(path))) Chris@1494: Chris@1494: known = set() Chris@1494: pathprefix = (path.rstrip('/') + '/').lstrip('/') Chris@1494: for f, n in sorted(ctx.manifest().iteritems(), key=lambda e: e[0]): Chris@1494: if not f.startswith(pathprefix): Chris@1494: continue Chris@1494: name = re.sub(r'/.*', '/', f[len(pathprefix):]) Chris@1494: if name in known: Chris@1494: continue Chris@1494: known.add(name) Chris@1494: Chris@1494: if name.endswith('/'): Chris@1494: ui.write('\n' Chris@1494: % _x(urllib.quote(name[:-1]))) Chris@1494: else: Chris@1494: fctx = repo.filectx(f, fileid=n) Chris@1494: tm, tzoffset = fctx.date() Chris@1494: ui.write('\n' Chris@1494: % (_u(name), fctx.rev(), _x(node.short(fctx.node())), Chris@1494: tm, fctx.size(), )) Chris@1494: Chris@1494: ui.write('\n') Chris@1494: Chris@1494: def rhannotate(ui, repo, *pats, **opts): Chris@1494: rev = urllib.unquote_plus(opts.pop('rev', None)) Chris@1494: opts['rev'] = rev Chris@1494: return commands.annotate(ui, repo, *map(urllib.unquote_plus, pats), **opts) Chris@1494: Chris@1494: def rhcat(ui, repo, file1, *pats, **opts): Chris@1494: rev = urllib.unquote_plus(opts.pop('rev', None)) Chris@1494: opts['rev'] = rev Chris@1494: return commands.cat(ui, repo, urllib.unquote_plus(file1), *map(urllib.unquote_plus, pats), **opts) Chris@1494: Chris@1494: def rhdiff(ui, repo, *pats, **opts): Chris@1494: """diff repository (or selected files)""" Chris@1494: change = opts.pop('change', None) Chris@1494: if change: # add -c option for Mercurial<1.1 Chris@1494: base = repo.changectx(change).parents()[0].rev() Chris@1494: opts['rev'] = [str(base), change] Chris@1494: opts['nodates'] = True Chris@1494: return commands.diff(ui, repo, *map(urllib.unquote_plus, pats), **opts) Chris@1494: Chris@1494: def rhlog(ui, repo, *pats, **opts): Chris@1494: rev = opts.pop('rev') Chris@1494: bra0 = opts.pop('branch') Chris@1494: from_rev = urllib.unquote_plus(opts.pop('from', None)) Chris@1494: to_rev = urllib.unquote_plus(opts.pop('to' , None)) Chris@1494: bra = urllib.unquote_plus(opts.pop('rhbranch', None)) Chris@1494: from_rev = from_rev.replace('"', '\\"') Chris@1494: to_rev = to_rev.replace('"', '\\"') Chris@1494: if hg.util.version() >= '1.6': Chris@1494: opts['rev'] = ['"%s":"%s"' % (from_rev, to_rev)] Chris@1494: else: Chris@1494: opts['rev'] = ['%s:%s' % (from_rev, to_rev)] Chris@1494: opts['branch'] = [bra] Chris@1494: return commands.log(ui, repo, *map(urllib.unquote_plus, pats), **opts) Chris@1494: Chris@1494: def rhmanifest(ui, repo, path='', **opts): Chris@1494: """output the sub-manifest of the specified directory""" Chris@1494: ui.write('\n') Chris@1494: ui.write('\n') Chris@1494: ui.write('\n' % _u(repo.root)) Chris@1494: try: Chris@1494: _manifest(ui, repo, urllib.unquote_plus(path), urllib.unquote_plus(opts.get('rev'))) Chris@1494: finally: Chris@1494: ui.write('\n') Chris@1494: ui.write('\n') Chris@1494: Chris@1494: def rhsummary(ui, repo, **opts): Chris@1494: """output the summary of the repository""" Chris@1494: ui.write('\n') Chris@1494: ui.write('\n') Chris@1494: ui.write('\n' % _u(repo.root)) Chris@1494: try: Chris@1494: _tip(ui, repo) Chris@1494: _tags(ui, repo) Chris@1494: _branches(ui, repo) Chris@1494: # TODO: bookmarks in core (Mercurial>=1.8) Chris@1494: finally: Chris@1494: ui.write('\n') Chris@1494: ui.write('\n') Chris@1494: Chris@1494: cmdtable = { Chris@1494: 'rhannotate': (rhannotate, Chris@1494: [('r', 'rev', '', 'revision'), Chris@1494: ('u', 'user', None, 'list the author (long with -v)'), Chris@1494: ('n', 'number', None, 'list the revision number (default)'), Chris@1494: ('c', 'changeset', None, 'list the changeset'), Chris@1494: ], Chris@1494: 'hg rhannotate [-r REV] [-u] [-n] [-c] FILE...'), Chris@1494: 'rhcat': (rhcat, Chris@1494: [('r', 'rev', '', 'revision')], Chris@1494: 'hg rhcat ([-r REV] ...) FILE...'), Chris@1494: 'rhdiff': (rhdiff, Chris@1494: [('r', 'rev', [], 'revision'), Chris@1494: ('c', 'change', '', 'change made by revision')], Chris@1494: 'hg rhdiff ([-c REV] | [-r REV] ...) [FILE]...'), Chris@1494: 'rhlog': (rhlog, Chris@1494: [ Chris@1494: ('r', 'rev', [], 'show the specified revision'), Chris@1494: ('b', 'branch', [], Chris@1494: 'show changesets within the given named branch'), Chris@1494: ('l', 'limit', '', Chris@1494: 'limit number of changes displayed'), Chris@1494: ('d', 'date', '', Chris@1494: 'show revisions matching date spec'), Chris@1494: ('u', 'user', [], Chris@1494: 'revisions committed by user'), Chris@1494: ('', 'from', '', Chris@1494: ''), Chris@1494: ('', 'to', '', Chris@1494: ''), Chris@1494: ('', 'rhbranch', '', Chris@1494: ''), Chris@1494: ('', 'template', '', Chris@1494: 'display with template')], Chris@1494: 'hg rhlog [OPTION]... [FILE]'), Chris@1494: 'rhmanifest': (rhmanifest, Chris@1494: [('r', 'rev', '', 'show the specified revision')], Chris@1494: 'hg rhmanifest [-r REV] [PATH]'), Chris@1494: 'rhsummary': (rhsummary, [], 'hg rhsummary'), Chris@1494: }