hgext/strip.py
author Matt Mackall <mpm@selenic.com>
Fri, 11 Jul 2014 18:04:51 -0500
changeset 21854 ba3bc6474bbf
parent 21847 f6f122f4813b
child 22057 445472225ccd
permissions -rw-r--r--
strip: drop -b/--backup option (BC) This option had very limited utility and counterintuitive behavior and collided unfortunately with the much later -B option. Normally we would no-op such a feature so as to avoid annoying existing scripts. However, we have to weigh that against the silent misbehavior that results when users mistakenly intended to use -B: because -b takes no arg, the bookmark gets interpreted as a normal revision, and gets stripped without removing the associated bookmark, while also not backing up the revision in question. A no-op behavior or warning would only remove the latter half of the misadventure. The only users I can find of this feature were using it in error and have since stopped. The few (if any) remaining users of this feature would be better served by --no-backup.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19828
0a986233ecbf strip: bring extension description in line with style and copy-edit
Kevin Bullock <kbullock@ringworld.org>
parents: 19826
diff changeset
     1
"""strip changesets and their descendents from history
19826
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
     2
19945
3d42a85f6922 strip: fix spelling: "allows to" -> "allows you to"
Javi Merino <cibervicho@gmail.com>
parents: 19828
diff changeset
     3
This extension allows you to strip changesets and all their descendants from the
19826
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
     4
repository. See the command help for details.
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
     5
"""
19823
6fb14d21fe9d strip: move checksubstate from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19822
diff changeset
     6
from mercurial.i18n import _
19825
4b4997068143 strip: move the strip helper function for mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19824
diff changeset
     7
from mercurial.node import nullid
4b4997068143 strip: move the strip helper function for mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19824
diff changeset
     8
from mercurial.lock import release
19826
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
     9
from mercurial import cmdutil, hg, scmutil, util
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    10
from mercurial import repair, bookmarks
19822
a194a33f8cb2 mq: prepare a strip extension for extraction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    11
a194a33f8cb2 mq: prepare a strip extension for extraction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    12
cmdtable = {}
a194a33f8cb2 mq: prepare a strip extension for extraction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    13
command = cmdutil.command(cmdtable)
a194a33f8cb2 mq: prepare a strip extension for extraction
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
diff changeset
    14
testedwith = 'internal'
19823
6fb14d21fe9d strip: move checksubstate from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19822
diff changeset
    15
6fb14d21fe9d strip: move checksubstate from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19822
diff changeset
    16
def checksubstate(repo, baserev=None):
6fb14d21fe9d strip: move checksubstate from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19822
diff changeset
    17
    '''return list of subrepos at a different revision than substate.
6fb14d21fe9d strip: move checksubstate from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19822
diff changeset
    18
    Abort if any subrepos have uncommitted changes.'''
6fb14d21fe9d strip: move checksubstate from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19822
diff changeset
    19
    inclsubs = []
6fb14d21fe9d strip: move checksubstate from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19822
diff changeset
    20
    wctx = repo[None]
6fb14d21fe9d strip: move checksubstate from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19822
diff changeset
    21
    if baserev:
6fb14d21fe9d strip: move checksubstate from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19822
diff changeset
    22
        bctx = repo[baserev]
6fb14d21fe9d strip: move checksubstate from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19822
diff changeset
    23
    else:
6fb14d21fe9d strip: move checksubstate from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19822
diff changeset
    24
        bctx = wctx.parents()[0]
6fb14d21fe9d strip: move checksubstate from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19822
diff changeset
    25
    for s in sorted(wctx.substate):
6fb14d21fe9d strip: move checksubstate from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19822
diff changeset
    26
        if wctx.sub(s).dirty(True):
6fb14d21fe9d strip: move checksubstate from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19822
diff changeset
    27
            raise util.Abort(
6fb14d21fe9d strip: move checksubstate from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19822
diff changeset
    28
                _("uncommitted changes in subrepository %s") % s)
6fb14d21fe9d strip: move checksubstate from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19822
diff changeset
    29
        elif s not in bctx.substate or bctx.sub(s).dirty():
