2007-06-25 10:26:44 +04:00
|
|
|
# repair.py - functions for repository repair for mercurial
|
|
|
|
#
|
|
|
|
# Copyright 2005, 2006 Chris Mason <mason@suse.com>
|
|
|
|
# Copyright 2007 Matt Mackall
|
|
|
|
#
|
2009-04-26 03:08:54 +04:00
|
|
|
# This software may be used and distributed according to the terms of the
|
2010-01-20 07:20:08 +03:00
|
|
|
# GNU General Public License version 2 or any later version.
|
2007-06-25 10:26:44 +04:00
|
|
|
|
2013-01-05 18:50:12 +04:00
|
|
|
from mercurial import changegroup
|
2011-04-30 15:59:14 +04:00
|
|
|
from mercurial.node import short
|
|
|
|
from mercurial.i18n import _
|
2009-04-28 19:40:46 +04:00
|
|
|
import os
|
2012-04-16 12:33:18 +04:00
|
|
|
import errno
|
2007-06-25 10:26:44 +04:00
|
|
|
|
2011-10-30 21:10:09 +04:00
|
|
|
def _bundle(repo, bases, heads, node, suffix, compress=True):
|
2008-01-19 23:01:16 +03:00
|
|
|
"""create a bundle with the specified revisions as a backup"""
|
2014-04-02 01:25:03 +04:00
|
|
|
cg = changegroup.changegroupsubset(repo, bases, heads, 'strip')
|
2008-01-19 23:01:16 +03:00
|
|
|
backupdir = repo.join("strip-backup")
|
|
|
|
if not os.path.isdir(backupdir):
|
|
|
|
os.mkdir(backupdir)
|
2010-06-11 00:23:31 +04:00
|
|
|
name = os.path.join(backupdir, "%s-%s.hg" % (short(node), suffix))
|
2010-08-12 11:53:23 +04:00
|
|
|
if compress:
|
|
|
|
bundletype = "HG10BZ"
|
|
|
|
else:
|
|
|
|
bundletype = "HG10UN"
|
|
|
|
return changegroup.writebundle(cg, name, bundletype)
|
2008-01-19 23:01:16 +03:00
|
|
|
|
2008-01-19 23:01:16 +03:00
|
|
|
def _collectfiles(repo, striprev):
|
|
|
|
"""find out the filelogs affected by the strip"""
|
2009-05-17 05:38:03 +04:00
|
|
|
files = set()
|
2008-01-19 23:01:16 +03:00
|
|
|
|
2008-06-26 23:35:50 +04:00
|
|
|
for x in xrange(striprev, len(repo)):
|
2009-05-17 18:55:51 +04:00
|
|
|
files.update(repo[x].files())
|
2008-01-19 23:01:16 +03:00
|
|
|
|
2009-05-17 05:38:03 +04:00
|
|
|
return sorted(files)
|
2008-01-19 23:01:16 +03:00
|
|
|
|
2011-03-20 02:50:22 +03:00
|
|
|
def _collectbrokencsets(repo, files, striprev):
|
|
|
|
"""return the changesets which will be broken by the truncation"""
|
2011-03-21 03:43:28 +03:00
|
|
|
s = set()
|
2011-03-20 02:50:22 +03:00
|
|
|
def collectone(revlog):
|
2013-11-12 04:42:49 +04:00
|
|
|
_, brokenset = revlog.getstrippoint(striprev)
|
|
|
|
s.update([revlog.linkrev(r) for r in brokenset])
|
2008-01-19 23:01:16 +03:00
|
|
|
|
2011-03-21 03:43:28 +03:00
|
|
|
collectone(repo.manifest)
|
2008-01-19 23:01:16 +03:00
|
|
|
for fname in files:
|
2011-03-21 03:43:28 +03:00
|
|
|
collectone(repo.file(fname))
|
|
|
|
|
|
|
|
return s
|
2008-01-19 23:01:16 +03:00
|
|
|
|
2012-04-06 17:18:33 +04:00
|
|
|
def strip(ui, repo, nodelist, backup="all", topic='backup'):
|
2012-10-08 19:12:09 +04:00
|
|
|
repo = repo.unfiltered()
|
2012-12-15 22:08:13 +04:00
|
|
|
repo.destroying()
|
2012-05-18 23:45:47 +04:00
|
|
|
|
2008-01-19 23:01:16 +03:00
|
|
|
cl = repo.changelog
|
2012-03-06 20:43:05 +04:00
|
|
|
# TODO handle undo of merge sets
|
strip: enhance repair.strip to receive a list of nodes (issue3299)
Originally, mq.strip called repair.strip a single rev at a time.
repair.strip stores in a backup bundle any revision greater than
the revision being stripped, strips, then restores the backup with
repo.addchangegroup. So, when stripping revisions on more than one
topological branch, some could end up being restored from the backup
bundle, only to be later removed by a subsequent repair.strip call.
But repo.addchangegroup calls hooks for all those restore operations.
And 1671d21e8e41 changed it to delay all hook calls until the
repository lock were released - by mq.strip, after stripping all
revisions. Thus, the hooks could be called over revisions already
removed from the repository at that point.
By generating the revision lists at once inside repo.strip, we avoid
calling addchangegroup for temporary restores. Incidentally, this
also avoids creating many backup files for a single strip command.
2012-03-13 00:02:45 +04:00
|
|
|
if isinstance(nodelist, str):
|
|
|
|
nodelist = [nodelist]
|
|
|
|
striplist = [cl.rev(node) for node in nodelist]
|
|
|
|
striprev = min(striplist)
|
2007-06-25 10:26:44 +04:00
|
|
|
|
2010-08-12 11:53:23 +04:00
|
|
|
keeppartialbundle = backup == 'strip'
|
|
|
|
|
2008-02-20 01:20:10 +03:00
|
|
|
# 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)
|
strip: enhance repair.strip to receive a list of nodes (issue3299)
Originally, mq.strip called repair.strip a single rev at a time.
repair.strip stores in a backup bundle any revision greater than
the revision being stripped, strips, then restores the backup with
repo.addchangegroup. So, when stripping revisions on more than one
topological branch, some could end up being restored from the backup
bundle, only to be later removed by a subsequent repair.strip call.
But repo.addchangegroup calls hooks for all those restore operations.
And 1671d21e8e41 changed it to delay all hook calls until the
repository lock were released - by mq.strip, after stripping all
revisions. Thus, the hooks could be called over revisions already
removed from the repository at that point.
By generating the revision lists at once inside repo.strip, we avoid
calling addchangegroup for temporary restores. Incidentally, this
also avoids creating many backup files for a single strip command.
2012-03-13 00:02:45 +04:00
|
|
|
tostrip = set(striplist)
|
|
|
|
for rev in striplist:
|
2012-06-01 23:45:16 +04:00
|
|
|
for desc in cl.descendants([rev]):
|
strip: enhance repair.strip to receive a list of nodes (issue3299)
Originally, mq.strip called repair.strip a single rev at a time.
repair.strip stores in a backup bundle any revision greater than
the revision being stripped, strips, then restores the backup with
repo.addchangegroup. So, when stripping revisions on more than one
topological branch, some could end up being restored from the backup
bundle, only to be later removed by a subsequent repair.strip call.
But repo.addchangegroup calls hooks for all those restore operations.
And 1671d21e8e41 changed it to delay all hook calls until the
repository lock were released - by mq.strip, after stripping all
revisions. Thus, the hooks could be called over revisions already
removed from the repository at that point.
By generating the revision lists at once inside repo.strip, we avoid
calling addchangegroup for temporary restores. Incidentally, this
also avoids creating many backup files for a single strip command.
2012-03-13 00:02:45 +04:00
|
|
|
tostrip.add(desc)
|
2011-03-20 02:50:22 +03:00
|
|
|
|
|
|
|
files = _collectfiles(repo, striprev)
|
2011-03-21 03:43:28 +03:00
|
|
|
saverevs = _collectbrokencsets(repo, files, striprev)
|
2011-03-20 02:50:22 +03:00
|
|
|
|
|
|
|
# compute heads
|
|
|
|
saveheads = set(saverevs)
|
2008-06-26 23:35:50 +04:00
|
|
|
for r in xrange(striprev + 1, len(cl)):
|
2011-03-20 02:50:22 +03:00
|
|
|
if r not in tostrip:
|
|
|
|
saverevs.add(r)
|
|
|
|
saveheads.difference_update(cl.parentrevs(r))
|
2009-05-17 05:38:03 +04:00
|
|
|
saveheads.add(r)
|
2011-03-20 02:50:22 +03:00
|
|
|
saveheads = [cl.node(r) for r in saveheads]
|
|
|
|
|
2011-10-30 21:10:09 +04:00
|
|
|
# compute base nodes
|
|
|
|
if saverevs:
|
2012-06-01 23:45:16 +04:00
|
|
|
descendants = set(cl.descendants(saverevs))
|
2011-10-30 21:10:09 +04:00
|
|
|
saverevs.difference_update(descendants)
|
|
|
|
savebases = [cl.node(r) for r in saverevs]
|
strip: enhance repair.strip to receive a list of nodes (issue3299)
Originally, mq.strip called repair.strip a single rev at a time.
repair.strip stores in a backup bundle any revision greater than
the revision being stripped, strips, then restores the backup with
repo.addchangegroup. So, when stripping revisions on more than one
topological branch, some could end up being restored from the backup
bundle, only to be later removed by a subsequent repair.strip call.
But repo.addchangegroup calls hooks for all those restore operations.
And 1671d21e8e41 changed it to delay all hook calls until the
repository lock were released - by mq.strip, after stripping all
revisions. Thus, the hooks could be called over revisions already
removed from the repository at that point.
By generating the revision lists at once inside repo.strip, we avoid
calling addchangegroup for temporary restores. Incidentally, this
also avoids creating many backup files for a single strip command.
2012-03-13 00:02:45 +04:00
|
|
|
stripbases = [cl.node(r) for r in tostrip]
|
strip: make query to get new bookmark target cheaper
The current query to get the new bookmark target for stripped revisions
involves multiple walks up the DAG, and is really expensive, taking over 2.5
seconds on a repository with over 400,000 changesets even if just one
changeset is being stripped.
A slightly simplified version of the current query is
max(heads(::<tostrip> - <tostrip>))
We make two observations here.
1. For any set s, max(heads(s)) == max(s). That is because revision numbers
define a topological order, so that the element with the highest revision
number in s will not have any children in s.
2. For any set s, max(::s - s) == max(parents(s) - s). In other words, the
ancestor of s with the highest revision number not in s is a parent of one
of the revs in s. Why? Because if it were an ancestor but not a parent of s,
it would have a descendant that would be a parent of s. This descendant
would have a higher revision number, leading to a contradiction.
Combining these two observations, we rewrite the revset query as
max(parents(<tostrip>) - <tostrip>)
The time complexity is now linear in the number of changesets being stripped.
For the above repository, the query now takes 0.1 seconds when one changeset
is stripped. This speeds up operations that use repair.strip, like the rebase
and strip commands.
2012-12-06 02:33:15 +04:00
|
|
|
|
|
|
|
# For a set s, max(parents(s) - s) is the same as max(heads(::s - s)), but
|
|
|
|
# is much faster
|
|
|
|
newbmtarget = repo.revs('max(parents(%ld) - (%ld))', tostrip, tostrip)
|
2012-07-27 01:57:50 +04:00
|
|
|
if newbmtarget:
|
2012-10-18 00:37:25 +04:00
|
|
|
newbmtarget = repo[newbmtarget[0]].node()
|
2012-07-27 01:57:50 +04:00
|
|
|
else:
|
|
|
|
newbmtarget = '.'
|
2008-02-20 01:20:10 +03:00
|
|
|
|
2011-02-10 22:46:28 +03:00
|
|
|
bm = repo._bookmarks
|
|
|
|
updatebm = []
|
|
|
|
for m in bm:
|
|
|
|
rev = repo[bm[m]].rev()
|
|
|
|
if rev in tostrip:
|
|
|
|
updatebm.append(m)
|
|
|
|
|
2007-06-25 10:26:44 +04:00
|
|
|
# create a changegroup for all the branches we need to keep
|
2010-05-20 04:46:39 +04:00
|
|
|
backupfile = None
|
2007-06-25 10:26:44 +04:00
|
|
|
if backup == "all":
|
2012-04-06 17:18:33 +04:00
|
|
|
backupfile = _bundle(repo, stripbases, cl.heads(), node, topic)
|
2010-05-20 21:15:44 +04:00
|
|
|
repo.ui.status(_("saved backup bundle to %s\n") % backupfile)
|
2013-03-13 21:43:51 +04:00
|
|
|
repo.ui.log("backupbundle", "saved backup bundle to %s\n", backupfile)
|
2011-10-30 21:10:09 +04:00
|
|
|
if saveheads or savebases:
|
2010-08-12 11:53:23 +04:00
|
|
|
# do not compress partial bundle if we remove it from disk later
|
2011-10-30 21:10:09 +04:00
|
|
|
chgrpfile = _bundle(repo, savebases, saveheads, node, 'temp',
|
|
|
|
compress=keeppartialbundle)
|
2007-06-25 10:26:44 +04:00
|
|
|
|
2009-04-16 17:34:03 +04:00
|
|
|
mfst = repo.manifest
|
|
|
|
|
2010-04-10 02:23:35 +04:00
|
|
|
tr = repo.transaction("strip")
|
2009-04-16 17:34:03 +04:00
|
|
|
offset = len(tr.entries)
|
|
|
|
|
|
|
|
try:
|
2010-05-20 04:46:39 +04:00
|
|
|
tr.startgroup()
|
|
|
|
cl.strip(striprev, tr)
|
|
|
|
mfst.strip(striprev, tr)
|
|
|
|
for fn in files:
|
|
|
|
repo.file(fn).strip(striprev, tr)
|
|
|
|
tr.endgroup()
|
|
|
|
|
|
|
|
try:
|
|
|
|
for i in xrange(offset, len(tr.entries)):
|
|
|
|
file, troffset, ignore = tr.entries[i]
|
|
|
|
repo.sopener(file, 'a').truncate(troffset)
|
2014-03-25 02:43:15 +04:00
|
|
|
if troffset == 0:
|
|
|
|
repo.store.markremoved(file)
|
2010-05-20 04:46:39 +04:00
|
|
|
tr.close()
|
2012-05-13 15:18:06 +04:00
|
|
|
except: # re-raises
|
2010-05-20 04:46:39 +04:00
|
|
|
tr.abort()
|
|
|
|
raise
|
|
|
|
|
2011-10-30 21:10:09 +04:00
|
|
|
if saveheads or savebases:
|
2010-05-20 22:27:28 +04:00
|
|
|
ui.note(_("adding branch\n"))
|
2010-05-20 04:46:39 +04:00
|
|
|
f = open(chgrpfile, "rb")
|
|
|
|
gen = changegroup.readbundle(f, chgrpfile)
|
2010-05-20 22:27:28 +04:00
|
|
|
if not repo.ui.verbose:
|
|
|
|
# silence internal shuffling chatter
|
|
|
|
repo.ui.pushbuffer()
|
2010-05-20 04:46:39 +04:00
|
|
|
repo.addchangegroup(gen, 'strip', 'bundle:' + chgrpfile, True)
|
2010-05-20 22:27:28 +04:00
|
|
|
if not repo.ui.verbose:
|
|
|
|
repo.ui.popbuffer()
|
2010-05-20 04:46:39 +04:00
|
|
|
f.close()
|
2010-08-12 11:53:23 +04:00
|
|
|
if not keeppartialbundle:
|
2010-05-20 04:46:39 +04:00
|
|
|
os.unlink(chgrpfile)
|
2011-02-10 22:46:28 +03:00
|
|
|
|
2012-03-06 20:43:05 +04:00
|
|
|
# remove undo files
|
|
|
|
for undofile in repo.undofiles():
|
|
|
|
try:
|
|
|
|
os.unlink(undofile)
|
|
|
|
except OSError, e:
|
|
|
|
if e.errno != errno.ENOENT:
|
|
|
|
ui.warn(_('error removing %s: %s\n') % (undofile, str(e)))
|
|
|
|
|
2011-02-10 22:46:28 +03:00
|
|
|
for m in updatebm:
|
2012-07-27 01:57:50 +04:00
|
|
|
bm[m] = repo[newbmtarget].node()
|
2012-11-08 02:21:39 +04:00
|
|
|
bm.write()
|
2012-05-13 15:18:06 +04:00
|
|
|
except: # re-raises
|
2010-05-20 04:46:39 +04:00
|
|
|
if backupfile:
|
2010-07-16 16:44:30 +04:00
|
|
|
ui.warn(_("strip failed, full bundle stored in '%s'\n")
|
|
|
|
% backupfile)
|
2010-05-20 04:46:39 +04:00
|
|
|
elif saveheads:
|
2010-07-16 16:44:30 +04:00
|
|
|
ui.warn(_("strip failed, partial bundle stored in '%s'\n")
|
2010-05-20 04:46:39 +04:00
|
|
|
% chgrpfile)
|
2009-04-16 17:34:03 +04:00
|
|
|
raise
|
2007-06-25 10:26:44 +04:00
|
|
|
|
2013-01-16 03:09:26 +04:00
|
|
|
repo.destroyed()
|