view mercurial/shelve.py @ 45892:06b64fabf91c

copies: cache the ancestor checking call when tracing copy A good share of the time spent in this function is spent doing ancestors checking. To avoid spending time in duplicated call, we cache the result of calls. In the slower case, this provide a quite significant performance boost. Below are the result for a set of selected pairs (many of them pathological): (And further down is another table that summarize the current state of filelog based vs changeset base copy tracing) The benchmark have been configured to be killed after 6 minutes of runtime, which mean that any detect slower than 2 minutes will be marked as "killed". This drop some useful information about how much slower these case are… but also prevent 99% of the benchmark time to be spent on case that can be labelled "very slow" anyway. Repo Case Source-Rev Dest-Rev Old-Time New-Time Difference Factor ------------------------------------------------------------------------------------------------------------------------------------ mercurial x_revs_x_added_0_copies ad6b123de1c7 39cfcef4f463 : 0.000044 s, 0.000044 s, +0.000000 s, × 1.0000 mercurial x_revs_x_added_x_copies 2b1c78674230 0c1d10351869 : 0.000138 s, 0.000138 s, +0.000000 s, × 1.0000 mercurial x000_revs_x000_added_x_copies 81f8ff2a9bf2 dd3267698d84 : 0.005067 s, 0.005052 s, -0.000015 s, × 0.9970 pypy x_revs_x_added_0_copies aed021ee8ae8 099ed31b181b : 0.000218 s, 0.000219 s, +0.000001 s, × 1.0046 pypy x_revs_x000_added_0_copies 4aa4e1f8e19a 359343b9ac0e : 0.000053 s, 0.000055 s, +0.000002 s, × 1.0377 pypy x_revs_x_added_x_copies ac52eb7bbbb0 72e022663155 : 0.000125 s, 0.000128 s, +0.000003 s, × 1.0240 pypy x_revs_x00_added_x_copies c3b14617fbd7 ace7255d9a26 : 0.001098 s, 0.001089 s, -0.000009 s, × 0.9918 pypy x_revs_x000_added_x000_copies df6f7a526b60 a83dc6a2d56f : 0.017546 s, 0.017407 s, -0.000139 s, × 0.9921 pypy x000_revs_xx00_added_0_copies 89a76aede314 2f22446ff07e : 0.096723 s, 0.094175 s, -0.002548 s, × 0.9737 pypy x000_revs_x000_added_x_copies 8a3b5bfd266e 2c68e87c3efe : 0.271796 s, 0.238009 s, -0.033787 s, × 0.8757 pypy x000_revs_x000_added_x000_copies 89a76aede314 7b3dda341c84 : 0.128602 s, 0.125876 s, -0.002726 s, × 0.9788 pypy x0000_revs_x_added_0_copies d1defd0dc478 c9cb1334cc78 : 7.086742 s, 3.581556 s, -3.505186 s, × 0.5054 pypy x0000_revs_xx000_added_0_copies bf2c629d0071 4ffed77c095c : 0.016634 s, 0.016721 s, +0.000087 s, × 1.0052 pypy x0000_revs_xx000_added_x000_copies 08ea3258278e d9fa043f30c0 : 0.254225 s, 0.242367 s, -0.011858 s, × 0.9534 netbeans x_revs_x_added_0_copies fb0955ffcbcd a01e9239f9e7 : 0.000166 s, 0.000165 s, -0.000001 s, × 0.9940 netbeans x_revs_x000_added_0_copies 6f360122949f 20eb231cc7d0 : 0.000118 s, 0.000114 s, -0.000004 s, × 0.9661 netbeans x_revs_x_added_x_copies 1ada3faf6fb6 5a39d12eecf4 : 0.000296 s, 0.000296 s, +0.000000 s, × 1.0000 netbeans x_revs_x00_added_x_copies 35be93ba1e2c 9eec5e90c05f : 0.001137 s, 0.001124 s, -0.000013 s, × 0.9886 netbeans x000_revs_xx00_added_0_copies eac3045b4fdd 51d4ae7f1290 : 0.014133 s, 0.013060 s, -0.001073 s, × 0.9241 netbeans x000_revs_x000_added_x_copies e2063d266acd 6081d72689dc : 0.016988 s, 0.017112 s, +0.000124 s, × 1.0073 netbeans x000_revs_x000_added_x000_copies ff453e9fee32 411350406ec2 : 0.676361 s, 0.660350 s, -0.016011 s, × 0.9763 netbeans x0000_revs_xx000_added_x000_copies 588c2d1ced70 1aad62e59ddd : 12.515149 s, 10.032499 s, -2.482650 s, × 0.8016 mozilla-central x_revs_x_added_0_copies 3697f962bb7b 7015fcdd43a2 : 0.000186 s, 0.000189 s, +0.000003 s, × 1.0161 mozilla-central x_revs_x000_added_0_copies dd390860c6c9 40d0c5bed75d : 0.000459 s, 0.000462 s, +0.000003 s, × 1.0065 mozilla-central x_revs_x_added_x_copies 8d198483ae3b 14207ffc2b2f : 0.000273 s, 0.000270 s, -0.000003 s, × 0.9890 mozilla-central x_revs_x00_added_x_copies 98cbc58cc6bc 446a150332c3 : 0.001503 s, 0.001474 s, -0.000029 s, × 0.9807 mozilla-central x_revs_x000_added_x000_copies 3c684b4b8f68 0a5e72d1b479 : 0.004862 s, 0.004806 s, -0.000056 s, × 0.9885 mozilla-central x_revs_x0000_added_x0000_copies effb563bb7e5 c07a39dc4e80 : 0.088291 s, 0.085150 s, -0.003141 s, × 0.9644 mozilla-central x000_revs_xx00_added_0_copies 6100d773079a 04a55431795e : 0.007113 s, 0.007064 s, -0.000049 s, × 0.9931 mozilla-central x000_revs_x000_added_x_copies 9f17a6fc04f9 2d37b966abed : 0.004687 s, 0.004741 s, +0.000054 s, × 1.0115 mozilla-central x000_revs_x000_added_x000_copies 7c97034feb78 4407bd0c6330 : 0.198710 s, 0.190133 s, -0.008577 s, × 0.9568 mozilla-central x0000_revs_xx000_added_0_copies 9eec5917337d 67118cc6dcad : 0.036068 s, 0.035651 s, -0.000417 s, × 0.9884 mozilla-central x0000_revs_xx000_added_x000_copies f78c615a656c 96a38b690156 : 0.465362 s, 0.440694 s, -0.024668 s, × 0.9470 mozilla-central x00000_revs_x0000_added_x0000_copies 6832ae71433c 4c222a1d9a00 : 24.519684 s, 18.454163 s, -6.065521 s, × 0.7526 mozilla-central x00000_revs_x00000_added_x000_copies 76caed42cf7c 1daa622bbe42 : 42.711897 s, 31.562719 s, -11.149178 s, × 0.7390 mozilla-try x_revs_x_added_0_copies aaf6dde0deb8 9790f499805a : 0.001201 s, 0.001189 s, -0.000012 s, × 0.9900 mozilla-try x_revs_x000_added_0_copies d8d0222927b4 5bb8ce8c7450 : 0.001216 s, 0.001204 s, -0.000012 s, × 0.9901 mozilla-try x_revs_x_added_x_copies 092fcca11bdb 936255a0384a : 0.000595 s, 0.000586 s, -0.000009 s, × 0.9849 mozilla-try x_revs_x00_added_x_copies b53d2fadbdb5 017afae788ec : 0.001856 s, 0.001845 s, -0.000011 s, × 0.9941 mozilla-try x_revs_x000_added_x000_copies 20408ad61ce5 6f0ee96e21ad : 0.064936 s, 0.063822 s, -0.001114 s, × 0.9828 mozilla-try x_revs_x0000_added_x0000_copies effb563bb7e5 c07a39dc4e80 : 0.090601 s, 0.088038 s, -0.002563 s, × 0.9717 mozilla-try x000_revs_xx00_added_0_copies 6100d773079a 04a55431795e : 0.007510 s, 0.007389 s, -0.000121 s, × 0.9839 mozilla-try x000_revs_x000_added_x_copies 9f17a6fc04f9 2d37b966abed : 0.004911 s, 0.004868 s, -0.000043 s, × 0.9912 mozilla-try x000_revs_x000_added_x000_copies 1346fd0130e4 4c65cbdabc1f : 0.233231 s, 0.222450 s, -0.010781 s, × 0.9538 mozilla-try x0000_revs_x_added_0_copies 63519bfd42ee a36a2a865d92 : 0.419989 s, 0.370675 s, -0.049314 s, × 0.8826 mozilla-try x0000_revs_x_added_x_copies 9fe69ff0762d bcabf2a78927 : 0.401521 s, 0.358020 s, -0.043501 s, × 0.8917 mozilla-try x0000_revs_xx000_added_x_copies 156f6e2674f2 4d0f2c178e66 : 0.179555 s, 0.145235 s, -0.034320 s, × 0.8089 mozilla-try x0000_revs_xx000_added_0_copies 9eec5917337d 67118cc6dcad : 0.038004 s, 0.037606 s, -0.000398 s, × 0.9895 mozilla-try x0000_revs_xx000_added_x000_copies 89294cd501d9 7ccb2fc7ccb5 : 52.838482 s, 7.382439 s, -45.456043 s, × 0.1397 mozilla-try x0000_revs_x0000_added_x0000_copies e928c65095ed e951f4ad123a : 8.705874 s, 7.273506 s, -1.432368 s, × 0.8355 mozilla-try x00000_revs_x00000_added_0_copies dc8a3ca7010e d16fde900c9c : 1.126708 s, 1.074593 s, -0.052115 s, × 0.9537 mozilla-try x00000_revs_x0000_added_x0000_copies 8d3fafa80d4b eb884023b810 : 83.854020 s, 27.746195 s, -56.107825 s, × 0.3309 Below is a table comparing the runtime of the current "filelog centric" algorithm, with the "changeset centric" one, we just modified. The changeset centric algorithm is a significant win in many scenario, but they are still various cases where it is quite slower. When many revision has to be considered the cost of retrieving the copy information, creating new dictionaries, merging dictionaries and checking if revision are ancestors of each other can slow things down. The rest of this series, will introduce a rust version of the copy tracing code to deal with most of theses issues. Repo Case Source-Rev Dest-Rev filelog sidedata Difference Factor --------------------------------------------------------------------------------------------------------------------------------------- mercurial x_revs_x_added_0_copies ad6b123de1c7 39cfcef4f463 : 0.000914 s, 0.000044 s, - 0.000870 s, × 0.048140 mercurial x_revs_x_added_x_copies 2b1c78674230 0c1d10351869 : 0.001812 s, 0.000138 s, - 0.001674 s, × 0.076159 mercurial x000_revs_x000_added_x_copies 81f8ff2a9bf2 dd3267698d84 : 0.017954 s, 0.005052 s, - 0.012902 s, × 0.281386 pypy x_revs_x_added_0_copies aed021ee8ae8 099ed31b181b : 0.001509 s, 0.000219 s, - 0.001290 s, × 0.145129 pypy x_revs_x000_added_0_copies 4aa4e1f8e19a 359343b9ac0e : 0.206881 s, 0.000055 s, - 0.206826 s, × 0.000266 pypy x_revs_x_added_x_copies ac52eb7bbbb0 72e022663155 : 0.016951 s, 0.000128 s, - 0.016823 s, × 0.007551 pypy x_revs_x00_added_x_copies c3b14617fbd7 ace7255d9a26 : 0.019096 s, 0.001089 s, - 0.018007 s, × 0.057028 pypy x_revs_x000_added_x000_copies df6f7a526b60 a83dc6a2d56f : 0.762506 s, 0.017407 s, - 0.745099 s, × 0.022829 pypy x000_revs_xx00_added_0_copies 89a76aede314 2f22446ff07e : 1.179211 s, 0.094175 s, - 1.085036 s, × 0.079863 pypy x000_revs_x000_added_x_copies 8a3b5bfd266e 2c68e87c3efe : 1.249058 s, 0.238009 s, - 1.011049 s, × 0.190551 pypy x000_revs_x000_added_x000_copies 89a76aede314 7b3dda341c84 : 1.614107 s, 0.125876 s, - 1.488231 s, × 0.077985 pypy x0000_revs_x_added_0_copies d1defd0dc478 c9cb1334cc78 : 0.001064 s, 3.581556 s, + 3.580492 s, × 3366.124060 pypy x0000_revs_xx000_added_0_copies bf2c629d0071 4ffed77c095c : 1.061275 s, 0.016721 s, - 1.044554 s, × 0.015756 pypy x0000_revs_xx000_added_x000_copies 08ea3258278e d9fa043f30c0 : 1.341119 s, 0.242367 s, - 1.098752 s, × 0.180720 netbeans x_revs_x_added_0_copies fb0955ffcbcd a01e9239f9e7 : 0.027803 s, 0.000165 s, - 0.027638 s, × 0.005935 netbeans x_revs_x000_added_0_copies 6f360122949f 20eb231cc7d0 : 0.130014 s, 0.000114 s, - 0.129900 s, × 0.000877 netbeans x_revs_x_added_x_copies 1ada3faf6fb6 5a39d12eecf4 : 0.024990 s, 0.000296 s, - 0.024694 s, × 0.011845 netbeans x_revs_x00_added_x_copies 35be93ba1e2c 9eec5e90c05f : 0.052201 s, 0.001124 s, - 0.051077 s, × 0.021532 netbeans x000_revs_xx00_added_0_copies eac3045b4fdd 51d4ae7f1290 : 0.037642 s, 0.013060 s, - 0.024582 s, × 0.346953 netbeans x000_revs_x000_added_x_copies e2063d266acd 6081d72689dc : 0.197086 s, 0.017112 s, - 0.179974 s, × 0.086825 netbeans x000_revs_x000_added_x000_copies ff453e9fee32 411350406ec2 : 0.935148 s, 0.660350 s, - 0.274798 s, × 0.706145 netbeans x0000_revs_xx000_added_x000_copies 588c2d1ced70 1aad62e59ddd : 3.920674 s, 10.032499 s, + 6.111825 s, × 2.558871 mozilla-central x_revs_x_added_0_copies 3697f962bb7b 7015fcdd43a2 : 0.024232 s, 0.000189 s, - 0.024043 s, × 0.007800 mozilla-central x_revs_x000_added_0_copies dd390860c6c9 40d0c5bed75d : 0.141483 s, 0.000462 s, - 0.141021 s, × 0.003265 mozilla-central x_revs_x_added_x_copies 8d198483ae3b 14207ffc2b2f : 0.025775 s, 0.000270 s, - 0.025505 s, × 0.010475 mozilla-central x_revs_x00_added_x_copies 98cbc58cc6bc 446a150332c3 : 0.084922 s, 0.001474 s, - 0.083448 s, × 0.017357 mozilla-central x_revs_x000_added_x000_copies 3c684b4b8f68 0a5e72d1b479 : 0.194784 s, 0.004806 s, - 0.189978 s, × 0.024673 mozilla-central x_revs_x0000_added_x0000_copies effb563bb7e5 c07a39dc4e80 : 2.161103 s, 0.085150 s, - 2.075953 s, × 0.039401 mozilla-central x000_revs_xx00_added_0_copies 6100d773079a 04a55431795e : 0.089347 s, 0.007064 s, - 0.082283 s, × 0.079063 mozilla-central x000_revs_x000_added_x_copies 9f17a6fc04f9 2d37b966abed : 0.732171 s, 0.004741 s, - 0.727430 s, × 0.006475 mozilla-central x000_revs_x000_added_x000_copies 7c97034feb78 4407bd0c6330 : 1.157287 s, 0.190133 s, - 0.967154 s, × 0.164292 mozilla-central x0000_revs_xx000_added_0_copies 9eec5917337d 67118cc6dcad : 6.726568 s, 0.035651 s, - 6.690917 s, × 0.005300 mozilla-central x0000_revs_xx000_added_x000_copies f78c615a656c 96a38b690156 : 3.266229 s, 0.440694 s, - 2.825535 s, × 0.134924 mozilla-central x00000_revs_x0000_added_x0000_copies 6832ae71433c 4c222a1d9a00 : 15.860534 s, 18.454163 s, + 2.593629 s, × 1.163527 mozilla-central x00000_revs_x00000_added_x000_copies 76caed42cf7c 1daa622bbe42 : 20.450475 s, 31.562719 s, +11.112244 s, × 1.543373 mozilla-try x_revs_x_added_0_copies aaf6dde0deb8 9790f499805a : 0.080442 s, 0.001189 s, - 0.079253 s, × 0.014781 mozilla-try x_revs_x000_added_0_copies d8d0222927b4 5bb8ce8c7450 : 0.497672 s, 0.001204 s, - 0.496468 s, × 0.002419 mozilla-try x_revs_x_added_x_copies 092fcca11bdb 936255a0384a : 0.021183 s, 0.000586 s, - 0.020597 s, × 0.027664 mozilla-try x_revs_x00_added_x_copies b53d2fadbdb5 017afae788ec : 0.230991 s, 0.001845 s, - 0.229146 s, × 0.007987 mozilla-try x_revs_x000_added_x000_copies 20408ad61ce5 6f0ee96e21ad : 1.118461 s, 0.063822 s, - 1.054639 s, × 0.057062 mozilla-try x_revs_x0000_added_x0000_copies effb563bb7e5 c07a39dc4e80 : 2.206083 s, 0.088038 s, - 2.118045 s, × 0.039907 mozilla-try x000_revs_xx00_added_0_copies 6100d773079a 04a55431795e : 0.089404 s, 0.007389 s, - 0.082015 s, × 0.082647 mozilla-try x000_revs_x000_added_x_copies 9f17a6fc04f9 2d37b966abed : 0.733043 s, 0.004868 s, - 0.728175 s, × 0.006641 mozilla-try x000_revs_x000_added_x000_copies 1346fd0130e4 4c65cbdabc1f : 1.163367 s, 0.222450 s, - 0.940917 s, × 0.191212 mozilla-try x0000_revs_x_added_0_copies 63519bfd42ee a36a2a865d92 : 0.085456 s, 0.370675 s, + 0.285219 s, × 4.337612 mozilla-try x0000_revs_x_added_x_copies 9fe69ff0762d bcabf2a78927 : 0.083601 s, 0.358020 s, + 0.274419 s, × 4.282485 mozilla-try x0000_revs_xx000_added_x_copies 156f6e2674f2 4d0f2c178e66 : 7.366614 s, 0.145235 s, - 7.221379 s, × 0.019715 mozilla-try x0000_revs_xx000_added_0_copies 9eec5917337d 67118cc6dcad : 6.664464 s, 0.037606 s, - 6.626858 s, × 0.005643 mozilla-try x0000_revs_xx000_added_x000_copies 89294cd501d9 7ccb2fc7ccb5 : 7.467836 s, 7.382439 s, - 0.085397 s, × 0.988565 mozilla-try x0000_revs_x0000_added_x0000_copies e928c65095ed e951f4ad123a : 9.801294 s, 7.273506 s, - 2.527788 s, × 0.742097 mozilla-try x00000_revs_x_added_0_copies 6a320851d377 1ebb79acd503 : 0.091886 s, killed mozilla-try x00000_revs_x00000_added_0_copies dc8a3ca7010e d16fde900c9c : 26.491140 s, 1.074593 s, -25.416547 s, × 0.040564 mozilla-try x00000_revs_x_added_x_copies 5173c4b6f97c 95d83ee7242d : 0.092863 s, killed mozilla-try x00000_revs_x000_added_x_copies 9126823d0e9c ca82787bb23c : 0.226823 s, killed mozilla-try x00000_revs_x0000_added_x0000_copies 8d3fafa80d4b eb884023b810 : 18.914630 s, 27.746195 s, + 8.831565 s, × 1.466917 mozilla-try x00000_revs_x00000_added_x0000_copies 1b661134e2ca 1ae03d022d6d : 21.198903 s, killed mozilla-try x00000_revs_x00000_added_x000_copies 9b2a99adc05e 8e29777b48e6 : 24.952268 s, killed Differential Revision: https://phab.mercurial-scm.org/D9296
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Mon, 02 Nov 2020 11:03:56 +0100
parents 073bb7563931
children 89a2afe31e82
line wrap: on
line source