6fb14d21fe9d strip: move checksubstate from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19822
diff changeset
    30
            inclsubs.append(s)
6fb14d21fe9d strip: move checksubstate from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19822
diff changeset
    31
    return inclsubs
6fb14d21fe9d strip: move checksubstate from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19822
diff changeset
    32
19824
237e40b2c1ff strip: move checklocalchanges from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19823
diff changeset
    33
def checklocalchanges(repo, force=False, excsuffix=''):
237e40b2c1ff strip: move checklocalchanges from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19823
diff changeset
    34
    cmdutil.checkunfinished(repo)
237e40b2c1ff strip: move checklocalchanges from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19823
diff changeset
    35
    m, a, r, d = repo.status()[:4]
237e40b2c1ff strip: move checklocalchanges from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19823
diff changeset
    36
    if not force:
237e40b2c1ff strip: move checklocalchanges from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19823
diff changeset
    37
        if (m or a or r or d):
237e40b2c1ff strip: move checklocalchanges from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19823
diff changeset
    38
            _("local changes found") # i18n tool detection
237e40b2c1ff strip: move checklocalchanges from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19823
diff changeset
    39
            raise util.Abort(_("local changes found" + excsuffix))
237e40b2c1ff strip: move checklocalchanges from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19823
diff changeset
    40
        if checksubstate(repo):
237e40b2c1ff strip: move checklocalchanges from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19823
diff changeset
    41
            _("local changed subrepos found") # i18n tool detection
237e40b2c1ff strip: move checklocalchanges from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19823
diff changeset
    42
            raise util.Abort(_("local changed subrepos found" + excsuffix))
237e40b2c1ff strip: move checklocalchanges from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19823
diff changeset
    43
    return m, a, r, d
237e40b2c1ff strip: move checklocalchanges from mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19823
diff changeset
    44
21847
f6f122f4813b strip: remove bookmarks after strip succeed (issue4295)
David Soria Parra <davidsp@fb.com>
parents: 20102
diff changeset
    45
def strip(ui, repo, revs, update=True, backup="all", force=None, bookmark=None):
19825
4b4997068143 strip: move the strip helper function for mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19824
diff changeset
    46
    wlock = lock = None
4b4997068143 strip: move the strip helper function for mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19824
diff changeset
    47
    try:
4b4997068143 strip: move the strip helper function for mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19824
diff changeset
    48
        wlock = repo.wlock()
4b4997068143 strip: move the strip helper function for mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19824
diff changeset
    49
        lock = repo.lock()
4b4997068143 strip: move the strip helper function for mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19824
diff changeset
    50
4b4997068143 strip: move the strip helper function for mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19824
diff changeset
    51
        if update:
4b4997068143 strip: move the strip helper function for mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19824
diff changeset
    52
            checklocalchanges(repo, force=force)
4b4997068143 strip: move the strip helper function for mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19824
diff changeset
    53
            urev, p2 = repo.changelog.parents(revs[0])
20009
2802bedbd45f strip: fix last unprotected mq reference (issue4097)
Matt Mackall <mpm@selenic.com>
parents: 19945
diff changeset
    54
            if (util.safehasattr(repo, 'mq') and
2802bedbd45f strip: fix last unprotected mq reference (issue4097)
Matt Mackall <mpm@selenic.com>
parents: 19945
diff changeset
    55
                p2 != nullid
2802bedbd45f strip: fix last unprotected mq reference (issue4097)
Matt Mackall <mpm@selenic.com>
parents: 19945
diff changeset
    56
                and p2 in [x.node for x in repo.mq.applied]):
19825
4b4997068143 strip: move the strip helper function for mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19824
diff changeset
    57
                urev = p2
4b4997068143 strip: move the strip helper function for mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19824
diff changeset
    58
            hg.clean(repo, urev)
4b4997068143 strip: move the strip helper function for mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19824
diff changeset
    59
            repo.dirstate.write()
4b4997068143 strip: move the strip helper function for mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19824
diff changeset
    60
4b4997068143 strip: move the strip helper function for mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19824
diff changeset
    61
        repair.strip(ui, repo, revs, backup)
