hgext3rd/evolve/state.py
author Anton Shestakov <av6@dwimlabs.net>
Fri, 08 May 2020 22:50:09 +0800
branchmercurial-4.6
changeset 5368 844b1ad5b34b
parent 4989 25532ce787d9
permissions -rw-r--r--
test-compat: merge mercurial-4.7 into mercurial-4.6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3391
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     1
# This software may be used and distributed according to the terms of the
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     2
# GNU General Public License version 2 or any later version.
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     3
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     4
"""
3457
82e9f9603b1b evolvestate: rename the file to state.py and class name to cmdstate
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3391
diff changeset
     5
This file contains class to wrap the state for commands and other
3391
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     6
related logic.
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     7
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     8
All the data related to the command state is stored as dictionary in the object.
3457
82e9f9603b1b evolvestate: rename the file to state.py and class name to cmdstate
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3391
diff changeset
     9
The class has methods using which the data can be stored to disk in a file under
82e9f9603b1b evolvestate: rename the file to state.py and class name to cmdstate
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3391
diff changeset
    10
.hg/ directory.
3391
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    11
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    12
We store the data on disk in cbor, for which we use cbor library to serialize
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    13
and deserialize data.
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    14
"""
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    15
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    16
from __future__ import absolute_import
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    17
4848
535ab2609e45 cmdstate: introduce a "saver" contextmanager and use it in place of save()
Kyle Lippincott <spectral@google.com>
parents: 4847
diff changeset
    18
import contextlib
3497
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
    19
import errno
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
    20
import struct
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
    21
3391
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    22
from .thirdparty import cbor
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    23
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    24
from mercurial import (
3497
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
    25
    error,
3391
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    26
    util,
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    27
)
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    28
3497
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
    29
from mercurial.i18n import _
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
    30
4846
38ce7fe4d3f2 cmdstate: switch to new-style classes
Kyle Lippincott <spectral@google.com>
parents: 4814
diff changeset
    31
class cmdstate(object):
4054
46cd437fd3d2 pick: fix last references of grab in code
Boris Feld <boris.feld@octobus.net>
parents: 3874
diff changeset
    32
    """a wrapper class to store the state of commands like `evolve`, `pick`
3391
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    33
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    34
    All the data for the state is stored in the form of key-value pairs in a
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    35
    dictionary.
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    36
3457
82e9f9603b1b evolvestate: rename the file to state.py and class name to cmdstate
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3391
diff changeset
    37
    The class object can write all the data to a file in .hg/ directory and also
82e9f9603b1b evolvestate: rename the file to state.py and class name to cmdstate
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3391
diff changeset
    38
    can populate the object data reading that file
3391
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    39
    """
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    40
4847
0fecff9ac36d cmdstate: avoid setting a default argument to a mutable object (`{}`)
Kyle Lippincott <spectral@google.com>
parents: 4846
diff changeset
    41
    def __init__(self, repo, path=b'evolvestate', opts=None):
3391
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    42
        self._repo = repo
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    43
        self.path = path
4847
0fecff9ac36d cmdstate: avoid setting a default argument to a mutable object (`{}`)
Kyle Lippincott <spectral@google.com>
parents: 4846
diff changeset
    44
        if opts is None:
0fecff9ac36d cmdstate: avoid setting a default argument to a mutable object (`{}`)
Kyle Lippincott <spectral@google.com>
parents: 4846
diff changeset
    45
            opts = {}
3391
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    46
        self.opts = opts
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    47
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    48
    def __nonzero__(self):
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    49
        return self.exists()
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    50
4739
ce6e69b2f759 py3: implement __bool__ in addition to __nonzero__
Martin von Zweigbergk <martinvonz@google.com>
parents: 4504
diff changeset
    51
    __bool__ = __nonzero__
ce6e69b2f759 py3: implement __bool__ in addition to __nonzero__
Martin von Zweigbergk <martinvonz@google.com>
parents: 4504
diff changeset
    52
4504
4d8db57e24fc state: implement cmdstate.__contains__()
Anton Shestakov <av6@dwimlabs.net>
parents: 4054
diff changeset
    53
    def __contains__(self, key):
4d8db57e24fc state: implement cmdstate.__contains__()
Anton Shestakov <av6@dwimlabs.net>
parents: 4054
diff changeset
    54
        return key in self.opts