# shelve.py - save/restore working directory state
#
# Copyright 2013 Facebook, Inc.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.

"""save and restore changes to the working directory

The "hg shelve" command saves changes made to the working directory
and reverts those changes, resetting the working directory to a clean
state.

Later on, the "hg unshelve" command restores the changes saved by "hg
shelve". Changes can be restored even after updating to a different
parent, in which case Mercurial's merge machinery will resolve any
conflicts if necessary.

You can have more than one shelved change outstanding at a time; each
shelved change has a distinct name. For details, see the help for "hg
shelve".
"""
from __future__ import absolute_import

import collections
import errno
import itertools
import stat

from .i18n import _
from .pycompat import open
from . import (
    bookmarks,
    bundle2,
    bundlerepo,
    changegroup,
    cmdutil,
    discovery,
    error,
    exchange,
    hg,
    lock as lockmod,
    mdiff,
    merge,
    mergestate as mergestatemod,
    node as nodemod,
    patch,
    phases,
    pycompat,
    repair,
    scmutil,
    templatefilters,
    util,
    vfs as vfsmod,
)
from .utils import (
    dateutil,
    stringutil,
)

backupdir = b'shelve-backup'
shelvedir = b'shelved'
shelvefileextensions = [b'hg', b'patch', b'shelve']
# universal extension is present in all types of shelves
patchextension = b'patch'

