##// END OF EJS Templates
changelog: drop unnecessary override of "hasnode"...
Yuya Nishihara -
r24961:8d81b36f default
parent child Browse files
Show More
@@ -1,387 +1,379 b''
1 1 # changelog.py - changelog class for mercurial
2 2 #
3 3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 4 #
5 5 # This software may be used and distributed according to the terms of the
6 6 # GNU General Public License version 2 or any later version.
7 7
8 8 from node import bin, hex, nullid
9 9 from i18n import _
10 10 import util, error, revlog, encoding
11 11
12 12 _defaultextra = {'branch': 'default'}
13 13
14 14 def _string_escape(text):
15 15 """
16 16 >>> d = {'nl': chr(10), 'bs': chr(92), 'cr': chr(13), 'nul': chr(0)}
17 17 >>> s = "ab%(nl)scd%(bs)s%(bs)sn%(nul)sab%(cr)scd%(bs)s%(nl)s" % d
18 18 >>> s
19 19 'ab\\ncd\\\\\\\\n\\x00ab\\rcd\\\\\\n'
20 20 >>> res = _string_escape(s)
21 21 >>> s == res.decode('string_escape')
22 22 True
23 23 """
24 24 # subset of the string_escape codec
25 25 text = text.replace('\\', '\\\\').replace('\n', '\\n').replace('\r', '\\r')
26 26 return text.replace('\0', '\\0')
27 27
28 28 def decodeextra(text):
29 29 """
30 30 >>> sorted(decodeextra(encodeextra({'foo': 'bar', 'baz': chr(0) + '2'})
31 31 ... ).iteritems())
32 32 [('baz', '\\x002'), ('branch', 'default'), ('foo', 'bar')]
33 33 >>> sorted(decodeextra(encodeextra({'foo': 'bar',
34 34 ... 'baz': chr(92) + chr(0) + '2'})
35 35 ... ).iteritems())
36 36 [('baz', '\\\\\\x002'), ('branch', 'default'), ('foo', 'bar')]
37 37 """
38 38 extra = _defaultextra.copy()
39 39 for l in text.split('\0'):
40 40 if l:
41 41 if '\\0' in l:
42 42 # fix up \0 without getting into trouble with \\0
43 43 l = l.replace('\\\\', '\\\\\n')
44 44 l = l.replace('\\0', '\0')
45 45 l = l.replace('\n', '')
46 46 k, v = l.decode('string_escape').split(':', 1)
47 47 extra[k] = v
48 48 return extra
49 49
50 50 def encodeextra(d):
51 51 # keys must be sorted to produce a deterministic changelog entry
52 52 items = [_string_escape('%s:%s' % (k, d[k])) for k in sorted(d)]
53 53 return "\0".join(items)
54 54
55 55 def stripdesc(desc):
56 56 """strip trailing whitespace and leading and trailing empty lines"""
57 57 return '\n'.join([l.rstrip() for l in desc.splitlines()]).strip('\n')
58 58
59 59 class appender(object):
60 60 '''the changelog index must be updated last on disk, so we use this class
61 61 to delay writes to it'''
62 62 def __init__(self, vfs, name, mode, buf):
63 63 self.data = buf
64 64 fp = vfs(name, mode)
65 65 self.fp = fp
66 66 self.offset = fp.tell()
67 67 self.size = vfs.fstat(fp).st_size
68 68
69 69 def end(self):
70 70 return self.size + len("".join(self.data))
71 71 def tell(self):
72 72 return self.offset
73 73 def flush(self):
74 74 pass
75 75 def close(self):
76 76 self.fp.close()
77 77
78 78 def seek(self, offset, whence=0):
79 79 '''virtual file offset spans real file and data'''
80 80 if whence == 0:
81 81 self.offset = offset
82 82 elif whence == 1:
83 83 self.offset += offset
84 84 elif whence == 2:
85 85 self.offset = self.end() + offset
86 86 if self.offset < self.size:
87 87 self.fp.seek(self.offset)
88 88
89 89 def read(self, count=-1):
90 90 '''only trick here is reads that span real file and data'''
91 91 ret = ""
92 92 if self.offset < self.size:
93 93 s = self.fp.read(count)
94 94 ret = s
95 95 self.offset += len(s)
96 96 if count > 0:
97 97 count -= len(s)
98 98 if count != 0:
99 99 doff = self.offset - self.size
100 100 self.data.insert(0, "".join(self.data))
101 101 del self.data[1:]
102 102 s = self.data[0][doff:doff + count]
103 103 self.offset += len(s)
104 104 ret += s
105 105 return ret
106 106
107 107 def write(self, s):
108 108 self.data.append(str(s))
109 109 self.offset += len(s)
110 110
111 111 def _divertopener(opener, target):
112 112 """build an opener that writes in 'target.a' instead of 'target'"""
113 113 def _divert(name, mode='r'):
114 114 if name != target:
115 115 return opener(name, mode)
116 116 return opener(name + ".a", mode)
117 117 return _divert
118 118
119 119 def _delayopener(opener, target, buf):
120 120 """build an opener that stores chunks in 'buf' instead of 'target'"""
121 121 def _delay(name, mode='r'):
122 122 if name != target:
123 123 return opener(name, mode)
124 124 return appender(opener, name, mode, buf)
125 125 return _delay
126 126
127 127 class changelog(revlog.revlog):
128 128 def __init__(self, opener):
129 129 revlog.revlog.__init__(self, opener, "00changelog.i")
130 130 if self._initempty:
131 131 # changelogs don't benefit from generaldelta
132 132 self.version &= ~revlog.REVLOGGENERALDELTA
133 133 self._generaldelta = False
134 134 self._realopener = opener
135 135 self._delayed = False
136 136 self._delaybuf = None
137 137 self._divert = False
138 138 self.filteredrevs = frozenset()
139 139
140 140 def tip(self):
141 141 """filtered version of revlog.tip"""
142 142 for i in xrange(len(self) -1, -2, -1):
143 143 if i not in self.filteredrevs:
144 144 return self.node(i)
145 145
146 146 def __contains__(self, rev):
147 147 """filtered version of revlog.__contains__"""
148 148 return (0 <= rev < len(self)
149 149 and rev not in self.filteredrevs)
150 150
151 151 def __iter__(self):
152 152 """filtered version of revlog.__iter__"""
153 153 if len(self.filteredrevs) == 0:
154 154 return revlog.revlog.__iter__(self)
155 155
156 156 def filterediter():
157 157 for i in xrange(len(self)):
158 158 if i not in self.filteredrevs:
159 159 yield i
160 160
161 161 return filterediter()
162 162
163 163 def revs(self, start=0, stop=None):
164 164 """filtered version of revlog.revs"""
165 165 for i in super(changelog, self).revs(start, stop):
166 166 if i not in self.filteredrevs:
167 167 yield i
168 168
169 169 @util.propertycache
170 170 def nodemap(self):
171 171 # XXX need filtering too
172 172 self.rev(self.node(0))
173 173 return self._nodecache
174 174
175 def hasnode(self, node):
176 """filtered version of revlog.hasnode"""
177 try:
178 i = self.rev(node)
179 return i not in self.filteredrevs
180 except KeyError:
181 return False
182
183 175 def headrevs(self):
184 176 if self.filteredrevs:
185 177 try:
186 178 return self.index.headrevsfiltered(self.filteredrevs)
187 179 # AttributeError covers non-c-extension environments and
188 180 # old c extensions without filter handling.
189 181 except AttributeError:
190 182 return self._headrevs()
191 183
192 184 return super(changelog, self).headrevs()
193 185
194 186 def strip(self, *args, **kwargs):
195 187 # XXX make something better than assert
196 188 # We can't expect proper strip behavior if we are filtered.
197 189 assert not self.filteredrevs
198 190 super(changelog, self).strip(*args, **kwargs)
199 191
200 192 def rev(self, node):
201 193 """filtered version of revlog.rev"""
202 194 r = super(changelog, self).rev(node)
203 195 if r in self.filteredrevs:
204 196 raise error.FilteredLookupError(hex(node), self.indexfile,
205 197 _('filtered node'))
206 198 return r
207 199
208 200 def node(self, rev):
209 201 """filtered version of revlog.node"""
210 202 if rev in self.filteredrevs:
211 203 raise error.FilteredIndexError(rev)
212 204 return super(changelog, self).node(rev)
213 205
214 206 def linkrev(self, rev):
215 207 """filtered version of revlog.linkrev"""
216 208 if rev in self.filteredrevs:
217 209 raise error.FilteredIndexError(rev)
218 210 return super(changelog, self).linkrev(rev)
219 211
220 212 def parentrevs(self, rev):
221 213 """filtered version of revlog.parentrevs"""
222 214 if rev in self.filteredrevs:
223 215 raise error.FilteredIndexError(rev)
224 216 return super(changelog, self).parentrevs(rev)
225 217
226 218 def flags(self, rev):
227 219 """filtered version of revlog.flags"""
228 220 if rev in self.filteredrevs:
229 221 raise error.FilteredIndexError(rev)
230 222 return super(changelog, self).flags(rev)
231 223
232 224 def delayupdate(self, tr):
233 225 "delay visibility of index updates to other readers"
234 226
235 227 if not self._delayed:
236 228 if len(self) == 0:
237 229 self._divert = True
238 230 if self._realopener.exists(self.indexfile + '.a'):
239 231 self._realopener.unlink(self.indexfile + '.a')
240 232 self.opener = _divertopener(self._realopener, self.indexfile)
241 233 else:
242 234 self._delaybuf = []
243 235 self.opener = _delayopener(self._realopener, self.indexfile,
244 236 self._delaybuf)
245 237 self._delayed = True
246 238 tr.addpending('cl-%i' % id(self), self._writepending)
247 239 tr.addfinalize('cl-%i' % id(self), self._finalize)
248 240
249 241 def _finalize(self, tr):
250 242 "finalize index updates"
251 243 self._delayed = False
252 244 self.opener = self._realopener
253 245 # move redirected index data back into place
254 246 if self._divert:
255 247 assert not self._delaybuf
256 248 tmpname = self.indexfile + ".a"
257 249 nfile = self.opener.open(tmpname)
258 250 nfile.close()
259 251 self.opener.rename(tmpname, self.indexfile)
260 252 elif self._delaybuf:
261 253 fp = self.opener(self.indexfile, 'a')
262 254 fp.write("".join(self._delaybuf))
263 255 fp.close()
264 256 self._delaybuf = None
265 257 self._divert = False
266 258 # split when we're done
267 259 self.checkinlinesize(tr)
268 260
269 261 def readpending(self, file):
270 262 if not self.opener.exists(file):
271 263 return # no pending data for changelog
272 264 r = revlog.revlog(self.opener, file)
273 265 self.index = r.index
274 266 self.nodemap = r.nodemap
275 267 self._nodecache = r._nodecache
276 268 self._chunkcache = r._chunkcache
277 269
278 270 def _writepending(self, tr):
279 271 "create a file containing the unfinalized state for pretxnchangegroup"
280 272 if self._delaybuf:
281 273 # make a temporary copy of the index
282 274 fp1 = self._realopener(self.indexfile)
283 275 pendingfilename = self.indexfile + ".a"
284 276 # register as a temp file to ensure cleanup on failure
285 277 tr.registertmp(pendingfilename)
286 278 # write existing data
287 279 fp2 = self._realopener(pendingfilename, "w")
288 280 fp2.write(fp1.read())
289 281 # add pending data
290 282 fp2.write("".join(self._delaybuf))
291 283 fp2.close()
292 284 # switch modes so finalize can simply rename
293 285 self._delaybuf = None
294 286 self._divert = True
295 287 self.opener = _divertopener(self._realopener, self.indexfile)
296 288
297 289 if self._divert:
298 290 return True
299 291
300 292 return False
301 293
302 294 def checkinlinesize(self, tr, fp=None):
303 295 if not self._delayed:
304 296 revlog.revlog.checkinlinesize(self, tr, fp)
305 297
306 298 def read(self, node):
307 299 """
308 300 format used:
309 301 nodeid\n : manifest node in ascii
310 302 user\n : user, no \n or \r allowed
311 303 time tz extra\n : date (time is int or float, timezone is int)
312 304 : extra is metadata, encoded and separated by '\0'
313 305 : older versions ignore it
314 306 files\n\n : files modified by the cset, no \n or \r allowed
315 307 (.*) : comment (free text, ideally utf-8)
316 308
317 309 changelog v0 doesn't use extra
318 310 """
319 311 text = self.revision(node)
320 312 if not text:
321 313 return (nullid, "", (0, 0), [], "", _defaultextra)
322 314 last = text.index("\n\n")
323 315 desc = encoding.tolocal(text[last + 2:])
324 316 l = text[:last].split('\n')
325 317 manifest = bin(l[0])
326 318 user = encoding.tolocal(l[1])
327 319
328 320 tdata = l[2].split(' ', 2)
329 321 if len(tdata) != 3:
330 322 time = float(tdata[0])
331 323 try:
332 324 # various tools did silly things with the time zone field.
333 325 timezone = int(tdata[1])
334 326 except ValueError:
335 327 timezone = 0
336 328 extra = _defaultextra
337 329 else:
338 330 time, timezone = float(tdata[0]), int(tdata[1])
339 331 extra = decodeextra(tdata[2])
340 332
341 333 files = l[3:]
342 334 return (manifest, user, (time, timezone), files, desc, extra)
343 335
344 336 def add(self, manifest, files, desc, transaction, p1, p2,
345 337 user, date=None, extra=None):
346 338 # Convert to UTF-8 encoded bytestrings as the very first
347 339 # thing: calling any method on a localstr object will turn it
348 340 # into a str object and the cached UTF-8 string is thus lost.
349 341 user, desc = encoding.fromlocal(user), encoding.fromlocal(desc)
350 342
351 343 user = user.strip()
352 344 # An empty username or a username with a "\n" will make the
353 345 # revision text contain two "\n\n" sequences -> corrupt
354 346 # repository since read cannot unpack the revision.
355 347 if not user:
356 348 raise error.RevlogError(_("empty username"))
357 349 if "\n" in user:
358 350 raise error.RevlogError(_("username %s contains a newline")
359 351 % repr(user))
360 352
361 353 desc = stripdesc(desc)
362 354
363 355 if date:
364 356 parseddate = "%d %d" % util.parsedate(date)
365 357 else:
366 358 parseddate = "%d %d" % util.makedate()
367 359 if extra:
368 360 branch = extra.get("branch")
369 361 if branch in ("default", ""):
370 362 del extra["branch"]
371 363 elif branch in (".", "null", "tip"):
372 364 raise error.RevlogError(_('the name \'%s\' is reserved')
373 365 % branch)
374 366 if extra:
375 367 extra = encodeextra(extra)
376 368 parseddate = "%s %s" % (parseddate, extra)
377 369 l = [hex(manifest), user, parseddate] + sorted(files) + ["", desc]
378 370 text = "\n".join(l)
379 371 return self.addrevision(text, transaction, len(self), p1, p2)
380 372
381 373 def branchinfo(self, rev):
382 374 """return the branch name and open/close state of a revision
383 375
384 376 This function exists because creating a changectx object
385 377 just to access this is costly."""
386 378 extra = self.read(rev)[5]
387 379 return encoding.tolocal(extra.get("branch")), 'close' in extra
General Comments 0
You need to be logged in to leave comments. Login now