4d8db57e24fc state: implement cmdstate.__contains__()
Anton Shestakov <av6@dwimlabs.net>
parents: 4054
diff changeset
    55
3391
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    56
    def __getitem__(self, key):
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    57
        return self.opts[key]
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    58
3874
830a2597ba26 state: add the get() method that evolvecmd:1836 expects
Martin von Zweigbergk <martinvonz@google.com>
parents: 3572
diff changeset
    59
    def get(self, key, default=None):
830a2597ba26 state: add the get() method that evolvecmd:1836 expects
Martin von Zweigbergk <martinvonz@google.com>
parents: 3572
diff changeset
    60
        return self.opts.get(key, default)
830a2597ba26 state: add the get() method that evolvecmd:1836 expects
Martin von Zweigbergk <martinvonz@google.com>
parents: 3572
diff changeset
    61
3572
7934e9751d69 state: add __setitem__() for cmdstate class
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3498
diff changeset
    62
    def __setitem__(self, key, value):
7934e9751d69 state: add __setitem__() for cmdstate class
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3498
diff changeset
    63
        updates = {key: value}
7934e9751d69 state: add __setitem__() for cmdstate class
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3498
diff changeset
    64
        self.opts.update(updates)
7934e9751d69 state: add __setitem__() for cmdstate class
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3498
diff changeset
    65
3391
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    66
    def load(self):
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    67
        """load the existing evolvestate file into the class object"""
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    68
        op = self._read()
3498
152daa6967af evolve: add comptability to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3497
diff changeset
    69
        if isinstance(op, dict):
152daa6967af evolve: add comptability to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3497
diff changeset
    70
            self.opts.update(op)
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4748
diff changeset
    71
        elif self.path == b'evolvestate':
3498
152daa6967af evolve: add comptability to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3497
diff changeset
    72
            # it is the old evolvestate file
152daa6967af evolve: add comptability to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3497
diff changeset
    73
            oldop = _oldevolvestateread(self._repo)
152daa6967af evolve: add comptability to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3497
diff changeset
    74
            self.opts.update(oldop)
3391
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    75
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    76
    def addopts(self, opts):
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    77
        """add more key-value pairs to the data stored by the object"""
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    78
        self.opts.update(opts)
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    79
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    80
    def save(self):
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    81
        """write all the evolvestate data stored in .hg/evolvestate file
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    82
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    83
        we use third-party library cbor to serialize data to write in the file.
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    84
        """
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4748
diff changeset
    85
        with self._repo.vfs(self.path, b'wb', atomictemp=True) as fp:
3391
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    86
            cbor.dump(self.opts, fp)
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    87
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    88
    def _read(self):
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    89
        """reads the evolvestate file and returns a dictionary which contain
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    90
        data in the same format as it was before storing"""
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4748
diff changeset
    91
        with self._repo.vfs(self.path, b'rb') as fp:
3391
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    92
            return cbor.load(fp)
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    93
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    94
    def delete(self):
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    95
        """drop the evolvestate file if exists"""
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    96
        util.unlinkpath(self._repo.vfs.join(self.path), ignoremissing=True)
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    97
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    98
    def exists(self):
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    99
        """check whether the evolvestate file exists or not"""
d2fc2c2783f8 evolvestate: add a class to wrap the state of `hg evolve` command
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   100
        return self._repo.vfs.exists(self.path)
3497
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   101
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   102
def _oldevolvestateread(repo):
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   103
    """function to read the old evolvestate file
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   104
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   105
    This exists for BC reasons."""
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   106
    try:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4748
diff changeset
   107
        f = repo.vfs(b'evolvestate')
3497
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   108
    except IOError as err:
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   109
        if err.errno != errno.ENOENT:
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   110
            raise
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   111
    try:
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   112
        versionblob = f.read(4)
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   113
        if len(versionblob) < 4:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4748
diff changeset
   114
            repo.ui.debug(b'ignoring corrupted evolvestate (file contains %i bits)'
3497
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   115
                          % len(versionblob))
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   116
            return None
4989
25532ce787d9 state: there's no _unpack in struct
Anton Shestakov <av6@dwimlabs.net>
parents: 4848
diff changeset
   117
        version = struct.unpack(b'>I', versionblob)[0]
3497
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   118
        if version != 0:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4748