# we never need the user, so we use a
# generic user for all shelve operations
shelveuser = b'shelve@localhost'


class shelvedfile(object):
    """Helper for the file storing a single shelve

    Handles common functions on shelve files (.hg/.patch) using
    the vfs layer"""

    def __init__(self, repo, name, filetype=None):
        self.repo = repo
        self.name = name
        self.vfs = vfsmod.vfs(repo.vfs.join(shelvedir))
        self.backupvfs = vfsmod.vfs(repo.vfs.join(backupdir))
        self.ui = self.repo.ui
        if filetype:
            self.fname = name + b'.' + filetype
        else:
            self.fname = name

    def exists(self):
        return self.vfs.exists(self.fname)

    def filename(self):
        return self.vfs.join(self.fname)

    def backupfilename(self):
        def gennames(base):
            yield base
            base, ext = base.rsplit(b'.', 1)
            for i in itertools.count(1):
                yield b'%s-%d.%s' % (base, i, ext)

        name = self.backupvfs.join(self.fname)
        for n in gennames(name):
            if not self.backupvfs.exists(n):
                return n

    def movetobackup(self):
        if not self.backupvfs.isdir():
            self.backupvfs.makedir()
        util.rename(self.filename(), self.backupfilename())

    def stat(self):
        return self.vfs.stat(self.fname)

    def opener(self, mode=b'rb'):
        try:
            return self.vfs(self.fname, mode)
        except IOError as err:
            if err.errno != errno.ENOENT:
                raise
            raise error.Abort(_(b"shelved change '%s' not found") % self.name)

    def applybundle(self, tr):
        fp = self.opener()
        try:
            targetphase = phases.internal
            if not phases.supportinternal(self.repo):
                targetphase = phases.secret
            gen = exchange.readbundle(self.repo.ui, fp, self.fname, self.vfs)
            pretip = self.repo[b'tip']
            bundle2.applybundle(
                self.repo,
                gen,
                tr,
                source=b'unshelve',
                url=b'bundle:' + self.vfs.join(self.fname),
                targetphase=targetphase,
            )
            shelvectx = self.repo[b'tip']
            if pretip == shelvectx:
                shelverev = tr.changes[b'revduplicates'][-1]
                shelvectx = self.repo[shelverev]
            return shelvectx
        finally:
            fp.close()

    def bundlerepo(self):
        path = self.vfs.join(self.fname)
        return bundlerepo.instance(
            self.repo.baseui, b'bundle://%s+%s' % (self.repo.root, path), False
        )

    def writebundle(self, bases, node):
        cgversion = changegroup.safeversion(self.repo)
        if cgversion == b'01':
            btype = b'HG10BZ'
            compression = None
        else:
            btype = b'HG20'
            compression = b'BZ'

        repo = self.repo.unfiltered()

        outgoing = discovery.outgoing(
            repo, missingroots=bases, ancestorsof=[node]
        )
        cg = changegroup.makechangegroup(repo, outgoing, cgversion, b'shelve')

        bundle2.writebundle(
            self.ui, cg, self.fname, btype, self.vfs, compression=compression
        )

    def writeinfo(self, info):
        scmutil.simplekeyvaluefile(self.vfs, self.fname).write(info)

    def readinfo(self):
        return scmutil.simplekeyvaluefile(self.vfs, self.fname).read()


