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