diff changeset
   119
            msg = _(b'unknown evolvestate version %i') % version
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4748
diff changeset
   120
            raise error.Abort(msg, hint=_(b'upgrade your evolve'))
3497
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   121
        records = []
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   122
        data = f.read()
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   123
        off = 0
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   124
        end = len(data)
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   125
        while off < end:
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   126
            rtype = data[off]
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   127
            off += 1
4989
25532ce787d9 state: there's no _unpack in struct
Anton Shestakov <av6@dwimlabs.net>
parents: 4848
diff changeset
   128
            length = struct.unpack(b'>I', data[off:(off + 4)])[0]
3497
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   129
            off += 4
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   130
            record = data[off:(off + length)]
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   131
            off += length
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4748
diff changeset
   132
            if rtype == b't':
3497
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   133
                rtype, record = record[0], record[1:]
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   134
            records.append((rtype, record))
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   135
        state = {}
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   136
        for rtype, rdata in records:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4748
diff changeset
   137
            if rtype == b'C':
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4748
diff changeset
   138
                state[b'current'] = rdata
3497
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   139
            elif rtype.lower():
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4748
diff changeset
   140
                repo.ui.debug(b'ignore evolve state record type %s' % rtype)
3497
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   141
            else:
4814
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4748
diff changeset
   142
                raise error.Abort(_(b"unknown evolvestate field type '%s'")
48b30ff742cb python3: use format-source to run byteify-strings in .py files
Raphaël Gomès <rgomes@octobus.net>
parents: 4748
diff changeset
   143
                                  % rtype, hint=_(b'upgrade your evolve'))
3497
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   144
        return state
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   145
    finally:
dae819761c0e state: bring back the function to read old evolvestate files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 3457
diff changeset
   146
        f.close()
4848
535ab2609e45 cmdstate: introduce a "saver" contextmanager and use it in place of save()
Kyle Lippincott <spectral@google.com>
parents: 4847
diff changeset
   147
535ab2609e45 cmdstate: introduce a "saver" contextmanager and use it in place of save()
Kyle Lippincott <spectral@google.com>
parents: 4847
diff changeset
   148
@contextlib.contextmanager
535ab2609e45 cmdstate: introduce a "saver" contextmanager and use it in place of save()
Kyle Lippincott <spectral@google.com>
parents: 4847
diff changeset
   149
def saver(state, opts=None):
535ab2609e45 cmdstate: introduce a "saver" contextmanager and use it in place of save()
Kyle Lippincott <spectral@google.com>
parents: 4847
diff changeset
   150
    """ensure the state is saved on disk during the duration of the context
535ab2609e45 cmdstate: introduce a "saver" contextmanager and use it in place of save()
Kyle Lippincott <spectral@google.com>
parents: 4847
diff changeset
   151
535ab2609e45 cmdstate: introduce a "saver" contextmanager and use it in place of save()
Kyle Lippincott <spectral@google.com>
parents: 4847
diff changeset
   152
    The state is preserved if the context is exited through an exception.
535ab2609e45 cmdstate: introduce a "saver" contextmanager and use it in place of save()
Kyle Lippincott <spectral@google.com>
parents: 4847
diff changeset
   153
    """
535ab2609e45 cmdstate: introduce a "saver" contextmanager and use it in place of save()
Kyle Lippincott <spectral@google.com>
parents: 4847
diff changeset
   154
    if opts:
535ab2609e45 cmdstate: introduce a "saver" contextmanager and use it in place of save()
Kyle Lippincott <spectral@google.com>
parents: 4847
diff changeset
   155
        state.addopts(opts)
535ab2609e45 cmdstate: introduce a "saver" contextmanager and use it in place of save()
Kyle Lippincott <spectral@google.com>
parents: 4847
diff changeset
   156
    state.save()
535ab2609e45 cmdstate: introduce a "saver" contextmanager and use it in place of save()
Kyle Lippincott <spectral@google.com>
parents: 4847
diff changeset
   157
    yield
535ab2609e45 cmdstate: introduce a "saver" contextmanager and use it in place of save()
Kyle Lippincott <spectral@google.com>
parents: 4847
diff changeset
   158
    # delete only if no exception where raised
535ab2609e45 cmdstate: introduce a "saver" contextmanager and use it in place of save()
Kyle Lippincott <spectral@google.com>
parents: 4847
diff changeset
   159
    state.delete()