##// END OF EJS Templates
revlog: remove lazy index
Matt Mackall -
r13253:61c9bc3d default
parent child Browse files
Show More
@@ -1,165 +1,164 b''
1 1 # perf.py - performance test routines
2 2 '''helper extension to measure performance'''
3 3
4 4 from mercurial import cmdutil, match, commands
5 5 import time, os, sys
6 6
7 7 def timer(func, title=None):
8 8 results = []
9 9 begin = time.time()
10 10 count = 0
11 11 while 1:
12 12 ostart = os.times()
13 13 cstart = time.time()
14 14 r = func()
15 15 cstop = time.time()
16 16 ostop = os.times()
17 17 count += 1
18 18 a, b = ostart, ostop
19 19 results.append((cstop - cstart, b[0] - a[0], b[1]-a[1]))
20 20 if cstop - begin > 3 and count >= 100:
21 21 break
22 22 if cstop - begin > 10 and count >= 3:
23 23 break
24 24 if title:
25 25 sys.stderr.write("! %s\n" % title)
26 26 if r:
27 27 sys.stderr.write("! result: %s\n" % r)
28 28 m = min(results)
29 29 sys.stderr.write("! wall %f comb %f user %f sys %f (best of %d)\n"
30 30 % (m[0], m[1] + m[2], m[1], m[2], count))
31 31
32 32 def perfwalk(ui, repo, *pats):
33 33 try:
34 34 m = cmdutil.match(repo, pats, {})
35 35 timer(lambda: len(list(repo.dirstate.walk(m, [], True, False))))
36 36 except:
37 37 try:
38 38 m = cmdutil.match(repo, pats, {})
39 39 timer(lambda: len([b for a, b, c in repo.dirstate.statwalk([], m)]))
40 40 except:
41 41 timer(lambda: len(list(cmdutil.walk(repo, pats, {}))))
42 42
43 43 def perfstatus(ui, repo, *pats):
44 44 #m = match.always(repo.root, repo.getcwd())
45 45 #timer(lambda: sum(map(len, repo.dirstate.status(m, [], False, False, False))))
46 46 timer(lambda: sum(map(len, repo.status())))
47 47
48 48 def perfheads(ui, repo):
49 49 timer(lambda: len(repo.changelog.heads()))
50 50
51 51 def perftags(ui, repo):
52 52 import mercurial.changelog, mercurial.manifest
53 53 def t():
54 54 repo.changelog = mercurial.changelog.changelog(repo.sopener)
55 55 repo.manifest = mercurial.manifest.manifest(repo.sopener)
56 56 repo._tags = None
57 57 return len(repo.tags())
58 58 timer(t)
59 59
60 60 def perfdirstate(ui, repo):
61 61 "a" in repo.dirstate
62 62 def d():
63 63 repo.dirstate.invalidate()
64 64 "a" in repo.dirstate
65 65 timer(d)
66 66
67 67 def perfdirstatedirs(ui, repo):
68 68 "a" in repo.dirstate
69 69 def d():
70 70 "a" in repo.dirstate._dirs
71 71 del repo.dirstate._dirs
72 72 timer(d)
73 73
74 74 def perfmanifest(ui, repo):
75 75 def d():
76 76 t = repo.manifest.tip()
77 77 m = repo.manifest.read(t)
78 78 repo.manifest.mapcache = None
79 79 repo.manifest._cache = None
80 80 timer(d)
81 81
82 82 def perfindex(ui, repo):
83 83 import mercurial.changelog
84 84 def d():
85 85 t = repo.changelog.tip()
86 repo.changelog = mercurial.changelog.changelog(repo.sopener)
87 repo.changelog._loadindexmap()
86 repo.invalidate()
88 87 timer(d)
89 88
90 89 def perfstartup(ui, repo):
91 90 cmd = sys.argv[0]
92 91 def d():
93 92 os.system("HGRCPATH= %s version -q > /dev/null" % cmd)
94 93 timer(d)
95 94
96 95 def perfparents(ui, repo):
97 96 nl = [repo.changelog.node(i) for i in xrange(1000)]
98 97 def d():
99 98 for n in nl:
100 99 repo.changelog.parents(n)
101 100 timer(d)
102 101
103 102 def perflookup(ui, repo, rev):
104 103 timer(lambda: len(repo.lookup(rev)))
105 104
106 105 def perflog(ui, repo, **opts):
107 106 ui.pushbuffer()
108 107 timer(lambda: commands.log(ui, repo, rev=[], date='', user='',
109 108 copies=opts.get('rename')))
110 109 ui.popbuffer()
111 110
112 111 def perftemplating(ui, repo):
113 112 ui.pushbuffer()
114 113 timer(lambda: commands.log(ui, repo, rev=[], date='', user='',
115 114 template='{date|shortdate} [{rev}:{node|short}]'
116 115 ' {author|person}: {desc|firstline}\n'))
117 116 ui.popbuffer()
118 117
119 118 def perfdiffwd(ui, repo):
120 119 """Profile diff of working directory changes"""
121 120 options = {
122 121 'w': 'ignore_all_space',
123 122 'b': 'ignore_space_change',
124 123 'B': 'ignore_blank_lines',
125 124 }
126 125
127 126 for diffopt in ('', 'w', 'b', 'B', 'wB'):
128 127 opts = dict((options[c], '1') for c in diffopt)
129 128 def d():
130 129 ui.pushbuffer()
131 130 commands.diff(ui, repo, **opts)
132 131 ui.popbuffer()
133 132 title = 'diffopts: %s' % (diffopt and ('-' + diffopt) or 'none')
134 133 timer(d, title)
135 134
136 135 def perfrevlog(ui, repo, file_, **opts):
137 136 from mercurial import revlog
138 137 dist = opts['dist']
139 138 def d():
140 139 r = revlog.revlog(lambda fn: open(fn, 'rb'), file_)
141 140 for x in xrange(0, len(r), dist):
142 141 r.revision(r.node(x))
143 142
144 143 timer(d)
145 144
146 145 cmdtable = {
147 146 'perflookup': (perflookup, []),
148 147 'perfparents': (perfparents, []),
149 148 'perfstartup': (perfstartup, []),
150 149 'perfstatus': (perfstatus, []),
151 150 'perfwalk': (perfwalk, []),
152 151 'perfmanifest': (perfmanifest, []),
153 152 'perfindex': (perfindex, []),
154 153 'perfheads': (perfheads, []),
155 154 'perftags': (perftags, []),
156 155 'perfdirstate': (perfdirstate, []),
157 156 'perfdirstatedirs': (perfdirstate, []),
158 157 'perflog': (perflog,
159 158 [('', 'rename', False, 'ask log to follow renames')]),
160 159 'perftemplating': (perftemplating, []),
161 160 'perfdiffwd': (perfdiffwd, []),
162 161 'perfrevlog': (perfrevlog,
163 162 [('d', 'dist', 100, 'distance between the revisions')],
164 163 "[INDEXFILE]"),
165 164 }
@@ -1,1938 +1,1935 b''
1 1 # localrepo.py - read/write repository 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, nullrev, short
9 9 from i18n import _
10 10 import repo, changegroup, subrepo, discovery, pushkey
11 11 import changelog, dirstate, filelog, manifest, context
12 12 import lock, transaction, store, encoding
13 13 import util, extensions, hook, error
14 14 import match as matchmod
15 15 import merge as mergemod
16 16 import tags as tagsmod
17 17 import url as urlmod
18 18 from lock import release
19 19 import weakref, errno, os, time, inspect
20 20 propertycache = util.propertycache
21 21
22 22 class localrepository(repo.repository):
23 23 capabilities = set(('lookup', 'changegroupsubset', 'branchmap', 'pushkey'))
24 24 supportedformats = set(('revlogv1', 'parentdelta'))
25 25 supported = supportedformats | set(('store', 'fncache', 'shared',
26 26 'dotencode'))
27 27
28 28 def __init__(self, baseui, path=None, create=0):
29 29 repo.repository.__init__(self)
30 30 self.root = os.path.realpath(util.expandpath(path))
31 31 self.path = os.path.join(self.root, ".hg")
32 32 self.origroot = path
33 33 self.auditor = util.path_auditor(self.root, self._checknested)
34 34 self.opener = util.opener(self.path)
35 35 self.wopener = util.opener(self.root)
36 36 self.baseui = baseui
37 37 self.ui = baseui.copy()
38 38
39 39 try:
40 40 self.ui.readconfig(self.join("hgrc"), self.root)
41 41 extensions.loadall(self.ui)
42 42 except IOError:
43 43 pass
44 44
45 45 if not os.path.isdir(self.path):
46 46 if create:
47 47 if not os.path.exists(path):
48 48 util.makedirs(path)
49 49 os.mkdir(self.path)
50 50 requirements = ["revlogv1"]
51 51 if self.ui.configbool('format', 'usestore', True):
52 52 os.mkdir(os.path.join(self.path, "store"))
53 53 requirements.append("store")
54 54 if self.ui.configbool('format', 'usefncache', True):
55 55 requirements.append("fncache")
56 56 if self.ui.configbool('format', 'dotencode', True):
57 57 requirements.append('dotencode')
58 58 # create an invalid changelog
59 59 self.opener("00changelog.i", "a").write(
60 60 '\0\0\0\2' # represents revlogv2
61 61 ' dummy changelog to prevent using the old repo layout'
62 62 )
63 63 if self.ui.configbool('format', 'parentdelta', False):
64 64 requirements.append("parentdelta")
65 65 else:
66 66 raise error.RepoError(_("repository %s not found") % path)
67 67 elif create:
68 68 raise error.RepoError(_("repository %s already exists") % path)
69 69 else:
70 70 # find requirements
71 71 requirements = set()
72 72 try:
73 73 requirements = set(self.opener("requires").read().splitlines())
74 74 except IOError, inst:
75 75 if inst.errno != errno.ENOENT:
76 76 raise
77 77 for r in requirements - self.supported:
78 78 raise error.RepoError(_("requirement '%s' not supported") % r)
79 79
80 80 self.sharedpath = self.path
81 81 try:
82 82 s = os.path.realpath(self.opener("sharedpath").read())
83 83 if not os.path.exists(s):
84 84 raise error.RepoError(
85 85 _('.hg/sharedpath points to nonexistent directory %s') % s)
86 86 self.sharedpath = s
87 87 except IOError, inst:
88 88 if inst.errno != errno.ENOENT:
89 89 raise
90 90
91 91 self.store = store.store(requirements, self.sharedpath, util.opener)
92 92 self.spath = self.store.path
93 93 self.sopener = self.store.opener
94 94 self.sjoin = self.store.join
95 95 self.opener.createmode = self.store.createmode
96 96 self._applyrequirements(requirements)
97 97 if create:
98 98 self._writerequirements()
99 99
100 100 # These two define the set of tags for this repository. _tags
101 101 # maps tag name to node; _tagtypes maps tag name to 'global' or
102 102 # 'local'. (Global tags are defined by .hgtags across all
103 103 # heads, and local tags are defined in .hg/localtags.) They
104 104 # constitute the in-memory cache of tags.
105 105 self._tags = None
106 106 self._tagtypes = None
107 107
108 108 self._branchcache = None
109 109 self._branchcachetip = None
110 110 self.nodetagscache = None
111 111 self.filterpats = {}
112 112 self._datafilters = {}
113 113 self._transref = self._lockref = self._wlockref = None
114 114
115 115 def _applyrequirements(self, requirements):
116 116 self.requirements = requirements
117 117 self.sopener.options = {}
118 118 if 'parentdelta' in requirements:
119 119 self.sopener.options['parentdelta'] = 1
120 120
121 121 def _writerequirements(self):
122 122 reqfile = self.opener("requires", "w")
123 123 for r in self.requirements:
124 124 reqfile.write("%s\n" % r)
125 125 reqfile.close()
126 126
127 127 def _checknested(self, path):
128 128 """Determine if path is a legal nested repository."""
129 129 if not path.startswith(self.root):
130 130 return False
131 131 subpath = path[len(self.root) + 1:]
132 132
133 133 # XXX: Checking against the current working copy is wrong in
134 134 # the sense that it can reject things like
135 135 #
136 136 # $ hg cat -r 10 sub/x.txt
137 137 #
138 138 # if sub/ is no longer a subrepository in the working copy
139 139 # parent revision.
140 140 #
141 141 # However, it can of course also allow things that would have
142 142 # been rejected before, such as the above cat command if sub/
143 143 # is a subrepository now, but was a normal directory before.
144 144 # The old path auditor would have rejected by mistake since it
145 145 # panics when it sees sub/.hg/.
146 146 #
147 147 # All in all, checking against the working copy seems sensible
148 148 # since we want to prevent access to nested repositories on
149 149 # the filesystem *now*.
150 150 ctx = self[None]
151 151 parts = util.splitpath(subpath)
152 152 while parts:
153 153 prefix = os.sep.join(parts)
154 154 if prefix in ctx.substate:
155 155 if prefix == subpath:
156 156 return True
157 157 else:
158 158 sub = ctx.sub(prefix)
159 159 return sub.checknested(subpath[len(prefix) + 1:])
160 160 else:
161 161 parts.pop()
162 162 return False
163 163
164 164
165 165 @propertycache
166 166 def changelog(self):
167 167 c = changelog.changelog(self.sopener)
168 168 if 'HG_PENDING' in os.environ:
169 169 p = os.environ['HG_PENDING']
170 170 if p.startswith(self.root):
171 171 c.readpending('00changelog.i.a')
172 172 self.sopener.options['defversion'] = c.version
173 173 return c
174 174
175 175 @propertycache
176 176 def manifest(self):
177 177 return manifest.manifest(self.sopener)
178 178
179 179 @propertycache
180 180 def dirstate(self):
181 181 warned = [0]
182 182 def validate(node):
183 183 try:
184 184 r = self.changelog.rev(node)
185 185 return node
186 186 except error.LookupError:
187 187 if not warned[0]:
188 188 warned[0] = True
189 189 self.ui.warn(_("warning: ignoring unknown"
190 190 " working parent %s!\n") % short(node))
191 191 return nullid
192 192
193 193 return dirstate.dirstate(self.opener, self.ui, self.root, validate)
194 194
195 195 def __getitem__(self, changeid):
196 196 if changeid is None:
197 197 return context.workingctx(self)
198 198 return context.changectx(self, changeid)
199 199
200 200 def __contains__(self, changeid):
201 201 try:
202 202 return bool(self.lookup(changeid))
203 203 except error.RepoLookupError:
204 204 return False
205 205
206 206 def __nonzero__(self):
207 207 return True
208 208
209 209 def __len__(self):
210 210 return len(self.changelog)
211 211
212 212 def __iter__(self):
213 213 for i in xrange(len(self)):
214 214 yield i
215 215
216 216 def url(self):
217 217 return 'file:' + self.root
218 218
219 219 def hook(self, name, throw=False, **args):
220 220 return hook.hook(self.ui, self, name, throw, **args)
221 221
222 222 tag_disallowed = ':\r\n'
223 223
224 224 def _tag(self, names, node, message, local, user, date, extra={}):
225 225 if isinstance(names, str):
226 226 allchars = names
227 227 names = (names,)
228 228 else:
229 229 allchars = ''.join(names)
230 230 for c in self.tag_disallowed:
231 231 if c in allchars:
232 232 raise util.Abort(_('%r cannot be used in a tag name') % c)
233 233
234 234 branches = self.branchmap()
235 235 for name in names:
236 236 self.hook('pretag', throw=True, node=hex(node), tag=name,
237 237 local=local)
238 238 if name in branches:
239 239 self.ui.warn(_("warning: tag %s conflicts with existing"
240 240 " branch name\n") % name)
241 241
242 242 def writetags(fp, names, munge, prevtags):
243 243 fp.seek(0, 2)
244 244 if prevtags and prevtags[-1] != '\n':
245 245 fp.write('\n')
246 246 for name in names:
247 247 m = munge and munge(name) or name
248 248 if self._tagtypes and name in self._tagtypes:
249 249 old = self._tags.get(name, nullid)
250 250 fp.write('%s %s\n' % (hex(old), m))
251 251 fp.write('%s %s\n' % (hex(node), m))
252 252 fp.close()
253 253
254 254 prevtags = ''
255 255 if local:
256 256 try:
257 257 fp = self.opener('localtags', 'r+')
258 258 except IOError:
259 259 fp = self.opener('localtags', 'a')
260 260 else:
261 261 prevtags = fp.read()
262 262
263 263 # local tags are stored in the current charset
264 264 writetags(fp, names, None, prevtags)
265 265 for name in names:
266 266 self.hook('tag', node=hex(node), tag=name, local=local)
267 267 return
268 268
269 269 try:
270 270 fp = self.wfile('.hgtags', 'rb+')
271 271 except IOError:
272 272 fp = self.wfile('.hgtags', 'ab')
273 273 else:
274 274 prevtags = fp.read()
275 275
276 276 # committed tags are stored in UTF-8
277 277 writetags(fp, names, encoding.fromlocal, prevtags)
278 278
279 279 if '.hgtags' not in self.dirstate:
280 280 self[None].add(['.hgtags'])
281 281
282 282 m = matchmod.exact(self.root, '', ['.hgtags'])
283 283 tagnode = self.commit(message, user, date, extra=extra, match=m)
284 284
285 285 for name in names:
286 286 self.hook('tag', node=hex(node), tag=name, local=local)
287 287
288 288 return tagnode
289 289
290 290 def tag(self, names, node, message, local, user, date):
291 291 '''tag a revision with one or more symbolic names.
292 292
293 293 names is a list of strings or, when adding a single tag, names may be a
294 294 string.
295 295
296 296 if local is True, the tags are stored in a per-repository file.
297 297 otherwise, they are stored in the .hgtags file, and a new
298 298 changeset is committed with the change.
299 299
300 300 keyword arguments:
301 301
302 302 local: whether to store tags in non-version-controlled file
303 303 (default False)
304 304
305 305 message: commit message to use if committing
306 306
307 307 user: name of user to use if committing
308 308
309 309 date: date tuple to use if committing'''
310 310
311 311 if not local:
312 312 for x in self.status()[:5]:
313 313 if '.hgtags' in x:
314 314 raise util.Abort(_('working copy of .hgtags is changed '
315 315 '(please commit .hgtags manually)'))
316 316
317 317 self.tags() # instantiate the cache
318 318 self._tag(names, node, message, local, user, date)
319 319
320 320 def tags(self):
321 321 '''return a mapping of tag to node'''
322 322 if self._tags is None:
323 323 (self._tags, self._tagtypes) = self._findtags()
324 324
325 325 return self._tags
326 326
327 327 def _findtags(self):
328 328 '''Do the hard work of finding tags. Return a pair of dicts
329 329 (tags, tagtypes) where tags maps tag name to node, and tagtypes
330 330 maps tag name to a string like \'global\' or \'local\'.
331 331 Subclasses or extensions are free to add their own tags, but
332 332 should be aware that the returned dicts will be retained for the
333 333 duration of the localrepo object.'''
334 334
335 335 # XXX what tagtype should subclasses/extensions use? Currently
336 336 # mq and bookmarks add tags, but do not set the tagtype at all.
337 337 # Should each extension invent its own tag type? Should there
338 338 # be one tagtype for all such "virtual" tags? Or is the status
339 339 # quo fine?
340 340
341 341 alltags = {} # map tag name to (node, hist)
342 342 tagtypes = {}
343 343
344 344 tagsmod.findglobaltags(self.ui, self, alltags, tagtypes)
345 345 tagsmod.readlocaltags(self.ui, self, alltags, tagtypes)
346 346
347 347 # Build the return dicts. Have to re-encode tag names because
348 348 # the tags module always uses UTF-8 (in order not to lose info
349 349 # writing to the cache), but the rest of Mercurial wants them in
350 350 # local encoding.
351 351 tags = {}
352 352 for (name, (node, hist)) in alltags.iteritems():
353 353 if node != nullid:
354 354 tags[encoding.tolocal(name)] = node
355 355 tags['tip'] = self.changelog.tip()
356 356 tagtypes = dict([(encoding.tolocal(name), value)
357 357 for (name, value) in tagtypes.iteritems()])
358 358 return (tags, tagtypes)
359 359
360 360 def tagtype(self, tagname):
361 361 '''
362 362 return the type of the given tag. result can be:
363 363
364 364 'local' : a local tag
365 365 'global' : a global tag
366 366 None : tag does not exist
367 367 '''
368 368
369 369 self.tags()
370 370
371 371 return self._tagtypes.get(tagname)
372 372
373 373 def tagslist(self):
374 374 '''return a list of tags ordered by revision'''
375 375 l = []
376 376 for t, n in self.tags().iteritems():
377 377 try:
378 378 r = self.changelog.rev(n)
379 379 except:
380 380 r = -2 # sort to the beginning of the list if unknown
381 381 l.append((r, t, n))
382 382 return [(t, n) for r, t, n in sorted(l)]
383 383
384 384 def nodetags(self, node):
385 385 '''return the tags associated with a node'''
386 386 if not self.nodetagscache:
387 387 self.nodetagscache = {}
388 388 for t, n in self.tags().iteritems():
389 389 self.nodetagscache.setdefault(n, []).append(t)
390 390 for tags in self.nodetagscache.itervalues():
391 391 tags.sort()
392 392 return self.nodetagscache.get(node, [])
393 393
394 394 def _branchtags(self, partial, lrev):
395 395 # TODO: rename this function?
396 396 tiprev = len(self) - 1
397 397 if lrev != tiprev:
398 398 ctxgen = (self[r] for r in xrange(lrev + 1, tiprev + 1))
399 399 self._updatebranchcache(partial, ctxgen)
400 400 self._writebranchcache(partial, self.changelog.tip(), tiprev)
401 401
402 402 return partial
403 403
404 404 def updatebranchcache(self):
405 405 tip = self.changelog.tip()
406 406 if self._branchcache is not None and self._branchcachetip == tip:
407 407 return self._branchcache
408 408
409 409 oldtip = self._branchcachetip
410 410 self._branchcachetip = tip
411 411 if oldtip is None or oldtip not in self.changelog.nodemap:
412 412 partial, last, lrev = self._readbranchcache()
413 413 else:
414 414 lrev = self.changelog.rev(oldtip)
415 415 partial = self._branchcache
416 416
417 417 self._branchtags(partial, lrev)
418 418 # this private cache holds all heads (not just tips)
419 419 self._branchcache = partial
420 420
421 421 def branchmap(self):
422 422 '''returns a dictionary {branch: [branchheads]}'''
423 423 self.updatebranchcache()
424 424 return self._branchcache
425 425
426 426 def branchtags(self):
427 427 '''return a dict where branch names map to the tipmost head of
428 428 the branch, open heads come before closed'''
429 429 bt = {}
430 430 for bn, heads in self.branchmap().iteritems():
431 431 tip = heads[-1]
432 432 for h in reversed(heads):
433 433 if 'close' not in self.changelog.read(h)[5]:
434 434 tip = h
435 435 break
436 436 bt[bn] = tip
437 437 return bt
438 438
439 439 def _readbranchcache(self):
440 440 partial = {}
441 441 try:
442 442 f = self.opener("branchheads.cache")
443 443 lines = f.read().split('\n')
444 444 f.close()
445 445 except (IOError, OSError):
446 446 return {}, nullid, nullrev
447 447
448 448 try:
449 449 last, lrev = lines.pop(0).split(" ", 1)
450 450 last, lrev = bin(last), int(lrev)
451 451 if lrev >= len(self) or self[lrev].node() != last:
452 452 # invalidate the cache
453 453 raise ValueError('invalidating branch cache (tip differs)')
454 454 for l in lines:
455 455 if not l:
456 456 continue
457 457 node, label = l.split(" ", 1)
458 458 label = encoding.tolocal(label.strip())
459 459 partial.setdefault(label, []).append(bin(node))
460 460 except KeyboardInterrupt:
461 461 raise
462 462 except Exception, inst:
463 463 if self.ui.debugflag:
464 464 self.ui.warn(str(inst), '\n')
465 465 partial, last, lrev = {}, nullid, nullrev
466 466 return partial, last, lrev
467 467
468 468 def _writebranchcache(self, branches, tip, tiprev):
469 469 try:
470 470 f = self.opener("branchheads.cache", "w", atomictemp=True)
471 471 f.write("%s %s\n" % (hex(tip), tiprev))
472 472 for label, nodes in branches.iteritems():
473 473 for node in nodes:
474 474 f.write("%s %s\n" % (hex(node), encoding.fromlocal(label)))
475 475 f.rename()
476 476 except (IOError, OSError):
477 477 pass
478 478
479 479 def _updatebranchcache(self, partial, ctxgen):
480 480 # collect new branch entries
481 481 newbranches = {}
482 482 for c in ctxgen:
483 483 newbranches.setdefault(c.branch(), []).append(c.node())
484 484 # if older branchheads are reachable from new ones, they aren't
485 485 # really branchheads. Note checking parents is insufficient:
486 486 # 1 (branch a) -> 2 (branch b) -> 3 (branch a)
487 487 for branch, newnodes in newbranches.iteritems():
488 488 bheads = partial.setdefault(branch, [])
489 489 bheads.extend(newnodes)
490 490 if len(bheads) <= 1:
491 491 continue
492 492 # starting from tip means fewer passes over reachable
493 493 while newnodes:
494 494 latest = newnodes.pop()
495 495 if latest not in bheads:
496 496 continue
497 497 minbhrev = self[min([self[bh].rev() for bh in bheads])].node()
498 498 reachable = self.changelog.reachable(latest, minbhrev)
499 499 reachable.remove(latest)
500 500 bheads = [b for b in bheads if b not in reachable]
501 501 partial[branch] = bheads
502 502
503 503 def lookup(self, key):
504 504 if isinstance(key, int):
505 505 return self.changelog.node(key)
506 506 elif key == '.':
507 507 return self.dirstate.parents()[0]
508 508 elif key == 'null':
509 509 return nullid
510 510 elif key == 'tip':
511 511 return self.changelog.tip()
512 512 n = self.changelog._match(key)
513 513 if n:
514 514 return n
515 515 if key in self.tags():
516 516 return self.tags()[key]
517 517 if key in self.branchtags():
518 518 return self.branchtags()[key]
519 519 n = self.changelog._partialmatch(key)
520 520 if n:
521 521 return n
522 522
523 523 # can't find key, check if it might have come from damaged dirstate
524 524 if key in self.dirstate.parents():
525 525 raise error.Abort(_("working directory has unknown parent '%s'!")
526 526 % short(key))
527 527 try:
528 528 if len(key) == 20:
529 529 key = hex(key)
530 530 except:
531 531 pass
532 532 raise error.RepoLookupError(_("unknown revision '%s'") % key)
533 533
534 534 def lookupbranch(self, key, remote=None):
535 535 repo = remote or self
536 536 if key in repo.branchmap():
537 537 return key
538 538
539 539 repo = (remote and remote.local()) and remote or self
540 540 return repo[key].branch()
541 541
542 542 def local(self):
543 543 return True
544 544
545 545 def join(self, f):
546 546 return os.path.join(self.path, f)
547 547
548 548 def wjoin(self, f):
549 549 return os.path.join(self.root, f)
550 550
551 551 def file(self, f):
552 552 if f[0] == '/':
553 553 f = f[1:]
554 554 return filelog.filelog(self.sopener, f)
555 555
556 556 def changectx(self, changeid):
557 557 return self[changeid]
558 558
559 559 def parents(self, changeid=None):
560 560 '''get list of changectxs for parents of changeid'''
561 561 return self[changeid].parents()
562 562
563 563 def filectx(self, path, changeid=None, fileid=None):
564 564 """changeid can be a changeset revision, node, or tag.
565 565 fileid can be a file revision or node."""
566 566 return context.filectx(self, path, changeid, fileid)
567 567
568 568 def getcwd(self):
569 569 return self.dirstate.getcwd()
570 570
571 571 def pathto(self, f, cwd=None):
572 572 return self.dirstate.pathto(f, cwd)
573 573
574 574 def wfile(self, f, mode='r'):
575 575 return self.wopener(f, mode)
576 576
577 577 def _link(self, f):
578 578 return os.path.islink(self.wjoin(f))
579 579
580 580 def _loadfilter(self, filter):
581 581 if filter not in self.filterpats:
582 582 l = []
583 583 for pat, cmd in self.ui.configitems(filter):
584 584 if cmd == '!':
585 585 continue
586 586 mf = matchmod.match(self.root, '', [pat])
587 587 fn = None
588 588 params = cmd
589 589 for name, filterfn in self._datafilters.iteritems():
590 590 if cmd.startswith(name):
591 591 fn = filterfn
592 592 params = cmd[len(name):].lstrip()
593 593 break
594 594 if not fn:
595 595 fn = lambda s, c, **kwargs: util.filter(s, c)
596 596 # Wrap old filters not supporting keyword arguments
597 597 if not inspect.getargspec(fn)[2]:
598 598 oldfn = fn
599 599 fn = lambda s, c, **kwargs: oldfn(s, c)
600 600 l.append((mf, fn, params))
601 601 self.filterpats[filter] = l
602 602 return self.filterpats[filter]
603 603
604 604 def _filter(self, filterpats, filename, data):
605 605 for mf, fn, cmd in filterpats:
606 606 if mf(filename):
607 607 self.ui.debug("filtering %s through %s\n" % (filename, cmd))
608 608 data = fn(data, cmd, ui=self.ui, repo=self, filename=filename)
609 609 break
610 610
611 611 return data
612 612
613 613 @propertycache
614 614 def _encodefilterpats(self):
615 615 return self._loadfilter('encode')
616 616
617 617 @propertycache
618 618 def _decodefilterpats(self):
619 619 return self._loadfilter('decode')
620 620
621 621 def adddatafilter(self, name, filter):
622 622 self._datafilters[name] = filter
623 623
624 624 def wread(self, filename):
625 625 if self._link(filename):
626 626 data = os.readlink(self.wjoin(filename))
627 627 else:
628 628 data = self.wopener(filename, 'r').read()
629 629 return self._filter(self._encodefilterpats, filename, data)
630 630
631 631 def wwrite(self, filename, data, flags):
632 632 data = self._filter(self._decodefilterpats, filename, data)
633 633 if 'l' in flags:
634 634 self.wopener.symlink(data, filename)
635 635 else:
636 636 self.wopener(filename, 'w').write(data)
637 637 if 'x' in flags:
638 638 util.set_flags(self.wjoin(filename), False, True)
639 639
640 640 def wwritedata(self, filename, data):
641 641 return self._filter(self._decodefilterpats, filename, data)
642 642
643 643 def transaction(self, desc):
644 644 tr = self._transref and self._transref() or None
645 645 if tr and tr.running():
646 646 return tr.nest()
647 647
648 648 # abort here if the journal already exists
649 649 if os.path.exists(self.sjoin("journal")):
650 650 raise error.RepoError(
651 651 _("abandoned transaction found - run hg recover"))
652 652
653 653 # save dirstate for rollback
654 654 try:
655 655 ds = self.opener("dirstate").read()
656 656 except IOError:
657 657 ds = ""
658 658 self.opener("journal.dirstate", "w").write(ds)
659 659 self.opener("journal.branch", "w").write(
660 660 encoding.fromlocal(self.dirstate.branch()))
661 661 self.opener("journal.desc", "w").write("%d\n%s\n" % (len(self), desc))
662 662
663 663 renames = [(self.sjoin("journal"), self.sjoin("undo")),
664 664 (self.join("journal.dirstate"), self.join("undo.dirstate")),
665 665 (self.join("journal.branch"), self.join("undo.branch")),
666 666 (self.join("journal.desc"), self.join("undo.desc"))]
667 667 tr = transaction.transaction(self.ui.warn, self.sopener,
668 668 self.sjoin("journal"),
669 669 aftertrans(renames),
670 670 self.store.createmode)
671 671 self._transref = weakref.ref(tr)
672 672 return tr
673 673
674 674 def recover(self):
675 675 lock = self.lock()
676 676 try:
677 677 if os.path.exists(self.sjoin("journal")):
678 678 self.ui.status(_("rolling back interrupted transaction\n"))
679 679 transaction.rollback(self.sopener, self.sjoin("journal"),
680 680 self.ui.warn)
681 681 self.invalidate()
682 682 return True
683 683 else:
684 684 self.ui.warn(_("no interrupted transaction available\n"))
685 685 return False
686 686 finally:
687 687 lock.release()
688 688
689 689 def rollback(self, dryrun=False):
690 690 wlock = lock = None
691 691 try:
692 692 wlock = self.wlock()
693 693 lock = self.lock()
694 694 if os.path.exists(self.sjoin("undo")):
695 695 try:
696 696 args = self.opener("undo.desc", "r").read().splitlines()
697 697 if len(args) >= 3 and self.ui.verbose:
698 698 desc = _("rolling back to revision %s"
699 699 " (undo %s: %s)\n") % (
700 700 int(args[0]) - 1, args[1], args[2])
701 701 elif len(args) >= 2:
702 702 desc = _("rolling back to revision %s (undo %s)\n") % (
703 703 int(args[0]) - 1, args[1])
704 704 except IOError:
705 705 desc = _("rolling back unknown transaction\n")
706 706 self.ui.status(desc)
707 707 if dryrun:
708 708 return
709 709 transaction.rollback(self.sopener, self.sjoin("undo"),
710 710 self.ui.warn)
711 711 util.rename(self.join("undo.dirstate"), self.join("dirstate"))
712 712 try:
713 713 branch = self.opener("undo.branch").read()
714 714 self.dirstate.setbranch(branch)
715 715 except IOError:
716 716 self.ui.warn(_("Named branch could not be reset, "
717 717 "current branch still is: %s\n")
718 718 % self.dirstate.branch())
719 719 self.invalidate()
720 720 self.dirstate.invalidate()
721 721 self.destroyed()
722 722 else:
723 723 self.ui.warn(_("no rollback information available\n"))
724 724 return 1
725 725 finally:
726 726 release(lock, wlock)
727 727
728 728 def invalidatecaches(self):
729 729 self._tags = None
730 730 self._tagtypes = None
731 731 self.nodetagscache = None
732 732 self._branchcache = None # in UTF-8
733 733 self._branchcachetip = None
734 734
735 735 def invalidate(self):
736 736 for a in ("changelog", "manifest"):
737 737 if a in self.__dict__:
738 738 delattr(self, a)
739 739 self.invalidatecaches()
740 740
741 741 def _lock(self, lockname, wait, releasefn, acquirefn, desc):
742 742 try:
743 743 l = lock.lock(lockname, 0, releasefn, desc=desc)
744 744 except error.LockHeld, inst:
745 745 if not wait:
746 746 raise
747 747 self.ui.warn(_("waiting for lock on %s held by %r\n") %
748 748 (desc, inst.locker))
749 749 # default to 600 seconds timeout
750 750 l = lock.lock(lockname, int(self.ui.config("ui", "timeout", "600")),
751 751 releasefn, desc=desc)
752 752 if acquirefn:
753 753 acquirefn()
754 754 return l
755 755
756 756 def lock(self, wait=True):
757 757 '''Lock the repository store (.hg/store) and return a weak reference
758 758 to the lock. Use this before modifying the store (e.g. committing or
759 759 stripping). If you are opening a transaction, get a lock as well.)'''
760 760 l = self._lockref and self._lockref()
761 761 if l is not None and l.held:
762 762 l.lock()
763 763 return l
764 764
765 765 l = self._lock(self.sjoin("lock"), wait, None, self.invalidate,
766 766 _('repository %s') % self.origroot)
767 767 self._lockref = weakref.ref(l)
768 768 return l
769 769
770 770 def wlock(self, wait=True):
771 771 '''Lock the non-store parts of the repository (everything under
772 772 .hg except .hg/store) and return a weak reference to the lock.
773 773 Use this before modifying files in .hg.'''
774 774 l = self._wlockref and self._wlockref()
775 775 if l is not None and l.held:
776 776 l.lock()
777 777 return l
778 778
779 779 l = self._lock(self.join("wlock"), wait, self.dirstate.write,
780 780 self.dirstate.invalidate, _('working directory of %s') %
781 781 self.origroot)
782 782 self._wlockref = weakref.ref(l)
783 783 return l
784 784
785 785 def _filecommit(self, fctx, manifest1, manifest2, linkrev, tr, changelist):
786 786 """
787 787 commit an individual file as part of a larger transaction
788 788 """
789 789
790 790 fname = fctx.path()
791 791 text = fctx.data()
792 792 flog = self.file(fname)
793 793 fparent1 = manifest1.get(fname, nullid)
794 794 fparent2 = fparent2o = manifest2.get(fname, nullid)
795 795
796 796 meta = {}
797 797 copy = fctx.renamed()
798 798 if copy and copy[0] != fname:
799 799 # Mark the new revision of this file as a copy of another
800 800 # file. This copy data will effectively act as a parent
801 801 # of this new revision. If this is a merge, the first
802 802 # parent will be the nullid (meaning "look up the copy data")
803 803 # and the second one will be the other parent. For example:
804 804 #
805 805 # 0 --- 1 --- 3 rev1 changes file foo
806 806 # \ / rev2 renames foo to bar and changes it
807 807 # \- 2 -/ rev3 should have bar with all changes and
808 808 # should record that bar descends from
809 809 # bar in rev2 and foo in rev1
810 810 #
811 811 # this allows this merge to succeed:
812 812 #
813 813 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
814 814 # \ / merging rev3 and rev4 should use bar@rev2
815 815 # \- 2 --- 4 as the merge base
816 816 #
817 817
818 818 cfname = copy[0]
819 819 crev = manifest1.get(cfname)
820 820 newfparent = fparent2
821 821
822 822 if manifest2: # branch merge
823 823 if fparent2 == nullid or crev is None: # copied on remote side
824 824 if cfname in manifest2:
825 825 crev = manifest2[cfname]
826 826 newfparent = fparent1
827 827
828 828 # find source in nearest ancestor if we've lost track
829 829 if not crev:
830 830 self.ui.debug(" %s: searching for copy revision for %s\n" %
831 831 (fname, cfname))
832 832 for ancestor in self[None].ancestors():
833 833 if cfname in ancestor:
834 834 crev = ancestor[cfname].filenode()
835 835 break
836 836
837 837 if crev:
838 838 self.ui.debug(" %s: copy %s:%s\n" % (fname, cfname, hex(crev)))
839 839 meta["copy"] = cfname
840 840 meta["copyrev"] = hex(crev)
841 841 fparent1, fparent2 = nullid, newfparent
842 842 else:
843 843 self.ui.warn(_("warning: can't find ancestor for '%s' "
844 844 "copied from '%s'!\n") % (fname, cfname))
845 845
846 846 elif fparent2 != nullid:
847 847 # is one parent an ancestor of the other?
848 848 fparentancestor = flog.ancestor(fparent1, fparent2)
849 849 if fparentancestor == fparent1:
850 850 fparent1, fparent2 = fparent2, nullid
851 851 elif fparentancestor == fparent2:
852 852 fparent2 = nullid
853 853
854 854 # is the file changed?
855 855 if fparent2 != nullid or flog.cmp(fparent1, text) or meta:
856 856 changelist.append(fname)
857 857 return flog.add(text, meta, tr, linkrev, fparent1, fparent2)
858 858
859 859 # are just the flags changed during merge?
860 860 if fparent1 != fparent2o and manifest1.flags(fname) != fctx.flags():
861 861 changelist.append(fname)
862 862
863 863 return fparent1
864 864
865 865 def commit(self, text="", user=None, date=None, match=None, force=False,
866 866 editor=False, extra={}):
867 867 """Add a new revision to current repository.
868 868
869 869 Revision information is gathered from the working directory,
870 870 match can be used to filter the committed files. If editor is
871 871 supplied, it is called to get a commit message.
872 872 """
873 873
874 874 def fail(f, msg):
875 875 raise util.Abort('%s: %s' % (f, msg))
876 876
877 877 if not match:
878 878 match = matchmod.always(self.root, '')
879 879
880 880 if not force:
881 881 vdirs = []
882 882 match.dir = vdirs.append
883 883 match.bad = fail
884 884
885 885 wlock = self.wlock()
886 886 try:
887 887 wctx = self[None]
888 888 merge = len(wctx.parents()) > 1
889 889
890 890 if (not force and merge and match and
891 891 (match.files() or match.anypats())):
892 892 raise util.Abort(_('cannot partially commit a merge '
893 893 '(do not specify files or patterns)'))
894 894
895 895 changes = self.status(match=match, clean=force)
896 896 if force:
897 897 changes[0].extend(changes[6]) # mq may commit unchanged files
898 898
899 899 # check subrepos
900 900 subs = []
901 901 removedsubs = set()
902 902 for p in wctx.parents():
903 903 removedsubs.update(s for s in p.substate if match(s))
904 904 for s in wctx.substate:
905 905 removedsubs.discard(s)
906 906 if match(s) and wctx.sub(s).dirty():
907 907 subs.append(s)
908 908 if (subs or removedsubs):
909 909 if (not match('.hgsub') and
910 910 '.hgsub' in (wctx.modified() + wctx.added())):
911 911 raise util.Abort(_("can't commit subrepos without .hgsub"))
912 912 if '.hgsubstate' not in changes[0]:
913 913 changes[0].insert(0, '.hgsubstate')
914 914
915 915 # make sure all explicit patterns are matched
916 916 if not force and match.files():
917 917 matched = set(changes[0] + changes[1] + changes[2])
918 918
919 919 for f in match.files():
920 920 if f == '.' or f in matched or f in wctx.substate:
921 921 continue
922 922 if f in changes[3]: # missing
923 923 fail(f, _('file not found!'))
924 924 if f in vdirs: # visited directory
925 925 d = f + '/'
926 926 for mf in matched:
927 927 if mf.startswith(d):
928 928 break
929 929 else:
930 930 fail(f, _("no match under directory!"))
931 931 elif f not in self.dirstate:
932 932 fail(f, _("file not tracked!"))
933 933
934 934 if (not force and not extra.get("close") and not merge
935 935 and not (changes[0] or changes[1] or changes[2])
936 936 and wctx.branch() == wctx.p1().branch()):
937 937 return None
938 938
939 939 ms = mergemod.mergestate(self)
940 940 for f in changes[0]:
941 941 if f in ms and ms[f] == 'u':
942 942 raise util.Abort(_("unresolved merge conflicts "
943 943 "(see hg resolve)"))
944 944
945 945 cctx = context.workingctx(self, text, user, date, extra, changes)
946 946 if editor:
947 947 cctx._text = editor(self, cctx, subs)
948 948 edited = (text != cctx._text)
949 949
950 950 # commit subs
951 951 if subs or removedsubs:
952 952 state = wctx.substate.copy()
953 953 for s in sorted(subs):
954 954 sub = wctx.sub(s)
955 955 self.ui.status(_('committing subrepository %s\n') %
956 956 subrepo.subrelpath(sub))
957 957 sr = sub.commit(cctx._text, user, date)
958 958 state[s] = (state[s][0], sr)
959 959 subrepo.writestate(self, state)
960 960
961 961 # Save commit message in case this transaction gets rolled back
962 962 # (e.g. by a pretxncommit hook). Leave the content alone on
963 963 # the assumption that the user will use the same editor again.
964 964 msgfile = self.opener('last-message.txt', 'wb')
965 965 msgfile.write(cctx._text)
966 966 msgfile.close()
967 967
968 968 p1, p2 = self.dirstate.parents()
969 969 hookp1, hookp2 = hex(p1), (p2 != nullid and hex(p2) or '')
970 970 try:
971 971 self.hook("precommit", throw=True, parent1=hookp1, parent2=hookp2)
972 972 ret = self.commitctx(cctx, True)
973 973 except:
974 974 if edited:
975 975 msgfn = self.pathto(msgfile.name[len(self.root)+1:])
976 976 self.ui.write(
977 977 _('note: commit message saved in %s\n') % msgfn)
978 978 raise
979 979
980 980 # update dirstate and mergestate
981 981 for f in changes[0] + changes[1]:
982 982 self.dirstate.normal(f)
983 983 for f in changes[2]:
984 984 self.dirstate.forget(f)
985 985 self.dirstate.setparents(ret)
986 986 ms.reset()
987 987 finally:
988 988 wlock.release()
989 989
990 990 self.hook("commit", node=hex(ret), parent1=hookp1, parent2=hookp2)
991 991 return ret
992 992
993 993 def commitctx(self, ctx, error=False):
994 994 """Add a new revision to current repository.
995 995 Revision information is passed via the context argument.
996 996 """
997 997
998 998 tr = lock = None
999 999 removed = list(ctx.removed())
1000 1000 p1, p2 = ctx.p1(), ctx.p2()
1001 1001 m1 = p1.manifest().copy()
1002 1002 m2 = p2.manifest()
1003 1003 user = ctx.user()
1004 1004
1005 1005 lock = self.lock()
1006 1006 try:
1007 1007 tr = self.transaction("commit")
1008 1008 trp = weakref.proxy(tr)
1009 1009
1010 1010 # check in files
1011 1011 new = {}
1012 1012 changed = []
1013 1013 linkrev = len(self)
1014 1014 for f in sorted(ctx.modified() + ctx.added()):
1015 1015 self.ui.note(f + "\n")
1016 1016 try:
1017 1017 fctx = ctx[f]
1018 1018 new[f] = self._filecommit(fctx, m1, m2, linkrev, trp,
1019 1019 changed)
1020 1020 m1.set(f, fctx.flags())
1021 1021 except OSError, inst:
1022 1022 self.ui.warn(_("trouble committing %s!\n") % f)
1023 1023 raise
1024 1024 except IOError, inst:
1025 1025 errcode = getattr(inst, 'errno', errno.ENOENT)
1026 1026 if error or errcode and errcode != errno.ENOENT:
1027 1027 self.ui.warn(_("trouble committing %s!\n") % f)
1028 1028 raise
1029 1029 else:
1030 1030 removed.append(f)
1031 1031
1032 1032 # update manifest
1033 1033 m1.update(new)
1034 1034 removed = [f for f in sorted(removed) if f in m1 or f in m2]
1035 1035 drop = [f for f in removed if f in m1]
1036 1036 for f in drop:
1037 1037 del m1[f]
1038 1038 mn = self.manifest.add(m1, trp, linkrev, p1.manifestnode(),
1039 1039 p2.manifestnode(), (new, drop))
1040 1040
1041 1041 # update changelog
1042 1042 self.changelog.delayupdate()
1043 1043 n = self.changelog.add(mn, changed + removed, ctx.description(),
1044 1044 trp, p1.node(), p2.node(),
1045 1045 user, ctx.date(), ctx.extra().copy())
1046 1046 p = lambda: self.changelog.writepending() and self.root or ""
1047 1047 xp1, xp2 = p1.hex(), p2 and p2.hex() or ''
1048 1048 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
1049 1049 parent2=xp2, pending=p)
1050 1050 self.changelog.finalize(trp)
1051 1051 tr.close()
1052 1052
1053 1053 if self._branchcache:
1054 1054 self.updatebranchcache()
1055 1055 return n
1056 1056 finally:
1057 1057 if tr:
1058 1058 tr.release()
1059 1059 lock.release()
1060 1060
1061 1061 def destroyed(self):
1062 1062 '''Inform the repository that nodes have been destroyed.
1063 1063 Intended for use by strip and rollback, so there's a common
1064 1064 place for anything that has to be done after destroying history.'''
1065 1065 # XXX it might be nice if we could take the list of destroyed
1066 1066 # nodes, but I don't see an easy way for rollback() to do that
1067 1067
1068 1068 # Ensure the persistent tag cache is updated. Doing it now
1069 1069 # means that the tag cache only has to worry about destroyed
1070 1070 # heads immediately after a strip/rollback. That in turn
1071 1071 # guarantees that "cachetip == currenttip" (comparing both rev
1072 1072 # and node) always means no nodes have been added or destroyed.
1073 1073
1074 1074 # XXX this is suboptimal when qrefresh'ing: we strip the current
1075 1075 # head, refresh the tag cache, then immediately add a new head.
1076 1076 # But I think doing it this way is necessary for the "instant
1077 1077 # tag cache retrieval" case to work.
1078 1078 self.invalidatecaches()
1079 1079
1080 1080 def walk(self, match, node=None):
1081 1081 '''
1082 1082 walk recursively through the directory tree or a given
1083 1083 changeset, finding all files matched by the match
1084 1084 function
1085 1085 '''
1086 1086 return self[node].walk(match)
1087 1087
1088 1088 def status(self, node1='.', node2=None, match=None,
1089 1089 ignored=False, clean=False, unknown=False,
1090 1090 listsubrepos=False):
1091 1091 """return status of files between two nodes or node and working directory
1092 1092
1093 1093 If node1 is None, use the first dirstate parent instead.
1094 1094 If node2 is None, compare node1 with working directory.
1095 1095 """
1096 1096
1097 1097 def mfmatches(ctx):
1098 1098 mf = ctx.manifest().copy()
1099 1099 for fn in mf.keys():
1100 1100 if not match(fn):
1101 1101 del mf[fn]
1102 1102 return mf
1103 1103
1104 1104 if isinstance(node1, context.changectx):
1105 1105 ctx1 = node1
1106 1106 else:
1107 1107 ctx1 = self[node1]
1108 1108 if isinstance(node2, context.changectx):
1109 1109 ctx2 = node2
1110 1110 else:
1111 1111 ctx2 = self[node2]
1112 1112
1113 1113 working = ctx2.rev() is None
1114 1114 parentworking = working and ctx1 == self['.']
1115 1115 match = match or matchmod.always(self.root, self.getcwd())
1116 1116 listignored, listclean, listunknown = ignored, clean, unknown
1117 1117
1118 1118 # load earliest manifest first for caching reasons
1119 1119 if not working and ctx2.rev() < ctx1.rev():
1120 1120 ctx2.manifest()
1121 1121
1122 1122 if not parentworking:
1123 1123 def bad(f, msg):
1124 1124 if f not in ctx1:
1125 1125 self.ui.warn('%s: %s\n' % (self.dirstate.pathto(f), msg))
1126 1126 match.bad = bad
1127 1127
1128 1128 if working: # we need to scan the working dir
1129 1129 subrepos = []
1130 1130 if '.hgsub' in self.dirstate:
1131 1131 subrepos = ctx1.substate.keys()
1132 1132 s = self.dirstate.status(match, subrepos, listignored,
1133 1133 listclean, listunknown)
1134 1134 cmp, modified, added, removed, deleted, unknown, ignored, clean = s
1135 1135
1136 1136 # check for any possibly clean files
1137 1137 if parentworking and cmp:
1138 1138 fixup = []
1139 1139 # do a full compare of any files that might have changed
1140 1140 for f in sorted(cmp):
1141 1141 if (f not in ctx1 or ctx2.flags(f) != ctx1.flags(f)
1142 1142 or ctx1[f].cmp(ctx2[f])):
1143 1143 modified.append(f)
1144 1144 else:
1145 1145 fixup.append(f)
1146 1146
1147 1147 # update dirstate for files that are actually clean
1148 1148 if fixup:
1149 1149 if listclean:
1150 1150 clean += fixup
1151 1151
1152 1152 try:
1153 1153 # updating the dirstate is optional
1154 1154 # so we don't wait on the lock
1155 1155 wlock = self.wlock(False)
1156 1156 try:
1157 1157 for f in fixup:
1158 1158 self.dirstate.normal(f)
1159 1159 finally:
1160 1160 wlock.release()
1161 1161 except error.LockError:
1162 1162 pass
1163 1163
1164 1164 if not parentworking:
1165 1165 mf1 = mfmatches(ctx1)
1166 1166 if working:
1167 1167 # we are comparing working dir against non-parent
1168 1168 # generate a pseudo-manifest for the working dir
1169 1169 mf2 = mfmatches(self['.'])
1170 1170 for f in cmp + modified + added:
1171 1171 mf2[f] = None
1172 1172 mf2.set(f, ctx2.flags(f))
1173 1173 for f in removed:
1174 1174 if f in mf2:
1175 1175 del mf2[f]
1176 1176 else:
1177 1177 # we are comparing two revisions
1178 1178 deleted, unknown, ignored = [], [], []
1179 1179 mf2 = mfmatches(ctx2)
1180 1180
1181 1181 modified, added, clean = [], [], []
1182 1182 for fn in mf2:
1183 1183 if fn in mf1:
1184 1184 if (mf1.flags(fn) != mf2.flags(fn) or
1185 1185 (mf1[fn] != mf2[fn] and
1186 1186 (mf2[fn] or ctx1[fn].cmp(ctx2[fn])))):
1187 1187 modified.append(fn)
1188 1188 elif listclean:
1189 1189 clean.append(fn)
1190 1190 del mf1[fn]
1191 1191 else:
1192 1192 added.append(fn)
1193 1193 removed = mf1.keys()
1194 1194
1195 1195 r = modified, added, removed, deleted, unknown, ignored, clean
1196 1196
1197 1197 if listsubrepos:
1198 1198 for subpath, sub in subrepo.itersubrepos(ctx1, ctx2):
1199 1199 if working:
1200 1200 rev2 = None
1201 1201 else:
1202 1202 rev2 = ctx2.substate[subpath][1]
1203 1203 try:
1204 1204 submatch = matchmod.narrowmatcher(subpath, match)
1205 1205 s = sub.status(rev2, match=submatch, ignored=listignored,
1206 1206 clean=listclean, unknown=listunknown,
1207 1207 listsubrepos=True)
1208 1208 for rfiles, sfiles in zip(r, s):
1209 1209 rfiles.extend("%s/%s" % (subpath, f) for f in sfiles)
1210 1210 except error.LookupError:
1211 1211 self.ui.status(_("skipping missing subrepository: %s\n")
1212 1212 % subpath)
1213 1213
1214 1214 [l.sort() for l in r]
1215 1215 return r
1216 1216
1217 1217 def heads(self, start=None):
1218 1218 heads = self.changelog.heads(start)
1219 1219 # sort the output in rev descending order
1220 1220 return sorted(heads, key=self.changelog.rev, reverse=True)
1221 1221
1222 1222 def branchheads(self, branch=None, start=None, closed=False):
1223 1223 '''return a (possibly filtered) list of heads for the given branch
1224 1224
1225 1225 Heads are returned in topological order, from newest to oldest.
1226 1226 If branch is None, use the dirstate branch.
1227 1227 If start is not None, return only heads reachable from start.
1228 1228 If closed is True, return heads that are marked as closed as well.
1229 1229 '''
1230 1230 if branch is None:
1231 1231 branch = self[None].branch()
1232 1232 branches = self.branchmap()
1233 1233 if branch not in branches:
1234 1234 return []
1235 1235 # the cache returns heads ordered lowest to highest
1236 1236 bheads = list(reversed(branches[branch]))
1237 1237 if start is not None:
1238 1238 # filter out the heads that cannot be reached from startrev
1239 1239 fbheads = set(self.changelog.nodesbetween([start], bheads)[2])
1240 1240 bheads = [h for h in bheads if h in fbheads]
1241 1241 if not closed:
1242 1242 bheads = [h for h in bheads if
1243 1243 ('close' not in self.changelog.read(h)[5])]
1244 1244 return bheads
1245 1245
1246 1246 def branches(self, nodes):
1247 1247 if not nodes:
1248 1248 nodes = [self.changelog.tip()]
1249 1249 b = []
1250 1250 for n in nodes:
1251 1251 t = n
1252 1252 while 1:
1253 1253 p = self.changelog.parents(n)
1254 1254 if p[1] != nullid or p[0] == nullid:
1255 1255 b.append((t, n, p[0], p[1]))
1256 1256 break
1257 1257 n = p[0]
1258 1258 return b
1259 1259
1260 1260 def between(self, pairs):
1261 1261 r = []
1262 1262
1263 1263 for top, bottom in pairs:
1264 1264 n, l, i = top, [], 0
1265 1265 f = 1
1266 1266
1267 1267 while n != bottom and n != nullid:
1268 1268 p = self.changelog.parents(n)[0]
1269 1269 if i == f:
1270 1270 l.append(n)
1271 1271 f = f * 2
1272 1272 n = p
1273 1273 i += 1
1274 1274
1275 1275 r.append(l)
1276 1276
1277 1277 return r
1278 1278
1279 1279 def pull(self, remote, heads=None, force=False):
1280 1280 lock = self.lock()
1281 1281 try:
1282 1282 tmp = discovery.findcommonincoming(self, remote, heads=heads,
1283 1283 force=force)
1284 1284 common, fetch, rheads = tmp
1285 1285 if not fetch:
1286 1286 self.ui.status(_("no changes found\n"))
1287 1287 return 0
1288 1288
1289 1289 if heads is None and fetch == [nullid]:
1290 1290 self.ui.status(_("requesting all changes\n"))
1291 1291 elif heads is None and remote.capable('changegroupsubset'):
1292 1292 # issue1320, avoid a race if remote changed after discovery
1293 1293 heads = rheads
1294 1294
1295 1295 if heads is None:
1296 1296 cg = remote.changegroup(fetch, 'pull')
1297 1297 else:
1298 1298 if not remote.capable('changegroupsubset'):
1299 1299 raise util.Abort(_("partial pull cannot be done because "
1300 1300 "other repository doesn't support "
1301 1301 "changegroupsubset."))
1302 1302 cg = remote.changegroupsubset(fetch, heads, 'pull')
1303 1303 return self.addchangegroup(cg, 'pull', remote.url(), lock=lock)
1304 1304 finally:
1305 1305 lock.release()
1306 1306
1307 1307 def push(self, remote, force=False, revs=None, newbranch=False):
1308 1308 '''Push outgoing changesets (limited by revs) from the current
1309 1309 repository to remote. Return an integer:
1310 1310 - 0 means HTTP error *or* nothing to push
1311 1311 - 1 means we pushed and remote head count is unchanged *or*
1312 1312 we have outgoing changesets but refused to push
1313 1313 - other values as described by addchangegroup()
1314 1314 '''
1315 1315 # there are two ways to push to remote repo:
1316 1316 #
1317 1317 # addchangegroup assumes local user can lock remote
1318 1318 # repo (local filesystem, old ssh servers).
1319 1319 #
1320 1320 # unbundle assumes local user cannot lock remote repo (new ssh
1321 1321 # servers, http servers).
1322 1322
1323 1323 lock = None
1324 1324 unbundle = remote.capable('unbundle')
1325 1325 if not unbundle:
1326 1326 lock = remote.lock()
1327 1327 try:
1328 1328 ret = discovery.prepush(self, remote, force, revs, newbranch)
1329 1329 if ret[0] is None:
1330 1330 # and here we return 0 for "nothing to push" or 1 for
1331 1331 # "something to push but I refuse"
1332 1332 return ret[1]
1333 1333
1334 1334 cg, remote_heads = ret
1335 1335 if unbundle:
1336 1336 # local repo finds heads on server, finds out what revs it must
1337 1337 # push. once revs transferred, if server finds it has
1338 1338 # different heads (someone else won commit/push race), server
1339 1339 # aborts.
1340 1340 if force:
1341 1341 remote_heads = ['force']
1342 1342 # ssh: return remote's addchangegroup()
1343 1343 # http: return remote's addchangegroup() or 0 for error
1344 1344 return remote.unbundle(cg, remote_heads, 'push')
1345 1345 else:
1346 1346 # we return an integer indicating remote head count change
1347 1347 return remote.addchangegroup(cg, 'push', self.url(), lock=lock)
1348 1348 finally:
1349 1349 if lock is not None:
1350 1350 lock.release()
1351 1351
1352 1352 def changegroupinfo(self, nodes, source):
1353 1353 if self.ui.verbose or source == 'bundle':
1354 1354 self.ui.status(_("%d changesets found\n") % len(nodes))
1355 1355 if self.ui.debugflag:
1356 1356 self.ui.debug("list of changesets:\n")
1357 1357 for node in nodes:
1358 1358 self.ui.debug("%s\n" % hex(node))
1359 1359
1360 1360 def changegroupsubset(self, bases, heads, source, extranodes=None):
1361 1361 """Compute a changegroup consisting of all the nodes that are
1362 1362 descendents of any of the bases and ancestors of any of the heads.
1363 1363 Return a chunkbuffer object whose read() method will return
1364 1364 successive changegroup chunks.
1365 1365
1366 1366 It is fairly complex as determining which filenodes and which
1367 1367 manifest nodes need to be included for the changeset to be complete
1368 1368 is non-trivial.
1369 1369
1370 1370 Another wrinkle is doing the reverse, figuring out which changeset in
1371 1371 the changegroup a particular filenode or manifestnode belongs to.
1372 1372
1373 1373 The caller can specify some nodes that must be included in the
1374 1374 changegroup using the extranodes argument. It should be a dict
1375 1375 where the keys are the filenames (or 1 for the manifest), and the
1376 1376 values are lists of (node, linknode) tuples, where node is a wanted
1377 1377 node and linknode is the changelog node that should be transmitted as
1378 1378 the linkrev.
1379 1379 """
1380 1380
1381 1381 # Set up some initial variables
1382 1382 # Make it easy to refer to self.changelog
1383 1383 cl = self.changelog
1384 1384 # Compute the list of changesets in this changegroup.
1385 1385 # Some bases may turn out to be superfluous, and some heads may be
1386 1386 # too. nodesbetween will return the minimal set of bases and heads
1387 1387 # necessary to re-create the changegroup.
1388 1388 if not bases:
1389 1389 bases = [nullid]
1390 1390 msng_cl_lst, bases, heads = cl.nodesbetween(bases, heads)
1391 1391
1392 1392 if extranodes is None:
1393 1393 # can we go through the fast path ?
1394 1394 heads.sort()
1395 1395 allheads = self.heads()
1396 1396 allheads.sort()
1397 1397 if heads == allheads:
1398 1398 return self._changegroup(msng_cl_lst, source)
1399 1399
1400 1400 # slow path
1401 1401 self.hook('preoutgoing', throw=True, source=source)
1402 1402
1403 1403 self.changegroupinfo(msng_cl_lst, source)
1404 1404
1405 1405 # We assume that all ancestors of bases are known
1406 1406 commonrevs = set(cl.ancestors(*[cl.rev(n) for n in bases]))
1407 1407
1408 1408 # Make it easy to refer to self.manifest
1409 1409 mnfst = self.manifest
1410 1410 # We don't know which manifests are missing yet
1411 1411 msng_mnfst_set = {}
1412 1412 # Nor do we know which filenodes are missing.
1413 1413 msng_filenode_set = {}
1414 1414
1415 junk = mnfst.index[len(mnfst) - 1] # Get around a bug in lazyindex
1416 junk = None
1417
1418 1415 # A changeset always belongs to itself, so the changenode lookup
1419 1416 # function for a changenode is identity.
1420 1417 def identity(x):
1421 1418 return x
1422 1419
1423 1420 # A function generating function that sets up the initial environment
1424 1421 # the inner function.
1425 1422 def filenode_collector(changedfiles):
1426 1423 # This gathers information from each manifestnode included in the
1427 1424 # changegroup about which filenodes the manifest node references
1428 1425 # so we can include those in the changegroup too.
1429 1426 #
1430 1427 # It also remembers which changenode each filenode belongs to. It
1431 1428 # does this by assuming the a filenode belongs to the changenode
1432 1429 # the first manifest that references it belongs to.
1433 1430 def collect_msng_filenodes(mnfstnode):
1434 1431 r = mnfst.rev(mnfstnode)
1435 1432 if mnfst.deltaparent(r) in mnfst.parentrevs(r):
1436 1433 # If the previous rev is one of the parents,
1437 1434 # we only need to see a diff.
1438 1435 deltamf = mnfst.readdelta(mnfstnode)
1439 1436 # For each line in the delta
1440 1437 for f, fnode in deltamf.iteritems():
1441 1438 # And if the file is in the list of files we care
1442 1439 # about.
1443 1440 if f in changedfiles:
1444 1441 # Get the changenode this manifest belongs to
1445 1442 clnode = msng_mnfst_set[mnfstnode]
1446 1443 # Create the set of filenodes for the file if
1447 1444 # there isn't one already.
1448 1445 ndset = msng_filenode_set.setdefault(f, {})
1449 1446 # And set the filenode's changelog node to the
1450 1447 # manifest's if it hasn't been set already.
1451 1448 ndset.setdefault(fnode, clnode)
1452 1449 else:
1453 1450 # Otherwise we need a full manifest.
1454 1451 m = mnfst.read(mnfstnode)
1455 1452 # For every file in we care about.
1456 1453 for f in changedfiles:
1457 1454 fnode = m.get(f, None)
1458 1455 # If it's in the manifest
1459 1456 if fnode is not None:
1460 1457 # See comments above.
1461 1458 clnode = msng_mnfst_set[mnfstnode]
1462 1459 ndset = msng_filenode_set.setdefault(f, {})
1463 1460 ndset.setdefault(fnode, clnode)
1464 1461 return collect_msng_filenodes
1465 1462
1466 1463 # If we determine that a particular file or manifest node must be a
1467 1464 # node that the recipient of the changegroup will already have, we can
1468 1465 # also assume the recipient will have all the parents. This function
1469 1466 # prunes them from the set of missing nodes.
1470 1467 def prune(revlog, missingnodes):
1471 1468 hasset = set()
1472 1469 # If a 'missing' filenode thinks it belongs to a changenode we
1473 1470 # assume the recipient must have, then the recipient must have
1474 1471 # that filenode.
1475 1472 for n in missingnodes:
1476 1473 clrev = revlog.linkrev(revlog.rev(n))
1477 1474 if clrev in commonrevs:
1478 1475 hasset.add(n)
1479 1476 for n in hasset:
1480 1477 missingnodes.pop(n, None)
1481 1478 for r in revlog.ancestors(*[revlog.rev(n) for n in hasset]):
1482 1479 missingnodes.pop(revlog.node(r), None)
1483 1480
1484 1481 # Add the nodes that were explicitly requested.
1485 1482 def add_extra_nodes(name, nodes):
1486 1483 if not extranodes or name not in extranodes:
1487 1484 return
1488 1485
1489 1486 for node, linknode in extranodes[name]:
1490 1487 if node not in nodes:
1491 1488 nodes[node] = linknode
1492 1489
1493 1490 # Now that we have all theses utility functions to help out and
1494 1491 # logically divide up the task, generate the group.
1495 1492 def gengroup():
1496 1493 # The set of changed files starts empty.
1497 1494 changedfiles = set()
1498 1495 collect = changegroup.collector(cl, msng_mnfst_set, changedfiles)
1499 1496
1500 1497 # Create a changenode group generator that will call our functions
1501 1498 # back to lookup the owning changenode and collect information.
1502 1499 group = cl.group(msng_cl_lst, identity, collect)
1503 1500 for cnt, chnk in enumerate(group):
1504 1501 yield chnk
1505 1502 # revlog.group yields three entries per node, so
1506 1503 # dividing by 3 gives an approximation of how many
1507 1504 # nodes have been processed.
1508 1505 self.ui.progress(_('bundling'), cnt / 3,
1509 1506 unit=_('changesets'))
1510 1507 changecount = cnt / 3
1511 1508 self.ui.progress(_('bundling'), None)
1512 1509
1513 1510 prune(mnfst, msng_mnfst_set)
1514 1511 add_extra_nodes(1, msng_mnfst_set)
1515 1512 msng_mnfst_lst = msng_mnfst_set.keys()
1516 1513 # Sort the manifestnodes by revision number.
1517 1514 msng_mnfst_lst.sort(key=mnfst.rev)
1518 1515 # Create a generator for the manifestnodes that calls our lookup
1519 1516 # and data collection functions back.
1520 1517 group = mnfst.group(msng_mnfst_lst,
1521 1518 lambda mnode: msng_mnfst_set[mnode],
1522 1519 filenode_collector(changedfiles))
1523 1520 efiles = {}
1524 1521 for cnt, chnk in enumerate(group):
1525 1522 if cnt % 3 == 1:
1526 1523 mnode = chnk[:20]
1527 1524 efiles.update(mnfst.readdelta(mnode))
1528 1525 yield chnk
1529 1526 # see above comment for why we divide by 3
1530 1527 self.ui.progress(_('bundling'), cnt / 3,
1531 1528 unit=_('manifests'), total=changecount)
1532 1529 self.ui.progress(_('bundling'), None)
1533 1530 efiles = len(efiles)
1534 1531
1535 1532 # These are no longer needed, dereference and toss the memory for
1536 1533 # them.
1537 1534 msng_mnfst_lst = None
1538 1535 msng_mnfst_set.clear()
1539 1536
1540 1537 if extranodes:
1541 1538 for fname in extranodes:
1542 1539 if isinstance(fname, int):
1543 1540 continue
1544 1541 msng_filenode_set.setdefault(fname, {})
1545 1542 changedfiles.add(fname)
1546 1543 # Go through all our files in order sorted by name.
1547 1544 for idx, fname in enumerate(sorted(changedfiles)):
1548 1545 filerevlog = self.file(fname)
1549 1546 if not len(filerevlog):
1550 1547 raise util.Abort(_("empty or missing revlog for %s") % fname)
1551 1548 # Toss out the filenodes that the recipient isn't really
1552 1549 # missing.
1553 1550 missingfnodes = msng_filenode_set.pop(fname, {})
1554 1551 prune(filerevlog, missingfnodes)
1555 1552 add_extra_nodes(fname, missingfnodes)
1556 1553 # If any filenodes are left, generate the group for them,
1557 1554 # otherwise don't bother.
1558 1555 if missingfnodes:
1559 1556 yield changegroup.chunkheader(len(fname))
1560 1557 yield fname
1561 1558 # Sort the filenodes by their revision # (topological order)
1562 1559 nodeiter = list(missingfnodes)
1563 1560 nodeiter.sort(key=filerevlog.rev)
1564 1561 # Create a group generator and only pass in a changenode
1565 1562 # lookup function as we need to collect no information
1566 1563 # from filenodes.
1567 1564 group = filerevlog.group(nodeiter,
1568 1565 lambda fnode: missingfnodes[fnode])
1569 1566 for chnk in group:
1570 1567 # even though we print the same progress on
1571 1568 # most loop iterations, put the progress call
1572 1569 # here so that time estimates (if any) can be updated
1573 1570 self.ui.progress(
1574 1571 _('bundling'), idx, item=fname,
1575 1572 unit=_('files'), total=efiles)
1576 1573 yield chnk
1577 1574 # Signal that no more groups are left.
1578 1575 yield changegroup.closechunk()
1579 1576 self.ui.progress(_('bundling'), None)
1580 1577
1581 1578 if msng_cl_lst:
1582 1579 self.hook('outgoing', node=hex(msng_cl_lst[0]), source=source)
1583 1580
1584 1581 return changegroup.unbundle10(util.chunkbuffer(gengroup()), 'UN')
1585 1582
1586 1583 def changegroup(self, basenodes, source):
1587 1584 # to avoid a race we use changegroupsubset() (issue1320)
1588 1585 return self.changegroupsubset(basenodes, self.heads(), source)
1589 1586
1590 1587 def _changegroup(self, nodes, source):
1591 1588 """Compute the changegroup of all nodes that we have that a recipient
1592 1589 doesn't. Return a chunkbuffer object whose read() method will return
1593 1590 successive changegroup chunks.
1594 1591
1595 1592 This is much easier than the previous function as we can assume that
1596 1593 the recipient has any changenode we aren't sending them.
1597 1594
1598 1595 nodes is the set of nodes to send"""
1599 1596
1600 1597 self.hook('preoutgoing', throw=True, source=source)
1601 1598
1602 1599 cl = self.changelog
1603 1600 revset = set([cl.rev(n) for n in nodes])
1604 1601 self.changegroupinfo(nodes, source)
1605 1602
1606 1603 def identity(x):
1607 1604 return x
1608 1605
1609 1606 def gennodelst(log):
1610 1607 for r in log:
1611 1608 if log.linkrev(r) in revset:
1612 1609 yield log.node(r)
1613 1610
1614 1611 def lookuplinkrev_func(revlog):
1615 1612 def lookuplinkrev(n):
1616 1613 return cl.node(revlog.linkrev(revlog.rev(n)))
1617 1614 return lookuplinkrev
1618 1615
1619 1616 def gengroup():
1620 1617 '''yield a sequence of changegroup chunks (strings)'''
1621 1618 # construct a list of all changed files
1622 1619 changedfiles = set()
1623 1620 mmfs = {}
1624 1621 collect = changegroup.collector(cl, mmfs, changedfiles)
1625 1622
1626 1623 for cnt, chnk in enumerate(cl.group(nodes, identity, collect)):
1627 1624 # revlog.group yields three entries per node, so
1628 1625 # dividing by 3 gives an approximation of how many
1629 1626 # nodes have been processed.
1630 1627 self.ui.progress(_('bundling'), cnt / 3, unit=_('changesets'))
1631 1628 yield chnk
1632 1629 changecount = cnt / 3
1633 1630 self.ui.progress(_('bundling'), None)
1634 1631
1635 1632 mnfst = self.manifest
1636 1633 nodeiter = gennodelst(mnfst)
1637 1634 efiles = {}
1638 1635 for cnt, chnk in enumerate(mnfst.group(nodeiter,
1639 1636 lookuplinkrev_func(mnfst))):
1640 1637 if cnt % 3 == 1:
1641 1638 mnode = chnk[:20]
1642 1639 efiles.update(mnfst.readdelta(mnode))
1643 1640 # see above comment for why we divide by 3
1644 1641 self.ui.progress(_('bundling'), cnt / 3,
1645 1642 unit=_('manifests'), total=changecount)
1646 1643 yield chnk
1647 1644 efiles = len(efiles)
1648 1645 self.ui.progress(_('bundling'), None)
1649 1646
1650 1647 for idx, fname in enumerate(sorted(changedfiles)):
1651 1648 filerevlog = self.file(fname)
1652 1649 if not len(filerevlog):
1653 1650 raise util.Abort(_("empty or missing revlog for %s") % fname)
1654 1651 nodeiter = gennodelst(filerevlog)
1655 1652 nodeiter = list(nodeiter)
1656 1653 if nodeiter:
1657 1654 yield changegroup.chunkheader(len(fname))
1658 1655 yield fname
1659 1656 lookup = lookuplinkrev_func(filerevlog)
1660 1657 for chnk in filerevlog.group(nodeiter, lookup):
1661 1658 self.ui.progress(
1662 1659 _('bundling'), idx, item=fname,
1663 1660 total=efiles, unit=_('files'))
1664 1661 yield chnk
1665 1662 self.ui.progress(_('bundling'), None)
1666 1663
1667 1664 yield changegroup.closechunk()
1668 1665
1669 1666 if nodes:
1670 1667 self.hook('outgoing', node=hex(nodes[0]), source=source)
1671 1668
1672 1669 return changegroup.unbundle10(util.chunkbuffer(gengroup()), 'UN')
1673 1670
1674 1671 def addchangegroup(self, source, srctype, url, emptyok=False, lock=None):
1675 1672 """Add the changegroup returned by source.read() to this repo.
1676 1673 srctype is a string like 'push', 'pull', or 'unbundle'. url is
1677 1674 the URL of the repo where this changegroup is coming from.
1678 1675
1679 1676 Return an integer summarizing the change to this repo:
1680 1677 - nothing changed or no source: 0
1681 1678 - more heads than before: 1+added heads (2..n)
1682 1679 - fewer heads than before: -1-removed heads (-2..-n)
1683 1680 - number of heads stays the same: 1
1684 1681 """
1685 1682 def csmap(x):
1686 1683 self.ui.debug("add changeset %s\n" % short(x))
1687 1684 return len(cl)
1688 1685
1689 1686 def revmap(x):
1690 1687 return cl.rev(x)
1691 1688
1692 1689 if not source:
1693 1690 return 0
1694 1691
1695 1692 self.hook('prechangegroup', throw=True, source=srctype, url=url)
1696 1693
1697 1694 changesets = files = revisions = 0
1698 1695 efiles = set()
1699 1696
1700 1697 # write changelog data to temp files so concurrent readers will not see
1701 1698 # inconsistent view
1702 1699 cl = self.changelog
1703 1700 cl.delayupdate()
1704 1701 oldheads = len(cl.heads())
1705 1702
1706 1703 tr = self.transaction("\n".join([srctype, urlmod.hidepassword(url)]))
1707 1704 try:
1708 1705 trp = weakref.proxy(tr)
1709 1706 # pull off the changeset group
1710 1707 self.ui.status(_("adding changesets\n"))
1711 1708 clstart = len(cl)
1712 1709 class prog(object):
1713 1710 step = _('changesets')
1714 1711 count = 1
1715 1712 ui = self.ui
1716 1713 total = None
1717 1714 def __call__(self):
1718 1715 self.ui.progress(self.step, self.count, unit=_('chunks'),
1719 1716 total=self.total)
1720 1717 self.count += 1
1721 1718 pr = prog()
1722 1719 source.callback = pr
1723 1720
1724 1721 if (cl.addgroup(source, csmap, trp) is None
1725 1722 and not emptyok):
1726 1723 raise util.Abort(_("received changelog group is empty"))
1727 1724 clend = len(cl)
1728 1725 changesets = clend - clstart
1729 1726 for c in xrange(clstart, clend):
1730 1727 efiles.update(self[c].files())
1731 1728 efiles = len(efiles)
1732 1729 self.ui.progress(_('changesets'), None)
1733 1730
1734 1731 # pull off the manifest group
1735 1732 self.ui.status(_("adding manifests\n"))
1736 1733 pr.step = _('manifests')
1737 1734 pr.count = 1
1738 1735 pr.total = changesets # manifests <= changesets
1739 1736 # no need to check for empty manifest group here:
1740 1737 # if the result of the merge of 1 and 2 is the same in 3 and 4,
1741 1738 # no new manifest will be created and the manifest group will
1742 1739 # be empty during the pull
1743 1740 self.manifest.addgroup(source, revmap, trp)
1744 1741 self.ui.progress(_('manifests'), None)
1745 1742
1746 1743 needfiles = {}
1747 1744 if self.ui.configbool('server', 'validate', default=False):
1748 1745 # validate incoming csets have their manifests
1749 1746 for cset in xrange(clstart, clend):
1750 1747 mfest = self.changelog.read(self.changelog.node(cset))[0]
1751 1748 mfest = self.manifest.readdelta(mfest)
1752 1749 # store file nodes we must see
1753 1750 for f, n in mfest.iteritems():
1754 1751 needfiles.setdefault(f, set()).add(n)
1755 1752
1756 1753 # process the files
1757 1754 self.ui.status(_("adding file changes\n"))
1758 1755 pr.step = 'files'
1759 1756 pr.count = 1
1760 1757 pr.total = efiles
1761 1758 source.callback = None
1762 1759
1763 1760 while 1:
1764 1761 f = source.chunk()
1765 1762 if not f:
1766 1763 break
1767 1764 self.ui.debug("adding %s revisions\n" % f)
1768 1765 pr()
1769 1766 fl = self.file(f)
1770 1767 o = len(fl)
1771 1768 if fl.addgroup(source, revmap, trp) is None:
1772 1769 raise util.Abort(_("received file revlog group is empty"))
1773 1770 revisions += len(fl) - o
1774 1771 files += 1
1775 1772 if f in needfiles:
1776 1773 needs = needfiles[f]
1777 1774 for new in xrange(o, len(fl)):
1778 1775 n = fl.node(new)
1779 1776 if n in needs:
1780 1777 needs.remove(n)
1781 1778 if not needs:
1782 1779 del needfiles[f]
1783 1780 self.ui.progress(_('files'), None)
1784 1781
1785 1782 for f, needs in needfiles.iteritems():
1786 1783 fl = self.file(f)
1787 1784 for n in needs:
1788 1785 try:
1789 1786 fl.rev(n)
1790 1787 except error.LookupError:
1791 1788 raise util.Abort(
1792 1789 _('missing file data for %s:%s - run hg verify') %
1793 1790 (f, hex(n)))
1794 1791
1795 1792 newheads = len(cl.heads())
1796 1793 heads = ""
1797 1794 if oldheads and newheads != oldheads:
1798 1795 heads = _(" (%+d heads)") % (newheads - oldheads)
1799 1796
1800 1797 self.ui.status(_("added %d changesets"
1801 1798 " with %d changes to %d files%s\n")
1802 1799 % (changesets, revisions, files, heads))
1803 1800
1804 1801 if changesets > 0:
1805 1802 p = lambda: cl.writepending() and self.root or ""
1806 1803 self.hook('pretxnchangegroup', throw=True,
1807 1804 node=hex(cl.node(clstart)), source=srctype,
1808 1805 url=url, pending=p)
1809 1806
1810 1807 # make changelog see real files again
1811 1808 cl.finalize(trp)
1812 1809
1813 1810 tr.close()
1814 1811 finally:
1815 1812 tr.release()
1816 1813 if lock:
1817 1814 lock.release()
1818 1815
1819 1816 if changesets > 0:
1820 1817 # forcefully update the on-disk branch cache
1821 1818 self.ui.debug("updating the branch cache\n")
1822 1819 self.updatebranchcache()
1823 1820 self.hook("changegroup", node=hex(cl.node(clstart)),
1824 1821 source=srctype, url=url)
1825 1822
1826 1823 for i in xrange(clstart, clend):
1827 1824 self.hook("incoming", node=hex(cl.node(i)),
1828 1825 source=srctype, url=url)
1829 1826
1830 1827 # never return 0 here:
1831 1828 if newheads < oldheads:
1832 1829 return newheads - oldheads - 1
1833 1830 else:
1834 1831 return newheads - oldheads + 1
1835 1832
1836 1833
1837 1834 def stream_in(self, remote, requirements):
1838 1835 fp = remote.stream_out()
1839 1836 l = fp.readline()
1840 1837 try:
1841 1838 resp = int(l)
1842 1839 except ValueError:
1843 1840 raise error.ResponseError(
1844 1841 _('Unexpected response from remote server:'), l)
1845 1842 if resp == 1:
1846 1843 raise util.Abort(_('operation forbidden by server'))
1847 1844 elif resp == 2:
1848 1845 raise util.Abort(_('locking the remote repository failed'))
1849 1846 elif resp != 0:
1850 1847 raise util.Abort(_('the server sent an unknown error code'))
1851 1848 self.ui.status(_('streaming all changes\n'))
1852 1849 l = fp.readline()
1853 1850 try:
1854 1851 total_files, total_bytes = map(int, l.split(' ', 1))
1855 1852 except (ValueError, TypeError):
1856 1853 raise error.ResponseError(
1857 1854 _('Unexpected response from remote server:'), l)
1858 1855 self.ui.status(_('%d files to transfer, %s of data\n') %
1859 1856 (total_files, util.bytecount(total_bytes)))
1860 1857 start = time.time()
1861 1858 for i in xrange(total_files):
1862 1859 # XXX doesn't support '\n' or '\r' in filenames
1863 1860 l = fp.readline()
1864 1861 try:
1865 1862 name, size = l.split('\0', 1)
1866 1863 size = int(size)
1867 1864 except (ValueError, TypeError):
1868 1865 raise error.ResponseError(
1869 1866 _('Unexpected response from remote server:'), l)
1870 1867 self.ui.debug('adding %s (%s)\n' % (name, util.bytecount(size)))
1871 1868 # for backwards compat, name was partially encoded
1872 1869 ofp = self.sopener(store.decodedir(name), 'w')
1873 1870 for chunk in util.filechunkiter(fp, limit=size):
1874 1871 ofp.write(chunk)
1875 1872 ofp.close()
1876 1873 elapsed = time.time() - start
1877 1874 if elapsed <= 0:
1878 1875 elapsed = 0.001
1879 1876 self.ui.status(_('transferred %s in %.1f seconds (%s/sec)\n') %
1880 1877 (util.bytecount(total_bytes), elapsed,
1881 1878 util.bytecount(total_bytes / elapsed)))
1882 1879
1883 1880 # new requirements = old non-format requirements + new format-related
1884 1881 # requirements from the streamed-in repository
1885 1882 requirements.update(set(self.requirements) - self.supportedformats)
1886 1883 self._applyrequirements(requirements)
1887 1884 self._writerequirements()
1888 1885
1889 1886 self.invalidate()
1890 1887 return len(self.heads()) + 1
1891 1888
1892 1889 def clone(self, remote, heads=[], stream=False):
1893 1890 '''clone remote repository.
1894 1891
1895 1892 keyword arguments:
1896 1893 heads: list of revs to clone (forces use of pull)
1897 1894 stream: use streaming clone if possible'''
1898 1895
1899 1896 # now, all clients that can request uncompressed clones can
1900 1897 # read repo formats supported by all servers that can serve
1901 1898 # them.
1902 1899
1903 1900 # if revlog format changes, client will have to check version
1904 1901 # and format flags on "stream" capability, and use
1905 1902 # uncompressed only if compatible.
1906 1903
1907 1904 if stream and not heads:
1908 1905 # 'stream' means remote revlog format is revlogv1 only
1909 1906 if remote.capable('stream'):
1910 1907 return self.stream_in(remote, set(('revlogv1',)))
1911 1908 # otherwise, 'streamreqs' contains the remote revlog format
1912 1909 streamreqs = remote.capable('streamreqs')
1913 1910 if streamreqs:
1914 1911 streamreqs = set(streamreqs.split(','))
1915 1912 # if we support it, stream in and adjust our requirements
1916 1913 if not streamreqs - self.supportedformats:
1917 1914 return self.stream_in(remote, streamreqs)
1918 1915 return self.pull(remote, heads)
1919 1916
1920 1917 def pushkey(self, namespace, key, old, new):
1921 1918 return pushkey.push(self, namespace, key, old, new)
1922 1919
1923 1920 def listkeys(self, namespace):
1924 1921 return pushkey.list(self, namespace)
1925 1922
1926 1923 # used to avoid circular references so destructors work
1927 1924 def aftertrans(files):
1928 1925 renamefiles = [tuple(t) for t in files]
1929 1926 def a():
1930 1927 for src, dest in renamefiles:
1931 1928 util.rename(src, dest)
1932 1929 return a
1933 1930
1934 1931 def instance(ui, path, create):
1935 1932 return localrepository(ui, util.drop_scheme('file', path), create)
1936 1933
1937 1934 def islocal(path):
1938 1935 return True
@@ -1,90 +1,90 b''
1 1 # parsers.py - Python implementation of parsers.c
2 2 #
3 3 # Copyright 2009 Matt Mackall <mpm@selenic.com> and others
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 mercurial.node import bin, nullid, nullrev
9 9 from mercurial import util
10 10 import struct, zlib
11 11
12 12 _pack = struct.pack
13 13 _unpack = struct.unpack
14 14 _compress = zlib.compress
15 15 _decompress = zlib.decompress
16 16 _sha = util.sha1
17 17
18 18 def parse_manifest(mfdict, fdict, lines):
19 19 for l in lines.splitlines():
20 20 f, n = l.split('\0')
21 21 if len(n) > 40:
22 22 fdict[f] = n[40:]
23 23 mfdict[f] = bin(n[:40])
24 24 else:
25 25 mfdict[f] = bin(n)
26 26
27 27 def parse_index(data, inline):
28 28 def gettype(q):
29 29 return int(q & 0xFFFF)
30 30
31 31 def offset_type(offset, type):
32 32 return long(long(offset) << 16 | type)
33 33
34 34 indexformatng = ">Qiiiiii20s12x"
35 35
36 36 s = struct.calcsize(indexformatng)
37 37 index = []
38 38 cache = None
39 39 nodemap = {nullid: nullrev}
40 40 n = off = 0
41 # if we're not using lazymap, always read the whole index
41
42 42 l = len(data) - s
43 43 append = index.append
44 44 if inline:
45 45 cache = (0, data)
46 46 while off <= l:
47 47 e = _unpack(indexformatng, data[off:off + s])
48 48 nodemap[e[7]] = n
49 49 append(e)
50 50 n += 1
51 51 if e[1] < 0:
52 52 break
53 53 off += e[1] + s
54 54 else:
55 55 while off <= l:
56 56 e = _unpack(indexformatng, data[off:off + s])
57 57 nodemap[e[7]] = n
58 58 append(e)
59 59 n += 1
60 60 off += s
61 61
62 62 e = list(index[0])
63 63 type = gettype(e[0])
64 64 e[0] = offset_type(0, type)
65 65 index[0] = tuple(e)
66 66
67 67 # add the magic null revision at -1
68 68 index.append((0, 0, 0, -1, -1, -1, -1, nullid))
69 69
70 70 return index, nodemap, cache
71 71
72 72 def parse_dirstate(dmap, copymap, st):
73 73 parents = [st[:20], st[20: 40]]
74 74 # deref fields so they will be local in loop
75 75 format = ">cllll"
76 76 e_size = struct.calcsize(format)
77 77 pos1 = 40
78 78 l = len(st)
79 79
80 80 # the inner loop
81 81 while pos1 < l:
82 82 pos2 = pos1 + e_size
83 83 e = _unpack(">cllll", st[pos1:pos2]) # a literal here is faster
84 84 pos1 = pos2 + e[4]
85 85 f = st[pos2:pos1]
86 86 if '\0' in f:
87 87 f, c = f.split('\0')
88 88 copymap[f] = c
89 89 dmap[f] = e[:4]
90 90 return parents
@@ -1,1482 +1,1235 b''
1 1 # revlog.py - storage back-end 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 """Storage back-end for Mercurial.
9 9
10 10 This provides efficient delta storage with O(1) retrieve and append
11 11 and O(changes) merge between branches.
12 12 """
13 13
14 14 # import stuff from node for others to import from revlog
15 15 from node import bin, hex, nullid, nullrev, short #@UnusedImport
16 16 from i18n import _
17 17 import changegroup, ancestor, mdiff, parsers, error, util
18 18 import struct, zlib, errno
19 19
20 20 _pack = struct.pack
21 21 _unpack = struct.unpack
22 22 _compress = zlib.compress
23 23 _decompress = zlib.decompress
24 24 _sha = util.sha1
25 25
26 26 # revlog header flags
27 27 REVLOGV0 = 0
28 28 REVLOGNG = 1
29 29 REVLOGNGINLINEDATA = (1 << 16)
30 30 REVLOGSHALLOW = (1 << 17)
31 31 REVLOG_DEFAULT_FLAGS = REVLOGNGINLINEDATA
32 32 REVLOG_DEFAULT_FORMAT = REVLOGNG
33 33 REVLOG_DEFAULT_VERSION = REVLOG_DEFAULT_FORMAT | REVLOG_DEFAULT_FLAGS
34 34 REVLOGNG_FLAGS = REVLOGNGINLINEDATA | REVLOGSHALLOW
35 35
36 36 # revlog index flags
37 37 REVIDX_PARENTDELTA = 1
38 38 REVIDX_PUNCHED_FLAG = 2
39 39 REVIDX_KNOWN_FLAGS = REVIDX_PUNCHED_FLAG | REVIDX_PARENTDELTA
40 40
41 # amount of data read unconditionally, should be >= 4
42 # when not inline: threshold for using lazy index
43 _prereadsize = 1048576
44 41 # max size of revlog with inline data
45 42 _maxinline = 131072
43 _chunksize = 1048576
46 44
47 45 RevlogError = error.RevlogError
48 46 LookupError = error.LookupError
49 47
50 48 def getoffset(q):
51 49 return int(q >> 16)
52 50
53 51 def gettype(q):
54 52 return int(q & 0xFFFF)
55 53
56 54 def offset_type(offset, type):
57 55 return long(long(offset) << 16 | type)
58 56
59 57 nullhash = _sha(nullid)
60 58
61 59 def hash(text, p1, p2):
62 60 """generate a hash from the given text and its parent hashes
63 61
64 62 This hash combines both the current file contents and its history
65 63 in a manner that makes it easy to distinguish nodes with the same
66 64 content in the revision graph.
67 65 """
68 66 # As of now, if one of the parent node is null, p2 is null
69 67 if p2 == nullid:
70 68 # deep copy of a hash is faster than creating one
71 69 s = nullhash.copy()
72 70 s.update(p1)
73 71 else:
74 72 # none of the parent nodes are nullid
75 73 l = [p1, p2]
76 74 l.sort()
77 75 s = _sha(l[0])
78 76 s.update(l[1])
79 77 s.update(text)
80 78 return s.digest()
81 79
82 80 def compress(text):
83 81 """ generate a possibly-compressed representation of text """
84 82 if not text:
85 83 return ("", text)
86 84 l = len(text)
87 85 bin = None
88 86 if l < 44:
89 87 pass
90 88 elif l > 1000000:
91 89 # zlib makes an internal copy, thus doubling memory usage for
92 90 # large files, so lets do this in pieces
93 91 z = zlib.compressobj()
94 92 p = []
95 93 pos = 0
96 94 while pos < l:
97 95 pos2 = pos + 2**20
98 96 p.append(z.compress(text[pos:pos2]))
99 97 pos = pos2
100 98 p.append(z.flush())
101 99 if sum(map(len, p)) < l:
102 100 bin = "".join(p)
103 101 else:
104 102 bin = _compress(text)
105 103 if bin is None or len(bin) > l:
106 104 if text[0] == '\0':
107 105 return ("", text)
108 106 return ('u', text)
109 107 return ("", bin)
110 108
111 109 def decompress(bin):
112 110 """ decompress the given input """
113 111 if not bin:
114 112 return bin
115 113 t = bin[0]
116 114 if t == '\0':
117 115 return bin
118 116 if t == 'x':
119 117 return _decompress(bin)
120 118 if t == 'u':
121 119 return bin[1:]
122 120 raise RevlogError(_("unknown compression type %r") % t)
123 121
124 class lazyparser(object):
125 """
126 this class avoids the need to parse the entirety of large indices
127 """
128
129 # lazyparser is not safe to use on windows if win32 extensions not
130 # available. it keeps file handle open, which make it not possible
131 # to break hardlinks on local cloned repos.
132
133 def __init__(self, dataf):
134 try:
135 size = util.fstat(dataf).st_size
136 except AttributeError:
137 size = 0
138 self.dataf = dataf
139 self.s = struct.calcsize(indexformatng)
140 self.datasize = size
141 self.l = size // self.s
142 self.index = [None] * self.l
143 self.map = {nullid: nullrev}
144 self.allmap = 0
145 self.all = 0
146 self.mapfind_count = 0
147
148 def loadmap(self):
149 """
150 during a commit, we need to make sure the rev being added is
151 not a duplicate. This requires loading the entire index,
152 which is fairly slow. loadmap can load up just the node map,
153 which takes much less time.
154 """
155 if self.allmap:
156 return
157 end = self.datasize
158 self.allmap = 1
159 cur = 0
160 count = 0
161 blocksize = self.s * 256
162 self.dataf.seek(0)
163 while cur < end:
164 data = self.dataf.read(blocksize)
165 off = 0
166 for x in xrange(256):
167 n = data[off + ngshaoffset:off + ngshaoffset + 20]
168 self.map[n] = count
169 count += 1
170 if count >= self.l:
171 break
172 off += self.s
173 cur += blocksize
174
175 def loadblock(self, blockstart, blocksize, data=None):
176 if self.all:
177 return
178 if data is None:
179 self.dataf.seek(blockstart)
180 if blockstart + blocksize > self.datasize:
181 # the revlog may have grown since we've started running,
182 # but we don't have space in self.index for more entries.
183 # limit blocksize so that we don't get too much data.
184 blocksize = max(self.datasize - blockstart, 0)
185 data = self.dataf.read(blocksize)
186 lend = len(data) // self.s
187 i = blockstart // self.s
188 off = 0
189 # lazyindex supports __delitem__
190 if lend > len(self.index) - i:
191 lend = len(self.index) - i
192 for x in xrange(lend):
193 if self.index[i + x] is None:
194 b = data[off : off + self.s]
195 self.index[i + x] = b
196 n = b[ngshaoffset:ngshaoffset + 20]
197 self.map[n] = i + x
198 off += self.s
199
200 def findnode(self, node):
201 """search backwards through the index file for a specific node"""
202 if self.allmap:
203 return None
204
205 # hg log will cause many many searches for the manifest
206 # nodes. After we get called a few times, just load the whole
207 # thing.
208 if self.mapfind_count > 8:
209 self.loadmap()
210 if node in self.map:
211 return node
212 return None
213 self.mapfind_count += 1
214 last = self.l - 1
215 while self.index[last] is not None:
216 if last == 0:
217 self.all = 1
218 self.allmap = 1
219 return None
220 last -= 1
221 end = (last + 1) * self.s
222 blocksize = self.s * 256
223 while end >= 0:
224 start = max(end - blocksize, 0)
225 self.dataf.seek(start)
226 data = self.dataf.read(end - start)
227 findend = end - start
228 while True:
229 # we're searching backwards, so we have to make sure
230 # we don't find a changeset where this node is a parent
231 off = data.find(node, 0, findend)
232 findend = off
233 if off >= 0:
234 i = off / self.s
235 off = i * self.s
236 n = data[off + ngshaoffset:off + ngshaoffset + 20]
237 if n == node:
238 self.map[n] = i + start / self.s
239 return node
240 else:
241 break
242 end -= blocksize
243 return None
244
245 def loadindex(self, i=None, end=None):
246 if self.all:
247 return
248 all = False
249 if i is None:
250 blockstart = 0
251 blocksize = (65536 / self.s) * self.s
252 end = self.datasize
253 all = True
254 else:
255 if end:
256 blockstart = i * self.s
257 end = end * self.s
258 blocksize = end - blockstart
259 else:
260 blockstart = (i & ~1023) * self.s
261 blocksize = self.s * 1024
262 end = blockstart + blocksize
263 while blockstart < end:
264 self.loadblock(blockstart, blocksize)
265 blockstart += blocksize
266 if all:
267 self.all = True
268
269 class lazyindex(object):
270 """a lazy version of the index array"""
271 def __init__(self, parser):
272 self.p = parser
273 def __len__(self):
274 return len(self.p.index)
275 def load(self, pos):
276 if pos < 0:
277 pos += len(self.p.index)
278 self.p.loadindex(pos)
279 return self.p.index[pos]
280 def __getitem__(self, pos):
281 return _unpack(indexformatng, self.p.index[pos] or self.load(pos))
282 def __setitem__(self, pos, item):
283 self.p.index[pos] = _pack(indexformatng, *item)
284 def __delitem__(self, pos):
285 del self.p.index[pos]
286 def insert(self, pos, e):
287 self.p.index.insert(pos, _pack(indexformatng, *e))
288 def append(self, e):
289 self.p.index.append(_pack(indexformatng, *e))
290
291 class lazymap(object):
292 """a lazy version of the node map"""
293 def __init__(self, parser):
294 self.p = parser
295 def load(self, key):
296 n = self.p.findnode(key)
297 if n is None:
298 raise KeyError(key)
299 def __contains__(self, key):
300 if key in self.p.map:
301 return True
302 self.p.loadmap()
303 return key in self.p.map
304 def __iter__(self):
305 yield nullid
306 for i, ret in enumerate(self.p.index):
307 if not ret:
308 self.p.loadindex(i)
309 ret = self.p.index[i]
310 if isinstance(ret, str):
311 ret = _unpack(indexformatng, ret)
312 yield ret[7]
313 def __getitem__(self, key):
314 try:
315 return self.p.map[key]
316 except KeyError:
317 try:
318 self.load(key)
319 return self.p.map[key]
320 except KeyError:
321 raise KeyError("node " + hex(key))
322 def __setitem__(self, key, val):
323 self.p.map[key] = val
324 def __delitem__(self, key):
325 del self.p.map[key]
326
327 122 indexformatv0 = ">4l20s20s20s"
328 123 v0shaoffset = 56
329 124
330 125 class revlogoldio(object):
331 126 def __init__(self):
332 127 self.size = struct.calcsize(indexformatv0)
333 128
334 129 def parseindex(self, fp, data, inline):
335 130 s = self.size
336 131 index = []
337 132 nodemap = {nullid: nullrev}
338 133 n = off = 0
339 if len(data) == _prereadsize:
340 data += fp.read() # read the rest
341 134 l = len(data)
342 135 while off + s <= l:
343 136 cur = data[off:off + s]
344 137 off += s
345 138 e = _unpack(indexformatv0, cur)
346 139 # transform to revlogv1 format
347 140 e2 = (offset_type(e[0], 0), e[1], -1, e[2], e[3],
348 141 nodemap.get(e[4], nullrev), nodemap.get(e[5], nullrev), e[6])
349 142 index.append(e2)
350 143 nodemap[e[6]] = n
351 144 n += 1
352 145
353 146 return index, nodemap, None
354 147
355 148 def packentry(self, entry, node, version, rev):
356 149 if gettype(entry[0]):
357 150 raise RevlogError(_("index entry flags need RevlogNG"))
358 151 e2 = (getoffset(entry[0]), entry[1], entry[3], entry[4],
359 152 node(entry[5]), node(entry[6]), entry[7])
360 153 return _pack(indexformatv0, *e2)
361 154
362 155 # index ng:
363 156 # 6 bytes: offset
364 157 # 2 bytes: flags
365 158 # 4 bytes: compressed length
366 159 # 4 bytes: uncompressed length
367 160 # 4 bytes: base rev
368 161 # 4 bytes: link rev
369 162 # 4 bytes: parent 1 rev
370 163 # 4 bytes: parent 2 rev
371 164 # 32 bytes: nodeid
372 165 indexformatng = ">Qiiiiii20s12x"
373 166 ngshaoffset = 32
374 167 versionformat = ">I"
375 168
376 169 class revlogio(object):
377 170 def __init__(self):
378 171 self.size = struct.calcsize(indexformatng)
379 172
380 173 def parseindex(self, fp, data, inline):
381 if len(data) == _prereadsize:
382 if util.openhardlinks() and not inline:
383 # big index, let's parse it on demand
384 parser = lazyparser(fp)
385 index = lazyindex(parser)
386 nodemap = lazymap(parser)
387 e = list(index[0])
388 type = gettype(e[0])
389 e[0] = offset_type(0, type)
390 index[0] = e
391 return index, nodemap, None
392 else:
393 data += fp.read()
394
395 174 # call the C implementation to parse the index data
396 175 index, nodemap, cache = parsers.parse_index(data, inline)
397 176 return index, nodemap, cache
398 177
399 178 def packentry(self, entry, node, version, rev):
400 179 p = _pack(indexformatng, *entry)
401 180 if rev == 0:
402 181 p = _pack(versionformat, version) + p[4:]
403 182 return p
404 183
405 184 class revlog(object):
406 185 """
407 186 the underlying revision storage object
408 187
409 188 A revlog consists of two parts, an index and the revision data.
410 189
411 190 The index is a file with a fixed record size containing
412 191 information on each revision, including its nodeid (hash), the
413 192 nodeids of its parents, the position and offset of its data within
414 193 the data file, and the revision it's based on. Finally, each entry
415 194 contains a linkrev entry that can serve as a pointer to external
416 195 data.
417 196
418 197 The revision data itself is a linear collection of data chunks.
419 198 Each chunk represents a revision and is usually represented as a
420 199 delta against the previous chunk. To bound lookup time, runs of
421 200 deltas are limited to about 2 times the length of the original
422 201 version data. This makes retrieval of a version proportional to
423 202 its size, or O(1) relative to the number of revisions.
424 203
425 204 Both pieces of the revlog are written to in an append-only
426 205 fashion, which means we never need to rewrite a file to insert or
427 206 remove data, and can use some simple techniques to avoid the need
428 207 for locking while reading.
429 208 """
430 209 def __init__(self, opener, indexfile, shallowroot=None):
431 210 """
432 211 create a revlog object
433 212
434 213 opener is a function that abstracts the file opening operation
435 214 and can be used to implement COW semantics or the like.
436 215 """
437 216 self.indexfile = indexfile
438 217 self.datafile = indexfile[:-2] + ".d"
439 218 self.opener = opener
440 219 self._cache = None
441 220 self._chunkcache = (0, '')
442 221 self.nodemap = {nullid: nullrev}
443 222 self.index = []
444 223 self._shallowroot = shallowroot
445 224 self._parentdelta = 0
446 225
447 226 v = REVLOG_DEFAULT_VERSION
448 227 if hasattr(opener, 'options') and 'defversion' in opener.options:
449 228 v = opener.options['defversion']
450 229 if v & REVLOGNG:
451 230 v |= REVLOGNGINLINEDATA
452 231 if v & REVLOGNG and 'parentdelta' in opener.options:
453 232 self._parentdelta = 1
454 233
455 234 if shallowroot:
456 235 v |= REVLOGSHALLOW
457 236
458 237 i = ''
459 238 try:
460 239 f = self.opener(self.indexfile)
461 if "nonlazy" in getattr(self.opener, 'options', {}):
462 i = f.read()
463 else:
464 i = f.read(_prereadsize)
240 i = f.read()
465 241 if len(i) > 0:
466 242 v = struct.unpack(versionformat, i[:4])[0]
467 243 except IOError, inst:
468 244 if inst.errno != errno.ENOENT:
469 245 raise
470 246
471 247 self.version = v
472 248 self._inline = v & REVLOGNGINLINEDATA
473 249 self._shallow = v & REVLOGSHALLOW
474 250 flags = v & ~0xFFFF
475 251 fmt = v & 0xFFFF
476 252 if fmt == REVLOGV0 and flags:
477 253 raise RevlogError(_("index %s unknown flags %#04x for format v0")
478 254 % (self.indexfile, flags >> 16))
479 255 elif fmt == REVLOGNG and flags & ~REVLOGNG_FLAGS:
480 256 raise RevlogError(_("index %s unknown flags %#04x for revlogng")
481 257 % (self.indexfile, flags >> 16))
482 258 elif fmt > REVLOGNG:
483 259 raise RevlogError(_("index %s unknown format %d")
484 260 % (self.indexfile, fmt))
485 261
486 262 self._io = revlogio()
487 263 if self.version == REVLOGV0:
488 264 self._io = revlogoldio()
489 265 if i:
490 266 try:
491 267 d = self._io.parseindex(f, i, self._inline)
492 268 except (ValueError, IndexError):
493 269 raise RevlogError(_("index %s is corrupted") % (self.indexfile))
494 270 self.index, self.nodemap, self._chunkcache = d
495 271 if not self._chunkcache:
496 272 self._chunkclear()
497 273
498 274 # add the magic null revision at -1 (if it hasn't been done already)
499 if (self.index == [] or isinstance(self.index, lazyindex) or
500 self.index[-1][7] != nullid) :
275 if self.index == [] or self.index[-1][7] != nullid:
501 276 self.index.append((0, 0, 0, -1, -1, -1, -1, nullid))
502 277
503 def _loadindex(self, start, end):
504 """load a block of indexes all at once from the lazy parser"""
505 if isinstance(self.index, lazyindex):
506 self.index.p.loadindex(start, end)
507
508 def _loadindexmap(self):
509 """loads both the map and the index from the lazy parser"""
510 if isinstance(self.index, lazyindex):
511 p = self.index.p
512 p.loadindex()
513 self.nodemap = p.map
514
515 def _loadmap(self):
516 """loads the map from the lazy parser"""
517 if isinstance(self.nodemap, lazymap):
518 self.nodemap.p.loadmap()
519 self.nodemap = self.nodemap.p.map
520
521 278 def tip(self):
522 279 return self.node(len(self.index) - 2)
523 280 def __len__(self):
524 281 return len(self.index) - 1
525 282 def __iter__(self):
526 283 for i in xrange(len(self)):
527 284 yield i
528 285 def rev(self, node):
529 286 try:
530 287 return self.nodemap[node]
531 288 except KeyError:
532 289 raise LookupError(node, self.indexfile, _('no node'))
533 290 def node(self, rev):
534 291 return self.index[rev][7]
535 292 def linkrev(self, rev):
536 293 return self.index[rev][4]
537 294 def parents(self, node):
538 295 i = self.index
539 296 d = i[self.rev(node)]
540 297 return i[d[5]][7], i[d[6]][7] # map revisions to nodes inline
541 298 def parentrevs(self, rev):
542 299 return self.index[rev][5:7]
543 300 def start(self, rev):
544 301 return int(self.index[rev][0] >> 16)
545 302 def end(self, rev):
546 303 return self.start(rev) + self.length(rev)
547 304 def length(self, rev):
548 305 return self.index[rev][1]
549 306 def base(self, rev):
550 307 return self.index[rev][3]
551 308 def flags(self, rev):
552 309 return self.index[rev][0] & 0xFFFF
553 310 def rawsize(self, rev):
554 311 """return the length of the uncompressed text for a given revision"""
555 312 l = self.index[rev][2]
556 313 if l >= 0:
557 314 return l
558 315
559 316 t = self.revision(self.node(rev))
560 317 return len(t)
561 318 size = rawsize
562 319
563 320 def reachable(self, node, stop=None):
564 321 """return the set of all nodes ancestral to a given node, including
565 322 the node itself, stopping when stop is matched"""
566 323 reachable = set((node,))
567 324 visit = [node]
568 325 if stop:
569 326 stopn = self.rev(stop)
570 327 else:
571 328 stopn = 0
572 329 while visit:
573 330 n = visit.pop(0)
574 331 if n == stop:
575 332 continue
576 333 if n == nullid:
577 334 continue
578 335 for p in self.parents(n):
579 336 if self.rev(p) < stopn:
580 337 continue
581 338 if p not in reachable:
582 339 reachable.add(p)
583 340 visit.append(p)
584 341 return reachable
585 342
586 343 def ancestors(self, *revs):
587 344 """Generate the ancestors of 'revs' in reverse topological order.
588 345
589 346 Yield a sequence of revision numbers starting with the parents
590 347 of each revision in revs, i.e., each revision is *not* considered
591 348 an ancestor of itself. Results are in breadth-first order:
592 349 parents of each rev in revs, then parents of those, etc. Result
593 350 does not include the null revision."""
594 351 visit = list(revs)
595 352 seen = set([nullrev])
596 353 while visit:
597 354 for parent in self.parentrevs(visit.pop(0)):
598 355 if parent not in seen:
599 356 visit.append(parent)
600 357 seen.add(parent)
601 358 yield parent
602 359
603 360 def descendants(self, *revs):
604 361 """Generate the descendants of 'revs' in revision order.
605 362
606 363 Yield a sequence of revision numbers starting with a child of
607 364 some rev in revs, i.e., each revision is *not* considered a
608 365 descendant of itself. Results are ordered by revision number (a
609 366 topological sort)."""
610 367 first = min(revs)
611 368 if first == nullrev:
612 369 for i in self:
613 370 yield i
614 371 return
615 372
616 373 seen = set(revs)
617 374 for i in xrange(first + 1, len(self)):
618 375 for x in self.parentrevs(i):
619 376 if x != nullrev and x in seen:
620 377 seen.add(i)
621 378 yield i
622 379 break
623 380
624 381 def findmissing(self, common=None, heads=None):
625 382 """Return the ancestors of heads that are not ancestors of common.
626 383
627 384 More specifically, return a list of nodes N such that every N
628 385 satisfies the following constraints:
629 386
630 387 1. N is an ancestor of some node in 'heads'
631 388 2. N is not an ancestor of any node in 'common'
632 389
633 390 The list is sorted by revision number, meaning it is
634 391 topologically sorted.
635 392
636 393 'heads' and 'common' are both lists of node IDs. If heads is
637 394 not supplied, uses all of the revlog's heads. If common is not
638 395 supplied, uses nullid."""
639 396 if common is None:
640 397 common = [nullid]
641 398 if heads is None:
642 399 heads = self.heads()
643 400
644 401 common = [self.rev(n) for n in common]
645 402 heads = [self.rev(n) for n in heads]
646 403
647 404 # we want the ancestors, but inclusive
648 405 has = set(self.ancestors(*common))
649 406 has.add(nullrev)
650 407 has.update(common)
651 408
652 409 # take all ancestors from heads that aren't in has
653 410 missing = set()
654 411 visit = [r for r in heads if r not in has]
655 412 while visit:
656 413 r = visit.pop(0)
657 414 if r in missing:
658 415 continue
659 416 else:
660 417 missing.add(r)
661 418 for p in self.parentrevs(r):
662 419 if p not in has:
663 420 visit.append(p)
664 421 missing = list(missing)
665 422 missing.sort()
666 423 return [self.node(r) for r in missing]
667 424
668 425 def nodesbetween(self, roots=None, heads=None):
669 426 """Return a topological path from 'roots' to 'heads'.
670 427
671 428 Return a tuple (nodes, outroots, outheads) where 'nodes' is a
672 429 topologically sorted list of all nodes N that satisfy both of
673 430 these constraints:
674 431
675 432 1. N is a descendant of some node in 'roots'
676 433 2. N is an ancestor of some node in 'heads'
677 434
678 435 Every node is considered to be both a descendant and an ancestor
679 436 of itself, so every reachable node in 'roots' and 'heads' will be
680 437 included in 'nodes'.
681 438
682 439 'outroots' is the list of reachable nodes in 'roots', i.e., the
683 440 subset of 'roots' that is returned in 'nodes'. Likewise,
684 441 'outheads' is the subset of 'heads' that is also in 'nodes'.
685 442
686 443 'roots' and 'heads' are both lists of node IDs. If 'roots' is
687 444 unspecified, uses nullid as the only root. If 'heads' is
688 445 unspecified, uses list of all of the revlog's heads."""
689 446 nonodes = ([], [], [])
690 447 if roots is not None:
691 448 roots = list(roots)
692 449 if not roots:
693 450 return nonodes
694 451 lowestrev = min([self.rev(n) for n in roots])
695 452 else:
696 453 roots = [nullid] # Everybody's a descendent of nullid
697 454 lowestrev = nullrev
698 455 if (lowestrev == nullrev) and (heads is None):
699 456 # We want _all_ the nodes!
700 457 return ([self.node(r) for r in self], [nullid], list(self.heads()))
701 458 if heads is None:
702 459 # All nodes are ancestors, so the latest ancestor is the last
703 460 # node.
704 461 highestrev = len(self) - 1
705 462 # Set ancestors to None to signal that every node is an ancestor.
706 463 ancestors = None
707 464 # Set heads to an empty dictionary for later discovery of heads
708 465 heads = {}
709 466 else:
710 467 heads = list(heads)
711 468 if not heads:
712 469 return nonodes
713 470 ancestors = set()
714 471 # Turn heads into a dictionary so we can remove 'fake' heads.
715 472 # Also, later we will be using it to filter out the heads we can't
716 473 # find from roots.
717 474 heads = dict.fromkeys(heads, 0)
718 475 # Start at the top and keep marking parents until we're done.
719 476 nodestotag = set(heads)
720 477 # Remember where the top was so we can use it as a limit later.
721 478 highestrev = max([self.rev(n) for n in nodestotag])
722 479 while nodestotag:
723 480 # grab a node to tag
724 481 n = nodestotag.pop()
725 482 # Never tag nullid
726 483 if n == nullid:
727 484 continue
728 485 # A node's revision number represents its place in a
729 486 # topologically sorted list of nodes.
730 487 r = self.rev(n)
731 488 if r >= lowestrev:
732 489 if n not in ancestors:
733 490 # If we are possibly a descendent of one of the roots
734 491 # and we haven't already been marked as an ancestor
735 492 ancestors.add(n) # Mark as ancestor
736 493 # Add non-nullid parents to list of nodes to tag.
737 494 nodestotag.update([p for p in self.parents(n) if
738 495 p != nullid])
739 496 elif n in heads: # We've seen it before, is it a fake head?
740 497 # So it is, real heads should not be the ancestors of
741 498 # any other heads.
742 499 heads.pop(n)
743 500 if not ancestors:
744 501 return nonodes
745 502 # Now that we have our set of ancestors, we want to remove any
746 503 # roots that are not ancestors.
747 504
748 505 # If one of the roots was nullid, everything is included anyway.
749 506 if lowestrev > nullrev:
750 507 # But, since we weren't, let's recompute the lowest rev to not
751 508 # include roots that aren't ancestors.
752 509
753 510 # Filter out roots that aren't ancestors of heads
754 511 roots = [n for n in roots if n in ancestors]
755 512 # Recompute the lowest revision
756 513 if roots:
757 514 lowestrev = min([self.rev(n) for n in roots])
758 515 else:
759 516 # No more roots? Return empty list
760 517 return nonodes
761 518 else:
762 519 # We are descending from nullid, and don't need to care about
763 520 # any other roots.
764 521 lowestrev = nullrev
765 522 roots = [nullid]
766 523 # Transform our roots list into a set.
767 524 descendents = set(roots)
768 525 # Also, keep the original roots so we can filter out roots that aren't
769 526 # 'real' roots (i.e. are descended from other roots).
770 527 roots = descendents.copy()
771 528 # Our topologically sorted list of output nodes.
772 529 orderedout = []
773 530 # Don't start at nullid since we don't want nullid in our output list,
774 531 # and if nullid shows up in descedents, empty parents will look like
775 532 # they're descendents.
776 533 for r in xrange(max(lowestrev, 0), highestrev + 1):
777 534 n = self.node(r)
778 535 isdescendent = False
779 536 if lowestrev == nullrev: # Everybody is a descendent of nullid
780 537 isdescendent = True
781 538 elif n in descendents:
782 539 # n is already a descendent
783 540 isdescendent = True
784 541 # This check only needs to be done here because all the roots
785 542 # will start being marked is descendents before the loop.
786 543 if n in roots:
787 544 # If n was a root, check if it's a 'real' root.
788 545 p = tuple(self.parents(n))
789 546 # If any of its parents are descendents, it's not a root.
790 547 if (p[0] in descendents) or (p[1] in descendents):
791 548 roots.remove(n)
792 549 else:
793 550 p = tuple(self.parents(n))
794 551 # A node is a descendent if either of its parents are
795 552 # descendents. (We seeded the dependents list with the roots
796 553 # up there, remember?)
797 554 if (p[0] in descendents) or (p[1] in descendents):
798 555 descendents.add(n)
799 556 isdescendent = True
800 557 if isdescendent and ((ancestors is None) or (n in ancestors)):
801 558 # Only include nodes that are both descendents and ancestors.
802 559 orderedout.append(n)
803 560 if (ancestors is not None) and (n in heads):
804 561 # We're trying to figure out which heads are reachable
805 562 # from roots.
806 563 # Mark this head as having been reached
807 564 heads[n] = 1
808 565 elif ancestors is None:
809 566 # Otherwise, we're trying to discover the heads.
810 567 # Assume this is a head because if it isn't, the next step
811 568 # will eventually remove it.
812 569 heads[n] = 1
813 570 # But, obviously its parents aren't.
814 571 for p in self.parents(n):
815 572 heads.pop(p, None)
816 573 heads = [n for n in heads.iterkeys() if heads[n] != 0]
817 574 roots = list(roots)
818 575 assert orderedout
819 576 assert roots
820 577 assert heads
821 578 return (orderedout, roots, heads)
822 579
823 580 def heads(self, start=None, stop=None):
824 581 """return the list of all nodes that have no children
825 582
826 583 if start is specified, only heads that are descendants of
827 584 start will be returned
828 585 if stop is specified, it will consider all the revs from stop
829 586 as if they had no children
830 587 """
831 588 if start is None and stop is None:
832 589 count = len(self)
833 590 if not count:
834 591 return [nullid]
835 592 ishead = [1] * (count + 1)
836 593 index = self.index
837 594 for r in xrange(count):
838 595 e = index[r]
839 596 ishead[e[5]] = ishead[e[6]] = 0
840 597 return [self.node(r) for r in xrange(count) if ishead[r]]
841 598
842 599 if start is None:
843 600 start = nullid
844 601 if stop is None:
845 602 stop = []
846 603 stoprevs = set([self.rev(n) for n in stop])
847 604 startrev = self.rev(start)
848 605 reachable = set((startrev,))
849 606 heads = set((startrev,))
850 607
851 608 parentrevs = self.parentrevs
852 609 for r in xrange(startrev + 1, len(self)):
853 610 for p in parentrevs(r):
854 611 if p in reachable:
855 612 if r not in stoprevs:
856 613 reachable.add(r)
857 614 heads.add(r)
858 615 if p in heads and p not in stoprevs:
859 616 heads.remove(p)
860 617
861 618 return [self.node(r) for r in heads]
862 619
863 620 def children(self, node):
864 621 """find the children of a given node"""
865 622 c = []
866 623 p = self.rev(node)
867 624 for r in range(p + 1, len(self)):
868 625 prevs = [pr for pr in self.parentrevs(r) if pr != nullrev]
869 626 if prevs:
870 627 for pr in prevs:
871 628 if pr == p:
872 629 c.append(self.node(r))
873 630 elif p == nullrev:
874 631 c.append(self.node(r))
875 632 return c
876 633
877 634 def descendant(self, start, end):
878 635 if start == nullrev:
879 636 return True
880 637 for i in self.descendants(start):
881 638 if i == end:
882 639 return True
883 640 elif i > end:
884 641 break
885 642 return False
886 643
887 644 def ancestor(self, a, b):
888 645 """calculate the least common ancestor of nodes a and b"""
889 646
890 647 # fast path, check if it is a descendant
891 648 a, b = self.rev(a), self.rev(b)
892 649 start, end = sorted((a, b))
893 650 if self.descendant(start, end):
894 651 return self.node(start)
895 652
896 653 def parents(rev):
897 654 return [p for p in self.parentrevs(rev) if p != nullrev]
898 655
899 656 c = ancestor.ancestor(a, b, parents)
900 657 if c is None:
901 658 return nullid
902 659
903 660 return self.node(c)
904 661
905 662 def _match(self, id):
906 663 if isinstance(id, (long, int)):
907 664 # rev
908 665 return self.node(id)
909 666 if len(id) == 20:
910 667 # possibly a binary node
911 668 # odds of a binary node being all hex in ASCII are 1 in 10**25
912 669 try:
913 670 node = id
914 671 self.rev(node) # quick search the index
915 672 return node
916 673 except LookupError:
917 674 pass # may be partial hex id
918 675 try:
919 676 # str(rev)
920 677 rev = int(id)
921 678 if str(rev) != id:
922 679 raise ValueError
923 680 if rev < 0:
924 681 rev = len(self) + rev
925 682 if rev < 0 or rev >= len(self):
926 683 raise ValueError
927 684 return self.node(rev)
928 685 except (ValueError, OverflowError):
929 686 pass
930 687 if len(id) == 40:
931 688 try:
932 689 # a full hex nodeid?
933 690 node = bin(id)
934 691 self.rev(node)
935 692 return node
936 693 except (TypeError, LookupError):
937 694 pass
938 695
939 696 def _partialmatch(self, id):
940 697 if len(id) < 40:
941 698 try:
942 699 # hex(node)[:...]
943 700 l = len(id) // 2 # grab an even number of digits
944 701 bin_id = bin(id[:l * 2])
945 702 nl = [n for n in self.nodemap if n[:l] == bin_id]
946 703 nl = [n for n in nl if hex(n).startswith(id)]
947 704 if len(nl) > 0:
948 705 if len(nl) == 1:
949 706 return nl[0]
950 707 raise LookupError(id, self.indexfile,
951 708 _('ambiguous identifier'))
952 709 return None
953 710 except TypeError:
954 711 pass
955 712
956 713 def lookup(self, id):
957 714 """locate a node based on:
958 715 - revision number or str(revision number)
959 716 - nodeid or subset of hex nodeid
960 717 """
961 718 n = self._match(id)
962 719 if n is not None:
963 720 return n
964 721 n = self._partialmatch(id)
965 722 if n:
966 723 return n
967 724
968 725 raise LookupError(id, self.indexfile, _('no match found'))
969 726
970 727 def cmp(self, node, text):
971 728 """compare text with a given file revision
972 729
973 730 returns True if text is different than what is stored.
974 731 """
975 732 p1, p2 = self.parents(node)
976 733 return hash(text, p1, p2) != node
977 734
978 735 def _addchunk(self, offset, data):
979 736 o, d = self._chunkcache
980 737 # try to add to existing cache
981 if o + len(d) == offset and len(d) + len(data) < _prereadsize:
738 if o + len(d) == offset and len(d) + len(data) < _chunksize:
982 739 self._chunkcache = o, d + data
983 740 else:
984 741 self._chunkcache = offset, data
985 742
986 743 def _loadchunk(self, offset, length):
987 744 if self._inline:
988 745 df = self.opener(self.indexfile)
989 746 else:
990 747 df = self.opener(self.datafile)
991 748
992 749 readahead = max(65536, length)
993 750 df.seek(offset)
994 751 d = df.read(readahead)
995 752 self._addchunk(offset, d)
996 753 if readahead > length:
997 754 return d[:length]
998 755 return d
999 756
1000 757 def _getchunk(self, offset, length):
1001 758 o, d = self._chunkcache
1002 759 l = len(d)
1003 760
1004 761 # is it in the cache?
1005 762 cachestart = offset - o
1006 763 cacheend = cachestart + length
1007 764 if cachestart >= 0 and cacheend <= l:
1008 765 if cachestart == 0 and cacheend == l:
1009 766 return d # avoid a copy
1010 767 return d[cachestart:cacheend]
1011 768
1012 769 return self._loadchunk(offset, length)
1013 770
1014 771 def _chunkraw(self, startrev, endrev):
1015 772 start = self.start(startrev)
1016 773 length = self.end(endrev) - start
1017 774 if self._inline:
1018 775 start += (startrev + 1) * self._io.size
1019 776 return self._getchunk(start, length)
1020 777
1021 778 def _chunk(self, rev):
1022 779 return decompress(self._chunkraw(rev, rev))
1023 780
1024 781 def _chunkclear(self):
1025 782 self._chunkcache = (0, '')
1026 783
1027 784 def deltaparent(self, rev):
1028 785 """return previous revision or parentrev according to flags"""
1029 786 if self.flags(rev) & REVIDX_PARENTDELTA:
1030 787 return self.parentrevs(rev)[0]
1031 788 else:
1032 789 return rev - 1
1033 790
1034 791 def revdiff(self, rev1, rev2):
1035 792 """return or calculate a delta between two revisions"""
1036 793 if self.base(rev2) != rev2 and self.deltaparent(rev2) == rev1:
1037 794 return self._chunk(rev2)
1038 795
1039 796 return mdiff.textdiff(self.revision(self.node(rev1)),
1040 797 self.revision(self.node(rev2)))
1041 798
1042 799 def revision(self, node):
1043 800 """return an uncompressed revision of a given node"""
1044 801 cachedrev = None
1045 802 if node == nullid:
1046 803 return ""
1047 804 if self._cache:
1048 805 if self._cache[0] == node:
1049 806 return self._cache[2]
1050 807 cachedrev = self._cache[1]
1051 808
1052 809 # look up what we need to read
1053 810 text = None
1054 811 rev = self.rev(node)
1055 812 base = self.base(rev)
1056 813
1057 814 # check rev flags
1058 815 if self.flags(rev) & ~REVIDX_KNOWN_FLAGS:
1059 816 raise RevlogError(_('incompatible revision flag %x') %
1060 817 (self.flags(rev) & ~REVIDX_KNOWN_FLAGS))
1061 818
1062 819 # build delta chain
1063 self._loadindex(base, rev + 1)
1064 820 chain = []
1065 821 index = self.index # for performance
1066 822 iterrev = rev
1067 823 e = index[iterrev]
1068 824 while iterrev != base and iterrev != cachedrev:
1069 825 chain.append(iterrev)
1070 826 if e[0] & REVIDX_PARENTDELTA:
1071 827 iterrev = e[5]
1072 828 else:
1073 829 iterrev -= 1
1074 830 e = index[iterrev]
1075 831 chain.reverse()
1076 832 base = iterrev
1077 833
1078 834 if iterrev == cachedrev:
1079 835 # cache hit
1080 836 text = self._cache[2]
1081 837
1082 838 # drop cache to save memory
1083 839 self._cache = None
1084 840
1085 841 self._chunkraw(base, rev)
1086 842 if text is None:
1087 843 text = self._chunk(base)
1088 844
1089 845 bins = [self._chunk(r) for r in chain]
1090 846 text = mdiff.patches(text, bins)
1091 847
1092 848 text = self._checkhash(text, node)
1093 849
1094 850 self._cache = (node, rev, text)
1095 851 return text
1096 852
1097 853 def _checkhash(self, text, node):
1098 854 p1, p2 = self.parents(node)
1099 855 if (node != hash(text, p1, p2) and
1100 856 not (self.flags(rev) & REVIDX_PUNCHED_FLAG)):
1101 857 raise RevlogError(_("integrity check failed on %s:%d")
1102 858 % (self.indexfile, rev))
1103 859 return text
1104 860
1105 861 def checkinlinesize(self, tr, fp=None):
1106 862 if not self._inline or (self.start(-2) + self.length(-2)) < _maxinline:
1107 863 return
1108 864
1109 865 trinfo = tr.find(self.indexfile)
1110 866 if trinfo is None:
1111 867 raise RevlogError(_("%s not found in the transaction")
1112 868 % self.indexfile)
1113 869
1114 870 trindex = trinfo[2]
1115 871 dataoff = self.start(trindex)
1116 872
1117 873 tr.add(self.datafile, dataoff)
1118 874
1119 875 if fp:
1120 876 fp.flush()
1121 877 fp.close()
1122 878
1123 879 df = self.opener(self.datafile, 'w')
1124 880 try:
1125 881 for r in self:
1126 882 df.write(self._chunkraw(r, r))
1127 883 finally:
1128 884 df.close()
1129 885
1130 886 fp = self.opener(self.indexfile, 'w', atomictemp=True)
1131 887 self.version &= ~(REVLOGNGINLINEDATA)
1132 888 self._inline = False
1133 889 for i in self:
1134 890 e = self._io.packentry(self.index[i], self.node, self.version, i)
1135 891 fp.write(e)
1136 892
1137 893 # if we don't call rename, the temp file will never replace the
1138 894 # real index
1139 895 fp.rename()
1140 896
1141 897 tr.replace(self.indexfile, trindex * self._io.size)
1142 898 self._chunkclear()
1143 899
1144 900 def addrevision(self, text, transaction, link, p1, p2, cachedelta=None):
1145 901 """add a revision to the log
1146 902
1147 903 text - the revision data to add
1148 904 transaction - the transaction object used for rollback
1149 905 link - the linkrev data to add
1150 906 p1, p2 - the parent nodeids of the revision
1151 907 cachedelta - an optional precomputed delta
1152 908 """
1153 909 node = hash(text, p1, p2)
1154 910 if (node in self.nodemap and
1155 911 (not self.flags(self.rev(node)) & REVIDX_PUNCHED_FLAG)):
1156 912 return node
1157 913
1158 914 dfh = None
1159 915 if not self._inline:
1160 916 dfh = self.opener(self.datafile, "a")
1161 917 ifh = self.opener(self.indexfile, "a+")
1162 918 try:
1163 919 return self._addrevision(node, text, transaction, link, p1, p2,
1164 920 cachedelta, ifh, dfh)
1165 921 finally:
1166 922 if dfh:
1167 923 dfh.close()
1168 924 ifh.close()
1169 925
1170 926 def _addrevision(self, node, text, transaction, link, p1, p2,
1171 927 cachedelta, ifh, dfh):
1172 928
1173 929 btext = [text]
1174 930 def buildtext():
1175 931 if btext[0] is not None:
1176 932 return btext[0]
1177 933 # flush any pending writes here so we can read it in revision
1178 934 if dfh:
1179 935 dfh.flush()
1180 936 ifh.flush()
1181 937 basetext = self.revision(self.node(cachedelta[0]))
1182 938 btext[0] = mdiff.patch(basetext, cachedelta[1])
1183 939 chk = hash(btext[0], p1, p2)
1184 940 if chk != node:
1185 941 raise RevlogError(_("consistency error in delta"))
1186 942 return btext[0]
1187 943
1188 944 def builddelta(rev):
1189 945 # can we use the cached delta?
1190 946 if cachedelta and cachedelta[0] == rev:
1191 947 delta = cachedelta[1]
1192 948 else:
1193 949 t = buildtext()
1194 950 ptext = self.revision(self.node(rev))
1195 951 delta = mdiff.textdiff(ptext, t)
1196 952 data = compress(delta)
1197 953 l = len(data[1]) + len(data[0])
1198 954 base = self.base(rev)
1199 955 dist = l + offset - self.start(base)
1200 956 return dist, l, data, base
1201 957
1202 958 curr = len(self)
1203 959 prev = curr - 1
1204 960 base = curr
1205 961 offset = self.end(prev)
1206 962 flags = 0
1207 963 d = None
1208 964 p1r, p2r = self.rev(p1), self.rev(p2)
1209 965
1210 966 # should we try to build a delta?
1211 967 if prev != nullrev:
1212 968 d = builddelta(prev)
1213 969 if self._parentdelta and prev != p1r:
1214 970 d2 = builddelta(p1r)
1215 971 if d2 < d:
1216 972 d = d2
1217 973 flags = REVIDX_PARENTDELTA
1218 974 dist, l, data, base = d
1219 975
1220 976 # full versions are inserted when the needed deltas
1221 977 # become comparable to the uncompressed text
1222 978 # or the base revision is punched
1223 979 if text is None:
1224 980 textlen = mdiff.patchedsize(self.rawsize(cachedelta[0]),
1225 981 cachedelta[1])
1226 982 else:
1227 983 textlen = len(text)
1228 984 if (d is None or dist > textlen * 2 or
1229 985 (self.flags(base) & REVIDX_PUNCHED_FLAG)):
1230 986 text = buildtext()
1231 987 data = compress(text)
1232 988 l = len(data[1]) + len(data[0])
1233 989 base = curr
1234 990
1235 991 e = (offset_type(offset, flags), l, textlen,
1236 992 base, link, p1r, p2r, node)
1237 993 self.index.insert(-1, e)
1238 994 self.nodemap[node] = curr
1239 995
1240 996 entry = self._io.packentry(e, self.node, self.version, curr)
1241 997 if not self._inline:
1242 998 transaction.add(self.datafile, offset)
1243 999 transaction.add(self.indexfile, curr * len(entry))
1244 1000 if data[0]:
1245 1001 dfh.write(data[0])
1246 1002 dfh.write(data[1])
1247 1003 dfh.flush()
1248 1004 ifh.write(entry)
1249 1005 else:
1250 1006 offset += curr * self._io.size
1251 1007 transaction.add(self.indexfile, offset, curr)
1252 1008 ifh.write(entry)
1253 1009 ifh.write(data[0])
1254 1010 ifh.write(data[1])
1255 1011 self.checkinlinesize(transaction, ifh)
1256 1012
1257 1013 if type(text) == str: # only accept immutable objects
1258 1014 self._cache = (node, curr, text)
1259 1015 return node
1260 1016
1261 1017 def group(self, nodelist, lookup, infocollect=None, fullrev=False):
1262 1018 """Calculate a delta group, yielding a sequence of changegroup chunks
1263 1019 (strings).
1264 1020
1265 1021 Given a list of changeset revs, return a set of deltas and
1266 1022 metadata corresponding to nodes. The first delta is
1267 1023 first parent(nodelist[0]) -> nodelist[0], the receiver is
1268 1024 guaranteed to have this parent as it has all history before
1269 1025 these changesets. In the case firstparent is nullrev the
1270 1026 changegroup starts with a full revision.
1271 1027 fullrev forces the insertion of the full revision, necessary
1272 1028 in the case of shallow clones where the first parent might
1273 1029 not exist at the reciever.
1274 1030 """
1275 1031
1276 1032 revs = [self.rev(n) for n in nodelist]
1277 1033
1278 1034 # if we don't have any revisions touched by these changesets, bail
1279 1035 if not revs:
1280 1036 yield changegroup.closechunk()
1281 1037 return
1282 1038
1283 1039 # add the parent of the first rev
1284 1040 p = self.parentrevs(revs[0])[0]
1285 1041 revs.insert(0, p)
1286 1042 if p == nullrev:
1287 1043 fullrev = True
1288 1044
1289 1045 # build deltas
1290 1046 for d in xrange(len(revs) - 1):
1291 1047 a, b = revs[d], revs[d + 1]
1292 1048 nb = self.node(b)
1293 1049
1294 1050 if infocollect is not None:
1295 1051 infocollect(nb)
1296 1052
1297 1053 p = self.parents(nb)
1298 1054 meta = nb + p[0] + p[1] + lookup(nb)
1299 1055 if fullrev:
1300 1056 d = self.revision(nb)
1301 1057 meta += mdiff.trivialdiffheader(len(d))
1302 1058 fullrev = False
1303 1059 else:
1304 1060 d = self.revdiff(a, b)
1305 1061 yield changegroup.chunkheader(len(meta) + len(d))
1306 1062 yield meta
1307 1063 yield d
1308 1064
1309 1065 yield changegroup.closechunk()
1310 1066
1311 1067 def addgroup(self, bundle, linkmapper, transaction):
1312 1068 """
1313 1069 add a delta group
1314 1070
1315 1071 given a set of deltas, add them to the revision log. the
1316 1072 first delta is against its parent, which should be in our
1317 1073 log, the rest are against the previous delta.
1318 1074 """
1319 1075
1320 1076 # track the base of the current delta log
1321 1077 node = None
1322 1078
1323 1079 r = len(self)
1324 1080 end = 0
1325 1081 if r:
1326 1082 end = self.end(r - 1)
1327 1083 ifh = self.opener(self.indexfile, "a+")
1328 1084 isize = r * self._io.size
1329 1085 if self._inline:
1330 1086 transaction.add(self.indexfile, end + isize, r)
1331 1087 dfh = None
1332 1088 else:
1333 1089 transaction.add(self.indexfile, isize, r)
1334 1090 transaction.add(self.datafile, end)
1335 1091 dfh = self.opener(self.datafile, "a")
1336 1092
1337 1093 try:
1338 1094 # loop through our set of deltas
1339 1095 chain = None
1340 1096 while 1:
1341 1097 chunkdata = bundle.parsechunk()
1342 1098 if not chunkdata:
1343 1099 break
1344 1100 node = chunkdata['node']
1345 1101 p1 = chunkdata['p1']
1346 1102 p2 = chunkdata['p2']
1347 1103 cs = chunkdata['cs']
1348 1104 delta = chunkdata['data']
1349 1105
1350 1106 link = linkmapper(cs)
1351 1107 if (node in self.nodemap and
1352 1108 (not self.flags(self.rev(node)) & REVIDX_PUNCHED_FLAG)):
1353 1109 # this can happen if two branches make the same change
1354 1110 chain = node
1355 1111 continue
1356 1112
1357 1113 for p in (p1, p2):
1358 1114 if not p in self.nodemap:
1359 1115 if self._shallow:
1360 1116 # add null entries for missing parents
1361 1117 # XXX FIXME
1362 1118 #if base == nullrev:
1363 1119 # base = len(self)
1364 1120 #e = (offset_type(end, REVIDX_PUNCHED_FLAG),
1365 1121 # 0, 0, base, nullrev, nullrev, nullrev, p)
1366 1122 #self.index.insert(-1, e)
1367 1123 #self.nodemap[p] = r
1368 1124 #entry = self._io.packentry(e, self.node,
1369 1125 # self.version, r)
1370 1126 #ifh.write(entry)
1371 1127 #t, r = r, r + 1
1372 1128 raise LookupError(p, self.indexfile,
1373 1129 _('unknown parent'))
1374 1130 else:
1375 1131 raise LookupError(p, self.indexfile,
1376 1132 _('unknown parent'))
1377 1133
1378 1134 if not chain:
1379 1135 # retrieve the parent revision of the delta chain
1380 1136 chain = p1
1381 1137 if not chain in self.nodemap:
1382 1138 raise LookupError(chain, self.indexfile, _('unknown base'))
1383 1139
1384 1140 chainrev = self.rev(chain)
1385 1141 chain = self._addrevision(node, None, transaction, link,
1386 1142 p1, p2, (chainrev, delta), ifh, dfh)
1387 1143 if not dfh and not self._inline:
1388 1144 # addrevision switched from inline to conventional
1389 1145 # reopen the index
1390 1146 dfh = self.opener(self.datafile, "a")
1391 1147 ifh = self.opener(self.indexfile, "a")
1392 1148 finally:
1393 1149 if dfh:
1394 1150 dfh.close()
1395 1151 ifh.close()
1396 1152
1397 1153 return node
1398 1154
1399 1155 def strip(self, minlink, transaction):
1400 1156 """truncate the revlog on the first revision with a linkrev >= minlink
1401 1157
1402 1158 This function is called when we're stripping revision minlink and
1403 1159 its descendants from the repository.
1404 1160
1405 1161 We have to remove all revisions with linkrev >= minlink, because
1406 1162 the equivalent changelog revisions will be renumbered after the
1407 1163 strip.
1408 1164
1409 1165 So we truncate the revlog on the first of these revisions, and
1410 1166 trust that the caller has saved the revisions that shouldn't be
1411 1167 removed and that it'll readd them after this truncation.
1412 1168 """
1413 1169 if len(self) == 0:
1414 1170 return
1415 1171
1416 if isinstance(self.index, lazyindex):
1417 self._loadindexmap()
1418
1419 1172 for rev in self:
1420 1173 if self.index[rev][4] >= minlink:
1421 1174 break
1422 1175 else:
1423 1176 return
1424 1177
1425 1178 # first truncate the files on disk
1426 1179 end = self.start(rev)
1427 1180 if not self._inline:
1428 1181 transaction.add(self.datafile, end)
1429 1182 end = rev * self._io.size
1430 1183 else:
1431 1184 end += rev * self._io.size
1432 1185
1433 1186 transaction.add(self.indexfile, end)
1434 1187
1435 1188 # then reset internal state in memory to forget those revisions
1436 1189 self._cache = None
1437 1190 self._chunkclear()
1438 1191 for x in xrange(rev, len(self)):
1439 1192 del self.nodemap[self.node(x)]
1440 1193
1441 1194 del self.index[rev:-1]
1442 1195
1443 1196 def checksize(self):
1444 1197 expected = 0
1445 1198 if len(self):
1446 1199 expected = max(0, self.end(len(self) - 1))
1447 1200
1448 1201 try:
1449 1202 f = self.opener(self.datafile)
1450 1203 f.seek(0, 2)
1451 1204 actual = f.tell()
1452 1205 dd = actual - expected
1453 1206 except IOError, inst:
1454 1207 if inst.errno != errno.ENOENT:
1455 1208 raise
1456 1209 dd = 0
1457 1210
1458 1211 try:
1459 1212 f = self.opener(self.indexfile)
1460 1213 f.seek(0, 2)
1461 1214 actual = f.tell()
1462 1215 s = self._io.size
1463 1216 i = max(0, actual // s)
1464 1217 di = actual - (i * s)
1465 1218 if self._inline:
1466 1219 databytes = 0
1467 1220 for r in self:
1468 1221 databytes += max(0, self.length(r))
1469 1222 dd = 0
1470 1223 di = actual - len(self) * s - databytes
1471 1224 except IOError, inst:
1472 1225 if inst.errno != errno.ENOENT:
1473 1226 raise
1474 1227 di = 0
1475 1228
1476 1229 return (dd, di)
1477 1230
1478 1231 def files(self):
1479 1232 res = [self.indexfile]
1480 1233 if not self._inline:
1481 1234 res.append(self.datafile)
1482 1235 return res
@@ -1,146 +1,145 b''
1 1 # statichttprepo.py - simple http repository class for mercurial
2 2 #
3 3 # This provides read-only repo access to repositories exported via static http
4 4 #
5 5 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
6 6 #
7 7 # This software may be used and distributed according to the terms of the
8 8 # GNU General Public License version 2 or any later version.
9 9
10 10 from i18n import _
11 11 import changelog, byterange, url, error
12 12 import localrepo, manifest, util, store
13 13 import urllib, urllib2, errno
14 14
15 15 class httprangereader(object):
16 16 def __init__(self, url, opener):
17 17 # we assume opener has HTTPRangeHandler
18 18 self.url = url
19 19 self.pos = 0
20 20 self.opener = opener
21 21 self.name = url
22 22 def seek(self, pos):
23 23 self.pos = pos
24 24 def read(self, bytes=None):
25 25 req = urllib2.Request(self.url)
26 26 end = ''
27 27 if bytes:
28 28 end = self.pos + bytes - 1
29 29 req.add_header('Range', 'bytes=%d-%s' % (self.pos, end))
30 30
31 31 try:
32 32 f = self.opener.open(req)
33 33 data = f.read()
34 34 if hasattr(f, 'getcode'):
35 35 # python 2.6+
36 36 code = f.getcode()
37 37 elif hasattr(f, 'code'):
38 38 # undocumented attribute, seems to be set in 2.4 and 2.5
39 39 code = f.code
40 40 else:
41 41 # Don't know how to check, hope for the best.
42 42 code = 206
43 43 except urllib2.HTTPError, inst:
44 44 num = inst.code == 404 and errno.ENOENT or None
45 45 raise IOError(num, inst)
46 46 except urllib2.URLError, inst:
47 47 raise IOError(None, inst.reason[1])
48 48
49 49 if code == 200:
50 50 # HTTPRangeHandler does nothing if remote does not support
51 51 # Range headers and returns the full entity. Let's slice it.
52 52 if bytes:
53 53 data = data[self.pos:self.pos + bytes]
54 54 else:
55 55 data = data[self.pos:]
56 56 elif bytes:
57 57 data = data[:bytes]
58 58 self.pos += len(data)
59 59 return data
60 60 def __iter__(self):
61 61 return iter(self.read().splitlines(1))
62 62 def close(self):
63 63 pass
64 64
65 65 def build_opener(ui, authinfo):
66 66 # urllib cannot handle URLs with embedded user or passwd
67 67 urlopener = url.opener(ui, authinfo)
68 68 urlopener.add_handler(byterange.HTTPRangeHandler())
69 69
70 70 def opener(base):
71 71 """return a function that opens files over http"""
72 72 p = base
73 73 def o(path, mode="r", atomictemp=None):
74 74 if 'a' in mode or 'w' in mode:
75 75 raise IOError('Permission denied')
76 76 f = "/".join((p, urllib.quote(path)))
77 77 return httprangereader(f, urlopener)
78 78 return o
79 79
80 opener.options = {'nonlazy': 1}
81 80 return opener
82 81
83 82 class statichttprepository(localrepo.localrepository):
84 83 def __init__(self, ui, path):
85 84 self._url = path
86 85 self.ui = ui
87 86
88 87 self.root = path
89 88 self.path, authinfo = url.getauthinfo(path.rstrip('/') + "/.hg")
90 89
91 90 opener = build_opener(ui, authinfo)
92 91 self.opener = opener(self.path)
93 92
94 93 # find requirements
95 94 try:
96 95 requirements = self.opener("requires").read().splitlines()
97 96 except IOError, inst:
98 97 if inst.errno != errno.ENOENT:
99 98 raise
100 99 # check if it is a non-empty old-style repository
101 100 try:
102 101 self.opener("00changelog.i").read(1)
103 102 except IOError, inst:
104 103 if inst.errno != errno.ENOENT:
105 104 raise
106 105 # we do not care about empty old-style repositories here
107 106 msg = _("'%s' does not appear to be an hg repository") % path
108 107 raise error.RepoError(msg)
109 108 requirements = []
110 109
111 110 # check them
112 111 for r in requirements:
113 112 if r not in self.supported:
114 113 raise error.RepoError(_("requirement '%s' not supported") % r)
115 114
116 115 # setup store
117 116 def pjoin(a, b):
118 117 return a + '/' + b
119 118 self.store = store.store(requirements, self.path, opener, pjoin)
120 119 self.spath = self.store.path
121 120 self.sopener = self.store.opener
122 121 self.sjoin = self.store.join
123 122
124 123 self.manifest = manifest.manifest(self.sopener)
125 124 self.changelog = changelog.changelog(self.sopener)
126 125 self._tags = None
127 126 self.nodetagscache = None
128 127 self._branchcache = None
129 128 self._branchcachetip = None
130 129 self.encodepats = None
131 130 self.decodepats = None
132 131 self.capabilities.remove("pushkey")
133 132
134 133 def url(self):
135 134 return self._url
136 135
137 136 def local(self):
138 137 return False
139 138
140 139 def lock(self, wait=True):
141 140 raise util.Abort(_('cannot lock static-http repository'))
142 141
143 142 def instance(ui, path, create):
144 143 if create:
145 144 raise util.Abort(_('cannot create new static-http repository'))
146 145 return statichttprepository(ui, path[7:])
@@ -1,113 +1,113 b''
1 1 from mercurial import parsers
2 2 from mercurial.node import nullid, nullrev
3 3 import struct
4 4
5 5 # This unit test compares the return value of the original Python
6 6 # implementation of parseindex and the new C implementation for
7 7 # an index file with and without inlined data
8 8
9 9 # original python implementation
10 10 def gettype(q):
11 11 return int(q & 0xFFFF)
12 12
13 13 def offset_type(offset, type):
14 14 return long(long(offset) << 16 | type)
15 15
16 16 indexformatng = ">Qiiiiii20s12x"
17 17
18 18 def py_parseindex(data, inline) :
19 19 s = 64
20 20 cache = None
21 21 index = []
22 22 nodemap = {nullid: nullrev}
23 23 n = off = 0
24 # if we're not using lazymap, always read the whole index
24
25 25 l = len(data) - s
26 26 append = index.append
27 27 if inline:
28 28 cache = (0, data)
29 29 while off <= l:
30 30 e = struct.unpack(indexformatng, data[off:off + s])
31 31 nodemap[e[7]] = n
32 32 append(e)
33 33 n += 1
34 34 if e[1] < 0:
35 35 break
36 36 off += e[1] + s
37 37 else:
38 38 while off <= l:
39 39 e = struct.unpack(indexformatng, data[off:off + s])
40 40 nodemap[e[7]] = n
41 41 append(e)
42 42 n += 1
43 43 off += s
44 44
45 45 e = list(index[0])
46 46 type = gettype(e[0])
47 47 e[0] = offset_type(0, type)
48 48 index[0] = tuple(e)
49 49
50 50 # add the magic null revision at -1
51 51 index.append((0, 0, 0, -1, -1, -1, -1, nullid))
52 52
53 53 return index, nodemap, cache
54 54
55 55
56 56 data_inlined = '\x00\x01\x00\x01\x00\x00\x00\x00\x00\x00\x01\x8c' \
57 57 '\x00\x00\x04\x07\x00\x00\x00\x00\x00\x00\x15\x15\xff\xff\xff' \
58 58 '\xff\xff\xff\xff\xff\xebG\x97\xb7\x1fB\x04\xcf\x13V\x81\tw\x1b' \
59 59 'w\xdduR\xda\xc6\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \
60 60 'x\x9c\x9d\x93?O\xc30\x10\xc5\xf7|\x8a\xdb\x9a\xa8m\x06\xd8*\x95' \
61 61 '\x81B\xa1\xa2\xa2R\xcb\x86Pd\x9a\x0b5$vd_\x04\xfd\xf6\x9c\xff@' \
62 62 '\x11!\x0b\xd9\xec\xf7\xbbw\xe7gG6\xad6\x04\xdaN\xc0\x92\xa0$)' \
63 63 '\xb1\x82\xa2\xd1%\x16\xa4\x8b7\xa9\xca\xd4-\xb2Y\x02\xfc\xc9' \
64 64 '\xcaS\xf9\xaeX\xed\xb6\xd77Q\x02\x83\xd4\x19\xf5--Y\xea\xe1W' \
65 65 '\xab\xed\x10\xceR\x0f_\xdf\xdf\r\xe1,\xf5\xf0\xcb\xf5 \xceR\x0f' \
66 66 '_\xdc\x0e\x0e\xc3R\x0f_\xae\x96\x9b!\x9e\xa5\x1e\xbf\xdb,\x06' \
67 67 '\xc7q\x9a/\x88\x82\xc3B\xea\xb5\xb4TJ\x93\xb6\x82\x0e\xe16\xe6' \
68 68 'KQ\xdb\xaf\xecG\xa3\xd1 \x01\xd3\x0b_^\xe8\xaa\xa0\xae\xad\xd1' \
69 69 '&\xbef\x1bz\x08\xb0|\xc9Xz\x06\xf6Z\x91\x90J\xaa\x17\x90\xaa' \
70 70 '\xd2\xa6\x11$5C\xcf\xba#\xa0\x03\x02*2\x92-\xfc\xb1\x94\xdf\xe2' \
71 71 '\xae\xb8\'m\x8ey0^\x85\xd3\x82\xb4\xf0`:\x9c\x00\x8a\xfd\x01' \
72 72 '\xb0\xc6\x86\x8b\xdd\xae\x80\xf3\xa9\x9fd\x16\n\x00R%\x1a\x06' \
73 73 '\xe9\xd8b\x98\x1d\xf4\xf3+\x9bf\x01\xd8p\x1b\xf3.\xed\x9f^g\xc3' \
74 74 '^\xd9W81T\xdb\xd5\x04sx|\xf2\xeb\xd6`%?x\xed"\x831\xbf\xf3\xdc' \
75 75 'b\xeb%gaY\xe1\xad\x9f\xb9f\'1w\xa9\xa5a\x83s\x82J\xb98\xbc4\x8b' \
76 76 '\x83\x00\x9f$z\xb8#\xa5\xb1\xdf\x98\xd9\xec\x1b\x89O\xe3Ts\x9a4' \
77 77 '\x17m\x8b\xfc\x8f\xa5\x95\x9a\xfc\xfa\xed,\xe5|\xa1\xfe\x15\xb9' \
78 78 '\xbc\xb2\x93\x1f\xf2\x95\xff\xdf,\x1a\xc5\xe7\x17*\x93Oz:>\x0e'
79 79
80 80 data_non_inlined = '\x00\x00\x00\x01\x00\x00\x00\x00\x00\x01D\x19' \
81 81 '\x00\x07e\x12\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff' \
82 82 '\xff\xff\xff\xff\xd1\xf4\xbb\xb0\xbe\xfc\x13\xbd\x8c\xd3\x9d' \
83 83 '\x0f\xcd\xd9;\x8c\x07\x8cJ/\x00\x00\x00\x00\x00\x00\x00\x00\x00' \
84 84 '\x00\x00\x00\x00\x00\x00\x01D\x19\x00\x00\x00\x00\x00\xdf\x00' \
85 85 '\x00\x01q\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\xff' \
86 86 '\xff\xff\xff\xc1\x12\xb9\x04\x96\xa4Z1t\x91\xdfsJ\x90\xf0\x9bh' \
87 87 '\x07l&\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \
88 88 '\x00\x01D\xf8\x00\x00\x00\x00\x01\x1b\x00\x00\x01\xb8\x00\x00' \
89 89 '\x00\x01\x00\x00\x00\x02\x00\x00\x00\x01\xff\xff\xff\xff\x02\n' \
90 90 '\x0e\xc6&\xa1\x92\xae6\x0b\x02i\xfe-\xe5\xbao\x05\xd1\xe7\x00' \
91 91 '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01F' \
92 92 '\x13\x00\x00\x00\x00\x01\xec\x00\x00\x03\x06\x00\x00\x00\x01' \
93 93 '\x00\x00\x00\x03\x00\x00\x00\x02\xff\xff\xff\xff\x12\xcb\xeby1' \
94 94 '\xb6\r\x98B\xcb\x07\xbd`\x8f\x92\xd9\xc4\x84\xbdK\x00\x00\x00' \
95 95 '\x00\x00\x00\x00\x00\x00\x00\x00\x00'
96 96
97 97 def runtest() :
98 98
99 99 py_res_1 = py_parseindex(data_inlined, True)
100 100 c_res_1 = parsers.parse_index(data_inlined, True)
101 101
102 102 py_res_2 = py_parseindex(data_non_inlined, False)
103 103 c_res_2 = parsers.parse_index(data_non_inlined, False)
104 104
105 105 if py_res_1 != c_res_1:
106 106 print "Parse index result (with inlined data) differs!"
107 107
108 108 if py_res_2 != c_res_2:
109 109 print "Parse index result (no inlined data) differs!"
110 110
111 111 print "done"
112 112
113 113 runtest()
General Comments 0
You need to be logged in to leave comments. Login now