21847
f6f122f4813b strip: remove bookmarks after strip succeed (issue4295)
David Soria Parra <davidsp@fb.com>
parents: 20102
diff changeset
    62
f6f122f4813b strip: remove bookmarks after strip succeed (issue4295)
David Soria Parra <davidsp@fb.com>
parents: 20102
diff changeset
    63
        marks = repo._bookmarks
f6f122f4813b strip: remove bookmarks after strip succeed (issue4295)
David Soria Parra <davidsp@fb.com>
parents: 20102
diff changeset
    64
        if bookmark:
f6f122f4813b strip: remove bookmarks after strip succeed (issue4295)
David Soria Parra <davidsp@fb.com>
parents: 20102
diff changeset
    65
            if bookmark == repo._bookmarkcurrent:
f6f122f4813b strip: remove bookmarks after strip succeed (issue4295)
David Soria Parra <davidsp@fb.com>
parents: 20102
diff changeset
    66
                bookmarks.unsetcurrent(repo)
f6f122f4813b strip: remove bookmarks after strip succeed (issue4295)
David Soria Parra <davidsp@fb.com>
parents: 20102
diff changeset
    67
            del marks[bookmark]
f6f122f4813b strip: remove bookmarks after strip succeed (issue4295)
David Soria Parra <davidsp@fb.com>
parents: 20102
diff changeset
    68
            marks.write()
f6f122f4813b strip: remove bookmarks after strip succeed (issue4295)
David Soria Parra <davidsp@fb.com>
parents: 20102
diff changeset
    69
            ui.write(_("bookmark '%s' deleted\n") % bookmark)
19825
4b4997068143 strip: move the strip helper function for mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19824
diff changeset
    70
    finally:
4b4997068143 strip: move the strip helper function for mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19824
diff changeset
    71
        release(lock, wlock)
4b4997068143 strip: move the strip helper function for mq to strip
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19824
diff changeset
    72
