Show More
@@ -1,358 +1,374 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 |
def |
|
|
112 | def o(name, mode='r'): | |
|
111 | def _divertopener(opener, target): | |
|
112 | """build an opener that writes in 'target.a' instead of 'target'""" | |
|
113 | def _divert(name, mode='r'): | |
|
113 | 114 | if name != target: |
|
114 | 115 | return opener(name, mode) |
|
115 | if divert: | |
|
116 | return opener(name + ".a", mode.replace('a', 'w')) | |
|
117 | # otherwise, divert to memory | |
|
116 | return opener(name + ".a", mode) | |
|
117 | return _divert | |
|
118 | ||
|
119 | def _delayopener(opener, target, buf): | |
|
120 | """build an opener that stores chunks in 'buf' instead of 'target'""" | |
|
121 | def _delay(name, mode='r'): | |
|
122 | if name != target: | |
|
123 | return opener(name, mode) | |
|
118 | 124 | return appender(opener, name, mode, buf) |
|
119 |
return |
|
|
125 | return _delay | |
|
120 | 126 | |
|
121 | 127 | class changelog(revlog.revlog): |
|
122 | 128 | def __init__(self, opener): |
|
123 | 129 | revlog.revlog.__init__(self, opener, "00changelog.i") |
|
124 | 130 | if self._initempty: |
|
125 | 131 | # changelogs don't benefit from generaldelta |
|
126 | 132 | self.version &= ~revlog.REVLOGGENERALDELTA |
|
127 | 133 | self._generaldelta = False |
|
128 | 134 | self._realopener = opener |
|
129 | 135 | self._delayed = False |
|
130 |
self._delaybuf = |
|
|
136 | self._delaybuf = None | |
|
131 | 137 | self._divert = False |
|
132 | 138 | self.filteredrevs = frozenset() |
|
133 | 139 | |
|
134 | 140 | def tip(self): |
|
135 | 141 | """filtered version of revlog.tip""" |
|
136 | 142 | for i in xrange(len(self) -1, -2, -1): |
|
137 | 143 | if i not in self.filteredrevs: |
|
138 | 144 | return self.node(i) |
|
139 | 145 | |
|
140 | 146 | def __iter__(self): |
|
141 | 147 | """filtered version of revlog.__iter__""" |
|
142 | 148 | if len(self.filteredrevs) == 0: |
|
143 | 149 | return revlog.revlog.__iter__(self) |
|
144 | 150 | |
|
145 | 151 | def filterediter(): |
|
146 | 152 | for i in xrange(len(self)): |
|
147 | 153 | if i not in self.filteredrevs: |
|
148 | 154 | yield i |
|
149 | 155 | |
|
150 | 156 | return filterediter() |
|
151 | 157 | |
|
152 | 158 | def revs(self, start=0, stop=None): |
|
153 | 159 | """filtered version of revlog.revs""" |
|
154 | 160 | for i in super(changelog, self).revs(start, stop): |
|
155 | 161 | if i not in self.filteredrevs: |
|
156 | 162 | yield i |
|
157 | 163 | |
|
158 | 164 | @util.propertycache |
|
159 | 165 | def nodemap(self): |
|
160 | 166 | # XXX need filtering too |
|
161 | 167 | self.rev(self.node(0)) |
|
162 | 168 | return self._nodecache |
|
163 | 169 | |
|
164 | 170 | def hasnode(self, node): |
|
165 | 171 | """filtered version of revlog.hasnode""" |
|
166 | 172 | try: |
|
167 | 173 | i = self.rev(node) |
|
168 | 174 | return i not in self.filteredrevs |
|
169 | 175 | except KeyError: |
|
170 | 176 | return False |
|
171 | 177 | |
|
172 | 178 | def headrevs(self): |
|
173 | 179 | if self.filteredrevs: |
|
174 | 180 | try: |
|
175 | 181 | return self.index.headrevsfiltered(self.filteredrevs) |
|
176 | 182 | # AttributeError covers non-c-extension environments and |
|
177 | 183 | # old c extensions without filter handling. |
|
178 | 184 | except AttributeError: |
|
179 | 185 | return self._headrevs() |
|
180 | 186 | |
|
181 | 187 | return super(changelog, self).headrevs() |
|
182 | 188 | |
|
183 | 189 | def strip(self, *args, **kwargs): |
|
184 | 190 | # XXX make something better than assert |
|
185 | 191 | # We can't expect proper strip behavior if we are filtered. |
|
186 | 192 | assert not self.filteredrevs |
|
187 | 193 | super(changelog, self).strip(*args, **kwargs) |
|
188 | 194 | |
|
189 | 195 | def rev(self, node): |
|
190 | 196 | """filtered version of revlog.rev""" |
|
191 | 197 | r = super(changelog, self).rev(node) |
|
192 | 198 | if r in self.filteredrevs: |
|
193 | 199 | raise error.FilteredLookupError(hex(node), self.indexfile, |
|
194 | 200 | _('filtered node')) |
|
195 | 201 | return r |
|
196 | 202 | |
|
197 | 203 | def node(self, rev): |
|
198 | 204 | """filtered version of revlog.node""" |
|
199 | 205 | if rev in self.filteredrevs: |
|
200 | 206 | raise error.FilteredIndexError(rev) |
|
201 | 207 | return super(changelog, self).node(rev) |
|
202 | 208 | |
|
203 | 209 | def linkrev(self, rev): |
|
204 | 210 | """filtered version of revlog.linkrev""" |
|
205 | 211 | if rev in self.filteredrevs: |
|
206 | 212 | raise error.FilteredIndexError(rev) |
|
207 | 213 | return super(changelog, self).linkrev(rev) |
|
208 | 214 | |
|
209 | 215 | def parentrevs(self, rev): |
|
210 | 216 | """filtered version of revlog.parentrevs""" |
|
211 | 217 | if rev in self.filteredrevs: |
|
212 | 218 | raise error.FilteredIndexError(rev) |
|
213 | 219 | return super(changelog, self).parentrevs(rev) |
|
214 | 220 | |
|
215 | 221 | def flags(self, rev): |
|
216 | 222 | """filtered version of revlog.flags""" |
|
217 | 223 | if rev in self.filteredrevs: |
|
218 | 224 | raise error.FilteredIndexError(rev) |
|
219 | 225 | return super(changelog, self).flags(rev) |
|
220 | 226 | |
|
221 | 227 | def delayupdate(self): |
|
222 | 228 | "delay visibility of index updates to other readers" |
|
223 | self._delayed = True | |
|
224 | self._divert = (len(self) == 0) | |
|
229 | ||
|
230 | if not self._delayed: | |
|
231 | if len(self) == 0: | |
|
232 | self._divert = True | |
|
233 | if self._realopener.exists(self.indexfile + '.a'): | |
|
234 | self._realopener.unlink(self.indexfile + '.a') | |
|
235 | self.opener = _divertopener(self._realopener, self.indexfile) | |
|
236 | else: | |
|
225 | 237 | self._delaybuf = [] |
|
226 | self.opener = delayopener(self._realopener, self.indexfile, | |
|
227 |
|
|
|
238 | self.opener = _delayopener(self._realopener, self.indexfile, | |
|
239 | self._delaybuf) | |
|
240 | self._delayed = True | |
|
228 | 241 | |
|
229 | 242 | def finalize(self, tr): |
|
230 | 243 | "finalize index updates" |
|
231 | 244 | self._delayed = False |
|
232 | 245 | self.opener = self._realopener |
|
233 | 246 | # move redirected index data back into place |
|
234 | 247 | if self._divert: |
|
248 | assert not self._delaybuf | |
|
235 | 249 | tmpname = self.indexfile + ".a" |
|
236 | 250 | nfile = self.opener.open(tmpname) |
|
237 | 251 | nfile.close() |
|
238 | 252 | self.opener.rename(tmpname, self.indexfile) |
|
239 | 253 | elif self._delaybuf: |
|
240 | 254 | fp = self.opener(self.indexfile, 'a') |
|
241 | 255 | fp.write("".join(self._delaybuf)) |
|
242 | 256 | fp.close() |
|
243 |
self._delaybuf = |
|
|
257 | self._delaybuf = None | |
|
258 | self._divert = False | |
|
244 | 259 | # split when we're done |
|
245 | 260 | self.checkinlinesize(tr) |
|
246 | 261 | |
|
247 | 262 | def readpending(self, file): |
|
248 | 263 | r = revlog.revlog(self.opener, file) |
|
249 | 264 | self.index = r.index |
|
250 | 265 | self.nodemap = r.nodemap |
|
251 | 266 | self._nodecache = r._nodecache |
|
252 | 267 | self._chunkcache = r._chunkcache |
|
253 | 268 | |
|
254 | 269 | def writepending(self): |
|
255 | 270 | "create a file containing the unfinalized state for pretxnchangegroup" |
|
256 | 271 | if self._delaybuf: |
|
257 | 272 | # make a temporary copy of the index |
|
258 | 273 | fp1 = self._realopener(self.indexfile) |
|
259 | 274 | fp2 = self._realopener(self.indexfile + ".a", "w") |
|
260 | 275 | fp2.write(fp1.read()) |
|
261 | 276 | # add pending data |
|
262 | 277 | fp2.write("".join(self._delaybuf)) |
|
263 | 278 | fp2.close() |
|
264 | 279 | # switch modes so finalize can simply rename |
|
265 |
self._delaybuf = |
|
|
280 | self._delaybuf = None | |
|
266 | 281 | self._divert = True |
|
282 | self.opener = _divertopener(self._realopener, self.indexfile) | |
|
267 | 283 | |
|
268 | 284 | if self._divert: |
|
269 | 285 | return True |
|
270 | 286 | |
|
271 | 287 | return False |
|
272 | 288 | |
|
273 | 289 | def checkinlinesize(self, tr, fp=None): |
|
274 | 290 | if not self._delayed: |
|
275 | 291 | revlog.revlog.checkinlinesize(self, tr, fp) |
|
276 | 292 | |
|
277 | 293 | def read(self, node): |
|
278 | 294 | """ |
|
279 | 295 | format used: |
|
280 | 296 | nodeid\n : manifest node in ascii |
|
281 | 297 | user\n : user, no \n or \r allowed |
|
282 | 298 | time tz extra\n : date (time is int or float, timezone is int) |
|
283 | 299 | : extra is metadata, encoded and separated by '\0' |
|
284 | 300 | : older versions ignore it |
|
285 | 301 | files\n\n : files modified by the cset, no \n or \r allowed |
|
286 | 302 | (.*) : comment (free text, ideally utf-8) |
|
287 | 303 | |
|
288 | 304 | changelog v0 doesn't use extra |
|
289 | 305 | """ |
|
290 | 306 | text = self.revision(node) |
|
291 | 307 | if not text: |
|
292 | 308 | return (nullid, "", (0, 0), [], "", _defaultextra) |
|
293 | 309 | last = text.index("\n\n") |
|
294 | 310 | desc = encoding.tolocal(text[last + 2:]) |
|
295 | 311 | l = text[:last].split('\n') |
|
296 | 312 | manifest = bin(l[0]) |
|
297 | 313 | user = encoding.tolocal(l[1]) |
|
298 | 314 | |
|
299 | 315 | tdata = l[2].split(' ', 2) |
|
300 | 316 | if len(tdata) != 3: |
|
301 | 317 | time = float(tdata[0]) |
|
302 | 318 | try: |
|
303 | 319 | # various tools did silly things with the time zone field. |
|
304 | 320 | timezone = int(tdata[1]) |
|
305 | 321 | except ValueError: |
|
306 | 322 | timezone = 0 |
|
307 | 323 | extra = _defaultextra |
|
308 | 324 | else: |
|
309 | 325 | time, timezone = float(tdata[0]), int(tdata[1]) |
|
310 | 326 | extra = decodeextra(tdata[2]) |
|
311 | 327 | |
|
312 | 328 | files = l[3:] |
|
313 | 329 | return (manifest, user, (time, timezone), files, desc, extra) |
|
314 | 330 | |
|
315 | 331 | def add(self, manifest, files, desc, transaction, p1, p2, |
|
316 | 332 | user, date=None, extra=None): |
|
317 | 333 | # Convert to UTF-8 encoded bytestrings as the very first |
|
318 | 334 | # thing: calling any method on a localstr object will turn it |
|
319 | 335 | # into a str object and the cached UTF-8 string is thus lost. |
|
320 | 336 | user, desc = encoding.fromlocal(user), encoding.fromlocal(desc) |
|
321 | 337 | |
|
322 | 338 | user = user.strip() |
|
323 | 339 | # An empty username or a username with a "\n" will make the |
|
324 | 340 | # revision text contain two "\n\n" sequences -> corrupt |
|
325 | 341 | # repository since read cannot unpack the revision. |
|
326 | 342 | if not user: |
|
327 | 343 | raise error.RevlogError(_("empty username")) |
|
328 | 344 | if "\n" in user: |
|
329 | 345 | raise error.RevlogError(_("username %s contains a newline") |
|
330 | 346 | % repr(user)) |
|
331 | 347 | |
|
332 | 348 | desc = stripdesc(desc) |
|
333 | 349 | |
|
334 | 350 | if date: |
|
335 | 351 | parseddate = "%d %d" % util.parsedate(date) |
|
336 | 352 | else: |
|
337 | 353 | parseddate = "%d %d" % util.makedate() |
|
338 | 354 | if extra: |
|
339 | 355 | branch = extra.get("branch") |
|
340 | 356 | if branch in ("default", ""): |
|
341 | 357 | del extra["branch"] |
|
342 | 358 | elif branch in (".", "null", "tip"): |
|
343 | 359 | raise error.RevlogError(_('the name \'%s\' is reserved') |
|
344 | 360 | % branch) |
|
345 | 361 | if extra: |
|
346 | 362 | extra = encodeextra(extra) |
|
347 | 363 | parseddate = "%s %s" % (parseddate, extra) |
|
348 | 364 | l = [hex(manifest), user, parseddate] + sorted(files) + ["", desc] |
|
349 | 365 | text = "\n".join(l) |
|
350 | 366 | return self.addrevision(text, transaction, len(self), p1, p2) |
|
351 | 367 | |
|
352 | 368 | def branchinfo(self, rev): |
|
353 | 369 | """return the branch name and open/close state of a revision |
|
354 | 370 | |
|
355 | 371 | This function exists because creating a changectx object |
|
356 | 372 | just to access this is costly.""" |
|
357 | 373 | extra = self.read(rev)[5] |
|
358 | 374 | return encoding.tolocal(extra.get("branch")), 'close' in extra |
General Comments 0
You need to be logged in to leave comments.
Login now