| 1 |
# uncommit - undo the actions of a commit |
| 2 |
# |
| 3 |
# Copyright 2011 Peter Arrenbrecht <[email protected]> |
| 4 |
# Logilab SA <[email protected]> |
| 5 |
# Pierre-Yves David <[email protected]> |
| 6 |
# Patrick Mezard <[email protected]> |
| 7 |
# Copyright 2016 Facebook, Inc. |
| 8 |
# |
| 9 |
# This software may be used and distributed according to the terms of the |
| 10 |
# GNU General Public License version 2 or any later version. |
| 11 |
|
| 12 |
"""uncommit part or all of a local changeset (EXPERIMENTAL) |
| 13 |
|
| 14 |
This command undoes the effect of a local commit, returning the affected |
| 15 |
files to their uncommitted state. This means that files modified, added or |
| 16 |
removed in the changeset will be left unchanged, and so will remain modified, |
| 17 |
added and removed in the working directory. |
| 18 |
""" |
| 19 |
|
| 20 |
from __future__ import annotations |
| 21 |
|
| 22 |
from mercurial.i18n import _ |
| 23 |
|
| 24 |
from mercurial import ( |
| 25 |
cmdutil, |
| 26 |
commands, |
| 27 |
context, |
| 28 |
copies as copiesmod, |
| 29 |
error, |
| 30 |
obsutil, |
| 31 |
pathutil, |
| 32 |
pycompat, |
| 33 |
registrar, |
| 34 |
rewriteutil, |
| 35 |
scmutil, |
| 36 |
) |
| 37 |
|
| 38 |
cmdtable = {} |
| 39 |
command = registrar.command(cmdtable) |
| 40 |
|
| 41 |
configtable = {} |
| 42 |
configitem = registrar.configitem(configtable) |
| 43 |
|
| 44 |
configitem( |
| 45 |
b'experimental', |
| 46 |
b'uncommitondirtywdir', |
| 47 |
default=False, |
| 48 |
) |
| 49 |
configitem( |
| 50 |
b'experimental', |
| 51 |
b'uncommit.keep', |
| 52 |
default=False, |
| 53 |
) |
| 54 |
|
| 55 |
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for |
| 56 |
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should |
| 57 |
# be specifying the version(s) of Mercurial they are tested with, or |
| 58 |
# leave the attribute unspecified. |
| 59 |
testedwith = b'ships-with-hg-core' |
| 60 |
|
| 61 |
|
| 62 |
def _commitfiltered( |
| 63 |
repo, ctx, match, keepcommit, message=None, user=None, date=None |
| 64 |
): |
| 65 |
"""Recommit ctx with changed files not in match. Return the new |
| 66 |
node identifier, or None if nothing changed. |
| 67 |
""" |
| 68 |
base = ctx.p1() |
| 69 |
# ctx |
| 70 |
initialfiles = set(ctx.files()) |
| 71 |
exclude = {f for f in initialfiles if match(f)} |
| 72 |
|
| 73 |
# No files matched commit, so nothing excluded |
| 74 |
if not exclude: |
| 75 |
return None |
| 76 |
|
| 77 |
# return the p1 so that we don't create an obsmarker later |
| 78 |
if not keepcommit: |
| 79 |
return ctx.p1().node() |
| 80 |
|
| 81 |
files = initialfiles - exclude |
| 82 |
# Filter copies |
| 83 |
copied = copiesmod.pathcopies(base, ctx) |
| 84 |
copied = {dst: src for dst, src in copied.items() if dst in files} |
| 85 |
|
| 86 |
def filectxfn(repo, memctx, path, contentctx=ctx, redirect=()): |
| 87 |
if path not in contentctx: |
| 88 |
return None |
| 89 |
fctx = contentctx[path] |
| 90 |
mctx = context.memfilectx( |
| 91 |
repo, |
| 92 |
memctx, |
| 93 |
fctx.path(), |
| 94 |
fctx.data(), |
| 95 |
fctx.islink(), |
| 96 |
fctx.isexec(), |
| 97 |
copysource=copied.get(path), |
| 98 |
) |
| 99 |
return mctx |
| 100 |
|
| 101 |
if not files: |
| 102 |
repo.ui.status(_(b"note: keeping empty commit\n")) |
| 103 |
|
| 104 |
if message is None: |
| 105 |
message = ctx.description() |
| 106 |
if not user: |
| 107 |
user = ctx.user() |
| 108 |
if not date: |
| 109 |
date = ctx.date() |
| 110 |
|
| 111 |
new = context.memctx( |
| 112 |
repo, |
| 113 |
parents=[base.node(), repo.nullid], |
| 114 |
text=message, |
| 115 |
files=files, |
| 116 |
filectxfn=filectxfn, |
| 117 |
user=user, |
| 118 |
date=date, |
| 119 |
extra=ctx.extra(), |
| 120 |
) |
| 121 |
return repo.commitctx(new) |
| 122 |
|
| 123 |
|
| 124 |
@command( |
| 125 |
b'uncommit', |
| 126 |
[ |
| 127 |
(b'', b'keep', None, _(b'allow an empty commit after uncommitting')), |
| 128 |
( |
| 129 |
b'', |
| 130 |
b'allow-dirty-working-copy', |
| 131 |
False, |
| 132 |
_(b'allow uncommit with outstanding changes'), |
| 133 |
), |
| 134 |
(b'n', b'note', b'', _(b'store a note on uncommit'), _(b'TEXT')), |
| 135 |
] |
| 136 |
+ commands.walkopts |
| 137 |
+ commands.commitopts |
| 138 |
+ commands.commitopts2 |
| 139 |
+ commands.commitopts3, |
| 140 |
_(b'[OPTION]... [FILE]...'), |
| 141 |
helpcategory=command.CATEGORY_CHANGE_MANAGEMENT, |
| 142 |
) |
| 143 |
def uncommit(ui, repo, *pats, **opts): |
| 144 |
"""uncommit part or all of a local changeset |
| 145 |
|
| 146 |
This command undoes the effect of a local commit, returning the affected |
| 147 |
files to their uncommitted state. This means that files modified or |
| 148 |
deleted in the changeset will be left unchanged, and so will remain |
| 149 |
modified in the working directory. |
| 150 |
|
| 151 |
If no files are specified, the commit will be pruned, unless --keep is |
| 152 |
given. |
| 153 |
""" |
| 154 |
cmdutil.check_note_size(opts) |
| 155 |
cmdutil.resolve_commit_options(ui, opts) |
| 156 |
|
| 157 |
with repo.wlock(), repo.lock(): |
| 158 |
st = repo.status() |
| 159 |
m, a, r, d = st.modified, st.added, st.removed, st.deleted |
| 160 |
isdirtypath = any(set(m + a + r + d) & set(pats)) |
| 161 |
allowdirtywcopy = opts[ |
| 162 |
'allow_dirty_working_copy' |
| 163 |
] or repo.ui.configbool(b'experimental', b'uncommitondirtywdir') |
| 164 |
if not allowdirtywcopy and (not pats or isdirtypath): |
| 165 |
scmutil.bail_if_changed( |
| 166 |
repo, |
| 167 |
hint=_(b'requires --allow-dirty-working-copy to uncommit'), |
| 168 |
) |
| 169 |
old = repo[b'.'] |
| 170 |
rewriteutil.precheck(repo, [old.rev()], b'uncommit') |
| 171 |
if len(old.parents()) > 1: |
| 172 |
raise error.InputError(_(b"cannot uncommit merge changeset")) |
| 173 |
|
| 174 |
match = scmutil.match(old, pats, pycompat.byteskwargs(opts)) |
| 175 |
|
| 176 |
# Check all explicitly given files; abort if there's a problem. |
| 177 |
if match.files(): |
| 178 |
s = old.status(old.p1(), match, listclean=True) |
| 179 |
eligible = set(s.added) | set(s.modified) | set(s.removed) |
| 180 |
|
| 181 |
badfiles = set(match.files()) - eligible |
| 182 |
|
| 183 |
# Naming a parent directory of an eligible file is OK, even |
| 184 |
# if not everything tracked in that directory can be |
| 185 |
# uncommitted. |
| 186 |
if badfiles: |
| 187 |
badfiles -= {f for f in pathutil.dirs(eligible)} |
| 188 |
|
| 189 |
for f in sorted(badfiles): |
| 190 |
if f in s.clean: |
| 191 |
hint = _( |
| 192 |
b"file was not changed in working directory parent" |
| 193 |
) |
| 194 |
elif repo.wvfs.exists(f): |
| 195 |
hint = _(b"file was untracked in working directory parent") |
| 196 |
else: |
| 197 |
hint = _(b"file does not exist") |
| 198 |
|
| 199 |
raise error.InputError( |
| 200 |
_(b'cannot uncommit "%s"') % scmutil.getuipathfn(repo)(f), |
| 201 |
hint=hint, |
| 202 |
) |
| 203 |
|
| 204 |
with repo.transaction(b'uncommit'): |
| 205 |
if not (opts['message'] or opts['logfile']): |
| 206 |
opts['message'] = old.description() |
| 207 |
message = cmdutil.logmessage(ui, pycompat.byteskwargs(opts)) |
| 208 |
|
| 209 |
keepcommit = pats |
| 210 |
if not keepcommit: |
| 211 |
if opts.get('keep') is not None: |
| 212 |
keepcommit = opts.get('keep') |
| 213 |
else: |
| 214 |
keepcommit = ui.configbool( |
| 215 |
b'experimental', b'uncommit.keep' |
| 216 |
) |
| 217 |
newid = _commitfiltered( |
| 218 |
repo, |
| 219 |
old, |
| 220 |
match, |
| 221 |
keepcommit, |
| 222 |
message=message, |
| 223 |
user=opts.get('user'), |
| 224 |
date=opts.get('date'), |
| 225 |
) |
| 226 |
if newid is None: |
| 227 |
ui.status(_(b"nothing to uncommit\n")) |
| 228 |
return 1 |
| 229 |
|
| 230 |
mapping = {} |
| 231 |
if newid != old.p1().node(): |
| 232 |
# Move local changes on filtered changeset |
| 233 |
mapping[old.node()] = (newid,) |
| 234 |
else: |
| 235 |
# Fully removed the old commit |
| 236 |
mapping[old.node()] = () |
| 237 |
|
| 238 |
with repo.dirstate.changing_parents(repo): |
| 239 |
scmutil.movedirstate(repo, repo[newid], match) |
| 240 |
|
| 241 |
cmdutil.cleanup_nodes(repo, mapping, b'uncommit', fixphase=True) |
| 242 |
|
| 243 |
|
| 244 |
def predecessormarkers(ctx): |
| 245 |
"""yields the obsolete markers marking the given changeset as a successor""" |
| 246 |
for data in ctx.repo().obsstore.predecessors.get(ctx.node(), ()): |
| 247 |
yield obsutil.marker(ctx.repo(), data) |
| 248 |
|
| 249 |
|
| 250 |
@command( |
| 251 |
b'unamend', |
| 252 |
[], |
| 253 |
helpcategory=command.CATEGORY_CHANGE_MANAGEMENT, |
| 254 |
helpbasic=True, |
| 255 |
) |
| 256 |
def unamend(ui, repo, **opts): |
| 257 |
"""undo the most recent amend operation on a current changeset |
| 258 |
|
| 259 |
This command will roll back to the previous version of a changeset, |
| 260 |
leaving working directory in state in which it was before running |
| 261 |
`hg amend` (e.g. files modified as part of an amend will be |
| 262 |
marked as modified `hg status`) |
| 263 |
""" |
| 264 |
|
| 265 |
unfi = repo.unfiltered() |
| 266 |
with repo.wlock(), repo.lock(), repo.transaction(b'unamend'): |
| 267 |
# identify the commit from which to unamend |
| 268 |
curctx = repo[b'.'] |
| 269 |
|
| 270 |
rewriteutil.precheck(repo, [curctx.rev()], b'unamend') |
| 271 |
if len(curctx.parents()) > 1: |
| 272 |
raise error.InputError(_(b"cannot unamend merge changeset")) |
| 273 |
|
| 274 |
expected_keys = (b'amend_source', b'unamend_source') |
| 275 |
if not any(key in curctx.extra() for key in expected_keys): |
| 276 |
raise error.InputError( |
| 277 |
_( |
| 278 |
b"working copy parent was not created by 'hg amend' or " |
| 279 |
b"'hg unamend'" |
| 280 |
) |
| 281 |
) |
| 282 |
|
| 283 |
# identify the commit to which to unamend |
| 284 |
markers = list(predecessormarkers(curctx)) |
| 285 |
if len(markers) != 1: |
| 286 |
e = _(b"changeset must have one predecessor, found %i predecessors") |
| 287 |
raise error.InputError(e % len(markers)) |
| 288 |
|
| 289 |
prednode = markers[0].prednode() |
| 290 |
predctx = unfi[prednode] |
| 291 |
|
| 292 |
# add an extra so that we get a new hash |
| 293 |
# note: allowing unamend to undo an unamend is an intentional feature |
| 294 |
extras = predctx.extra() |
| 295 |
extras[b'unamend_source'] = curctx.hex() |
| 296 |
|
| 297 |
def filectxfn(repo, ctx_, path): |
| 298 |
try: |
| 299 |
return predctx.filectx(path) |
| 300 |
except KeyError: |
| 301 |
return None |
| 302 |
|
| 303 |
# Make a new commit same as predctx |
| 304 |
newctx = context.memctx( |
| 305 |
repo, |
| 306 |
parents=(predctx.p1(), predctx.p2()), |
| 307 |
text=predctx.description(), |
| 308 |
files=predctx.files(), |
| 309 |
filectxfn=filectxfn, |
| 310 |
user=predctx.user(), |
| 311 |
date=predctx.date(), |
| 312 |
extra=extras, |
| 313 |
) |
| 314 |
newprednode = repo.commitctx(newctx) |
| 315 |
newpredctx = repo[newprednode] |
| 316 |
dirstate = repo.dirstate |
| 317 |
|
| 318 |
with dirstate.changing_parents(repo): |
| 319 |
scmutil.movedirstate(repo, newpredctx) |
| 320 |
|
| 321 |
mapping = {curctx.node(): (newprednode,)} |
| 322 |
cmdutil.cleanup_nodes(repo, mapping, b'unamend', fixphase=True) |