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