class shelvedstate(object):
    """Handle persistence during unshelving operations.

    Handles saving and restoring a shelved state. Ensures that different
    versions of a shelved state are possible and handles them appropriately.
    """

    _version = 2
    _filename = b'shelvedstate'
    _keep = b'keep'
    _nokeep = b'nokeep'
    # colon is essential to differentiate from a real bookmark name
    _noactivebook = b':no-active-bookmark'
    _interactive = b'interactive'

    @classmethod
    def _verifyandtransform(cls, d):
        """Some basic shelvestate syntactic verification and transformation"""
        try:
            d[b'originalwctx'] = nodemod.bin(d[b'originalwctx'])
            d[b'pendingctx'] = nodemod.bin(d[b'pendingctx'])
            d[b'parents'] = [nodemod.bin(h) for h in d[b'parents'].split(b' ')]
            d[b'nodestoremove'] = [
                nodemod.bin(h) for h in d[b'nodestoremove'].split(b' ')
            ]
        except (ValueError, TypeError, KeyError) as err:
            raise error.CorruptedState(pycompat.bytestr(err))

    @classmethod
    def _getversion(cls, repo):
        """Read version information from shelvestate file"""
        fp = repo.vfs(cls._filename)
        try:
            version = int(fp.readline().strip())
        except ValueError as err:
            raise error.CorruptedState(pycompat.bytestr(err))
        finally:
            fp.close()
        return version

    @classmethod
    def _readold(cls, repo):
        """Read the old position-based version of a shelvestate file"""
        # Order is important, because old shelvestate file uses it
        # to detemine values of fields (i.g. name is on the second line,
        # originalwctx is on the third and so forth). Please do not change.
        keys = [
            b'version',
            b'name',
            b'originalwctx',
            b'pendingctx',
            b'parents',
            b'nodestoremove',
            b'branchtorestore',
            b'keep',
            b'activebook',
        ]
        # this is executed only seldomly, so it is not a big deal
        # that we open this file twice
        fp = repo.vfs(cls._filename)
        d = {}
        try:
            for key in keys:
                d[key] = fp.readline().strip()
        finally:
            fp.close()
        return d

    @classmethod
    def load(cls, repo):
        version = cls._getversion(repo)
        if version < cls._version:
            d = cls._readold(repo)
        elif version == cls._version:
            d = scmutil.simplekeyvaluefile(repo.vfs, cls._filename).read(
                firstlinenonkeyval=True
            )
        else:
            raise error.Abort(
                _(
                    b'this version of shelve is incompatible '
                    b'with the version used in this repo'
                )
            )

        cls._verifyandtransform(d)
        try:
            obj = cls()
            obj.name = d[b'name']
            obj.wctx = repo[d[b'originalwctx']]
            obj.pendingctx = repo[d[b'pendingctx']]
            obj.parents = d[b'parents']
            obj.nodestoremove = d[b'nodestoremove']
            obj.branchtorestore = d.get(b'branchtorestore', b'')
            obj.keep = d.get(b'keep') == cls._keep
            obj.activebookmark = b''
            if d.get(b'activebook', b'') != cls._noactivebook:
                obj.activebookmark = d.get(b'activebook', b'')
            obj.interactive = d.get(b'interactive') == cls._interactive
        except (error.RepoLookupError, KeyError) as err:
            raise error.CorruptedState(pycompat.bytestr(err))

        return obj

    @classmethod
    def save(
        cls,
        repo,
        name,
        originalwctx,
        pendingctx,
        nodestoremove,
        branchtorestore,
        keep=False,
        activebook=b'',
        interactive=False,
    ):
        info = {
            b"name": name,
            b"originalwctx": nodemod.hex(originalwctx.node()),
            b"pendingctx": nodemod.hex(pendingctx.node()),
            b"parents": b' '.join(
                [nodemod.hex(p) for p in repo.dirstate.parents()]
            ),
            b"nodestoremove": b' '.join(
                [nodemod.hex(n) for n in nodestoremove]
            ),
            b"branchtorestore": branchtorestore,
            b"keep": cls._keep if keep else cls._nokeep,
            b"activebook": activebook or cls._noactivebook,
        }
        if interactive:
            info[b'interactive'] = cls._interactive
        scmutil.simplekeyvaluefile(repo.vfs, cls._filename).write(
            info, firstline=(b"%d" % cls._version)
        )

    @classmethod
    def clear(cls, repo):
        repo.vfs.unlinkpath(cls._filename, ignoremissing=True)


