hgext/uncommit.py
author Pulkit Goyal <7895pulkit@gmail.com>
Sat, 16 Sep 2017 02:34:13 +0530
changeset 34283 f94442d46984
parent 34192 da2f5f19312c
child 34284 624c53e4121d
permissions -rw-r--r--
uncommit: rename the flag 'empty' to 'keep' which retains empty changeset This patch renames the flag 'empty' which retains the empty changeset if all the files are uncommitted to 'keep'. Differential Revision: https://phab.mercurial-scm.org/D726
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34192
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     1
# uncommit - undo the actions of a commit
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     2
#
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     3
# Copyright 2011 Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     4
#                Logilab SA        <contact@logilab.fr>
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     5
#                Pierre-Yves David <pierre-yves.david@ens-lyon.org>
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     6
#                Patrick Mezard <patrick@mezard.eu>
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     7
# Copyright 2016 Facebook, Inc.
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     8
#
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
     9
# This software may be used and distributed according to the terms of the
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    10
# GNU General Public License version 2 or any later version.
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    11
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    12
"""uncommit part or all of a local changeset (EXPERIMENTAL)
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    13
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    14
This command undoes the effect of a local commit, returning the affected
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    15
files to their uncommitted state. This means that files modified, added or
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    16
removed in the changeset will be left unchanged, and so will remain modified,
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    17
added and removed in the working directory.
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    18
"""
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    19
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    20
from __future__ import absolute_import
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    21
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    22
from mercurial.i18n import _
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    23
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    24
from mercurial import (
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    25
    commands,
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    26
    context,
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    27
    copies,
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    28
    error,
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    29
    node,
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    30
    obsolete,
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    31
    registrar,
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    32
    scmutil,
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    33
)
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    34
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    35
cmdtable = {}
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    36
command = registrar.command(cmdtable)
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    37
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    38
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    39
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    40
# be specifying the version(s) of Mercurial they are tested with, or
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    41
# leave the attribute unspecified.
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    42
testedwith = 'ships-with-hg-core'
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    43
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    44
def _commitfiltered(repo, ctx, match, allowempty):
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    45
    """Recommit ctx with changed files not in match. Return the new
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    46
    node identifier, or None if nothing changed.
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    47
    """
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    48
    base = ctx.p1()
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    49
    # ctx
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    50
    initialfiles = set(ctx.files())
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    51
    exclude = set(f for f in initialfiles if match(f))
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    52
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    53
    # No files matched commit, so nothing excluded
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    54
    if not exclude:
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    55
        return None
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    56
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    57
    files = (initialfiles - exclude)
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    58
    # return the p1 so that we don't create an obsmarker later
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    59
    if not files and not allowempty:
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    60
        return ctx.parents()[0].node()
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    61
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    62
    # Filter copies
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    63
    copied = copies.pathcopies(base, ctx)
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    64
    copied = dict((dst, src) for dst, src in copied.iteritems()
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    65
                  if dst in files)
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    66
    def filectxfn(repo, memctx, path, contentctx=ctx, redirect=()):
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    67
        if path not in contentctx:
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    68
            return None
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    69
        fctx = contentctx[path]
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    70
        mctx = context.memfilectx(repo, fctx.path(), fctx.data(),
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    71
                                  fctx.islink(),
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    72
                                  fctx.isexec(),
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    73
                                  copied=copied.get(path))
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    74
        return mctx
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    75
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    76
    new = context.memctx(repo,
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    77
                         parents=[base.node(), node.nullid],
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    78
                         text=ctx.description(),
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    79
                         files=files,
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    80
                         filectxfn=filectxfn,
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    81
                         user=ctx.user(),
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    82
                         date=ctx.date(),
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    83
                         extra=ctx.extra())
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    84
    # phase handling
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    85
    commitphase = ctx.phase()
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    86
    overrides = {('phases', 'new-commit'): commitphase}
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    87
    with repo.ui.configoverride(overrides, 'uncommit'):
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    88
        newid = repo.commitctx(new)
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    89
    return newid
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    90
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    91
def _uncommitdirstate(repo, oldctx, match):
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    92
    """Fix the dirstate after switching the working directory from
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    93
    oldctx to a copy of oldctx not containing changed files matched by
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    94
    match.
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    95
    """
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    96
    ctx = repo['.']
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    97
    ds = repo.dirstate
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    98
    copies = dict(ds.copies())
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
    99
    s = repo.status(oldctx.p1(), oldctx, match=match)
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   100
    for f in s.modified:
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   101
        if ds[f] == 'r':
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   102
            # modified + removed -> removed
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   103
            continue
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   104
        ds.normallookup(f)
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   105
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   106
    for f in s.added:
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   107
        if ds[f] == 'r':
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   108
            # added + removed -> unknown
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   109
            ds.drop(f)
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   110
        elif ds[f] != 'a':
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   111
            ds.add(f)
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   112
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   113
    for f in s.removed:
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   114
        if ds[f] == 'a':
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   115
            # removed + added -> normal
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   116
            ds.normallookup(f)
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   117
        elif ds[f] != 'r':
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   118
            ds.remove(f)
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   119
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   120
    # Merge old parent and old working dir copies
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   121
    oldcopies = {}
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   122
    for f in (s.modified + s.added):
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   123
        src = oldctx[f].renamed()
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   124
        if src:
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   125
            oldcopies[f] = src[0]
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   126
    oldcopies.update(copies)
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   127
    copies = dict((dst, oldcopies.get(src, src))
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   128
                  for dst, src in oldcopies.iteritems())
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   129
    # Adjust the dirstate copies
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   130
    for dst, src in copies.iteritems():
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   131
        if (src not in ctx or dst in ctx or ds[dst] != 'a'):
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   132
            src = None
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   133
        ds.copy(src, dst)
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   134
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   135
@command('uncommit',
34283
f94442d46984 uncommit: rename the flag 'empty' to 'keep' which retains empty changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34192
diff changeset
   136
    [('', 'keep', False, _('allow an empty commit after uncommiting')),
34192
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   137
    ] + commands.walkopts,
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   138
    _('[OPTION]... [FILE]...'))
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   139
def uncommit(ui, repo, *pats, **opts):
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   140
    """uncommit part or all of a local changeset
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   141
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   142
    This command undoes the effect of a local commit, returning the affected
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   143
    files to their uncommitted state. This means that files modified or
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   144
    deleted in the changeset will be left unchanged, and so will remain
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   145
    modified in the working directory.
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   146
    """
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   147
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   148
    with repo.wlock(), repo.lock():
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   149
        wctx = repo[None]
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   150
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   151
        if wctx.parents()[0].node() == node.nullid:
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   152
            raise error.Abort(_("cannot uncommit null changeset"))
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   153
        if len(wctx.parents()) > 1:
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   154
            raise error.Abort(_("cannot uncommit while merging"))
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   155
        old = repo['.']
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   156
        if not old.mutable():
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   157
            raise error.Abort(_('cannot uncommit public changesets'))
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   158
        if len(old.parents()) > 1:
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   159
            raise error.Abort(_("cannot uncommit merge changeset"))
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   160
        allowunstable = obsolete.isenabled(repo, obsolete.allowunstableopt)
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   161
        if not allowunstable and old.children():
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   162
            raise error.Abort(_('cannot uncommit changeset with children'))
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   163
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   164
        with repo.transaction('uncommit'):
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   165
            match = scmutil.match(old, pats, opts)
34283
f94442d46984 uncommit: rename the flag 'empty' to 'keep' which retains empty changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34192
diff changeset
   166
            newid = _commitfiltered(repo, old, match, opts.get('keep'))
34192
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   167
            if newid is None:
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   168
                ui.status(_("nothing to uncommit\n"))
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   169
                return 1
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   170
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   171
            mapping = {}
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   172
            if newid != old.p1().node():
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   173
                # Move local changes on filtered changeset
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   174
                mapping[old.node()] = (newid,)
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   175
            else:
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   176
                # Fully removed the old commit
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   177
                mapping[old.node()] = ()
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   178
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   179
            scmutil.cleanupnodes(repo, mapping, 'uncommit')
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   180
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   181
            with repo.dirstate.parentchange():
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   182
                repo.dirstate.setparents(newid, node.nullid)
da2f5f19312c uncommit: move fb-extension to core which uncommits a changeset
Pulkit Goyal <7895pulkit@gmail.com>
parents:
diff changeset
   183
                _uncommitdirstate(repo, old, match)