hgext/show.py
author Ryan McElroy <rmcelroy@fb.com>
Fri, 07 Apr 2017 10:46:32 -0700
changeset 31859 8e282aa3c3ff
parent 31858 ad366d57d574
child 31943 3e9f118cc834
permissions -rw-r--r--
show: fix corrupt json output with no bookmarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31768
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,
31859
8e282aa3c3ff show: fix corrupt json output with no bookmarks
Ryan McElroy <rmcelroy@fb.com>
parents: 31858
diff changeset
    22
    formatter,
31820
45761ef1bc93 py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents: 31768
diff changeset
    23
    pycompat,
31768
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    24
    registrar,
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    25
)
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    26
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    27
# 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
    28
# 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
    29
# 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
    30
# leave the attribute unspecified.
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    31
testedwith = 'ships-with-hg-core'
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    32
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    33
cmdtable = {}
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    34
command = cmdutil.command(cmdtable)
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    35
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    36
class showcmdfunc(registrar._funcregistrarbase):
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    37
    """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
    38
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    39
    # Used by _formatdoc().
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    40
    _docformat = '%s -- %s'
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    41
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    42
    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
    43
        """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
    44
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    45
        ``name`` is the sub-command name.
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
        ``func`` is the function being decorated.
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    48
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    49
        ``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
    50
        this view.
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
        func._fmtopic = fmtopic
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
showview = showcmdfunc()
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    55
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    56
@command('show', commands.formatteropts, _('VIEW'))
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    57
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
    58
    """show various repository information
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
    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
    61
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    62
    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
    63
    command aborts.
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
    .. note::
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    66
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    67
       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
    68
       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
    69
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    70
       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
    71
       ``-T/--template``.
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
    List of available views:
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    74
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    75
    """
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
    76
    if ui.plain() and not template:
31858
ad366d57d574 show: tweak plain abort language for clarity
Ryan McElroy <rmcelroy@fb.com>
parents: 31820
diff changeset
    77
        hint = _('invoke with -T/--template to control output format')
ad366d57d574 show: tweak plain abort language for clarity
Ryan McElroy <rmcelroy@fb.com>
parents: 31820
diff changeset
    78
        raise error.Abort(_('must specify a template in plain mode'), hint=hint)
31768
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):
31859
8e282aa3c3ff show: fix corrupt json output with no bookmarks
Ryan McElroy <rmcelroy@fb.com>
parents: 31858
diff changeset
   113
        # This is a bit hacky. Ideally, templates would have a way to
8e282aa3c3ff show: fix corrupt json output with no bookmarks
Ryan McElroy <rmcelroy@fb.com>
parents: 31858
diff changeset
   114
        # specify an empty output, but we shouldn't corrupt JSON while
8e282aa3c3ff show: fix corrupt json output with no bookmarks
Ryan McElroy <rmcelroy@fb.com>
parents: 31858
diff changeset
   115
        # waiting for this functionality.
8e282aa3c3ff show: fix corrupt json output with no bookmarks
Ryan McElroy <rmcelroy@fb.com>
parents: 31858
diff changeset
   116
        if not isinstance(fm, formatter.jsonformatter):
8e282aa3c3ff show: fix corrupt json output with no bookmarks
Ryan McElroy <rmcelroy@fb.com>
parents: 31858
diff changeset
   117
            ui.write(_('(no bookmarks set)\n'))
31768
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   118
        return
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   119
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   120
    active = repo._activebookmark
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   121
    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
   122
    # TODO consider exposing longest shortest(node).
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   123
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   124
    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
   125
        fm.startitem()
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   126
        fm.context(ctx=repo[node])
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   127
        fm.write('bookmark', '%s', bm)
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   128
        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
   129
        fm.data(active=bm == active,
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   130
                longestbookmarklen=longestname)
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   131
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   132
# 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
   133
# 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
   134
# 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
   135
# to do this more robustly.
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   136
# TODO make this more robust.
264baeef3588 show: new extension for displaying various repository data
Gregory Szorc <gregory.szorc@gmail.com>
parents:
diff changeset
   137
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
   138
for key in sorted(showview._table.keys()):
31820
45761ef1bc93 py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents: 31768
diff changeset
   139
    cmdtable['show'][0].__doc__ += pycompat.sysstr(' %s   %s\n' % (
45761ef1bc93 py3: have registrar process docstrings in bytes
Yuya Nishihara <yuya@tcha.org>
parents: 31768
diff changeset
   140
        key.ljust(longest), showview._table[key]._origdoc))