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