19826
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    73
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    74
@command("strip",
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    75
         [
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    76
          ('r', 'rev', [], _('strip specified revision (optional, '
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    77
                               'can specify revisions without this '
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    78
                               'option)'), _('REV')),
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    79
          ('f', 'force', None, _('force removal of changesets, discard '
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    80
                                 'uncommitted changes (no backup)')),
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    81
          ('', 'no-backup', None, _('no backups')),
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    82
          ('', 'nobackup', None, _('no backups (DEPRECATED)')),
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    83
          ('n', '', None, _('ignored  (DEPRECATED)')),
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    84
          ('k', 'keep', None, _("do not modify working copy during strip")),
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    85
          ('B', 'bookmark', '', _("remove revs only reachable from given"
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    86
                                  " bookmark"))],
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    87
          _('hg strip [-k] [-f] [-n] [-B bookmark] [-r] REV...'))
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    88
def stripcmd(ui, repo, *revs, **opts):
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    89
    """strip changesets and all their descendants from the repository
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    90
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    91
    The strip command removes the specified changesets and all their
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    92
    descendants. If the working directory has uncommitted changes, the
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    93
    operation is aborted unless the --force flag is supplied, in which
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    94
    case changes will be discarded.
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    95
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    96
    If a parent of the working directory is stripped, then the working
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    97
    directory will automatically be updated to the most recent
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    98
    available ancestor of the stripped parent after the operation
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
    99
    completes.
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   100
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   101
    Any stripped changesets are stored in ``.hg/strip-backup`` as a
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   102
    bundle (see :hg:`help bundle` and :hg:`help unbundle`). They can
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   103
    be restored by running :hg:`unbundle .hg/strip-backup/BUNDLE`,
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   104
    where BUNDLE is the bundle file created by the strip. Note that
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   105
    the local revision numbers will in general be different after the
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   106
    restore.
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   107
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   108
    Use the --no-backup option to discard the backup bundle once the
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   109
    operation completes.
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   110
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   111
    Strip is not a history-rewriting operation and can be used on
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   112
    changesets in the public phase. But if the stripped changesets have
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   113
    been pushed to a remote repository you will likely pull them again.
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   114
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   115
    Return 0 on success.
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   116
    """
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   117
    backup = 'all'
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   118
    if opts.get('backup'):
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   119
        backup = 'strip'
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   120
    elif opts.get('no_backup') or opts.get('nobackup'):
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   121
        backup = 'none'
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   122
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   123
    cl = repo.changelog
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   124
    revs = list(revs) + opts.get('rev')
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   125
    revs = set(scmutil.revrange(repo, revs))
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   126
20096
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   127
    wlock = repo.wlock()
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   128
    try:
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   129
        if opts.get('bookmark'):
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   130
            mark = opts.get('bookmark')
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   131
            marks = repo._bookmarks
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   132
            if mark not in marks:
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   133
                raise util.Abort(_("bookmark '%s' not found") % mark)
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   134
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   135
            # If the requested bookmark is not the only one pointing to a
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   136
            # a revision we have to only delete the bookmark and not strip
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   137
            # anything. revsets cannot detect that case.
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   138
            uniquebm = True
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   139
            for m, n in marks.iteritems():
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   140
                if m != mark and n == repo[mark].node():
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   141
                    uniquebm = False
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   142
                    break
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   143
            if uniquebm:
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   144
                rsrevs = repo.revs("ancestors(bookmark(%s)) - "
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   145
                                   "ancestors(head() and not bookmark(%s)) - "
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   146
                                   "ancestors(bookmark() and not bookmark(%s))",
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   147
                                   mark, mark, mark)
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   148
                revs.update(set(rsrevs))
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   149
            if not revs:
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   150
                del marks[mark]
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   151
                marks.write()
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   152
                ui.write(_("bookmark '%s' deleted\n") % mark)
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   153
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   154
        if not revs:
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   155
            raise util.Abort(_('empty revision set'))
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   156
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   157
        descendants = set(cl.descendants(revs))
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   158
        strippedrevs = revs.union(descendants)
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   159
        roots = revs.difference(descendants)
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   160
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   161
        update = False
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   162
        # if one of the wdir parent is stripped we'll need
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   163
        # to update away to an earlier revision
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   164
        for p in repo.dirstate.parents():
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   165
            if p != nullid and cl.rev(p) in strippedrevs:
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   166
                update = True
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   167
                break
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   168
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   169
        rootnodes = set(cl.node(r) for r in roots)
19826
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   170
20096
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   171
        q = getattr(repo, 'mq', None)
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   172
        if q is not None and q.applied:
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   173
            # refresh queue state if we're about to strip
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   174
            # applied patches
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   175
            if cl.rev(repo.lookup('qtip')) in strippedrevs:
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   176
                q.applieddirty = True
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   177
                start = 0
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   178
                end = len(q.applied)
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   179
                for i, statusentry in enumerate(q.applied):
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   180
                    if statusentry.node in rootnodes:
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   181
                        # if one of the stripped roots is an applied
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   182
                        # patch, only part of the queue is stripped
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   183
                        start = i
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   184
                        break
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   185
                del q.applied[start:end]
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   186
                q.savedirty()
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   187
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   188
        revs = sorted(rootnodes)
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   189
        if update and opts.get('keep'):
20102
04eaa8eec6a0 strip.stripcmd: remove redundant wlock acquire/release
Siddharth Agarwal <sid0@fb.com>
parents: 20101
diff changeset
   190
            urev, p2 = repo.changelog.parents(revs[0])
04eaa8eec6a0 strip.stripcmd: remove redundant wlock acquire/release
Siddharth Agarwal <sid0@fb.com>
parents: 20101
diff changeset
   191
            if (util.safehasattr(repo, 'mq') and p2 != nullid
04eaa8eec6a0 strip.stripcmd: remove redundant wlock acquire/release
Siddharth Agarwal <sid0@fb.com>
parents: 20101
diff changeset
   192
                and p2 in [x.node for x in repo.mq.applied]):
04eaa8eec6a0 strip.stripcmd: remove redundant wlock acquire/release
Siddharth Agarwal <sid0@fb.com>
parents: 20101
diff changeset
   193
                urev = p2