def cleanupoldbackups(repo):
    vfs = vfsmod.vfs(repo.vfs.join(backupdir))
    maxbackups = repo.ui.configint(b'shelve', b'maxbackups')
    hgfiles = [f for f in vfs.listdir() if f.endswith(b'.' + patchextension)]
    hgfiles = sorted([(vfs.stat(f)[stat.ST_MTIME], f) for f in hgfiles])
    if maxbackups > 0 and maxbackups < len(hgfiles):
        bordermtime = hgfiles[-maxbackups][0]
    else:
        bordermtime = None
    for mtime, f in hgfiles[: len(hgfiles) - maxbackups]:
        if mtime == bordermtime:
            # keep it, because timestamp can't decide exact order of backups
            continue
        base = f[: -(1 + len(patchextension))]
        for ext in shelvefileextensions:
            vfs.tryunlink(base + b'.' + ext)


def _backupactivebookmark(repo):
    activebookmark = repo._activebookmark
    if activebookmark:
        bookmarks.deactivate(repo)
    return activebookmark


def _restoreactivebookmark(repo, mark):
    if mark:
        bookmarks.activate(repo, mark)


def _aborttransaction(repo, tr):
    '''Abort current transaction for shelve/unshelve, but keep dirstate
    '''
    dirstatebackupname = b'dirstate.shelve'
    repo.dirstate.savebackup(tr, dirstatebackupname)
    tr.abort()
    repo.dirstate.restorebackup(None, dirstatebackupname)


def getshelvename(repo, parent, opts):
    """Decide on the name this shelve is going to have"""

    def gennames():
        yield label
        for i in itertools.count(1):
            yield b'%s-%02d' % (label, i)

    name = opts.get(b'name')
    label = repo._activebookmark or parent.branch() or b'default'
    # slashes aren't allowed in filenames, therefore we rename it
    label = label.replace(b'/', b'_')
    label = label.replace(b'\\', b'_')
    # filenames must not start with '.' as it should not be hidden
    if label.startswith(b'.'):
        label = label.replace(b'.', b'_', 1)

    if name:
        if shelvedfile(repo, name, patchextension).exists():
            e = _(b"a shelved change named '%s' already exists") % name
            raise error.Abort(e)

        # ensure we are not creating a subdirectory or a hidden file
        if b'/' in name or b'\\' in name:
            raise error.Abort(
                _(b'shelved change names can not contain slashes')
            )
        if name.startswith(b'.'):
            raise error.Abort(_(b"shelved change names can not start with '.'"))

    else:
        for n in gennames():
            if not shelvedfile(repo, n, patchextension).exists():
                name = n
                break

    return name


def mutableancestors(ctx):
    """return all mutable ancestors for ctx (included)

    Much faster than the revset ancestors(ctx) & draft()"""
    seen = {nodemod.nullrev}
    visit = collections.deque()
    visit.append(ctx)
    while visit:
        ctx = visit.popleft()
        yield ctx.node()
        for parent in ctx.parents():
            rev = parent.rev()
            if rev not in seen:
                seen.add(rev)
                if parent.mutable():
                    visit.append(parent)


def getcommitfunc(extra, interactive, editor=False):
    def commitfunc(ui, repo, message, match, opts):
        hasmq = util.safehasattr(repo, b'mq')
        if hasmq:
            saved, repo.mq.checkapplied = repo.mq.checkapplied, False

        targetphase = phases.internal
        if not phases.supportinternal(repo):
            targetphase = phases.secret
        overrides = {(b'phases', b'new-commit'): targetphase}
        try:
            editor_ = False
            if editor:
                editor_ = cmdutil.getcommiteditor(
                    editform=b'shelve.shelve', **pycompat.strkwargs(opts)
                )
            with repo.ui.configoverride(overrides):
                return repo.commit(
                    message,
                    shelveuser,
                    opts.get(b'date'),
                    match,
                    editor=editor_,
                    extra=extra,
                )
        finally:
            if hasmq:
                repo.mq.checkapplied = saved

    def interactivecommitfunc(ui, repo, *pats, **opts):
        opts = pycompat.byteskwargs(opts)
        match = scmutil.match(repo[b'.'], pats, {})
        message = opts[b'message']
        return commitfunc(ui, repo, message, match, opts)

    return interactivecommitfunc if interactive else commitfunc


def _nothingtoshelvemessaging(ui, repo, pats, opts):
    stat = repo.status(match=scmutil.match(repo[None], pats, opts))
    if stat.deleted:
        ui.status(
            _(b"nothing changed (%d missing files, see 'hg status')\n")
            % len(stat.deleted)
        )
    else:
        ui.status(_(b"nothing changed\n"))


def _shelvecreatedcommit(repo, node, name, match):
    info = {b'node': nodemod.hex(node)}
    shelvedfile(repo, name, b'shelve').writeinfo(info)
    bases = list(mutableancestors(repo[node]))
    shelvedfile(repo, name, b'hg').writebundle(bases, node)
    with shelvedfile(repo, name, patchextension).opener(b'wb') as fp:
        cmdutil.exportfile(
            repo, [node], fp, opts=mdiff.diffopts(git=True), match=match
        )


def _includeunknownfiles(repo, pats, opts, extra):
    s = repo.status(match=scmutil.match(repo[None], pats, opts), unknown=True)
    if s.unknown:
        extra[b'shelve_unknown'] = b'\0'.join(s.unknown)
        repo[None].add(s.unknown)


def _finishshelve(repo, tr):
    if phases.supportinternal(repo):
        tr.close()
    else:
        _aborttransaction(repo, tr)


def createcmd(ui, repo, pats, opts):
    """subcommand that creates a new shelve"""
    with repo.wlock():
        cmdutil.checkunfinished(repo)
        return _docreatecmd(ui, repo, pats, opts)


