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