view extensions/revinfo.py @ 125:fb7e1e4e4d2c

Provide some SCCS markers also to be able to search with "what(1)" also
author Franz Glasner <hg@dom66.de>
date Fri, 17 Aug 2018 09:07:02 +0200
parents a51822141ccf
children 5e502e9dd5e5
line wrap: on
line source

# -*- coding: utf-8 -*-
# @(#) $HGheader$
# $HGnodeid$
#
"""write a revision summary similar to .hg_archival.txt
"""

__revision__ = "$Revision$"

__author__ = "Franz Glasner"


from mercurial.i18n import _
from mercurial import cmdutil, scmutil, util, error, archival


cmdtable = {}

command = cmdutil.command(cmdtable)

testedwith = "3.5 3.5.1 3.5.2 4.3.1 4.3.2 4.4.1 4.4.2 4.5.2 4.6.1"


@command(
    "revinfo",
    [
        ("r", "rev", "",  _('revision to handle'), _('REV')),
        ("", "amend", None, _('amend a given file with path information')),
        ("p", "path", [], _('the configured default path'), _('SOURCE')),
        ("d", "data", [], _("add an extra `KEY: VALUE' pair into the file"), _('KEY=VALUE')),
    ],
    _("hg revinfo [OPTION]... [DEST]"))
def revinfo(ui, repo, dest=None, **opts):
    """write a revision summary

    If DEST is given the info is written into the file DEST. Otherwise
    it is written to stdout.

    By default, the revision used is the parent of the working
    directory; use -r/--rev to specify a different revision.

    Use -p/--path to specify named path information from :hg:`paths`
    as the canonical repository location. It will be printed as "path"
    item to the output. If no path is given then "default" is
    assumed. Use "." to use the current workspace root.
    Disable this by providing an empty SOURCE.

    Use --amend to extend a given file with a "path" item.
    Can also be used to put more than one path item into the file.

    The printed information is the same as the :hg:`archive` command
    writes into ".hg_archival.txt". The "path" info is an extra item.

    """
    if opts.get("amend") and opts.get("rev"):
        raise error.Abort(_("cannot use -r/--rev together with --amend"))

    if opts.get("amend"):
        if not dest:
            raise error.Abort(_('need a destination file with --amend'))
        with open(dest, "rb") as rfile:
            msg = rfile.read()
    else:
        ctx = scmutil.revsingle(repo, opts.get("rev"))
        if not ctx:
            raise error.Abort(
                _("no working directory: please specify a revision"))

        msg = archival.buildmetadata(ctx)

    for kv in opts.get("data"):
        kvparts = [i.strip() for i in kv.split("=", 1)]
        if len(kvparts) == 2:
            msg += "%s: %s\n" % tuple(kvparts)
        else:
            raise error.Abort(
                _("given data `%s' is not a KEY=VALUE pair") % (kv, ))

    paths = opts.get("path")
    #
    # When not amending and no path options is given print the
    # "default" path
    #
    if not opts.get("amend"):
        if not paths:
            paths = ["default"]
    for canonicalpath in paths:
        if canonicalpath:
            if canonicalpath == ".":
                msg += "path: %s\n" % repo.root
            else:
                for name, path in sorted(ui.paths.iteritems()):
                    if name == canonicalpath:
                        msg += "path: %s\n" % util.hidepassword(path.loc)
                        break
                else:
                    msg += "path: %s\n" % canonicalpath
    if dest:
        with open(dest, "wb") as rfile:
            rfile.write(msg)
    else:
        ui.write(msg)