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