fastannotate: replace some assertions to formal errors

Summary:
fastannotate can rebuild cache automatically if it sees `CorruptedFileError` or
`CannotReuseError`, but not for `AssertionError`.

With `strip`, some assertion errors really mean file corruption. So let's use
correct exceptions so corrupted files get rebuilt automatically.

Test Plan: Run existing tests.

Reviewers: #mercurial, durham

Reviewed By: durham

Subscribers: mjpieters

Differential Revision: https://phabricator.intern.facebook.com/D5013658

Signature: t1:5013658:1494018897:a04032103b3e0cbc509adeae0d88c0a783a0dbdb
This commit is contained in:
Jun Wu 2017-05-05 14:17:35 -07:00
parent cd7eed4dd0
commit d2965bcd6a
2 changed files with 23 additions and 9 deletions

View File

@ -560,7 +560,8 @@ class _annotatecontext(object):
fctx = self._resolvefctx(hsh, revmap.rev2path(rev)) fctx = self._resolvefctx(hsh, revmap.rev2path(rev))
annotated = linelog.annotateresult annotated = linelog.annotateresult
lines = mdiff.splitnewlines(fctx.data()) lines = mdiff.splitnewlines(fctx.data())
assert len(lines) == len(annotated) if len(lines) != len(annotated):
raise faerror.CorruptedFileError('unexpected annotated lines')
# resolve lines from the annotate result # resolve lines from the annotate result
for i, line in enumerate(lines): for i, line in enumerate(lines):
k = annotated[i] k = annotated[i]
@ -581,8 +582,12 @@ class _annotatecontext(object):
else: else:
hsh = f.node() hsh = f.node()
llrev = self.revmap.hsh2rev(hsh) llrev = self.revmap.hsh2rev(hsh)
assert llrev if not llrev:
assert (self.revmap.rev2flag(llrev) & revmapmod.sidebranchflag) == 0 raise faerror.CorruptedFileError('%s is not in revmap'
% node.hex(hsh))
if (self.revmap.rev2flag(llrev) & revmapmod.sidebranchflag) != 0:
raise faerror.CorruptedFileError('%s is not in revmap mainbranch'
% node.hex(hsh))
self.linelog.annotate(llrev) self.linelog.annotate(llrev)
result = [(self.revmap.rev2hsh(r), l) result = [(self.revmap.rev2hsh(r), l)
for r, l in self.linelog.annotateresult] for r, l in self.linelog.annotateresult]
@ -619,7 +624,8 @@ class _annotatecontext(object):
def getllrev(f): def getllrev(f):
"""(fctx) -> int""" """(fctx) -> int"""
# f should not be a linelog revision # f should not be a linelog revision
assert not isinstance(f, int) if isinstance(f, int):
raise error.ProgrammingError('f should not be an int')
# f is a fctx, allocate linelog rev on demand # f is a fctx, allocate linelog rev on demand
hsh = f.node() hsh = f.node()
rev = revmap.hsh2rev(hsh) rev = revmap.hsh2rev(hsh)
@ -676,7 +682,8 @@ class _annotatecontext(object):
llrev = 0 llrev = 0
else: else:
llrev = self.revmap.hsh2rev(fctx.node()) llrev = self.revmap.hsh2rev(fctx.node())
assert llrev if not llrev:
raise faerror.CannotReuseError()
if self.linelog.maxrev != llrev: if self.linelog.maxrev != llrev:
raise faerror.CannotReuseError() raise faerror.CannotReuseError()

View File

@ -5,11 +5,15 @@
# This software may be used and distributed according to the terms of the # This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version. # GNU General Public License version 2 or any later version.
from __future__ import absolute_import
import bisect import bisect
import os import os
import struct import struct
from fastannotate import error from . import error
from mercurial import error as hgerror
from mercurial.node import hex
# the revmap file format is straightforward: # the revmap file format is straightforward:
# #
@ -88,8 +92,10 @@ class revmap(object):
"""add a binary hg hash and return the mapped linelog revision. """add a binary hg hash and return the mapped linelog revision.
if flush is True, incrementally update the file. if flush is True, incrementally update the file.
""" """
assert hsh not in self._hsh2rev if hsh in self._hsh2rev:
assert len(hsh) == _hshlen raise error.CorruptedFileError('%r is in revmap already' % hex(hsh))
if len(hsh) != _hshlen:
raise hgerror.ProgrammingError('hsh must be %d-char long' % _hshlen)
idx = len(self._rev2hsh) idx = len(self._rev2hsh)
flag = 0 flag = 0
if sidebranch: if sidebranch:
@ -195,7 +201,8 @@ class revmap(object):
f.write(struct.pack('B', flag)) f.write(struct.pack('B', flag))
if flag & renameflag: if flag & renameflag:
path = self.rev2path(rev) path = self.rev2path(rev)
assert path is not None if path is None:
raise error.CorruptedFileError('cannot find path for %s' % rev)
f.write(path + '\0') f.write(path + '\0')
f.write(hsh) f.write(hsh)