##// END OF EJS Templates
repair: revlog has an iterator
Matt Mackall -
r13715:2edf41a9 default
parent child Browse files
Show More
@@ -1,158 +1,158
1 # repair.py - functions for repository repair for mercurial
1 # repair.py - functions for repository repair for mercurial
2 #
2 #
3 # Copyright 2005, 2006 Chris Mason <mason@suse.com>
3 # Copyright 2005, 2006 Chris Mason <mason@suse.com>
4 # Copyright 2007 Matt Mackall
4 # Copyright 2007 Matt Mackall
5 #
5 #
6 # This software may be used and distributed according to the terms of the
6 # This software may be used and distributed according to the terms of the
7 # GNU General Public License version 2 or any later version.
7 # GNU General Public License version 2 or any later version.
8
8
9 import changegroup, bookmarks
9 import changegroup, bookmarks
10 from node import nullrev, short
10 from node import nullrev, short
11 from i18n import _
11 from i18n import _
12 import os
12 import os
13
13
14 def _bundle(repo, bases, heads, node, suffix, compress=True):
14 def _bundle(repo, bases, heads, node, suffix, compress=True):
15 """create a bundle with the specified revisions as a backup"""
15 """create a bundle with the specified revisions as a backup"""
16 cg = repo.changegroupsubset(bases, heads, 'strip')
16 cg = repo.changegroupsubset(bases, heads, 'strip')
17 backupdir = repo.join("strip-backup")
17 backupdir = repo.join("strip-backup")
18 if not os.path.isdir(backupdir):
18 if not os.path.isdir(backupdir):
19 os.mkdir(backupdir)
19 os.mkdir(backupdir)
20 name = os.path.join(backupdir, "%s-%s.hg" % (short(node), suffix))
20 name = os.path.join(backupdir, "%s-%s.hg" % (short(node), suffix))
21 if compress:
21 if compress:
22 bundletype = "HG10BZ"
22 bundletype = "HG10BZ"
23 else:
23 else:
24 bundletype = "HG10UN"
24 bundletype = "HG10UN"
25 return changegroup.writebundle(cg, name, bundletype)
25 return changegroup.writebundle(cg, name, bundletype)
26
26
27 def _collectfiles(repo, striprev):
27 def _collectfiles(repo, striprev):
28 """find out the filelogs affected by the strip"""
28 """find out the filelogs affected by the strip"""
29 files = set()
29 files = set()
30
30
31 for x in xrange(striprev, len(repo)):
31 for x in xrange(striprev, len(repo)):
32 files.update(repo[x].files())
32 files.update(repo[x].files())
33
33
34 return sorted(files)
34 return sorted(files)
35
35
36 def _collectbrokencsets(repo, files, striprev):
36 def _collectbrokencsets(repo, files, striprev):
37 """return the changesets which will be broken by the truncation"""
37 """return the changesets which will be broken by the truncation"""
38 s = set()
38 s = set()
39 def collectone(revlog):
39 def collectone(revlog):
40 links = (revlog.linkrev(i) for i in xrange(len(revlog)))
40 links = (revlog.linkrev(i) for i in revlog)
41 # find the truncation point of the revlog
41 # find the truncation point of the revlog
42 for lrev in links:
42 for lrev in links:
43 if lrev >= striprev:
43 if lrev >= striprev:
44 break
44 break
45 # see if any revision after this point has a linkrev
45 # see if any revision after this point has a linkrev
46 # less than striprev (those will be broken by strip)
46 # less than striprev (those will be broken by strip)
47 for lrev in links:
47 for lrev in links:
48 if lrev < striprev:
48 if lrev < striprev:
49 s.add(lrev)
49 s.add(lrev)
50
50
51 collectone(repo.manifest)
51 collectone(repo.manifest)
52 for fname in files:
52 for fname in files:
53 collectone(repo.file(fname))
53 collectone(repo.file(fname))
54
54
55 return s
55 return s
56
56
57 def strip(ui, repo, node, backup="all"):
57 def strip(ui, repo, node, backup="all"):
58 cl = repo.changelog
58 cl = repo.changelog
59 # TODO delete the undo files, and handle undo of merge sets
59 # TODO delete the undo files, and handle undo of merge sets
60 striprev = cl.rev(node)
60 striprev = cl.rev(node)
61
61
62 keeppartialbundle = backup == 'strip'
62 keeppartialbundle = backup == 'strip'
63
63
64 # Some revisions with rev > striprev may not be descendants of striprev.
64 # Some revisions with rev > striprev may not be descendants of striprev.
65 # We have to find these revisions and put them in a bundle, so that
65 # We have to find these revisions and put them in a bundle, so that
66 # we can restore them after the truncations.
66 # we can restore them after the truncations.
67 # To create the bundle we use repo.changegroupsubset which requires
67 # To create the bundle we use repo.changegroupsubset which requires
68 # the list of heads and bases of the set of interesting revisions.
68 # the list of heads and bases of the set of interesting revisions.
69 # (head = revision in the set that has no descendant in the set;
69 # (head = revision in the set that has no descendant in the set;
70 # base = revision in the set that has no ancestor in the set)
70 # base = revision in the set that has no ancestor in the set)
71 tostrip = set(cl.descendants(striprev))
71 tostrip = set(cl.descendants(striprev))
72 tostrip.add(striprev)
72 tostrip.add(striprev)
73
73
74 files = _collectfiles(repo, striprev)
74 files = _collectfiles(repo, striprev)
75 saverevs = _collectbrokencsets(repo, files, striprev)
75 saverevs = _collectbrokencsets(repo, files, striprev)
76
76
77 # compute heads
77 # compute heads
78 saveheads = set(saverevs)
78 saveheads = set(saverevs)
79 for r in xrange(striprev + 1, len(cl)):
79 for r in xrange(striprev + 1, len(cl)):
80 if r not in tostrip:
80 if r not in tostrip:
81 saverevs.add(r)
81 saverevs.add(r)
82 saveheads.difference_update(cl.parentrevs(r))
82 saveheads.difference_update(cl.parentrevs(r))
83 saveheads.add(r)
83 saveheads.add(r)
84 saveheads = [cl.node(r) for r in saveheads]
84 saveheads = [cl.node(r) for r in saveheads]
85
85
86 # compute base nodes
86 # compute base nodes
87 if saverevs:
87 if saverevs:
88 descendants = set(cl.descendants(*saverevs))
88 descendants = set(cl.descendants(*saverevs))
89 saverevs.difference_update(descendants)
89 saverevs.difference_update(descendants)
90 savebases = [cl.node(r) for r in saverevs]
90 savebases = [cl.node(r) for r in saverevs]
91
91
92 bm = repo._bookmarks
92 bm = repo._bookmarks
93 updatebm = []
93 updatebm = []
94 for m in bm:
94 for m in bm:
95 rev = repo[bm[m]].rev()
95 rev = repo[bm[m]].rev()
96 if rev in tostrip:
96 if rev in tostrip:
97 updatebm.append(m)
97 updatebm.append(m)
98
98
99 # create a changegroup for all the branches we need to keep
99 # create a changegroup for all the branches we need to keep
100 backupfile = None
100 backupfile = None
101 if backup == "all":
101 if backup == "all":
102 backupfile = _bundle(repo, [node], cl.heads(), node, 'backup')
102 backupfile = _bundle(repo, [node], cl.heads(), node, 'backup')
103 repo.ui.status(_("saved backup bundle to %s\n") % backupfile)
103 repo.ui.status(_("saved backup bundle to %s\n") % backupfile)
104 if saveheads or savebases:
104 if saveheads or savebases:
105 # do not compress partial bundle if we remove it from disk later
105 # do not compress partial bundle if we remove it from disk later
106 chgrpfile = _bundle(repo, savebases, saveheads, node, 'temp',
106 chgrpfile = _bundle(repo, savebases, saveheads, node, 'temp',
107 compress=keeppartialbundle)
107 compress=keeppartialbundle)
108
108
109 mfst = repo.manifest
109 mfst = repo.manifest
110
110
111 tr = repo.transaction("strip")
111 tr = repo.transaction("strip")
112 offset = len(tr.entries)
112 offset = len(tr.entries)
113
113
114 try:
114 try:
115 tr.startgroup()
115 tr.startgroup()
116 cl.strip(striprev, tr)
116 cl.strip(striprev, tr)
117 mfst.strip(striprev, tr)
117 mfst.strip(striprev, tr)
118 for fn in files:
118 for fn in files:
119 repo.file(fn).strip(striprev, tr)
119 repo.file(fn).strip(striprev, tr)
120 tr.endgroup()
120 tr.endgroup()
121
121
122 try:
122 try:
123 for i in xrange(offset, len(tr.entries)):
123 for i in xrange(offset, len(tr.entries)):
124 file, troffset, ignore = tr.entries[i]
124 file, troffset, ignore = tr.entries[i]
125 repo.sopener(file, 'a').truncate(troffset)
125 repo.sopener(file, 'a').truncate(troffset)
126 tr.close()
126 tr.close()
127 except:
127 except:
128 tr.abort()
128 tr.abort()
129 raise
129 raise
130
130
131 if saveheads or savebases:
131 if saveheads or savebases:
132 ui.note(_("adding branch\n"))
132 ui.note(_("adding branch\n"))
133 f = open(chgrpfile, "rb")
133 f = open(chgrpfile, "rb")
134 gen = changegroup.readbundle(f, chgrpfile)
134 gen = changegroup.readbundle(f, chgrpfile)
135 if not repo.ui.verbose:
135 if not repo.ui.verbose:
136 # silence internal shuffling chatter
136 # silence internal shuffling chatter
137 repo.ui.pushbuffer()
137 repo.ui.pushbuffer()
138 repo.addchangegroup(gen, 'strip', 'bundle:' + chgrpfile, True)
138 repo.addchangegroup(gen, 'strip', 'bundle:' + chgrpfile, True)
139 if not repo.ui.verbose:
139 if not repo.ui.verbose:
140 repo.ui.popbuffer()
140 repo.ui.popbuffer()
141 f.close()
141 f.close()
142 if not keeppartialbundle:
142 if not keeppartialbundle:
143 os.unlink(chgrpfile)
143 os.unlink(chgrpfile)
144
144
145 for m in updatebm:
145 for m in updatebm:
146 bm[m] = repo['.'].node()
146 bm[m] = repo['.'].node()
147 bookmarks.write(repo)
147 bookmarks.write(repo)
148
148
149 except:
149 except:
150 if backupfile:
150 if backupfile:
151 ui.warn(_("strip failed, full bundle stored in '%s'\n")
151 ui.warn(_("strip failed, full bundle stored in '%s'\n")
152 % backupfile)
152 % backupfile)
153 elif saveheads:
153 elif saveheads:
154 ui.warn(_("strip failed, partial bundle stored in '%s'\n")
154 ui.warn(_("strip failed, partial bundle stored in '%s'\n")
155 % chgrpfile)
155 % chgrpfile)
156 raise
156 raise
157
157
158 repo.destroyed()
158 repo.destroyed()
General Comments 0
You need to be logged in to leave comments. Login now