##// END OF EJS Templates
patch: fix patch hunk/metdata synchronization (issue3384)...
patch: fix patch hunk/metdata synchronization (issue3384) Git patches are parsed in two phases: 1) extract metadata, 2) parse actual deltas and merge them with the previous metadata. We do this to avoid dependency issues like "modify a; copy a to b", where "b" must be copied from the unmodified "a". Issue3384 is caused by flaky code I wrote to synchronize the patch metadata with the emitted hunk: if (gitpatches and (gitpatches[-1][0] == afile or gitpatches[-1][1] == bfile)): gp = gitpatches.pop()[2] With a patch like: diff --git a/a b/c copy from a copy to c --- a/a +++ b/c @@ -1,1 +1,2 @@ a +a @@ -2,1 +2,2 @@ a +a diff --git a/a b/a --- a/a +++ b/a @@ -1,1 +1,2 @@ a +b the first hunk of the first block is matched with the metadata for the block "diff --git a/a b/c", then the second hunk of the first block is matched with the metadata of the second block "diff --git a/a b/a", because of the "or" in the code paste above. Turning the "or" into an "and" is not enough as we have to deal with /dev/null cases for each file. We I remove this broken piece of code: # copy/rename + modify should modify target, not source if gp.op in ('COPY', 'DELETE', 'RENAME', 'ADD') or gp.mode: afile = bfile because "afile = bfile" set "afile" to stuff like "b/file" instead of "a/file", and because this only happens for git patches, which afile/bfile are ignored anyway by applydiff(). v2: - Avoid a traceback on git metadata desynchronization

File last commit:

r16440:692bf06b default
r16506:fc4e0fec stable
Show More
repair.py
176 lines | 5.7 KiB | text/x-python | PythonLexer
# repair.py - functions for repository repair for mercurial
#
# Copyright 2005, 2006 Chris Mason <mason@suse.com>
# Copyright 2007 Matt Mackall
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
from mercurial import changegroup, bookmarks, phases
from mercurial.node import short
from mercurial.i18n import _
import os
import errno
def _bundle(repo, bases, heads, node, suffix, compress=True):
"""create a bundle with the specified revisions as a backup"""
cg = repo.changegroupsubset(bases, heads, 'strip')
backupdir = repo.join("strip-backup")
if not os.path.isdir(backupdir):
os.mkdir(backupdir)
name = os.path.join(backupdir, "%s-%s.hg" % (short(node), suffix))
if compress:
bundletype = "HG10BZ"
else:
bundletype = "HG10UN"
return changegroup.writebundle(cg, name, bundletype)
def _collectfiles(repo, striprev):
"""find out the filelogs affected by the strip"""
files = set()
for x in xrange(striprev, len(repo)):
files.update(repo[x].files())
return sorted(files)
def _collectbrokencsets(repo, files, striprev):
"""return the changesets which will be broken by the truncation"""
s = set()
def collectone(revlog):
links = (revlog.linkrev(i) for i in revlog)
# find the truncation point of the revlog
for lrev in links:
if lrev >= striprev:
break
# see if any revision after this point has a linkrev
# less than striprev (those will be broken by strip)
for lrev in links:
if lrev < striprev:
s.add(lrev)
collectone(repo.manifest)
for fname in files:
collectone(repo.file(fname))
return s
def strip(ui, repo, nodelist, backup="all", topic='backup'):
cl = repo.changelog
# TODO handle undo of merge sets
if isinstance(nodelist, str):
nodelist = [nodelist]
striplist = [cl.rev(node) for node in nodelist]
striprev = min(striplist)
keeppartialbundle = backup == 'strip'
# 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)
tostrip = set(striplist)
for rev in striplist:
for desc in cl.descendants(rev):
tostrip.add(desc)
files = _collectfiles(repo, striprev)
saverevs = _collectbrokencsets(repo, files, striprev)
# compute heads
saveheads = set(saverevs)
for r in xrange(striprev + 1, len(cl)):
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]
# compute base nodes
if saverevs:
descendants = set(cl.descendants(*saverevs))
saverevs.difference_update(descendants)
savebases = [cl.node(r) for r in saverevs]
stripbases = [cl.node(r) for r in tostrip]
bm = repo._bookmarks
updatebm = []
for m in bm:
rev = repo[bm[m]].rev()
if rev in tostrip:
updatebm.append(m)
# create a changegroup for all the branches we need to keep
backupfile = None
if backup == "all":
backupfile = _bundle(repo, stripbases, cl.heads(), node, topic)
repo.ui.status(_("saved backup bundle to %s\n") % backupfile)
if saveheads or savebases:
# do not compress partial bundle if we remove it from disk later
chgrpfile = _bundle(repo, savebases, saveheads, node, 'temp',
compress=keeppartialbundle)
mfst = repo.manifest
tr = repo.transaction("strip")
offset = len(tr.entries)
try:
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)
tr.close()
except:
tr.abort()
raise
if saveheads or savebases:
ui.note(_("adding branch\n"))
f = open(chgrpfile, "rb")
gen = changegroup.readbundle(f, chgrpfile)
if not repo.ui.verbose:
# silence internal shuffling chatter
repo.ui.pushbuffer()
repo.addchangegroup(gen, 'strip', 'bundle:' + chgrpfile, True)
if not repo.ui.verbose:
repo.ui.popbuffer()
f.close()
if not keeppartialbundle:
os.unlink(chgrpfile)
# 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)))
for m in updatebm:
bm[m] = repo['.'].node()
bookmarks.write(repo)
except:
if backupfile:
ui.warn(_("strip failed, full bundle stored in '%s'\n")
% backupfile)
elif saveheads:
ui.warn(_("strip failed, partial bundle stored in '%s'\n")
% chgrpfile)
raise
repo.destroyed()
# remove potential unknown phase
# XXX using to_strip data would be faster
phases.filterunknown(repo)