def _docreatecmd(ui, repo, pats, opts):
    wctx = repo[None]
    parents = wctx.parents()
    parent = parents[0]
    origbranch = wctx.branch()

    if parent.node() != nodemod.nullid:
        desc = b"changes to: %s" % parent.description().split(b'\n', 1)[0]
    else:
        desc = b'(changes in empty repository)'

    if not opts.get(b'message'):
        opts[b'message'] = desc

    lock = tr = activebookmark = None
    try:
        lock = repo.lock()

        # use an uncommitted transaction to generate the bundle to avoid
        # pull races. ensure we don't print the abort message to stderr.
        tr = repo.transaction(b'shelve', report=lambda x: None)

        interactive = opts.get(b'interactive', False)
        includeunknown = opts.get(b'unknown', False) and not opts.get(
            b'addremove', False
        )

        name = getshelvename(repo, parent, opts)
        activebookmark = _backupactivebookmark(repo)
        extra = {b'internal': b'shelve'}
        if includeunknown:
            _includeunknownfiles(repo, pats, opts, extra)

        if _iswctxonnewbranch(repo) and not _isbareshelve(pats, opts):
            # In non-bare shelve we don't store newly created branch
            # at bundled commit
            repo.dirstate.setbranch(repo[b'.'].branch())

        commitfunc = getcommitfunc(extra, interactive, editor=True)
        if not interactive:
            node = cmdutil.commit(ui, repo, commitfunc, pats, opts)
        else:
            node = cmdutil.dorecord(
                ui,
                repo,
                commitfunc,
                None,
                False,
                cmdutil.recordfilter,
                *pats,
                **pycompat.strkwargs(opts)
            )
        if not node:
            _nothingtoshelvemessaging(ui, repo, pats, opts)
            return 1

        # Create a matcher so that prefetch doesn't attempt to fetch
        # the entire repository pointlessly, and as an optimisation
        # for movedirstate, if needed.
        match = scmutil.matchfiles(repo, repo[node].files())
        _shelvecreatedcommit(repo, node, name, match)

        ui.status(_(b'shelved as %s\n') % name)
        if opts[b'keep']:
            with repo.dirstate.parentchange():
                scmutil.movedirstate(repo, parent, match)
        else:
            hg.update(repo, parent.node())
            ms = mergestatemod.mergestate.read(repo)
            if not ms.unresolvedcount():
                ms.reset()

        if origbranch != repo[b'.'].branch() and not _isbareshelve(pats, opts):
            repo.dirstate.setbranch(origbranch)

        _finishshelve(repo, tr)
    finally:
        _restoreactivebookmark(repo, activebookmark)
        lockmod.release(tr, lock)


def _isbareshelve(pats, opts):
    return (
        not pats
        and not opts.get(b'interactive', False)
        and not opts.get(b'include', False)
        and not opts.get(b'exclude', False)
    )


def _iswctxonnewbranch(repo):
    return repo[None].branch() != repo[b'.'].branch()


def cleanupcmd(ui, repo):
    """subcommand that deletes all shelves"""

    with repo.wlock():
        for (name, _type) in repo.vfs.readdir(shelvedir):
            suffix = name.rsplit(b'.', 1)[-1]
            if suffix in shelvefileextensions:
                shelvedfile(repo, name).movetobackup()
            cleanupoldbackups(repo)


def deletecmd(ui, repo, pats):
    """subcommand that deletes a specific shelve"""
    if not pats:
        raise error.Abort(_(b'no shelved changes specified!'))
    with repo.wlock():
        for name in pats:
            try:
                for suffix in shelvefileextensions:
                    shfile = shelvedfile(repo, name, suffix)
                    # patch file is necessary, as it should
                    # be present for any kind of shelve,
                    # but the .hg file is optional as in future we
                    # will add obsolete shelve with does not create a
                    # bundle
                    if shfile.exists() or suffix == patchextension:
                        shfile.movetobackup()
            except OSError as err:
                if err.errno != errno.ENOENT:
                    raise
                raise error.Abort(_(b"shelved change '%s' not found") % name)
            cleanupoldbackups(repo)


def listshelves(repo):
    """return all shelves in repo as list of (time, filename)"""
    try:
        names = repo.vfs.readdir(shelvedir)
    except OSError as err:
        if err.errno != errno.ENOENT:
            raise
        return []
    info = []
    for (name, _type) in names:
        pfx, sfx = name.rsplit(b'.', 1)
        if not pfx or sfx != patchextension:
            continue
        st = shelvedfile(repo, name).stat()
        info.append((st[stat.ST_MTIME], shelvedfile(repo, pfx).filename()))
    return sorted(info, reverse=True)


def listcmd(ui, repo, pats, opts):
    """subcommand that displays the list of shelves"""
    pats = set(pats)
    width = 80
    if not ui.plain():
        width = ui.termwidth()
    namelabel = b'shelve.newest'
    ui.pager(b'shelve')
    for mtime, name in listshelves(repo):
        sname = util.split(name)[1]
        if pats and sname not in pats:
            continue
        ui.write(sname, label=namelabel)
        namelabel = b'shelve.name'
        if ui.quiet:
            ui.write(b'\n')
            continue
        ui.write(b' ' * (16 - len(sname)))
        used = 16
        date = dateutil.makedate(mtime)
        age = b'(%s)' % templatefilters.age(date, abbrev=True)
        ui.write(age, label=b'shelve.age')
        ui.write(b' ' * (12 - len(age)))
        used += 12
        with open(name + b'.' + patchextension, b'rb') as fp:
            while True:
                line = fp.readline()
                if not line:
                    break
                if not line.startswith(b'#'):
                    desc = line.rstrip()
                    if ui.formatted():
                        desc = stringutil.ellipsis(desc, width - used)
                    ui.write(desc)
                    break
            ui.write(b'\n')
            if not (opts[b'patch'] or opts[b'stat']):
                continue
            difflines = fp.readlines()
            if opts[b'patch']:
                for chunk, label in patch.difflabel(iter, difflines):
                    ui.write(chunk, label=label)
            if opts[b'stat']:
                for chunk, label in patch.diffstatui(difflines, width=width):
                    ui.write(chunk, label=label)


def patchcmds(ui, repo, pats, opts):
    """subcommand that displays shelves"""
    if len(pats) == 0:
        shelves = listshelves(repo)
        if not shelves:
            raise error.Abort(_(b"there are no shelves to show"))
        mtime, name = shelves[0]
        sname = util.split(name)[1]
        pats = [sname]

    for shelfname in pats:
        if not shelvedfile(repo, shelfname, patchextension).exists():
            raise error.Abort(_(b"cannot find shelf %s") % shelfname)

    listcmd(ui, repo, pats, opts)


def checkparents(repo, state):
    """check parent while resuming an unshelve"""
    if state.parents != repo.dirstate.parents():
        raise error.Abort(
            _(b'working directory parents do not match unshelve state')
        )


def _loadshelvedstate(ui, repo, opts):
    try:
        state = shelvedstate.load(repo)
        if opts.get(b'keep') is None:
            opts[b'keep'] = state.keep
    except IOError as err:
        if err.errno != errno.ENOENT:
            raise
        cmdutil.wrongtooltocontinue(repo, _(b'unshelve'))
    except error.CorruptedState as err:
        ui.debug(pycompat.bytestr(err) + b'\n')
        if opts.get(b'continue'):
            msg = _(b'corrupted shelved state file')
            hint = _(
                b'please run hg unshelve --abort to abort unshelve '
                b'operation'
            )
            raise error.Abort(msg, hint=hint)
        elif opts.get(b'abort'):
            shelvedstate.clear(repo)
            raise error.Abort(
                _(
                    b'could not read shelved state file, your '
                    b'working copy may be in an unexpected state\n'
                    b'please update to some commit\n'
                )
            )
    return state


def unshelveabort(ui, repo, state):
    """subcommand that abort an in-progress unshelve"""
    with repo.lock():
        try:
            checkparents(repo, state)

            merge.clean_update(state.pendingctx)
            if state.activebookmark and state.activebookmark in repo._bookmarks:
                bookmarks.activate(repo, state.activebookmark)
            mergefiles(ui, repo, state.wctx, state.pendingctx)
            if not phases.supportinternal(repo):
                repair.strip(
                    ui, repo, state.nodestoremove, backup=False, topic=b'shelve'
                )
        finally:
            shelvedstate.clear(repo)
            ui.warn(_(b"unshelve of '%s' aborted\n") % state.name)


