mercurial/state.py
author Taapas Agrawal <taapas2897@gmail.com>
Sun, 09 Jun 2019 01:13:13 +0530
changeset 42538 5bddd2244814
parent 42537 dc3fdd1b5af4
child 42539 12243f15d53e
permissions -rw-r--r--
state: moved cmdutil.STATES and utilities to state.py This commit moves `cmdutil.STATES` and adjoining functions to `state.py`. The existing users are updated accordingly. Tests remain unchanged. Differential Revision: https://phab.mercurial-scm.org/D6502

# state.py - writing and reading state files in Mercurial
#
# Copyright 2018 Pulkit Goyal <pulkitmgoyal@gmail.com>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

"""
This file contains class to wrap the state for commands and other
related logic.

All the data related to the command state is stored as dictionary in the object.
The class has methods using which the data can be stored to disk in a file under
.hg/ directory.

We store the data on disk in cbor, for which we use the CBOR format to encode
the data.
"""

from __future__ import absolute_import

from .i18n import _

from . import (
    error,
    util,
)
from .utils import (
    cborutil,
)

class cmdstate(object):
    """a wrapper class to store the state of commands like `rebase`, `graft`,
    `histedit`, `shelve` etc. Extensions can also use this to write state files.

    All the data for the state is stored in the form of key-value pairs in a
    dictionary.

    The class object can write all the data to a file in .hg/ directory and
    can populate the object data reading that file.

    Uses cbor to serialize and deserialize data while writing and reading from
    disk.
    """

    def __init__(self, repo, fname):
        """ repo is the repo object
        fname is the file name in which data should be stored in .hg directory
        """
        self._repo = repo
        self.fname = fname

    def read(self):
        """read the existing state file and return a dict of data stored"""
        return self._read()

    def save(self, version, data):
        """write all the state data stored to .hg/<filename> file

        we use third-party library cbor to serialize data to write in the file.
        """
        if not isinstance(version, int):
            raise error.ProgrammingError("version of state file should be"
                                         " an integer")

        with self._repo.vfs(self.fname, 'wb', atomictemp=True) as fp:
            fp.write('%d\n' % version)
            for chunk in cborutil.streamencode(data):
                fp.write(chunk)

    def _read(self):
        """reads the state file and returns a dictionary which contain
        data in the same format as it was before storing"""
        with self._repo.vfs(self.fname, 'rb') as fp:
            try:
                int(fp.readline())
            except ValueError:
                raise error.CorruptedState("unknown version of state file"
                                           " found")

            return cborutil.decodeall(fp.read())[0]

    def delete(self):
        """drop the state file if exists"""
        util.unlinkpath(self._repo.vfs.join(self.fname), ignoremissing=True)

    def exists(self):
        """check whether the state file exists or not"""
        return self._repo.vfs.exists(self.fname)

class _statecheck(object):
    """a utility class that deals with multistep operations like graft,
       histedit, bisect, update etc and check whether such commands
       are in an unfinished conditition or not and return appropriate message
       and hint.
       It also has the ability to register and determine the states of any new
       multistep operation or multistep command extension.
    """

    def __init__(self, opname, fname, clearable=False, allowcommit=False,
                 cmdmsg="", cmdhint=""):
        """opname is the name the command or operation
        fname is the file name in which data should be stored in .hg directory.
        It is None for merge command.
        clearable boolean determines whether or not interrupted states can be
        cleared by running `hg update -C .` which in turn deletes the
        state file.
        allowcommit boolean decides whether commit is allowed during interrupted
        state or not.
        cmdmsg is used to pass a different status message in case standard
        message of the format "abort: cmdname in progress" is not desired.
        cmdhint is used to pass a different hint message in case standard
        message of the format use 'hg cmdname --continue' or
        'hg cmdname --abort'" is not desired.
        """
        self._opname = opname
        self._fname = fname
        self._clearable = clearable
        self._allowcommit = allowcommit
        self._cmdhint = cmdhint
        self._cmdmsg = cmdmsg

    def hint(self):
        """returns the hint message corresponding to the command"""
        if not self._cmdhint:
                return (_("use 'hg %s --continue' or 'hg %s --abort'") %
                        (self._opname, self._opname))
        return self._cmdhint

    def msg(self):
        """returns the status message corresponding to the command"""
        if not self._cmdmsg:
            return _('%s in progress') % (self._opname)
        return self._cmdmsg

    def isunfinished(self, repo):
        """determines whether a multi-step operation is in progress or not"""
        return repo.vfs.exists(self._fname)

# A list of statecheck objects for multistep operations like graft.
_unfinishedstates = []

def addunfinished(opname, **kwargs):
    """this registers a new command or operation to unfinishedstates
    """
    statecheckobj = _statecheck(opname, **kwargs)
    _unfinishedstates.append(statecheckobj)

addunfinished(
    'graft', fname='graftstate', clearable=True,
    cmdhint=_("use 'hg graft --continue' or 'hg graft --stop' to stop")
)
addunfinished(
    'update', fname='updatestate', clearable=True,
    cmdmsg=_('last update was interrupted'),
    cmdhint=_("use 'hg update' to get a consistent checkout")
)

def _commentlines(raw):
    '''Surround lineswith a comment char and a new line'''
    lines = raw.splitlines()
    commentedlines = ['# %s' % line for line in lines]
    return '\n'.join(commentedlines) + '\n'

def _helpmessage(continuecmd, abortcmd):
    msg = _('To continue:    %s\n'
            'To abort:       %s') % (continuecmd, abortcmd)
    return _commentlines(msg)

def _rebasemsg():
    return _helpmessage('hg rebase --continue', 'hg rebase --abort')

def _histeditmsg():
    return _helpmessage('hg histedit --continue', 'hg histedit --abort')

def _unshelvemsg():
    return _helpmessage('hg unshelve --continue', 'hg unshelve --abort')

def _graftmsg():
    return _helpmessage('hg graft --continue', 'hg graft --abort')

def _mergemsg():
    return _helpmessage('hg commit', 'hg merge --abort')

def _bisectmsg():
    msg = _('To mark the changeset good:    hg bisect --good\n'
            'To mark the changeset bad:     hg bisect --bad\n'
            'To abort:                      hg bisect --reset\n')
    return _commentlines(msg)

def fileexistspredicate(filename):
    return lambda repo: repo.vfs.exists(filename)

def _mergepredicate(repo):
    return len(repo[None].parents()) > 1

STATES = (
    # (state, predicate to detect states, helpful message function)
    ('histedit', fileexistspredicate('histedit-state'), _histeditmsg),
    ('bisect', fileexistspredicate('bisect.state'), _bisectmsg),
    ('graft', fileexistspredicate('graftstate'), _graftmsg),
    ('unshelve', fileexistspredicate('shelvedstate'), _unshelvemsg),
    ('rebase', fileexistspredicate('rebasestate'), _rebasemsg),
    # The merge state is part of a list that will be iterated over.
    # They need to be last because some of the other unfinished states may also
    # be in a merge or update state (eg. rebase, histedit, graft, etc).
    # We want those to have priority.
    ('merge', _mergepredicate, _mergemsg),
)

def getrepostate(repo):
    # experimental config: commands.status.skipstates
    skip = set(repo.ui.configlist('commands', 'status.skipstates'))
    for state, statedetectionpredicate, msgfn in STATES:
        if state in skip:
            continue
        if statedetectionpredicate(repo):
            return (state, statedetectionpredicate, msgfn)