##// END OF EJS Templates
revlog: add a mechanism to verify expected file position before appending...
revlog: add a mechanism to verify expected file position before appending If someone uses `hg debuglocks`, or some non-hg process writes to the .hg directory without respecting the locks, or if the repo's on a networked filesystem, it's possible for the revlog code to write out corrupted data. The form of this corruption can vary depending on what data was written and how that happened. We are in the "networked filesystem" case (though I've had users also do this to themselves with the "`hg debuglocks`" scenario), and most often see this with the changelog. What ends up happening is we produce two items (let's call them rev1 and rev2) in the .i file that have the same linkrev, baserev, and offset into the .d file, while the data in the .d file is appended properly. rev2's compressed_size is accurate for rev2, but when we go to decompress the data in the .d file, we use the offset that's recorded in the index file, which is the same as rev1, and attempt to decompress rev2.compressed_size bytes of rev1's data. This usually does not succeed. :) When using inline data, this also fails, though I haven't investigated why too closely. This shows up as a "patch decode" error. I believe what's happening there is that we're basically ignoring the offset field, getting the data properly, but since baserev != rev, it thinks this is a delta based on rev (instead of a full text) and can't actually apply it as such. For now, I'm going to make this an optional component and default it to entirely off. I may increase the default severity of this in the future, once I've enabled it for my users and we gain more experience with it. Luckily, most of my users have a versioned filesystem and can roll back to before the corruption has been written, it's just a hassle to do so and not everyone knows how (so it's a support burden). Users on other filesystems will not have that luxury, and this can cause them to have a corrupted repository that they are unlikely to know how to resolve, and they'll see this as a data-loss event. Refusing to create the corruption is a much better user experience. This mechanism is not perfect. There may be false-negatives (racy writes that are not detected). There should not be any false-positives (non-racy writes that are detected as such). This is not a mechanism that makes putting a repo on a networked filesystem "safe" or "supported", just *less* likely to cause corruption. Differential Revision: https://phab.mercurial-scm.org/D9952

File last commit:

r46488:568c05d8 default
r47349:e9901d01 default
Show More
split.py
191 lines | 6.0 KiB | text/x-python | PythonLexer
# split.py - split a changeset into smaller ones
#
# Copyright 2015 Laurent Charignon <lcharignon@fb.com>
# Copyright 2017 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.
"""command to split a changeset into smaller ones (EXPERIMENTAL)"""
from __future__ import absolute_import
from mercurial.i18n import _
from mercurial.node import (
nullid,
short,
)
from mercurial import (
bookmarks,
cmdutil,
commands,
error,
hg,
pycompat,
registrar,
revsetlang,
rewriteutil,
scmutil,
)
# allow people to use split without explicitly enabling rebase extension
from . import rebase
cmdtable = {}
command = registrar.command(cmdtable)
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
# be specifying the version(s) of Mercurial they are tested with, or
# leave the attribute unspecified.
testedwith = b'ships-with-hg-core'
@command(
b'split',
[
(b'r', b'rev', b'', _(b"revision to split"), _(b'REV')),
(b'', b'rebase', True, _(b'rebase descendants after split')),
]
+ cmdutil.commitopts2,
_(b'hg split [--no-rebase] [[-r] REV]'),
helpcategory=command.CATEGORY_CHANGE_MANAGEMENT,
helpbasic=True,
)
def split(ui, repo, *revs, **opts):
"""split a changeset into smaller ones
Repeatedly prompt changes and commit message for new changesets until there
is nothing left in the original changeset.
If --rev was not given, split the working directory parent.
By default, rebase connected non-obsoleted descendants onto the new
changeset. Use --no-rebase to avoid the rebase.
"""
opts = pycompat.byteskwargs(opts)
revlist = []
if opts.get(b'rev'):
revlist.append(opts.get(b'rev'))
revlist.extend(revs)
with repo.wlock(), repo.lock(), repo.transaction(b'split') as tr:
revs = scmutil.revrange(repo, revlist or [b'.'])
if len(revs) > 1:
raise error.InputError(_(b'cannot split multiple revisions'))
rev = revs.first()
ctx = repo[rev]
# Handle nullid specially here (instead of leaving for precheck()
# below) so we get a nicer message and error code.
if rev is None or ctx.node() == nullid:
ui.status(_(b'nothing to split\n'))
return 1
if ctx.node() is None:
raise error.InputError(_(b'cannot split working directory'))
if opts.get(b'rebase'):
# Skip obsoleted descendants and their descendants so the rebase
# won't cause conflicts for sure.
descendants = list(repo.revs(b'(%d::) - (%d)', rev, rev))
torebase = list(
repo.revs(
b'%ld - (%ld & obsolete())::', descendants, descendants
)
)
else:
torebase = []
rewriteutil.precheck(repo, [rev] + torebase, b'split')
if len(ctx.parents()) > 1:
raise error.InputError(_(b'cannot split a merge changeset'))
cmdutil.bailifchanged(repo)
# Deactivate bookmark temporarily so it won't get moved unintentionally
bname = repo._activebookmark
if bname and repo._bookmarks[bname] != ctx.node():
bookmarks.deactivate(repo)
wnode = repo[b'.'].node()
top = None
try:
top = dosplit(ui, repo, tr, ctx, opts)
finally:
# top is None: split failed, need update --clean recovery.
# wnode == ctx.node(): wnode split, no need to update.
if top is None or wnode != ctx.node():
hg.clean(repo, wnode, show_stats=False)
if bname:
bookmarks.activate(repo, bname)
if torebase and top:
dorebase(ui, repo, torebase, top)
def dosplit(ui, repo, tr, ctx, opts):
committed = [] # [ctx]
# Set working parent to ctx.p1(), and keep working copy as ctx's content
if ctx.node() != repo.dirstate.p1():
hg.clean(repo, ctx.node(), show_stats=False)
with repo.dirstate.parentchange():
scmutil.movedirstate(repo, ctx.p1())
# Any modified, added, removed, deleted result means split is incomplete
def incomplete(repo):
st = repo.status()
return any((st.modified, st.added, st.removed, st.deleted))
# Main split loop
while incomplete(repo):
if committed:
header = _(
b'HG: Splitting %s. So far it has been split into:\n'
) % short(ctx.node())
# We don't want color codes in the commit message template, so
# disable the label() template function while we render it.
with ui.configoverride(
{(b'templatealias', b'label(l,x)'): b"x"}, b'split'
):
for c in committed:
summary = cmdutil.format_changeset_summary(ui, c, b'split')
header += _(b'HG: - %s\n') % summary
header += _(
b'HG: Write commit message for the next split changeset.\n'
)
else:
header = _(
b'HG: Splitting %s. Write commit message for the '
b'first split changeset.\n'
) % short(ctx.node())
opts.update(
{
b'edit': True,
b'interactive': True,
b'message': header + ctx.description(),
}
)
commands.commit(ui, repo, **pycompat.strkwargs(opts))
newctx = repo[b'.']
committed.append(newctx)
if not committed:
raise error.InputError(_(b'cannot split an empty revision'))
scmutil.cleanupnodes(
repo,
{ctx.node(): [c.node() for c in committed]},
operation=b'split',
fixphase=True,
)
return committed[-1]
def dorebase(ui, repo, src, destctx):
rebase.rebase(
ui,
repo,
rev=[revsetlang.formatspec(b'%ld', src)],
dest=revsetlang.formatspec(b'%d', destctx.rev()),
)