hgext/show.py
author Yuya Nishihara <yuya@tcha.org>
Sun, 02 Apr 2017 22:01:32 +0900
changeset 31809 35b8bb1ef02b
parent 31765 264baeef3588
child 31820 45761ef1bc93
permissions -rw-r--r--
revset: stop supporting predicate that returns plain list (API) It's said to be removed after 3.9.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31765
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     1
# show.py - Extension implementing `hg show`
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     2
#
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     3
# Copyright 2017 Gregory Szorc <gregory.szorc@gmail.com>
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     4
#
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     7
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     8
"""unified command to show various repository information (EXPERIMENTAL)
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
     9
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    10
This extension provides the :hg:`show` command, which provides a central
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    11
command for displaying commonly-accessed repository data and views of that
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    12
data.
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    13
"""
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    14
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    15
from __future__ import absolute_import
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    16
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    17
from mercurial.i18n import _
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    18
from mercurial import (
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    19
    cmdutil,
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    20
    commands,
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    21
    error,
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    22
    registrar,
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    23
)
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    24
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    25
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    26
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    27
# be specifying the version(s) of Mercurial they are tested with, or
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    28
# leave the attribute unspecified.
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    29
testedwith = 'ships-with-hg-core'
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    30
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    31
cmdtable = {}
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    32
command = cmdutil.command(cmdtable)
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    33
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    34
class showcmdfunc(registrar._funcregistrarbase):
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    35
    """Register a function to be invoked for an `hg show <thing>`."""
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    36
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    37
    # Used by _formatdoc().
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    38
    _docformat = '%s -- %s'
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    39
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    40
    def _extrasetup(self, name, func, fmtopic=None):
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    41
        """Called with decorator arguments to register a show view.
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    42
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    43
        ``name`` is the sub-command name.
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    44
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    45
        ``func`` is the function being decorated.
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    46
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    47
        ``fmtopic`` is the topic in the style that will be rendered for
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    48
        this view.
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    49
        """
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    50
        func._fmtopic = fmtopic
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    51
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    52
showview = showcmdfunc()
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    53
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    54
@command('show', commands.formatteropts, _('VIEW'))
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    55
def show(ui, repo, view=None, template=None):
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    56
    """show various repository information
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    57
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    58
    A requested view of repository data is displayed.
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    59
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    60
    If no view is requested, the list of available views is shown and the
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    61
    command aborts.
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    62
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    63
    .. note::
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    64
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    65
       There are no backwards compatibility guarantees for the output of this
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    66
       command. Output may change in any future Mercurial release.
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    67
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    68
       Consumers wanting stable command output should specify a template via
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    69
       ``-T/--template``.
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    70
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    71
    List of available views:
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    72
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    73
    """
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    74
    if ui.plain() and not template:
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    75
        raise error.Abort(_('"hg show" cannot be used in plain mode because '
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    76
                            'output is not stable'),
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    77
                          hint=_('unset HGPLAIN and invoke with -T/--template '
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    78
                                 'to control output'))
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    79
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    80
    views = showview._table
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    81
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    82
    if not view:
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    83
        ui.pager('show')
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    84
        # TODO consider using formatter here so available views can be
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    85
        # rendered to custom format.
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    86
        ui.write(_('available views:\n'))
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    87
        ui.write('\n')
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    88
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    89
        for name, func in sorted(views.items()):
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    90
            ui.write(('%s\n') % func.__doc__)
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    91
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    92
        ui.write('\n')
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    93
        raise error.Abort(_('no view requested'),
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    94
                          hint=_('use "hg show VIEW" to choose a view'))
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    95
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    96
    # TODO use same logic as dispatch to perform prefix matching.
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    97
    if view not in views:
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    98
        raise error.Abort(_('unknown view: %s') % view,
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    99
                          hint=_('run "hg show" to see available views'))
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   100
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   101
    template = template or 'show'
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   102
    fmtopic = 'show%s' % views[view]._fmtopic
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   103
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   104
    ui.pager('show')
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   105
    with ui.formatter(fmtopic, {'template': template}) as fm:
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   106
        return views[view](ui, repo, fm)
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   107
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   108
@showview('bookmarks', fmtopic='bookmarks')
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   109
def showbookmarks(ui, repo, fm):
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   110
    """bookmarks and their associated changeset"""
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   111
    marks = repo._bookmarks
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   112
    if not len(marks):
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   113
        # TODO json output is corrupted; consider using formatter
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   114
        ui.write(_('(no bookmarks set)\n'))
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   115
        return
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   116
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   117
    active = repo._activebookmark
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   118
    longestname = max(len(b) for b in marks)
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   119
    # TODO consider exposing longest shortest(node).
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   120
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   121
    for bm, node in sorted(marks.items()):
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   122
        fm.startitem()
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   123
        fm.context(ctx=repo[node])
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   124
        fm.write('bookmark', '%s', bm)
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   125
        fm.write('node', fm.hexfunc(node), fm.hexfunc(node))
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   126
        fm.data(active=bm == active,
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   127
                longestbookmarklen=longestname)
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   128
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   129
# Adjust the docstring of the show command so it shows all registered views.
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   130
# This is a bit hacky because it runs at the end of module load. When moved
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   131
# into core or when another extension wants to provide a view, we'll need
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   132
# to do this more robustly.
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   133
# TODO make this more robust.
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   134
longest = max(map(len, showview._table.keys()))
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   135
for key in sorted(showview._table.keys()):
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   136
    cmdtable['show'][0].__doc__ += ' %s   %s\n' % (
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   137
        key.ljust(longest), showview._table[key]._origdoc)