04eaa8eec6a0 strip.stripcmd: remove redundant wlock acquire/release
Siddharth Agarwal <sid0@fb.com>
parents: 20101
diff changeset
   194
            uctx = repo[urev]
20096
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   195
20102
04eaa8eec6a0 strip.stripcmd: remove redundant wlock acquire/release
Siddharth Agarwal <sid0@fb.com>
parents: 20101
diff changeset
   196
            # only reset the dirstate for files that would actually change
04eaa8eec6a0 strip.stripcmd: remove redundant wlock acquire/release
Siddharth Agarwal <sid0@fb.com>
parents: 20101
diff changeset
   197
            # between the working context and uctx
04eaa8eec6a0 strip.stripcmd: remove redundant wlock acquire/release
Siddharth Agarwal <sid0@fb.com>
parents: 20101
diff changeset
   198
            descendantrevs = repo.revs("%s::." % uctx.rev())
04eaa8eec6a0 strip.stripcmd: remove redundant wlock acquire/release
Siddharth Agarwal <sid0@fb.com>
parents: 20101
diff changeset
   199
            changedfiles = []
04eaa8eec6a0 strip.stripcmd: remove redundant wlock acquire/release
Siddharth Agarwal <sid0@fb.com>
parents: 20101
diff changeset
   200
            for rev in descendantrevs:
04eaa8eec6a0 strip.stripcmd: remove redundant wlock acquire/release
Siddharth Agarwal <sid0@fb.com>
parents: 20101
diff changeset
   201
                # blindly reset the files, regardless of what actually changed
04eaa8eec6a0 strip.stripcmd: remove redundant wlock acquire/release
Siddharth Agarwal <sid0@fb.com>
parents: 20101
diff changeset
   202
                changedfiles.extend(repo[rev].files())
20096
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   203
20102
04eaa8eec6a0 strip.stripcmd: remove redundant wlock acquire/release
Siddharth Agarwal <sid0@fb.com>
parents: 20101
diff changeset
   204
            # reset files that only changed in the dirstate too
04eaa8eec6a0 strip.stripcmd: remove redundant wlock acquire/release
Siddharth Agarwal <sid0@fb.com>
parents: 20101
diff changeset
   205
            dirstate = repo.dirstate
04eaa8eec6a0 strip.stripcmd: remove redundant wlock acquire/release
Siddharth Agarwal <sid0@fb.com>
parents: 20101
diff changeset
   206
            dirchanges = [f for f in dirstate if dirstate[f] != 'n']
04eaa8eec6a0 strip.stripcmd: remove redundant wlock acquire/release
Siddharth Agarwal <sid0@fb.com>
parents: 20101
diff changeset
   207
            changedfiles.extend(dirchanges)
20096
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   208
20102
04eaa8eec6a0 strip.stripcmd: remove redundant wlock acquire/release
Siddharth Agarwal <sid0@fb.com>
parents: 20101
diff changeset
   209
            repo.dirstate.rebuild(urev, uctx.manifest(), changedfiles)
04eaa8eec6a0 strip.stripcmd: remove redundant wlock acquire/release
Siddharth Agarwal <sid0@fb.com>
parents: 20101
diff changeset
   210
            repo.dirstate.write()
04eaa8eec6a0 strip.stripcmd: remove redundant wlock acquire/release
Siddharth Agarwal <sid0@fb.com>
parents: 20101
diff changeset
   211
            update = False
20096
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   212
19826
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   213
20096
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   214
        strip(ui, repo, revs, backup=backup, update=update,
21847
f6f122f4813b strip: remove bookmarks after strip succeed (issue4295)
David Soria Parra <davidsp@fb.com>
parents: 20102
diff changeset
   215
              force=opts.get('force'), bookmark=opts.get('bookmark'))
20096
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   216
    finally:
88e172871ad7 strip: hold wlock for entire duration
Siddharth Agarwal <sid0@fb.com>
parents: 20009
diff changeset
   217
        wlock.release()
19826
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   218
4b1cbcfdabf7 mq: extract strip function as its standalone extension (issue3824)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 19825
diff changeset
   219
    return 0