def hgabortunshelve(ui, repo):
    """logic to  abort unshelve using 'hg abort"""
    with repo.wlock():
        state = _loadshelvedstate(ui, repo, {b'abort': True})
        return unshelveabort(ui, repo, state)


def mergefiles(ui, repo, wctx, shelvectx):
    """updates to wctx and merges the changes from shelvectx into the
    dirstate."""
    with ui.configoverride({(b'ui', b'quiet'): True}):
        hg.update(repo, wctx.node())
        ui.pushbuffer(True)
        cmdutil.revert(ui, repo, shelvectx)
        ui.popbuffer()


def restorebranch(ui, repo, branchtorestore):
    if branchtorestore and branchtorestore != repo.dirstate.branch():
        repo.dirstate.setbranch(branchtorestore)
        ui.status(
            _(b'marked working directory as branch %s\n') % branchtorestore
        )


def unshelvecleanup(ui, repo, name, opts):
    """remove related files after an unshelve"""
    if not opts.get(b'keep'):
        for filetype in shelvefileextensions:
            shfile = shelvedfile(repo, name, filetype)
            if shfile.exists():
                shfile.movetobackup()
        cleanupoldbackups(repo)


def unshelvecontinue(ui, repo, state, opts):
    """subcommand to continue an in-progress unshelve"""
    # We're finishing off a merge. First parent is our original
    # parent, second is the temporary "fake" commit we're unshelving.
    interactive = state.interactive
    basename = state.name
    with repo.lock():
        checkparents(repo, state)
        ms = mergestatemod.mergestate.read(repo)
        if list(ms.unresolved()):
            raise error.Abort(
                _(b"unresolved conflicts, can't continue"),
                hint=_(b"see 'hg resolve', then 'hg unshelve --continue'"),
            )

        shelvectx = repo[state.parents[1]]
        pendingctx = state.pendingctx

        with repo.dirstate.parentchange():
            repo.setparents(state.pendingctx.node(), nodemod.nullid)
            repo.dirstate.write(repo.currenttransaction())

        targetphase = phases.internal
        if not phases.supportinternal(repo):
            targetphase = phases.secret
        overrides = {(b'phases', b'new-commit'): targetphase}
        with repo.ui.configoverride(overrides, b'unshelve'):
            with repo.dirstate.parentchange():
                repo.setparents(state.parents[0], nodemod.nullid)
                newnode, ispartialunshelve = _createunshelvectx(
                    ui, repo, shelvectx, basename, interactive, opts
                )

        if newnode is None:
            shelvectx = state.pendingctx
            msg = _(
                b'note: unshelved changes already existed '
                b'in the working copy\n'
            )
            ui.status(msg)
        else:
            # only strip the shelvectx if we produced one
            state.nodestoremove.append(newnode)
            shelvectx = repo[newnode]

        merge.update(pendingctx)
        mergefiles(ui, repo, state.wctx, shelvectx)
        restorebranch(ui, repo, state.branchtorestore)

        if not phases.supportinternal(repo):
            repair.strip(
                ui, repo, state.nodestoremove, backup=False, topic=b'shelve'
            )
        shelvedstate.clear(repo)
        if not ispartialunshelve:
            unshelvecleanup(ui, repo, state.name, opts)
        _restoreactivebookmark(repo, state.activebookmark)
        ui.status(_(b"unshelve of '%s' complete\n") % state.name)


def hgcontinueunshelve(ui, repo):
    """logic to resume unshelve using 'hg continue'"""
    with repo.wlock():
        state = _loadshelvedstate(ui, repo, {b'continue': True})
        return unshelvecontinue(ui, repo, state, {b'keep': state.keep})


def _commitworkingcopychanges(ui, repo, opts, tmpwctx):
    """Temporarily commit working copy changes before moving unshelve commit"""
    # Store pending changes in a commit and remember added in case a shelve
    # contains unknown files that are part of the pending change
    s = repo.status()
    addedbefore = frozenset(s.added)
    if not (s.modified or s.added or s.removed):
        return tmpwctx, addedbefore
    ui.status(
        _(
            b"temporarily committing pending changes "
            b"(restore with 'hg unshelve --abort')\n"
        )
    )
    extra = {b'internal': b'shelve'}
    commitfunc = getcommitfunc(extra=extra, interactive=False, editor=False)
    tempopts = {}
    tempopts[b'message'] = b"pending changes temporary commit"
    tempopts[b'date'] = opts.get(b'date')
    with ui.configoverride({(b'ui', b'quiet'): True}):
        node = cmdutil.commit(ui, repo, commitfunc, [], tempopts)
    tmpwctx = repo[node]
    return tmpwctx, addedbefore


def _unshelverestorecommit(ui, repo, tr, basename):
    """Recreate commit in the repository during the unshelve"""
    repo = repo.unfiltered()
    node = None
    if shelvedfile(repo, basename, b'shelve').exists():
        node = shelvedfile(repo, basename, b'shelve').readinfo()[b'node']
    if node is None or node not in repo:
        with ui.configoverride({(b'ui', b'quiet'): True}):
            shelvectx = shelvedfile(repo, basename, b'hg').applybundle(tr)
        # We might not strip the unbundled changeset, so we should keep track of
        # the unshelve node in case we need to reuse it (eg: unshelve --keep)
        if node is None:
            info = {b'node': nodemod.hex(shelvectx.node())}
            shelvedfile(repo, basename, b'shelve').writeinfo(info)
    else:
        shelvectx = repo[node]

    return repo, shelvectx


def _createunshelvectx(ui, repo, shelvectx, basename, interactive, opts):
    """Handles the creation of unshelve commit and updates the shelve if it
    was partially unshelved.

    If interactive is:

      * False: Commits all the changes in the working directory.
      * True: Prompts the user to select changes to unshelve and commit them.
              Update the shelve with remaining changes.

    Returns the node of the new commit formed and a bool indicating whether
    the shelve was partially unshelved.Creates a commit ctx to unshelve
    interactively or non-interactively.

    The user might want to unshelve certain changes only from the stored
    shelve in interactive. So, we would create two commits. One with requested
    changes to unshelve at that time and the latter is shelved for future.

    Here, we return both the newnode which is created interactively and a
    bool to know whether the shelve is partly done or completely done.
    """
    opts[b'message'] = shelvectx.description()
    opts[b'interactive-unshelve'] = True
    pats = []
    if not interactive:
        newnode = repo.commit(
            text=shelvectx.description(),
            extra=shelvectx.extra(),
            user=shelvectx.user(),
            date=shelvectx.date(),
        )
        return newnode, False

    commitfunc = getcommitfunc(shelvectx.extra(), interactive=True, editor=True)
    newnode = cmdutil.dorecord(
        ui,
        repo,
        commitfunc,
        None,
        False,
        cmdutil.recordfilter,
        *pats,
        **pycompat.strkwargs(opts)
    )
    snode = repo.commit(
        text=shelvectx.description(),
        extra=shelvectx.extra(),
        user=shelvectx.user(),
    )
    if snode:
        m = scmutil.matchfiles(repo, repo[snode].files())
        _shelvecreatedcommit(repo, snode, basename, m)

    return newnode, bool(snode)


