Source

hg-status-sections / sections.py

Full commit
#!/usr/bin/python

#
# Copyright (c) 2010 Juri Pakaste <juri@iki.fi>
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

from mercurial import hg, util, commands, extensions, cmdutil, merge
if hasattr(cmdutil, "revpair"):
    from cmdutil import revpair
    from cmdutil import match
else:
    from mercurial.scmutil import revpair
    from mercurial.scmutil import match
from mercurial.i18n import _

colortable = {
    'statussections.modified': 'blue bold',
    'statussections.added': 'green bold',
    'statussections.removed': 'red bold',
    'statussections.deleted': 'cyan bold underline',
    'statussections.unknown': 'magenta bold underline',
    'statussections.ignored': 'black bold',
    'statussections.clean': 'none',
    'statussections.unresolved': 'green bold',
    'statussections.unresolved': 'red bold',
    'statussections.header': 'black bold underline'
}

def status_sections(ui, repo, *pats, **opts):
    """Show status of files in sections, instead of prefixing a indicator."""

    showmerge = opts.get('merge')
    noshowmerge = opts.get('no_merge')
    revs = opts.get('rev')
    change = opts.get('change')

    if not showmerge:
        showmerge = len(ui.configlist("statussections", "showmerge")) and not noshowmerge
        if showmerge:
            noshowmerge = False

    if showmerge and (noshowmerge or revs or change):
        raise util.Abort(_('cannot specify --merge with --no-merge, --rev or --change'))

    if revs and change:
        msg = _('cannot specify --rev and --change at the same time')
        raise util.Abort(msg)
    elif change:
        node2 = repo.lookup(change)
        node1 = repo[node2].parents()[0].node()
    else:
        node1, node2 = revpair(repo, revs)

    states = 'modified added removed deleted unknown ignored clean'.split()
    show = [k for k in states if opts.get(k)]
    if opts.get('all'):
        show += ui.quiet and (states[:4] + ['clean']) or states
    if not show:
        show = ui.quiet and states[:4] or states[:5]

    m = match(repo[None], pats, opts)

    sections = repo.status(node1, node2, m, 'ignored' in show,
                           'clean' in show, 'unknown' in show)
    cwd = repo.getcwd()

    for header, files in zip(states, sections):
        flabel = 'statussections.{0}'.format(header)
        if len(files) > 0:
            ui.write("%s:\n" % _(header), label='statussections.header')
            for f in files:
                ui.write("  %s\n" % repo.pathto(f, cwd), label=flabel)

    if showmerge:
        ms = merge.mergestate(repo)
        bystate = {'r': [], 'u': []}
        for f in ms:
            bystate[ms[f]].append(f)
        def showgroup(name, group, label):
            if len(group):
                ui.write("%s:\n" % name, label='statussections.header')
                for f in group:
                    ui.write("  %s\n" % repo.pathto(f, cwd), label=label)
        if len(bystate['r']) or len(bystate['u']):
            ui.write("\n")
        showgroup(_("unresolved"), bystate['u'], 'statussections.unresolved')
        showgroup(_("resolved"), bystate['r'], 'statussections.resolved')



def uisetup(ui):
    """Initialize the extension."""
    def ss(orig, *args, **kwargs):
        if kwargs['sections']:
            return status_sections(*args, **kwargs)
        return orig(*args, **kwargs)
    entry = extensions.wrapcommand(commands.table, 'status', ss)
    entry[1].append(('s', 'sections', None, _("show status of files split into sections")))
    entry[1].append(('M', 'merge', None, _("show merge status with sections view")))
    entry[1].append(('', 'no-merge', None, _("don't show merge status with sections view")))

cmdtable = {
    "^sstatus|sst":
        (status_sections,
         [('A', 'all', None, _('show status of all files')),
          ('m', 'modified', None, _('show only modified files')),
          ('a', 'added', None, _('show only added files')),
          ('r', 'removed', None, _('show only removed files')),
          ('d', 'deleted', None, _('show only deleted (but tracked) files')),
          ('c', 'clean', None, _('show only files without changes')),
          ('u', 'unknown', None, _('show only unknown (not tracked) files')),
          ('i', 'ignored', None, _('show only ignored files')),
          ('', 'rev', [], _('show difference from revision')),
          ('', 'change', '', _('list the changed files of a revision')),
          ('M', 'merge', None, _('show merge status')),
          ('', 'no-merge', None, _("don't show merge status"))
          ] + commands.walkopts,
         _('[OPTION]... [FILE]...'))
}