##// END OF EJS Templates
repair: avoid string concatenation by + operator...
FUJIWARA Katsunori -
r24863:f3558829 stable
parent child Browse files
Show More
@@ -1,225 +1,225 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, exchange, util, bundle2
9 from mercurial import changegroup, exchange, util, bundle2
10 from mercurial.node import short, hex
10 from mercurial.node import short, hex
11 from mercurial.i18n import _
11 from mercurial.i18n import _
12 import errno
12 import errno
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 usebundle2 = (repo.ui.config('experimental', 'bundle2-exp') and
16 usebundle2 = (repo.ui.config('experimental', 'bundle2-exp') and
17 repo.ui.config('experimental', 'strip-bundle2-version'))
17 repo.ui.config('experimental', 'strip-bundle2-version'))
18 if usebundle2:
18 if usebundle2:
19 cgversion = repo.ui.config('experimental', 'strip-bundle2-version')
19 cgversion = repo.ui.config('experimental', 'strip-bundle2-version')
20 if cgversion not in changegroup.packermap:
20 if cgversion not in changegroup.packermap:
21 repo.ui.warn(_('unknown strip-bundle2-version value %r; ' +
21 repo.ui.warn(_('unknown strip-bundle2-version value %r; '
22 'should be one of %r\n') %
22 'should be one of %r\n') %
23 (cgversion, sorted(changegroup.packermap.keys()),))
23 (cgversion, sorted(changegroup.packermap.keys()),))
24 cgversion = '01'
24 cgversion = '01'
25 usebundle2 = False
25 usebundle2 = False
26 else:
26 else:
27 cgversion = '01'
27 cgversion = '01'
28
28
29 cg = changegroup.changegroupsubset(repo, bases, heads, 'strip',
29 cg = changegroup.changegroupsubset(repo, bases, heads, 'strip',
30 version=cgversion)
30 version=cgversion)
31 backupdir = "strip-backup"
31 backupdir = "strip-backup"
32 vfs = repo.vfs
32 vfs = repo.vfs
33 if not vfs.isdir(backupdir):
33 if not vfs.isdir(backupdir):
34 vfs.mkdir(backupdir)
34 vfs.mkdir(backupdir)
35
35
36 # Include a hash of all the nodes in the filename for uniqueness
36 # Include a hash of all the nodes in the filename for uniqueness
37 hexbases = (hex(n) for n in bases)
37 hexbases = (hex(n) for n in bases)
38 hexheads = (hex(n) for n in heads)
38 hexheads = (hex(n) for n in heads)
39 allcommits = repo.set('%ls::%ls', hexbases, hexheads)
39 allcommits = repo.set('%ls::%ls', hexbases, hexheads)
40 allhashes = sorted(c.hex() for c in allcommits)
40 allhashes = sorted(c.hex() for c in allcommits)
41 totalhash = util.sha1(''.join(allhashes)).hexdigest()
41 totalhash = util.sha1(''.join(allhashes)).hexdigest()
42 name = "%s/%s-%s-%s.hg" % (backupdir, short(node), totalhash[:8], suffix)
42 name = "%s/%s-%s-%s.hg" % (backupdir, short(node), totalhash[:8], suffix)
43
43
44 if usebundle2:
44 if usebundle2:
45 bundletype = "HG20"
45 bundletype = "HG20"
46 elif compress:
46 elif compress:
47 bundletype = "HG10BZ"
47 bundletype = "HG10BZ"
48 else:
48 else:
49 bundletype = "HG10UN"
49 bundletype = "HG10UN"
50 return changegroup.writebundle(repo.ui, cg, name, bundletype, vfs)
50 return changegroup.writebundle(repo.ui, cg, name, bundletype, vfs)
51
51
52 def _collectfiles(repo, striprev):
52 def _collectfiles(repo, striprev):
53 """find out the filelogs affected by the strip"""
53 """find out the filelogs affected by the strip"""
54 files = set()
54 files = set()
55
55
56 for x in xrange(striprev, len(repo)):
56 for x in xrange(striprev, len(repo)):
57 files.update(repo[x].files())
57 files.update(repo[x].files())
58
58
59 return sorted(files)
59 return sorted(files)
60
60
61 def _collectbrokencsets(repo, files, striprev):
61 def _collectbrokencsets(repo, files, striprev):
62 """return the changesets which will be broken by the truncation"""
62 """return the changesets which will be broken by the truncation"""
63 s = set()
63 s = set()
64 def collectone(revlog):
64 def collectone(revlog):
65 _, brokenset = revlog.getstrippoint(striprev)
65 _, brokenset = revlog.getstrippoint(striprev)
66 s.update([revlog.linkrev(r) for r in brokenset])
66 s.update([revlog.linkrev(r) for r in brokenset])
67
67
68 collectone(repo.manifest)
68 collectone(repo.manifest)
69 for fname in files:
69 for fname in files:
70 collectone(repo.file(fname))
70 collectone(repo.file(fname))
71
71
72 return s
72 return s
73
73
74 def strip(ui, repo, nodelist, backup=True, topic='backup'):
74 def strip(ui, repo, nodelist, backup=True, topic='backup'):
75
75
76 # Simple way to maintain backwards compatibility for this
76 # Simple way to maintain backwards compatibility for this
77 # argument.
77 # argument.
78 if backup in ['none', 'strip']:
78 if backup in ['none', 'strip']:
79 backup = False
79 backup = False
80
80
81 repo = repo.unfiltered()
81 repo = repo.unfiltered()
82 repo.destroying()
82 repo.destroying()
83
83
84 cl = repo.changelog
84 cl = repo.changelog
85 # TODO handle undo of merge sets
85 # TODO handle undo of merge sets
86 if isinstance(nodelist, str):
86 if isinstance(nodelist, str):
87 nodelist = [nodelist]
87 nodelist = [nodelist]
88 striplist = [cl.rev(node) for node in nodelist]
88 striplist = [cl.rev(node) for node in nodelist]
89 striprev = min(striplist)
89 striprev = min(striplist)
90
90
91 # Some revisions with rev > striprev may not be descendants of striprev.
91 # Some revisions with rev > striprev may not be descendants of striprev.
92 # We have to find these revisions and put them in a bundle, so that
92 # We have to find these revisions and put them in a bundle, so that
93 # we can restore them after the truncations.
93 # we can restore them after the truncations.
94 # To create the bundle we use repo.changegroupsubset which requires
94 # To create the bundle we use repo.changegroupsubset which requires
95 # the list of heads and bases of the set of interesting revisions.
95 # the list of heads and bases of the set of interesting revisions.
96 # (head = revision in the set that has no descendant in the set;
96 # (head = revision in the set that has no descendant in the set;
97 # base = revision in the set that has no ancestor in the set)
97 # base = revision in the set that has no ancestor in the set)
98 tostrip = set(striplist)
98 tostrip = set(striplist)
99 for rev in striplist:
99 for rev in striplist:
100 for desc in cl.descendants([rev]):
100 for desc in cl.descendants([rev]):
101 tostrip.add(desc)
101 tostrip.add(desc)
102
102
103 files = _collectfiles(repo, striprev)
103 files = _collectfiles(repo, striprev)
104 saverevs = _collectbrokencsets(repo, files, striprev)
104 saverevs = _collectbrokencsets(repo, files, striprev)
105
105
106 # compute heads
106 # compute heads
107 saveheads = set(saverevs)
107 saveheads = set(saverevs)
108 for r in xrange(striprev + 1, len(cl)):
108 for r in xrange(striprev + 1, len(cl)):
109 if r not in tostrip:
109 if r not in tostrip:
110 saverevs.add(r)
110 saverevs.add(r)
111 saveheads.difference_update(cl.parentrevs(r))
111 saveheads.difference_update(cl.parentrevs(r))
112 saveheads.add(r)
112 saveheads.add(r)
113 saveheads = [cl.node(r) for r in saveheads]
113 saveheads = [cl.node(r) for r in saveheads]
114
114
115 # compute base nodes
115 # compute base nodes
116 if saverevs:
116 if saverevs:
117 descendants = set(cl.descendants(saverevs))
117 descendants = set(cl.descendants(saverevs))
118 saverevs.difference_update(descendants)
118 saverevs.difference_update(descendants)
119 savebases = [cl.node(r) for r in saverevs]
119 savebases = [cl.node(r) for r in saverevs]
120 stripbases = [cl.node(r) for r in tostrip]
120 stripbases = [cl.node(r) for r in tostrip]
121
121
122 # For a set s, max(parents(s) - s) is the same as max(heads(::s - s)), but
122 # For a set s, max(parents(s) - s) is the same as max(heads(::s - s)), but
123 # is much faster
123 # is much faster
124 newbmtarget = repo.revs('max(parents(%ld) - (%ld))', tostrip, tostrip)
124 newbmtarget = repo.revs('max(parents(%ld) - (%ld))', tostrip, tostrip)
125 if newbmtarget:
125 if newbmtarget:
126 newbmtarget = repo[newbmtarget.first()].node()
126 newbmtarget = repo[newbmtarget.first()].node()
127 else:
127 else:
128 newbmtarget = '.'
128 newbmtarget = '.'
129
129
130 bm = repo._bookmarks
130 bm = repo._bookmarks
131 updatebm = []
131 updatebm = []
132 for m in bm:
132 for m in bm:
133 rev = repo[bm[m]].rev()
133 rev = repo[bm[m]].rev()
134 if rev in tostrip:
134 if rev in tostrip:
135 updatebm.append(m)
135 updatebm.append(m)
136
136
137 # create a changegroup for all the branches we need to keep
137 # create a changegroup for all the branches we need to keep
138 backupfile = None
138 backupfile = None
139 vfs = repo.vfs
139 vfs = repo.vfs
140 node = nodelist[-1]
140 node = nodelist[-1]
141 if backup:
141 if backup:
142 backupfile = _bundle(repo, stripbases, cl.heads(), node, topic)
142 backupfile = _bundle(repo, stripbases, cl.heads(), node, topic)
143 repo.ui.status(_("saved backup bundle to %s\n") %
143 repo.ui.status(_("saved backup bundle to %s\n") %
144 vfs.join(backupfile))
144 vfs.join(backupfile))
145 repo.ui.log("backupbundle", "saved backup bundle to %s\n",
145 repo.ui.log("backupbundle", "saved backup bundle to %s\n",
146 vfs.join(backupfile))
146 vfs.join(backupfile))
147 if saveheads or savebases:
147 if saveheads or savebases:
148 # do not compress partial bundle if we remove it from disk later
148 # do not compress partial bundle if we remove it from disk later
149 chgrpfile = _bundle(repo, savebases, saveheads, node, 'temp',
149 chgrpfile = _bundle(repo, savebases, saveheads, node, 'temp',
150 compress=False)
150 compress=False)
151
151
152 mfst = repo.manifest
152 mfst = repo.manifest
153
153
154 tr = repo.transaction("strip")
154 tr = repo.transaction("strip")
155 offset = len(tr.entries)
155 offset = len(tr.entries)
156
156
157 try:
157 try:
158 tr.startgroup()
158 tr.startgroup()
159 cl.strip(striprev, tr)
159 cl.strip(striprev, tr)
160 mfst.strip(striprev, tr)
160 mfst.strip(striprev, tr)
161 for fn in files:
161 for fn in files:
162 repo.file(fn).strip(striprev, tr)
162 repo.file(fn).strip(striprev, tr)
163 tr.endgroup()
163 tr.endgroup()
164
164
165 try:
165 try:
166 for i in xrange(offset, len(tr.entries)):
166 for i in xrange(offset, len(tr.entries)):
167 file, troffset, ignore = tr.entries[i]
167 file, troffset, ignore = tr.entries[i]
168 repo.svfs(file, 'a').truncate(troffset)
168 repo.svfs(file, 'a').truncate(troffset)
169 if troffset == 0:
169 if troffset == 0:
170 repo.store.markremoved(file)
170 repo.store.markremoved(file)
171 tr.close()
171 tr.close()
172 except: # re-raises
172 except: # re-raises
173 tr.abort()
173 tr.abort()
174 raise
174 raise
175
175
176 if saveheads or savebases:
176 if saveheads or savebases:
177 ui.note(_("adding branch\n"))
177 ui.note(_("adding branch\n"))
178 f = vfs.open(chgrpfile, "rb")
178 f = vfs.open(chgrpfile, "rb")
179 gen = exchange.readbundle(ui, f, chgrpfile, vfs)
179 gen = exchange.readbundle(ui, f, chgrpfile, vfs)
180 if not repo.ui.verbose:
180 if not repo.ui.verbose:
181 # silence internal shuffling chatter
181 # silence internal shuffling chatter
182 repo.ui.pushbuffer()
182 repo.ui.pushbuffer()
183 if isinstance(gen, bundle2.unbundle20):
183 if isinstance(gen, bundle2.unbundle20):
184 tr = repo.transaction('strip')
184 tr = repo.transaction('strip')
185 tr.hookargs = {'source': 'strip',
185 tr.hookargs = {'source': 'strip',
186 'url': 'bundle:' + vfs.join(chgrpfile)}
186 'url': 'bundle:' + vfs.join(chgrpfile)}
187 try:
187 try:
188 bundle2.processbundle(repo, gen, lambda: tr)
188 bundle2.processbundle(repo, gen, lambda: tr)
189 tr.close()
189 tr.close()
190 finally:
190 finally:
191 tr.release()
191 tr.release()
192 else:
192 else:
193 changegroup.addchangegroup(repo, gen, 'strip',
193 changegroup.addchangegroup(repo, gen, 'strip',
194 'bundle:' + vfs.join(chgrpfile),
194 'bundle:' + vfs.join(chgrpfile),
195 True)
195 True)
196 if not repo.ui.verbose:
196 if not repo.ui.verbose:
197 repo.ui.popbuffer()
197 repo.ui.popbuffer()
198 f.close()
198 f.close()
199
199
200 # remove undo files
200 # remove undo files
201 for undovfs, undofile in repo.undofiles():
201 for undovfs, undofile in repo.undofiles():
202 try:
202 try:
203 undovfs.unlink(undofile)
203 undovfs.unlink(undofile)
204 except OSError, e:
204 except OSError, e:
205 if e.errno != errno.ENOENT:
205 if e.errno != errno.ENOENT:
206 ui.warn(_('error removing %s: %s\n') %
206 ui.warn(_('error removing %s: %s\n') %
207 (undovfs.join(undofile), str(e)))
207 (undovfs.join(undofile), str(e)))
208
208
209 for m in updatebm:
209 for m in updatebm:
210 bm[m] = repo[newbmtarget].node()
210 bm[m] = repo[newbmtarget].node()
211 bm.write()
211 bm.write()
212 except: # re-raises
212 except: # re-raises
213 if backupfile:
213 if backupfile:
214 ui.warn(_("strip failed, full bundle stored in '%s'\n")
214 ui.warn(_("strip failed, full bundle stored in '%s'\n")
215 % vfs.join(backupfile))
215 % vfs.join(backupfile))
216 elif saveheads:
216 elif saveheads:
217 ui.warn(_("strip failed, partial bundle stored in '%s'\n")
217 ui.warn(_("strip failed, partial bundle stored in '%s'\n")
218 % vfs.join(chgrpfile))
218 % vfs.join(chgrpfile))
219 raise
219 raise
220 else:
220 else:
221 if saveheads or savebases:
221 if saveheads or savebases:
222 # Remove partial backup only if there were no exceptions
222 # Remove partial backup only if there were no exceptions
223 vfs.unlink(chgrpfile)
223 vfs.unlink(chgrpfile)
224
224
225 repo.destroyed()
225 repo.destroyed()
General Comments 0
You need to be logged in to leave comments. Login now