def _rebaserestoredcommit(
    ui,
    repo,
    opts,
    tr,
    oldtiprev,
    basename,
    pctx,
    tmpwctx,
    shelvectx,
    branchtorestore,
    activebookmark,
):
    """Rebase restored commit from its original location to a destination"""
    # If the shelve is not immediately on top of the commit
    # we'll be merging with, rebase it to be on top.
    interactive = opts.get(b'interactive')
    if tmpwctx.node() == shelvectx.p1().node() and not interactive:
        # We won't skip on interactive mode because, the user might want to
        # unshelve certain changes only.
        return shelvectx, False

    overrides = {
        (b'ui', b'forcemerge'): opts.get(b'tool', b''),
        (b'phases', b'new-commit'): phases.secret,
    }
    with repo.ui.configoverride(overrides, b'unshelve'):
        ui.status(_(b'rebasing shelved changes\n'))
        stats = merge.graft(
            repo,
            shelvectx,
            labels=[b'working-copy', b'shelve'],
            keepconflictparent=True,
        )
        if stats.unresolvedcount:
            tr.close()

            nodestoremove = [
                repo.changelog.node(rev)
                for rev in pycompat.xrange(oldtiprev, len(repo))
            ]
            shelvedstate.save(
                repo,
                basename,
                pctx,
                tmpwctx,
                nodestoremove,
                branchtorestore,
                opts.get(b'keep'),
                activebookmark,
                interactive,
            )
            raise error.ConflictResolutionRequired(b'unshelve')

        with repo.dirstate.parentchange():
            repo.setparents(tmpwctx.node(), nodemod.nullid)
            newnode, ispartialunshelve = _createunshelvectx(
                ui, repo, shelvectx, basename, interactive, opts
            )

        if newnode is None:
            shelvectx = tmpwctx
            msg = _(
                b'note: unshelved changes already existed '
                b'in the working copy\n'
            )
            ui.status(msg)
        else:
            shelvectx = repo[newnode]
            merge.update(tmpwctx)

    return shelvectx, ispartialunshelve


def _forgetunknownfiles(repo, shelvectx, addedbefore):
    # Forget any files that were unknown before the shelve, unknown before
    # unshelve started, but are now added.
    shelveunknown = shelvectx.extra().get(b'shelve_unknown')
    if not shelveunknown:
        return
    shelveunknown = frozenset(shelveunknown.split(b'\0'))
    addedafter = frozenset(repo.status().added)
    toforget = (addedafter & shelveunknown) - addedbefore
    repo[None].forget(toforget)


def _finishunshelve(repo, oldtiprev, tr, activebookmark):
    _restoreactivebookmark(repo, activebookmark)
    # The transaction aborting will strip all the commits for us,
    # but it doesn't update the inmemory structures, so addchangegroup
    # hooks still fire and try to operate on the missing commits.
    # Clean up manually to prevent this.
    repo.unfiltered().changelog.strip(oldtiprev, tr)
    _aborttransaction(repo, tr)


def _checkunshelveuntrackedproblems(ui, repo, shelvectx):
    """Check potential problems which may result from working
    copy having untracked changes."""
    wcdeleted = set(repo.status().deleted)
    shelvetouched = set(shelvectx.files())
    intersection = wcdeleted.intersection(shelvetouched)
    if intersection:
        m = _(b"shelved change touches missing files")
        hint = _(b"run hg status to see which files are missing")
        raise error.Abort(m, hint=hint)


def unshelvecmd(ui, repo, *shelved, **opts):
    opts = pycompat.byteskwargs(opts)
    abortf = opts.get(b'abort')
    continuef = opts.get(b'continue')
    interactive = opts.get(b'interactive')
    if not abortf and not continuef:
        cmdutil.checkunfinished(repo)
    shelved = list(shelved)
    if opts.get(b"name"):
        shelved.append(opts[b"name"])

    if interactive and opts.get(b'keep'):
        raise error.Abort(_(b'--keep on --interactive is not yet supported'))
    if abortf or continuef:
        if abortf and continuef:
            raise error.Abort(_(b'cannot use both abort and continue'))
        if shelved:
            raise error.Abort(
                _(
                    b'cannot combine abort/continue with '
                    b'naming a shelved change'
                )
            )
        if abortf and opts.get(b'tool', False):
            ui.warn(_(b'tool option will be ignored\n'))

        state = _loadshelvedstate(ui, repo, opts)
        if abortf:
            return unshelveabort(ui, repo, state)
        elif continuef and interactive:
            raise error.Abort(_(b'cannot use both continue and interactive'))
        elif continuef:
            return unshelvecontinue(ui, repo, state, opts)
    elif len(shelved) > 1:
        raise error.Abort(_(b'can only unshelve one change at a time'))
    elif not shelved:
        shelved = listshelves(repo)
        if not shelved:
            raise error.Abort(_(b'no shelved changes to apply!'))
        basename = util.split(shelved[0][1])[1]
        ui.status(_(b"unshelving change '%s'\n") % basename)
    else:
        basename = shelved[0]

    if not shelvedfile(repo, basename, patchextension).exists():
        raise error.Abort(_(b"shelved change '%s' not found") % basename)

    return _dounshelve(ui, repo, basename, opts)


def _dounshelve(ui, repo, basename, opts):
    repo = repo.unfiltered()
    lock = tr = None
    try:
        lock = repo.lock()
        tr = repo.transaction(b'unshelve', report=lambda x: None)
        oldtiprev = len(repo)

        pctx = repo[b'.']
        tmpwctx = pctx
        # The goal is to have a commit structure like so:
        # ...-> pctx -> tmpwctx -> shelvectx
        # where tmpwctx is an optional commit with the user's pending changes
        # and shelvectx is the unshelved changes. Then we merge it all down
        # to the original pctx.

        activebookmark = _backupactivebookmark(repo)
        tmpwctx, addedbefore = _commitworkingcopychanges(
            ui, repo, opts, tmpwctx
        )
        repo, shelvectx = _unshelverestorecommit(ui, repo, tr, basename)
        _checkunshelveuntrackedproblems(ui, repo, shelvectx)
        branchtorestore = b''
        if shelvectx.branch() != shelvectx.p1().branch():
            branchtorestore = shelvectx.branch()

        shelvectx, ispartialunshelve = _rebaserestoredcommit(
            ui,
            repo,
            opts,
            tr,
            oldtiprev,
            basename,
            pctx,
            tmpwctx,
            shelvectx,
            branchtorestore,
            activebookmark,
        )
        overrides = {(b'ui', b'forcemerge'): opts.get(b'tool', b'')}
        with ui.configoverride(overrides, b'unshelve'):
            mergefiles(ui, repo, pctx, shelvectx)
        restorebranch(ui, repo, branchtorestore)
        shelvedstate.clear(repo)
        _finishunshelve(repo, oldtiprev, tr, activebookmark)
        _forgetunknownfiles(repo, shelvectx, addedbefore)
        if not ispartialunshelve:
            unshelvecleanup(ui, repo, basename, opts)
    finally:
        if tr:
            tr.release()
        lockmod.release(lock)