repair.py
561 lines
| 18.4 KiB
| text/x-python
|
PythonLexer
/ mercurial / repair.py
Matt Mackall
|
r4702 | # repair.py - functions for repository repair for mercurial | ||
# | ||||
# Copyright 2005, 2006 Chris Mason <mason@suse.com> | ||||
Raphaël Gomès
|
r47575 | # Copyright 2007 Olivia Mackall | ||
Matt Mackall
|
r4702 | # | ||
Martin Geisler
|
r8225 | # This software may be used and distributed according to the terms of the | ||
Matt Mackall
|
r10263 | # GNU General Public License version 2 or any later version. | ||
Matt Mackall
|
r4702 | |||
Gregory Szorc
|
r25970 | |||
Alain Leufroy
|
r16440 | import errno | ||
Matt Mackall
|
r4702 | |||
Gregory Szorc
|
r25970 | from .i18n import _ | ||
Augie Fackler
|
r34219 | from .node import ( | ||
hex, | ||||
short, | ||||
) | ||||
Gregory Szorc
|
r25970 | from . import ( | ||
bundle2, | ||||
changegroup, | ||||
r32468 | discovery, | |||
Pierre-Yves David
|
r26587 | error, | ||
Gregory Szorc
|
r25970 | exchange, | ||
Kostia Balytskyi
|
r28868 | obsolete, | ||
r33144 | obsutil, | |||
r43923 | pathutil, | |||
Boris Feld
|
r39781 | phases, | ||
Pulkit Goyal
|
r45932 | requirements, | ||
Pulkit Goyal
|
r46129 | scmutil, | ||
Martin von Zweigbergk
|
r48230 | util, | ||
Gregory Szorc
|
r25970 | ) | ||
Augie Fackler
|
r44517 | from .utils import ( | ||
hashutil, | ||||
stringutil, | ||||
r47669 | urlutil, | |||
Augie Fackler
|
r44517 | ) | ||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r25970 | |||
Augie Fackler
|
r43346 | def backupbundle( | ||
repo, bases, heads, node, suffix, compress=True, obsolescence=True | ||||
): | ||||
Alexis S. L. Carvalho
|
r5905 | """create a bundle with the specified revisions as a backup""" | ||
Eric Sumner
|
r23898 | |||
Augie Fackler
|
r43347 | backupdir = b"strip-backup" | ||
FUJIWARA Katsunori
|
r20977 | vfs = repo.vfs | ||
if not vfs.isdir(backupdir): | ||||
vfs.mkdir(backupdir) | ||||
Durham Goode
|
r23835 | |||
# Include a hash of all the nodes in the filename for uniqueness | ||||
Augie Fackler
|
r43347 | allcommits = repo.set(b'%ln::%ln', bases, heads) | ||
Durham Goode
|
r23835 | allhashes = sorted(c.hex() for c in allcommits) | ||
Augie Fackler
|
r44517 | totalhash = hashutil.sha1(b''.join(allhashes)).digest() | ||
Augie Fackler
|
r43347 | name = b"%s/%s-%s-%s.hg" % ( | ||
Augie Fackler
|
r43346 | backupdir, | ||
short(node), | ||||
hex(totalhash[:4]), | ||||
suffix, | ||||
) | ||||
Durham Goode
|
r23835 | |||
Martin von Zweigbergk
|
r34179 | cgversion = changegroup.localversion(repo) | ||
Pierre-Yves David
|
r26425 | comp = None | ||
Augie Fackler
|
r43347 | if cgversion != b'01': | ||
bundletype = b"HG20" | ||||
Pierre-Yves David
|
r26425 | if compress: | ||
Augie Fackler
|
r43347 | comp = b'BZ' | ||
Eric Sumner
|
r23898 | elif compress: | ||
Augie Fackler
|
r43347 | bundletype = b"HG10BZ" | ||
Nicolas Dumazet
|
r11791 | else: | ||
Augie Fackler
|
r43347 | bundletype = b"HG10UN" | ||
r32468 | ||||
Manuel Jacob
|
r45704 | outgoing = discovery.outgoing(repo, missingroots=bases, ancestorsof=heads) | ||
Martin von Zweigbergk
|
r33032 | contentopts = { | ||
Augie Fackler
|
r43347 | b'cg.version': cgversion, | ||
b'obsolescence': obsolescence, | ||||
b'phases': True, | ||||
Martin von Zweigbergk
|
r33032 | } | ||
Augie Fackler
|
r43346 | return bundle2.writenewbundle( | ||
repo.ui, | ||||
repo, | ||||
Augie Fackler
|
r43347 | b'strip', | ||
Augie Fackler
|
r43346 | name, | ||
bundletype, | ||||
outgoing, | ||||
contentopts, | ||||
vfs, | ||||
compression=comp, | ||||
) | ||||
Matt Mackall
|
r4702 | |||
Alexis S. L. Carvalho
|
r5910 | def _collectfiles(repo, striprev): | ||
"""find out the filelogs affected by the strip""" | ||||
Benoit Boissinot
|
r8462 | files = set() | ||
Matt Mackall
|
r4702 | |||
Manuel Jacob
|
r50179 | for x in range(striprev, len(repo)): | ||
Martin Geisler
|
r8479 | files.update(repo[x].files()) | ||
Alexis S. L. Carvalho
|
r5902 | |||
Benoit Boissinot
|
r8462 | return sorted(files) | ||
Alexis S. L. Carvalho
|
r5902 | |||
Augie Fackler
|
r43346 | |||
Durham Goode
|
r33690 | def _collectrevlog(revlog, striprev): | ||
_, brokenset = revlog.getstrippoint(striprev) | ||||
return [revlog.linkrev(r) for r in brokenset] | ||||
Augie Fackler
|
r43346 | |||
Benoit Boissinot
|
r13702 | def _collectbrokencsets(repo, files, striprev): | ||
"""return the changesets which will be broken by the truncation""" | ||||
Matt Mackall
|
r13705 | s = set() | ||
Alexis S. L. Carvalho
|
r5909 | |||
Martin von Zweigbergk
|
r43183 | for revlog in manifestrevlogs(repo): | ||
s.update(_collectrevlog(revlog, striprev)) | ||||
Matt Mackall
|
r13705 | for fname in files: | ||
Durham Goode
|
r33690 | s.update(_collectrevlog(repo.file(fname), striprev)) | ||
Alexis S. L. Carvalho
|
r5909 | |||
Matt Mackall
|
r13705 | return s | ||
Alexis S. L. Carvalho
|
r5909 | |||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r43347 | def strip(ui, repo, nodelist, backup=True, topic=b'backup'): | ||
Martin von Zweigbergk
|
r32922 | # This function requires the caller to lock the repo, but it operates | ||
# within a transaction of its own, and thus requires there to be no current | ||||
# transaction when it is called. | ||||
Martin von Zweigbergk
|
r32924 | if repo.currenttransaction() is not None: | ||
Augie Fackler
|
r43347 | raise error.ProgrammingError(b'cannot strip from inside a transaction') | ||
Martin von Zweigbergk
|
r32924 | |||
Jordi Gutiérrez Hermoso
|
r22057 | # Simple way to maintain backwards compatibility for this | ||
# argument. | ||||
Augie Fackler
|
r43347 | if backup in [b'none', b'strip']: | ||
Jordi Gutiérrez Hermoso
|
r22057 | backup = False | ||
Pierre-Yves David
|
r18004 | repo = repo.unfiltered() | ||
Idan Kamara
|
r18310 | repo.destroying() | ||
Boris Feld
|
r41131 | vfs = repo.vfs | ||
r42902 | # load bookmark before changelog to avoid side effect from outdated | |||
# changelog (see repo._refreshchangelog) | ||||
repo._bookmarks | ||||
Boris Feld
|
r41131 | cl = repo.changelog | ||
Joshua Redstone
|
r17013 | |||
Idan Kamara
|
r16237 | # TODO handle undo of merge sets | ||
Matt Harbison
|
r44179 | if isinstance(nodelist, bytes): | ||
Wagner Bruna
|
r16252 | nodelist = [nodelist] | ||
striplist = [cl.rev(node) for node in nodelist] | ||||
striprev = min(striplist) | ||||
Matt Mackall
|
r4702 | |||
Martin von Zweigbergk
|
r30707 | files = _collectfiles(repo, striprev) | ||
saverevs = _collectbrokencsets(repo, files, striprev) | ||||
Alexis S. L. Carvalho
|
r6147 | # Some revisions with rev > striprev may not be descendants of striprev. | ||
# We have to find these revisions and put them in a bundle, so that | ||||
# we can restore them after the truncations. | ||||
# To create the bundle we use repo.changegroupsubset which requires | ||||
# the list of heads and bases of the set of interesting revisions. | ||||
# (head = revision in the set that has no descendant in the set; | ||||
# base = revision in the set that has no ancestor in the set) | ||||
Wagner Bruna
|
r16252 | tostrip = set(striplist) | ||
Martin von Zweigbergk
|
r30707 | saveheads = set(saverevs) | ||
Martin von Zweigbergk
|
r30706 | for r in cl.revs(start=striprev + 1): | ||
if any(p in tostrip for p in cl.parentrevs(r)): | ||||
tostrip.add(r) | ||||
Benoit Boissinot
|
r13702 | |||
if r not in tostrip: | ||||
saverevs.add(r) | ||||
saveheads.difference_update(cl.parentrevs(r)) | ||||
saveheads.add(r) | ||||
saveheads = [cl.node(r) for r in saveheads] | ||||
Matt Mackall
|
r4702 | |||
Matt Mackall
|
r15386 | # compute base nodes | ||
if saverevs: | ||||
Bryan O'Sullivan
|
r16867 | descendants = set(cl.descendants(saverevs)) | ||
Matt Mackall
|
r15386 | saverevs.difference_update(descendants) | ||
savebases = [cl.node(r) for r in saverevs] | ||||
Wagner Bruna
|
r16252 | stripbases = [cl.node(r) for r in tostrip] | ||
Siddharth Agarwal
|
r18040 | |||
r32629 | stripobsidx = obsmarkers = () | |||
Augie Fackler
|
r43347 | if repo.ui.configbool(b'devel', b'strip-obsmarkers'): | ||
r33144 | obsmarkers = obsutil.exclusivemarkers(repo, stripbases) | |||
r32629 | if obsmarkers: | |||
Augie Fackler
|
r43346 | stripobsidx = [ | ||
i for i, m in enumerate(repo.obsstore) if m in obsmarkers | ||||
] | ||||
r32629 | ||||
Boris Feld
|
r41135 | newbmtarget, updatebm = _bookmarkmovements(repo, tostrip) | ||
Matt Mackall
|
r13362 | |||
Matt Mackall
|
r11197 | backupfile = None | ||
Mike Edgar
|
r24252 | node = nodelist[-1] | ||
Jordi Gutiérrez Hermoso
|
r22057 | if backup: | ||
Boris Feld
|
r41133 | backupfile = _createstripbackup(repo, stripbases, node, topic) | ||
Boris Feld
|
r41132 | # create a changegroup for all the branches we need to keep | ||
Martin von Zweigbergk
|
r29954 | tmpbundlefile = None | ||
Martin von Zweigbergk
|
r29951 | if saveheads: | ||
Martin von Zweigbergk
|
r29954 | # do not compress temporary bundle if we remove it from disk later | ||
r32628 | # | |||
# We do not include obsolescence, it might re-introduce prune markers | ||||
# we are trying to strip. This is harmless since the stripped markers | ||||
# are already backed up and we did not touched the markers for the | ||||
# saved changesets. | ||||
Augie Fackler
|
r43346 | tmpbundlefile = backupbundle( | ||
repo, | ||||
savebases, | ||||
saveheads, | ||||
node, | ||||
Augie Fackler
|
r43347 | b'temp', | ||
Augie Fackler
|
r43346 | compress=False, | ||
obsolescence=False, | ||||
) | ||||
Matt Mackall
|
r4702 | |||
Kyle Lippincott
|
r41106 | with ui.uninterruptible(): | ||
Augie Fackler
|
r38546 | try: | ||
Augie Fackler
|
r43347 | with repo.transaction(b"strip") as tr: | ||
Gregory Szorc
|
r39722 | # TODO this code violates the interface abstraction of the | ||
# transaction and makes assumptions that file storage is | ||||
# using append-only files. We'll need some kind of storage | ||||
# API to handle stripping for us. | ||||
Joerg Sonnenberger
|
r46483 | oldfiles = set(tr._offsetmap.keys()) | ||
Joerg Sonnenberger
|
r46484 | oldfiles.update(tr._newfiles) | ||
Henrik Stuart
|
r8073 | |||
Augie Fackler
|
r38546 | tr.startgroup() | ||
cl.strip(striprev, tr) | ||||
stripmanifest(repo, striprev, tr, files) | ||||
Henrik Stuart
|
r8073 | |||
Augie Fackler
|
r38546 | for fn in files: | ||
repo.file(fn).strip(striprev, tr) | ||||
tr.endgroup() | ||||
Matt Mackall
|
r11197 | |||
Joerg Sonnenberger
|
r46483 | entries = tr.readjournal() | ||
for file, troffset in entries: | ||||
if file in oldfiles: | ||||
continue | ||||
Augie Fackler
|
r43347 | with repo.svfs(file, b'a', checkambig=True) as fp: | ||
Augie Fackler
|
r38546 | fp.truncate(troffset) | ||
if troffset == 0: | ||||
repo.store.markremoved(file) | ||||
deleteobsmarkers(repo.obsstore, stripobsidx) | ||||
del repo.obsstore | ||||
repo.invalidatevolatilesets() | ||||
repo._phasecache.filterunknown(repo) | ||||
r32629 | ||||
Augie Fackler
|
r38546 | if tmpbundlefile: | ||
Augie Fackler
|
r43347 | ui.note(_(b"adding branch\n")) | ||
f = vfs.open(tmpbundlefile, b"rb") | ||||
Augie Fackler
|
r38546 | gen = exchange.readbundle(ui, f, tmpbundlefile, vfs) | ||
Martin von Zweigbergk
|
r48230 | # silence internal shuffling chatter | ||
maybe_silent = ( | ||||
repo.ui.silent() | ||||
if not repo.ui.verbose | ||||
else util.nullcontextmanager() | ||||
) | ||||
with maybe_silent: | ||||
tmpbundleurl = b'bundle:' + vfs.join(tmpbundlefile) | ||||
txnname = b'strip' | ||||
if not isinstance(gen, bundle2.unbundle20): | ||||
txnname = b"strip\n%s" % urlutil.hidepassword( | ||||
tmpbundleurl | ||||
) | ||||
with repo.transaction(txnname) as tr: | ||||
bundle2.applybundle( | ||||
repo, gen, tr, source=b'strip', url=tmpbundleurl | ||||
) | ||||
Augie Fackler
|
r38546 | f.close() | ||
Matt Harbison
|
r31626 | |||
Augie Fackler
|
r43347 | with repo.transaction(b'repair') as tr: | ||
Augie Fackler
|
r38546 | bmchanges = [(m, repo[newbmtarget].node()) for m in updatebm] | ||
Boris Feld
|
r41135 | repo._bookmarks.applychanges(repo, tr, bmchanges) | ||
Laurent Charignon
|
r27157 | |||
Augie Fackler
|
r38546 | # remove undo files | ||
for undovfs, undofile in repo.undofiles(): | ||||
try: | ||||
undovfs.unlink(undofile) | ||||
except OSError as e: | ||||
if e.errno != errno.ENOENT: | ||||
Augie Fackler
|
r43346 | ui.warn( | ||
Augie Fackler
|
r43347 | _(b'error removing %s: %s\n') | ||
Augie Fackler
|
r43346 | % ( | ||
undovfs.join(undofile), | ||||
stringutil.forcebytestr(e), | ||||
) | ||||
) | ||||
Idan Kamara
|
r16237 | |||
Augie Fackler
|
r43346 | except: # re-raises | ||
Augie Fackler
|
r38546 | if backupfile: | ||
Augie Fackler
|
r43346 | ui.warn( | ||
Augie Fackler
|
r43347 | _(b"strip failed, backup bundle stored in '%s'\n") | ||
Augie Fackler
|
r43346 | % vfs.join(backupfile) | ||
) | ||||
Augie Fackler
|
r38546 | if tmpbundlefile: | ||
Augie Fackler
|
r43346 | ui.warn( | ||
Augie Fackler
|
r43347 | _(b"strip failed, unrecovered changes stored in '%s'\n") | ||
Augie Fackler
|
r43346 | % vfs.join(tmpbundlefile) | ||
) | ||||
ui.warn( | ||||
_( | ||||
Augie Fackler
|
r43347 | b"(fix the problem, then recover the changesets with " | ||
b"\"hg unbundle '%s'\")\n" | ||||
Augie Fackler
|
r43346 | ) | ||
% vfs.join(tmpbundlefile) | ||||
) | ||||
Augie Fackler
|
r38546 | raise | ||
else: | ||||
if tmpbundlefile: | ||||
# Remove temporary bundle only if there were no exceptions | ||||
vfs.unlink(tmpbundlefile) | ||||
Matt Mackall
|
r4702 | |||
Pierre-Yves David
|
r18395 | repo.destroyed() | ||
Martin von Zweigbergk
|
r30274 | # return the backup file path (or None if 'backup' was False) so | ||
# extensions can use it | ||||
return backupfile | ||||
Gregory Szorc
|
r25652 | |||
Augie Fackler
|
r43346 | |||
Augie Fackler
|
r43347 | def softstrip(ui, repo, nodelist, backup=True, topic=b'backup'): | ||
Boris Feld
|
r41960 | """perform a "soft" strip using the archived phase""" | ||
Augie Fackler
|
r43347 | tostrip = [c.node() for c in repo.set(b'sort(%ln::)', nodelist)] | ||
Boris Feld
|
r41960 | if not tostrip: | ||
return None | ||||
Martin von Zweigbergk
|
r47265 | backupfile = None | ||
Boris Feld
|
r41960 | if backup: | ||
node = tostrip[0] | ||||
backupfile = _createstripbackup(repo, tostrip, node, topic) | ||||
Martin von Zweigbergk
|
r47264 | newbmtarget, updatebm = _bookmarkmovements(repo, tostrip) | ||
Augie Fackler
|
r43347 | with repo.transaction(b'strip') as tr: | ||
Boris Feld
|
r41960 | phases.retractboundary(repo, tr, phases.archived, tostrip) | ||
bmchanges = [(m, repo[newbmtarget].node()) for m in updatebm] | ||||
repo._bookmarks.applychanges(repo, tr, bmchanges) | ||||
return backupfile | ||||
Boris Feld
|
r41135 | def _bookmarkmovements(repo, tostrip): | ||
# compute necessary bookmark movement | ||||
bm = repo._bookmarks | ||||
updatebm = [] | ||||
for m in bm: | ||||
rev = repo[bm[m]].rev() | ||||
if rev in tostrip: | ||||
updatebm.append(m) | ||||
newbmtarget = None | ||||
Augie Fackler
|
r42432 | # If we need to move bookmarks, compute bookmark | ||
# targets. Otherwise we can skip doing this logic. | ||||
if updatebm: | ||||
Boris Feld
|
r41135 | # For a set s, max(parents(s) - s) is the same as max(heads(::s - s)), | ||
# but is much faster | ||||
Augie Fackler
|
r43347 | newbmtarget = repo.revs(b'max(parents(%ld) - (%ld))', tostrip, tostrip) | ||
Boris Feld
|
r41135 | if newbmtarget: | ||
newbmtarget = repo[newbmtarget.first()].node() | ||||
else: | ||||
Augie Fackler
|
r43347 | newbmtarget = b'.' | ||
Boris Feld
|
r41135 | return newbmtarget, updatebm | ||
Augie Fackler
|
r43346 | |||
Boris Feld
|
r41133 | def _createstripbackup(repo, stripbases, node, topic): | ||
# backup the changeset we are about to strip | ||||
vfs = repo.vfs | ||||
cl = repo.changelog | ||||
backupfile = backupbundle(repo, stripbases, cl.heads(), node, topic) | ||||
Augie Fackler
|
r43347 | repo.ui.status(_(b"saved backup bundle to %s\n") % vfs.join(backupfile)) | ||
Augie Fackler
|
r43346 | repo.ui.log( | ||
Augie Fackler
|
r43347 | b"backupbundle", b"saved backup bundle to %s\n", vfs.join(backupfile) | ||
Augie Fackler
|
r43346 | ) | ||
Boris Feld
|
r41133 | return backupfile | ||
Augie Fackler
|
r43346 | |||
Jun Wu
|
r33087 | def safestriproots(ui, repo, nodes): | ||
"""return list of roots of nodes where descendants are covered by nodes""" | ||||
torev = repo.unfiltered().changelog.rev | ||||
Augie Fackler
|
r44937 | revs = {torev(n) for n in nodes} | ||
Jun Wu
|
r33087 | # tostrip = wanted - unsafe = wanted - ancestors(orphaned) | ||
# orphaned = affected - wanted | ||||
# affected = descendants(roots(wanted)) | ||||
# wanted = revs | ||||
Augie Fackler
|
r43347 | revset = b'%ld - ( ::( (roots(%ld):: and not _phase(%s)) -%ld) )' | ||
Boris Feld
|
r39781 | tostrip = set(repo.revs(revset, revs, revs, phases.internal, revs)) | ||
Jun Wu
|
r33087 | notstrip = revs - tostrip | ||
if notstrip: | ||||
Augie Fackler
|
r43347 | nodestr = b', '.join(sorted(short(repo[n].node()) for n in notstrip)) | ||
Augie Fackler
|
r43346 | ui.warn( | ||
Martin von Zweigbergk
|
r43387 | _(b'warning: orphaned descendants detected, not stripping %s\n') | ||
Augie Fackler
|
r43346 | % nodestr | ||
) | ||||
Augie Fackler
|
r43347 | return [c.node() for c in repo.set(b'roots(%ld)', tostrip)] | ||
Jun Wu
|
r33087 | |||
Augie Fackler
|
r43346 | |||
Gregory Szorc
|
r49801 | class stripcallback: | ||
Jun Wu
|
r33087 | """used as a transaction postclose callback""" | ||
def __init__(self, ui, repo, backup, topic): | ||||
self.ui = ui | ||||
self.repo = repo | ||||
self.backup = backup | ||||
Augie Fackler
|
r43347 | self.topic = topic or b'backup' | ||
Jun Wu
|
r33087 | self.nodelist = [] | ||
def addnodes(self, nodes): | ||||
self.nodelist.extend(nodes) | ||||
def __call__(self, tr): | ||||
roots = safestriproots(self.ui, self.repo, self.nodelist) | ||||
if roots: | ||||
Jun Wu
|
r33108 | strip(self.ui, self.repo, roots, self.backup, self.topic) | ||
Jun Wu
|
r33087 | |||
Augie Fackler
|
r43346 | |||
Sushil khanchi
|
r38835 | def delayedstrip(ui, repo, nodelist, topic=None, backup=True): | ||
Jun Wu
|
r33087 | """like strip, but works inside transaction and won't strip irreverent revs | ||
nodelist must explicitly contain all descendants. Otherwise a warning will | ||||
be printed that some nodes are not stripped. | ||||
Sushil khanchi
|
r38835 | Will do a backup if `backup` is True. The last non-None "topic" will be | ||
used as the backup topic name. The default backup topic name is "backup". | ||||
Jun Wu
|
r33087 | """ | ||
tr = repo.currenttransaction() | ||||
if not tr: | ||||
nodes = safestriproots(ui, repo, nodelist) | ||||
Sushil khanchi
|
r38835 | return strip(ui, repo, nodes, backup=backup, topic=topic) | ||
Jun Wu
|
r33087 | # transaction postclose callbacks are called in alphabet order. | ||
# use '\xff' as prefix so we are likely to be called last. | ||||
Augie Fackler
|
r43347 | callback = tr.getpostclose(b'\xffstrip') | ||
Jun Wu
|
r33087 | if callback is None: | ||
Sushil khanchi
|
r38835 | callback = stripcallback(ui, repo, backup=backup, topic=topic) | ||
Augie Fackler
|
r43347 | tr.addpostclose(b'\xffstrip', callback) | ||
Jun Wu
|
r33087 | if topic: | ||
callback.topic = topic | ||||
callback.addnodes(nodelist) | ||||
Augie Fackler
|
r43346 | |||
Durham Goode
|
r33691 | def stripmanifest(repo, striprev, tr, files): | ||
Martin von Zweigbergk
|
r43182 | for revlog in manifestrevlogs(repo): | ||
revlog.strip(striprev, tr) | ||||
Durham Goode
|
r33691 | |||
Augie Fackler
|
r43346 | |||
Martin von Zweigbergk
|
r43182 | def manifestrevlogs(repo): | ||
yield repo.manifestlog.getstorage(b'') | ||||
Pulkit Goyal
|
r46129 | if scmutil.istreemanifest(repo): | ||
Augie Fackler
|
r42439 | # This logic is safe if treemanifest isn't enabled, but also | ||
# pointless, so we skip it if treemanifest isn't enabled. | ||||
Valentin Gatien-Baron
|
r48691 | for t, unencoded, size in repo.store.datafiles(): | ||
Augie Fackler
|
r43347 | if unencoded.startswith(b'meta/') and unencoded.endswith( | ||
b'00manifest.i' | ||||
Augie Fackler
|
r43346 | ): | ||
Durham Goode
|
r32296 | dir = unencoded[5:-12] | ||
Martin von Zweigbergk
|
r43182 | yield repo.manifestlog.getstorage(dir) | ||
Durham Goode
|
r32196 | |||
Augie Fackler
|
r43346 | |||
Valentin Gatien-Baron
|
r48689 | def rebuildfncache(ui, repo, only_data=False): | ||
Gregory Szorc
|
r25652 | """Rebuilds the fncache file from repo history. | ||
Missing entries will be added. Extra entries will be removed. | ||||
""" | ||||
repo = repo.unfiltered() | ||||
Raphaël Gomès
|
r47383 | if requirements.FNCACHE_REQUIREMENT not in repo.requirements: | ||
Augie Fackler
|
r43346 | ui.warn( | ||
_( | ||||
Augie Fackler
|
r43347 | b'(not rebuilding fncache because repository does not ' | ||
b'support fncache)\n' | ||||
Augie Fackler
|
r43346 | ) | ||
) | ||||
Gregory Szorc
|
r25652 | return | ||
Bryan O'Sullivan
|
r27860 | with repo.lock(): | ||
Gregory Szorc
|
r25652 | fnc = repo.store.fncache | ||
Valentin Gatien-Baron
|
r42960 | fnc.ensureloaded(warn=ui.warn) | ||
Gregory Szorc
|
r25652 | |||
oldentries = set(fnc.entries) | ||||
newentries = set() | ||||
seenfiles = set() | ||||
Valentin Gatien-Baron
|
r48689 | if only_data: | ||
# Trust the listing of .i from the fncache, but not the .d. This is | ||||
# much faster, because we only need to stat every possible .d files, | ||||
# instead of reading the full changelog | ||||
for f in fnc: | ||||
if f[:5] == b'data/' and f[-2:] == b'.i': | ||||
seenfiles.add(f[5:-2]) | ||||
newentries.add(f) | ||||
dataf = f[:-2] + b'.d' | ||||
if repo.store._exists(dataf): | ||||
newentries.add(dataf) | ||||
else: | ||||
progress = ui.makeprogress( | ||||
_(b'rebuilding'), unit=_(b'changesets'), total=len(repo) | ||||
) | ||||
for rev in repo: | ||||
progress.update(rev) | ||||
Gregory Szorc
|
r25652 | |||
Valentin Gatien-Baron
|
r48689 | ctx = repo[rev] | ||
for f in ctx.files(): | ||||
# This is to minimize I/O. | ||||
if f in seenfiles: | ||||
continue | ||||
seenfiles.add(f) | ||||
Gregory Szorc
|
r25652 | |||
Valentin Gatien-Baron
|
r48689 | i = b'data/%s.i' % f | ||
d = b'data/%s.d' % f | ||||
Gregory Szorc
|
r25652 | |||
Valentin Gatien-Baron
|
r48689 | if repo.store._exists(i): | ||
newentries.add(i) | ||||
if repo.store._exists(d): | ||||
newentries.add(d) | ||||
Gregory Szorc
|
r25652 | |||
Valentin Gatien-Baron
|
r48689 | progress.complete() | ||
Gregory Szorc
|
r25652 | |||
Pulkit Goyal
|
r45932 | if requirements.TREEMANIFEST_REQUIREMENT in repo.requirements: | ||
Augie Fackler
|
r42439 | # This logic is safe if treemanifest isn't enabled, but also | ||
# pointless, so we skip it if treemanifest isn't enabled. | ||||
r43923 | for dir in pathutil.dirs(seenfiles): | |||
Augie Fackler
|
r43347 | i = b'meta/%s/00manifest.i' % dir | ||
d = b'meta/%s/00manifest.d' % dir | ||||
Martin von Zweigbergk
|
r28007 | |||
if repo.store._exists(i): | ||||
newentries.add(i) | ||||
if repo.store._exists(d): | ||||
newentries.add(d) | ||||
Gregory Szorc
|
r25652 | addcount = len(newentries - oldentries) | ||
removecount = len(oldentries - newentries) | ||||
for p in sorted(oldentries - newentries): | ||||
Augie Fackler
|
r43347 | ui.write(_(b'removing %s\n') % p) | ||
Gregory Szorc
|
r25652 | for p in sorted(newentries - oldentries): | ||
Augie Fackler
|
r43347 | ui.write(_(b'adding %s\n') % p) | ||
Gregory Szorc
|
r25652 | |||
if addcount or removecount: | ||||
Augie Fackler
|
r43346 | ui.write( | ||
Augie Fackler
|
r43347 | _(b'%d items added, %d removed from fncache\n') | ||
Augie Fackler
|
r43346 | % (addcount, removecount) | ||
) | ||||
Gregory Szorc
|
r25652 | fnc.entries = newentries | ||
fnc._dirty = True | ||||
Augie Fackler
|
r43347 | with repo.transaction(b'fncache') as tr: | ||
Gregory Szorc
|
r25652 | fnc.write(tr) | ||
else: | ||||
Augie Fackler
|
r43347 | ui.write(_(b'fncache already up to date\n')) | ||
Gregory Szorc
|
r25652 | |||
Augie Fackler
|
r43346 | |||
Kostia Balytskyi
|
r28868 | def deleteobsmarkers(obsstore, indices): | ||
"""Delete some obsmarkers from obsstore and return how many were deleted | ||||
'indices' is a list of ints which are the indices | ||||
of the markers to be deleted. | ||||
Every invocation of this function completely rewrites the obsstore file, | ||||
skipping the markers we want to be removed. The new temporary file is | ||||
created, remaining markers are written there and on .close() this file | ||||
gets atomically renamed to obsstore, thus guaranteeing consistency.""" | ||||
if not indices: | ||||
# we don't want to rewrite the obsstore with the same content | ||||
return | ||||
left = [] | ||||
current = obsstore._all | ||||
n = 0 | ||||
for i, m in enumerate(current): | ||||
if i in indices: | ||||
n += 1 | ||||
continue | ||||
left.append(m) | ||||
Augie Fackler
|
r43347 | newobsstorefile = obsstore.svfs(b'obsstore', b'w', atomictemp=True) | ||
Kostia Balytskyi
|
r28868 | for bytes in obsolete.encodemarkers(left, True, obsstore._version): | ||
newobsstorefile.write(bytes) | ||||
newobsstorefile.close() | ||||
return n | ||||