##// END OF EJS Templates
hg tag: run tag hook just once
John Coomes -
r5801:1b230f50 default
parent child Browse files
Show More
@@ -1,1992 +1,1992
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
6 6 # of the GNU General Public License, incorporated herein by reference.
7 7
8 8 from node import *
9 9 from i18n import _
10 10 import repo, changegroup
11 11 import changelog, dirstate, filelog, manifest, context, weakref
12 12 import re, lock, transaction, tempfile, stat, errno, ui
13 13 import os, revlog, time, util, extensions, hook
14 14
15 15 class localrepository(repo.repository):
16 16 capabilities = util.set(('lookup', 'changegroupsubset'))
17 17 supported = ('revlogv1', 'store')
18 18
19 19 def __init__(self, parentui, path=None, create=0):
20 20 repo.repository.__init__(self)
21 21 self.root = os.path.realpath(path)
22 22 self.path = os.path.join(self.root, ".hg")
23 23 self.origroot = path
24 24 self.opener = util.opener(self.path)
25 25 self.wopener = util.opener(self.root)
26 26
27 27 if not os.path.isdir(self.path):
28 28 if create:
29 29 if not os.path.exists(path):
30 30 os.mkdir(path)
31 31 os.mkdir(self.path)
32 32 requirements = ["revlogv1"]
33 33 if parentui.configbool('format', 'usestore', True):
34 34 os.mkdir(os.path.join(self.path, "store"))
35 35 requirements.append("store")
36 36 # create an invalid changelog
37 37 self.opener("00changelog.i", "a").write(
38 38 '\0\0\0\2' # represents revlogv2
39 39 ' dummy changelog to prevent using the old repo layout'
40 40 )
41 41 reqfile = self.opener("requires", "w")
42 42 for r in requirements:
43 43 reqfile.write("%s\n" % r)
44 44 reqfile.close()
45 45 else:
46 46 raise repo.RepoError(_("repository %s not found") % path)
47 47 elif create:
48 48 raise repo.RepoError(_("repository %s already exists") % path)
49 49 else:
50 50 # find requirements
51 51 try:
52 52 requirements = self.opener("requires").read().splitlines()
53 53 except IOError, inst:
54 54 if inst.errno != errno.ENOENT:
55 55 raise
56 56 requirements = []
57 57 # check them
58 58 for r in requirements:
59 59 if r not in self.supported:
60 60 raise repo.RepoError(_("requirement '%s' not supported") % r)
61 61
62 62 # setup store
63 63 if "store" in requirements:
64 64 self.encodefn = util.encodefilename
65 65 self.decodefn = util.decodefilename
66 66 self.spath = os.path.join(self.path, "store")
67 67 else:
68 68 self.encodefn = lambda x: x
69 69 self.decodefn = lambda x: x
70 70 self.spath = self.path
71 71 self.sopener = util.encodedopener(util.opener(self.spath),
72 72 self.encodefn)
73 73
74 74 self.ui = ui.ui(parentui=parentui)
75 75 try:
76 76 self.ui.readconfig(self.join("hgrc"), self.root)
77 77 extensions.loadall(self.ui)
78 78 except IOError:
79 79 pass
80 80
81 81 self.tagscache = None
82 82 self.branchcache = None
83 83 self.nodetagscache = None
84 84 self.filterpats = {}
85 85 self._transref = self._lockref = self._wlockref = None
86 86
87 87 def __getattr__(self, name):
88 88 if name == 'changelog':
89 89 self.changelog = changelog.changelog(self.sopener)
90 90 self.sopener.defversion = self.changelog.version
91 91 return self.changelog
92 92 if name == 'manifest':
93 93 self.changelog
94 94 self.manifest = manifest.manifest(self.sopener)
95 95 return self.manifest
96 96 if name == 'dirstate':
97 97 self.dirstate = dirstate.dirstate(self.opener, self.ui, self.root)
98 98 return self.dirstate
99 99 else:
100 100 raise AttributeError, name
101 101
102 102 def url(self):
103 103 return 'file:' + self.root
104 104
105 105 def hook(self, name, throw=False, **args):
106 106 return hook.hook(self.ui, self, name, throw, **args)
107 107
108 108 tag_disallowed = ':\r\n'
109 109
110 110 def _tag(self, name, node, message, local, user, date, parent=None,
111 111 extra={}):
112 112 use_dirstate = parent is None
113 113
114 114 for c in self.tag_disallowed:
115 115 if c in name:
116 116 raise util.Abort(_('%r cannot be used in a tag name') % c)
117 117
118 118 self.hook('pretag', throw=True, node=hex(node), tag=name, local=local)
119 119
120 120 def writetag(fp, name, munge, prevtags):
121 121 if prevtags and prevtags[-1] != '\n':
122 122 fp.write('\n')
123 123 fp.write('%s %s\n' % (hex(node), munge and munge(name) or name))
124 124 fp.close()
125 self.hook('tag', node=hex(node), tag=name, local=local)
126 125
127 126 prevtags = ''
128 127 if local:
129 128 try:
130 129 fp = self.opener('localtags', 'r+')
131 130 except IOError, err:
132 131 fp = self.opener('localtags', 'a')
133 132 else:
134 133 prevtags = fp.read()
135 134
136 135 # local tags are stored in the current charset
137 136 writetag(fp, name, None, prevtags)
137 self.hook('tag', node=hex(node), tag=name, local=local)
138 138 return
139 139
140 140 if use_dirstate:
141 141 try:
142 142 fp = self.wfile('.hgtags', 'rb+')
143 143 except IOError, err:
144 144 fp = self.wfile('.hgtags', 'ab')
145 145 else:
146 146 prevtags = fp.read()
147 147 else:
148 148 try:
149 149 prevtags = self.filectx('.hgtags', parent).data()
150 150 except revlog.LookupError:
151 151 pass
152 152 fp = self.wfile('.hgtags', 'wb')
153 153 if prevtags:
154 154 fp.write(prevtags)
155 155
156 156 # committed tags are stored in UTF-8
157 157 writetag(fp, name, util.fromlocal, prevtags)
158 158
159 159 if use_dirstate and '.hgtags' not in self.dirstate:
160 160 self.add(['.hgtags'])
161 161
162 162 tagnode = self.commit(['.hgtags'], message, user, date, p1=parent,
163 163 extra=extra)
164 164
165 165 self.hook('tag', node=hex(node), tag=name, local=local)
166 166
167 167 return tagnode
168 168
169 169 def tag(self, name, node, message, local, user, date):
170 170 '''tag a revision with a symbolic name.
171 171
172 172 if local is True, the tag is stored in a per-repository file.
173 173 otherwise, it is stored in the .hgtags file, and a new
174 174 changeset is committed with the change.
175 175
176 176 keyword arguments:
177 177
178 178 local: whether to store tag in non-version-controlled file
179 179 (default False)
180 180
181 181 message: commit message to use if committing
182 182
183 183 user: name of user to use if committing
184 184
185 185 date: date tuple to use if committing'''
186 186
187 187 for x in self.status()[:5]:
188 188 if '.hgtags' in x:
189 189 raise util.Abort(_('working copy of .hgtags is changed '
190 190 '(please commit .hgtags manually)'))
191 191
192 192
193 193 self._tag(name, node, message, local, user, date)
194 194
195 195 def tags(self):
196 196 '''return a mapping of tag to node'''
197 197 if self.tagscache:
198 198 return self.tagscache
199 199
200 200 globaltags = {}
201 201
202 202 def readtags(lines, fn):
203 203 filetags = {}
204 204 count = 0
205 205
206 206 def warn(msg):
207 207 self.ui.warn(_("%s, line %s: %s\n") % (fn, count, msg))
208 208
209 209 for l in lines:
210 210 count += 1
211 211 if not l:
212 212 continue
213 213 s = l.split(" ", 1)
214 214 if len(s) != 2:
215 215 warn(_("cannot parse entry"))
216 216 continue
217 217 node, key = s
218 218 key = util.tolocal(key.strip()) # stored in UTF-8
219 219 try:
220 220 bin_n = bin(node)
221 221 except TypeError:
222 222 warn(_("node '%s' is not well formed") % node)
223 223 continue
224 224 if bin_n not in self.changelog.nodemap:
225 225 warn(_("tag '%s' refers to unknown node") % key)
226 226 continue
227 227
228 228 h = []
229 229 if key in filetags:
230 230 n, h = filetags[key]
231 231 h.append(n)
232 232 filetags[key] = (bin_n, h)
233 233
234 234 for k, nh in filetags.items():
235 235 if k not in globaltags:
236 236 globaltags[k] = nh
237 237 continue
238 238 # we prefer the global tag if:
239 239 # it supercedes us OR
240 240 # mutual supercedes and it has a higher rank
241 241 # otherwise we win because we're tip-most
242 242 an, ah = nh
243 243 bn, bh = globaltags[k]
244 244 if (bn != an and an in bh and
245 245 (bn not in ah or len(bh) > len(ah))):
246 246 an = bn
247 247 ah.extend([n for n in bh if n not in ah])
248 248 globaltags[k] = an, ah
249 249
250 250 # read the tags file from each head, ending with the tip
251 251 f = None
252 252 for rev, node, fnode in self._hgtagsnodes():
253 253 f = (f and f.filectx(fnode) or
254 254 self.filectx('.hgtags', fileid=fnode))
255 255 readtags(f.data().splitlines(), f)
256 256
257 257 try:
258 258 data = util.fromlocal(self.opener("localtags").read())
259 259 # localtags are stored in the local character set
260 260 # while the internal tag table is stored in UTF-8
261 261 readtags(data.splitlines(), "localtags")
262 262 except IOError:
263 263 pass
264 264
265 265 self.tagscache = {}
266 266 for k,nh in globaltags.items():
267 267 n = nh[0]
268 268 if n != nullid:
269 269 self.tagscache[k] = n
270 270 self.tagscache['tip'] = self.changelog.tip()
271 271
272 272 return self.tagscache
273 273
274 274 def _hgtagsnodes(self):
275 275 heads = self.heads()
276 276 heads.reverse()
277 277 last = {}
278 278 ret = []
279 279 for node in heads:
280 280 c = self.changectx(node)
281 281 rev = c.rev()
282 282 try:
283 283 fnode = c.filenode('.hgtags')
284 284 except revlog.LookupError:
285 285 continue
286 286 ret.append((rev, node, fnode))
287 287 if fnode in last:
288 288 ret[last[fnode]] = None
289 289 last[fnode] = len(ret) - 1
290 290 return [item for item in ret if item]
291 291
292 292 def tagslist(self):
293 293 '''return a list of tags ordered by revision'''
294 294 l = []
295 295 for t, n in self.tags().items():
296 296 try:
297 297 r = self.changelog.rev(n)
298 298 except:
299 299 r = -2 # sort to the beginning of the list if unknown
300 300 l.append((r, t, n))
301 301 l.sort()
302 302 return [(t, n) for r, t, n in l]
303 303
304 304 def nodetags(self, node):
305 305 '''return the tags associated with a node'''
306 306 if not self.nodetagscache:
307 307 self.nodetagscache = {}
308 308 for t, n in self.tags().items():
309 309 self.nodetagscache.setdefault(n, []).append(t)
310 310 return self.nodetagscache.get(node, [])
311 311
312 312 def _branchtags(self):
313 313 partial, last, lrev = self._readbranchcache()
314 314
315 315 tiprev = self.changelog.count() - 1
316 316 if lrev != tiprev:
317 317 self._updatebranchcache(partial, lrev+1, tiprev+1)
318 318 self._writebranchcache(partial, self.changelog.tip(), tiprev)
319 319
320 320 return partial
321 321
322 322 def branchtags(self):
323 323 if self.branchcache is not None:
324 324 return self.branchcache
325 325
326 326 self.branchcache = {} # avoid recursion in changectx
327 327 partial = self._branchtags()
328 328
329 329 # the branch cache is stored on disk as UTF-8, but in the local
330 330 # charset internally
331 331 for k, v in partial.items():
332 332 self.branchcache[util.tolocal(k)] = v
333 333 return self.branchcache
334 334
335 335 def _readbranchcache(self):
336 336 partial = {}
337 337 try:
338 338 f = self.opener("branch.cache")
339 339 lines = f.read().split('\n')
340 340 f.close()
341 341 except (IOError, OSError):
342 342 return {}, nullid, nullrev
343 343
344 344 try:
345 345 last, lrev = lines.pop(0).split(" ", 1)
346 346 last, lrev = bin(last), int(lrev)
347 347 if not (lrev < self.changelog.count() and
348 348 self.changelog.node(lrev) == last): # sanity check
349 349 # invalidate the cache
350 350 raise ValueError('Invalid branch cache: unknown tip')
351 351 for l in lines:
352 352 if not l: continue
353 353 node, label = l.split(" ", 1)
354 354 partial[label.strip()] = bin(node)
355 355 except (KeyboardInterrupt, util.SignalInterrupt):
356 356 raise
357 357 except Exception, inst:
358 358 if self.ui.debugflag:
359 359 self.ui.warn(str(inst), '\n')
360 360 partial, last, lrev = {}, nullid, nullrev
361 361 return partial, last, lrev
362 362
363 363 def _writebranchcache(self, branches, tip, tiprev):
364 364 try:
365 365 f = self.opener("branch.cache", "w", atomictemp=True)
366 366 f.write("%s %s\n" % (hex(tip), tiprev))
367 367 for label, node in branches.iteritems():
368 368 f.write("%s %s\n" % (hex(node), label))
369 369 f.rename()
370 370 except (IOError, OSError):
371 371 pass
372 372
373 373 def _updatebranchcache(self, partial, start, end):
374 374 for r in xrange(start, end):
375 375 c = self.changectx(r)
376 376 b = c.branch()
377 377 partial[b] = c.node()
378 378
379 379 def lookup(self, key):
380 380 if key == '.':
381 381 key, second = self.dirstate.parents()
382 382 if key == nullid:
383 383 raise repo.RepoError(_("no revision checked out"))
384 384 if second != nullid:
385 385 self.ui.warn(_("warning: working directory has two parents, "
386 386 "tag '.' uses the first\n"))
387 387 elif key == 'null':
388 388 return nullid
389 389 n = self.changelog._match(key)
390 390 if n:
391 391 return n
392 392 if key in self.tags():
393 393 return self.tags()[key]
394 394 if key in self.branchtags():
395 395 return self.branchtags()[key]
396 396 n = self.changelog._partialmatch(key)
397 397 if n:
398 398 return n
399 399 try:
400 400 if len(key) == 20:
401 401 key = hex(key)
402 402 except:
403 403 pass
404 404 raise repo.RepoError(_("unknown revision '%s'") % key)
405 405
406 406 def dev(self):
407 407 return os.lstat(self.path).st_dev
408 408
409 409 def local(self):
410 410 return True
411 411
412 412 def join(self, f):
413 413 return os.path.join(self.path, f)
414 414
415 415 def sjoin(self, f):
416 416 f = self.encodefn(f)
417 417 return os.path.join(self.spath, f)
418 418
419 419 def wjoin(self, f):
420 420 return os.path.join(self.root, f)
421 421
422 422 def file(self, f):
423 423 if f[0] == '/':
424 424 f = f[1:]
425 425 return filelog.filelog(self.sopener, f)
426 426
427 427 def changectx(self, changeid=None):
428 428 return context.changectx(self, changeid)
429 429
430 430 def workingctx(self):
431 431 return context.workingctx(self)
432 432
433 433 def parents(self, changeid=None):
434 434 '''
435 435 get list of changectxs for parents of changeid or working directory
436 436 '''
437 437 if changeid is None:
438 438 pl = self.dirstate.parents()
439 439 else:
440 440 n = self.changelog.lookup(changeid)
441 441 pl = self.changelog.parents(n)
442 442 if pl[1] == nullid:
443 443 return [self.changectx(pl[0])]
444 444 return [self.changectx(pl[0]), self.changectx(pl[1])]
445 445
446 446 def filectx(self, path, changeid=None, fileid=None):
447 447 """changeid can be a changeset revision, node, or tag.
448 448 fileid can be a file revision or node."""
449 449 return context.filectx(self, path, changeid, fileid)
450 450
451 451 def getcwd(self):
452 452 return self.dirstate.getcwd()
453 453
454 454 def pathto(self, f, cwd=None):
455 455 return self.dirstate.pathto(f, cwd)
456 456
457 457 def wfile(self, f, mode='r'):
458 458 return self.wopener(f, mode)
459 459
460 460 def _link(self, f):
461 461 return os.path.islink(self.wjoin(f))
462 462
463 463 def _filter(self, filter, filename, data):
464 464 if filter not in self.filterpats:
465 465 l = []
466 466 for pat, cmd in self.ui.configitems(filter):
467 467 mf = util.matcher(self.root, "", [pat], [], [])[1]
468 468 l.append((mf, cmd))
469 469 self.filterpats[filter] = l
470 470
471 471 for mf, cmd in self.filterpats[filter]:
472 472 if mf(filename):
473 473 self.ui.debug(_("filtering %s through %s\n") % (filename, cmd))
474 474 data = util.filter(data, cmd)
475 475 break
476 476
477 477 return data
478 478
479 479 def wread(self, filename):
480 480 if self._link(filename):
481 481 data = os.readlink(self.wjoin(filename))
482 482 else:
483 483 data = self.wopener(filename, 'r').read()
484 484 return self._filter("encode", filename, data)
485 485
486 486 def wwrite(self, filename, data, flags):
487 487 data = self._filter("decode", filename, data)
488 488 if "l" in flags:
489 489 self.wopener.symlink(data, filename)
490 490 else:
491 491 try:
492 492 if self._link(filename):
493 493 os.unlink(self.wjoin(filename))
494 494 except OSError:
495 495 pass
496 496 self.wopener(filename, 'w').write(data)
497 497 util.set_exec(self.wjoin(filename), "x" in flags)
498 498
499 499 def wwritedata(self, filename, data):
500 500 return self._filter("decode", filename, data)
501 501
502 502 def transaction(self):
503 503 if self._transref and self._transref():
504 504 return self._transref().nest()
505 505
506 506 # save dirstate for rollback
507 507 try:
508 508 ds = self.opener("dirstate").read()
509 509 except IOError:
510 510 ds = ""
511 511 self.opener("journal.dirstate", "w").write(ds)
512 512
513 513 renames = [(self.sjoin("journal"), self.sjoin("undo")),
514 514 (self.join("journal.dirstate"), self.join("undo.dirstate"))]
515 515 tr = transaction.transaction(self.ui.warn, self.sopener,
516 516 self.sjoin("journal"),
517 517 aftertrans(renames))
518 518 self._transref = weakref.ref(tr)
519 519 return tr
520 520
521 521 def recover(self):
522 522 l = self.lock()
523 523 try:
524 524 if os.path.exists(self.sjoin("journal")):
525 525 self.ui.status(_("rolling back interrupted transaction\n"))
526 526 transaction.rollback(self.sopener, self.sjoin("journal"))
527 527 self.invalidate()
528 528 return True
529 529 else:
530 530 self.ui.warn(_("no interrupted transaction available\n"))
531 531 return False
532 532 finally:
533 533 del l
534 534
535 535 def rollback(self):
536 536 wlock = lock = None
537 537 try:
538 538 wlock = self.wlock()
539 539 lock = self.lock()
540 540 if os.path.exists(self.sjoin("undo")):
541 541 self.ui.status(_("rolling back last transaction\n"))
542 542 transaction.rollback(self.sopener, self.sjoin("undo"))
543 543 util.rename(self.join("undo.dirstate"), self.join("dirstate"))
544 544 self.invalidate()
545 545 self.dirstate.invalidate()
546 546 else:
547 547 self.ui.warn(_("no rollback information available\n"))
548 548 finally:
549 549 del lock, wlock
550 550
551 551 def invalidate(self):
552 552 for a in "changelog manifest".split():
553 553 if hasattr(self, a):
554 554 self.__delattr__(a)
555 555 self.tagscache = None
556 556 self.nodetagscache = None
557 557
558 558 def _lock(self, lockname, wait, releasefn, acquirefn, desc):
559 559 try:
560 560 l = lock.lock(lockname, 0, releasefn, desc=desc)
561 561 except lock.LockHeld, inst:
562 562 if not wait:
563 563 raise
564 564 self.ui.warn(_("waiting for lock on %s held by %r\n") %
565 565 (desc, inst.locker))
566 566 # default to 600 seconds timeout
567 567 l = lock.lock(lockname, int(self.ui.config("ui", "timeout", "600")),
568 568 releasefn, desc=desc)
569 569 if acquirefn:
570 570 acquirefn()
571 571 return l
572 572
573 573 def lock(self, wait=True):
574 574 if self._lockref and self._lockref():
575 575 return self._lockref()
576 576
577 577 l = self._lock(self.sjoin("lock"), wait, None, self.invalidate,
578 578 _('repository %s') % self.origroot)
579 579 self._lockref = weakref.ref(l)
580 580 return l
581 581
582 582 def wlock(self, wait=True):
583 583 if self._wlockref and self._wlockref():
584 584 return self._wlockref()
585 585
586 586 l = self._lock(self.join("wlock"), wait, self.dirstate.write,
587 587 self.dirstate.invalidate, _('working directory of %s') %
588 588 self.origroot)
589 589 self._wlockref = weakref.ref(l)
590 590 return l
591 591
592 592 def filecommit(self, fn, manifest1, manifest2, linkrev, tr, changelist):
593 593 """
594 594 commit an individual file as part of a larger transaction
595 595 """
596 596
597 597 t = self.wread(fn)
598 598 fl = self.file(fn)
599 599 fp1 = manifest1.get(fn, nullid)
600 600 fp2 = manifest2.get(fn, nullid)
601 601
602 602 meta = {}
603 603 cp = self.dirstate.copied(fn)
604 604 if cp:
605 605 # Mark the new revision of this file as a copy of another
606 606 # file. This copy data will effectively act as a parent
607 607 # of this new revision. If this is a merge, the first
608 608 # parent will be the nullid (meaning "look up the copy data")
609 609 # and the second one will be the other parent. For example:
610 610 #
611 611 # 0 --- 1 --- 3 rev1 changes file foo
612 612 # \ / rev2 renames foo to bar and changes it
613 613 # \- 2 -/ rev3 should have bar with all changes and
614 614 # should record that bar descends from
615 615 # bar in rev2 and foo in rev1
616 616 #
617 617 # this allows this merge to succeed:
618 618 #
619 619 # 0 --- 1 --- 3 rev4 reverts the content change from rev2
620 620 # \ / merging rev3 and rev4 should use bar@rev2
621 621 # \- 2 --- 4 as the merge base
622 622 #
623 623 meta["copy"] = cp
624 624 if not manifest2: # not a branch merge
625 625 meta["copyrev"] = hex(manifest1.get(cp, nullid))
626 626 fp2 = nullid
627 627 elif fp2 != nullid: # copied on remote side
628 628 meta["copyrev"] = hex(manifest1.get(cp, nullid))
629 629 elif fp1 != nullid: # copied on local side, reversed
630 630 meta["copyrev"] = hex(manifest2.get(cp))
631 631 fp2 = fp1
632 632 elif cp in manifest2: # directory rename on local side
633 633 meta["copyrev"] = hex(manifest2[cp])
634 634 else: # directory rename on remote side
635 635 meta["copyrev"] = hex(manifest1.get(cp, nullid))
636 636 self.ui.debug(_(" %s: copy %s:%s\n") %
637 637 (fn, cp, meta["copyrev"]))
638 638 fp1 = nullid
639 639 elif fp2 != nullid:
640 640 # is one parent an ancestor of the other?
641 641 fpa = fl.ancestor(fp1, fp2)
642 642 if fpa == fp1:
643 643 fp1, fp2 = fp2, nullid
644 644 elif fpa == fp2:
645 645 fp2 = nullid
646 646
647 647 # is the file unmodified from the parent? report existing entry
648 648 if fp2 == nullid and not fl.cmp(fp1, t) and not meta:
649 649 return fp1
650 650
651 651 changelist.append(fn)
652 652 return fl.add(t, meta, tr, linkrev, fp1, fp2)
653 653
654 654 def rawcommit(self, files, text, user, date, p1=None, p2=None, extra={}):
655 655 if p1 is None:
656 656 p1, p2 = self.dirstate.parents()
657 657 return self.commit(files=files, text=text, user=user, date=date,
658 658 p1=p1, p2=p2, extra=extra, empty_ok=True)
659 659
660 660 def commit(self, files=None, text="", user=None, date=None,
661 661 match=util.always, force=False, force_editor=False,
662 662 p1=None, p2=None, extra={}, empty_ok=False):
663 663 wlock = lock = tr = None
664 664 try:
665 665 commit = []
666 666 remove = []
667 667 changed = []
668 668 use_dirstate = (p1 is None) # not rawcommit
669 669 extra = extra.copy()
670 670
671 671 if use_dirstate:
672 672 if files:
673 673 for f in files:
674 674 s = self.dirstate[f]
675 675 if s in 'nma':
676 676 commit.append(f)
677 677 elif s == 'r':
678 678 remove.append(f)
679 679 else:
680 680 self.ui.warn(_("%s not tracked!\n") % f)
681 681 else:
682 682 changes = self.status(match=match)[:5]
683 683 modified, added, removed, deleted, unknown = changes
684 684 commit = modified + added
685 685 remove = removed
686 686 else:
687 687 commit = files
688 688
689 689 if use_dirstate:
690 690 p1, p2 = self.dirstate.parents()
691 691 update_dirstate = True
692 692 else:
693 693 p1, p2 = p1, p2 or nullid
694 694 update_dirstate = (self.dirstate.parents()[0] == p1)
695 695
696 696 c1 = self.changelog.read(p1)
697 697 c2 = self.changelog.read(p2)
698 698 m1 = self.manifest.read(c1[0]).copy()
699 699 m2 = self.manifest.read(c2[0])
700 700
701 701 if use_dirstate:
702 702 branchname = self.workingctx().branch()
703 703 try:
704 704 branchname = branchname.decode('UTF-8').encode('UTF-8')
705 705 except UnicodeDecodeError:
706 706 raise util.Abort(_('branch name not in UTF-8!'))
707 707 else:
708 708 branchname = ""
709 709
710 710 if use_dirstate:
711 711 oldname = c1[5].get("branch") # stored in UTF-8
712 712 if (not commit and not remove and not force and p2 == nullid
713 713 and branchname == oldname):
714 714 self.ui.status(_("nothing changed\n"))
715 715 return None
716 716
717 717 xp1 = hex(p1)
718 718 if p2 == nullid: xp2 = ''
719 719 else: xp2 = hex(p2)
720 720
721 721 self.hook("precommit", throw=True, parent1=xp1, parent2=xp2)
722 722
723 723 wlock = self.wlock()
724 724 lock = self.lock()
725 725 tr = self.transaction()
726 726 trp = weakref.proxy(tr)
727 727
728 728 # check in files
729 729 new = {}
730 730 linkrev = self.changelog.count()
731 731 commit.sort()
732 732 is_exec = util.execfunc(self.root, m1.execf)
733 733 is_link = util.linkfunc(self.root, m1.linkf)
734 734 for f in commit:
735 735 self.ui.note(f + "\n")
736 736 try:
737 737 new[f] = self.filecommit(f, m1, m2, linkrev, trp, changed)
738 738 new_exec = is_exec(f)
739 739 new_link = is_link(f)
740 740 if ((not changed or changed[-1] != f) and
741 741 m2.get(f) != new[f]):
742 742 # mention the file in the changelog if some
743 743 # flag changed, even if there was no content
744 744 # change.
745 745 old_exec = m1.execf(f)
746 746 old_link = m1.linkf(f)
747 747 if old_exec != new_exec or old_link != new_link:
748 748 changed.append(f)
749 749 m1.set(f, new_exec, new_link)
750 750 except (OSError, IOError):
751 751 if use_dirstate:
752 752 self.ui.warn(_("trouble committing %s!\n") % f)
753 753 raise
754 754 else:
755 755 remove.append(f)
756 756
757 757 # update manifest
758 758 m1.update(new)
759 759 remove.sort()
760 760 removed = []
761 761
762 762 for f in remove:
763 763 if f in m1:
764 764 del m1[f]
765 765 removed.append(f)
766 766 elif f in m2:
767 767 removed.append(f)
768 768 mn = self.manifest.add(m1, trp, linkrev, c1[0], c2[0],
769 769 (new, removed))
770 770
771 771 # add changeset
772 772 new = new.keys()
773 773 new.sort()
774 774
775 775 user = user or self.ui.username()
776 776 if (not empty_ok and not text) or force_editor:
777 777 edittext = []
778 778 if text:
779 779 edittext.append(text)
780 780 edittext.append("")
781 781 edittext.append("HG: user: %s" % user)
782 782 if p2 != nullid:
783 783 edittext.append("HG: branch merge")
784 784 if branchname:
785 785 edittext.append("HG: branch %s" % util.tolocal(branchname))
786 786 edittext.extend(["HG: changed %s" % f for f in changed])
787 787 edittext.extend(["HG: removed %s" % f for f in removed])
788 788 if not changed and not remove:
789 789 edittext.append("HG: no files changed")
790 790 edittext.append("")
791 791 # run editor in the repository root
792 792 olddir = os.getcwd()
793 793 os.chdir(self.root)
794 794 text = self.ui.edit("\n".join(edittext), user)
795 795 os.chdir(olddir)
796 796
797 797 if branchname:
798 798 extra["branch"] = branchname
799 799
800 800 if use_dirstate:
801 801 lines = [line.rstrip() for line in text.rstrip().splitlines()]
802 802 while lines and not lines[0]:
803 803 del lines[0]
804 804 if not lines:
805 805 return None
806 806 text = '\n'.join(lines)
807 807
808 808 n = self.changelog.add(mn, changed + removed, text, trp, p1, p2,
809 809 user, date, extra)
810 810 self.hook('pretxncommit', throw=True, node=hex(n), parent1=xp1,
811 811 parent2=xp2)
812 812 tr.close()
813 813
814 814 if self.branchcache and "branch" in extra:
815 815 self.branchcache[util.tolocal(extra["branch"])] = n
816 816
817 817 if use_dirstate or update_dirstate:
818 818 self.dirstate.setparents(n)
819 819 if use_dirstate:
820 820 for f in new:
821 821 self.dirstate.normal(f)
822 822 for f in removed:
823 823 self.dirstate.forget(f)
824 824
825 825 self.hook("commit", node=hex(n), parent1=xp1, parent2=xp2)
826 826 return n
827 827 finally:
828 828 del tr, lock, wlock
829 829
830 830 def walk(self, node=None, files=[], match=util.always, badmatch=None):
831 831 '''
832 832 walk recursively through the directory tree or a given
833 833 changeset, finding all files matched by the match
834 834 function
835 835
836 836 results are yielded in a tuple (src, filename), where src
837 837 is one of:
838 838 'f' the file was found in the directory tree
839 839 'm' the file was only in the dirstate and not in the tree
840 840 'b' file was not found and matched badmatch
841 841 '''
842 842
843 843 if node:
844 844 fdict = dict.fromkeys(files)
845 845 # for dirstate.walk, files=['.'] means "walk the whole tree".
846 846 # follow that here, too
847 847 fdict.pop('.', None)
848 848 mdict = self.manifest.read(self.changelog.read(node)[0])
849 849 mfiles = mdict.keys()
850 850 mfiles.sort()
851 851 for fn in mfiles:
852 852 for ffn in fdict:
853 853 # match if the file is the exact name or a directory
854 854 if ffn == fn or fn.startswith("%s/" % ffn):
855 855 del fdict[ffn]
856 856 break
857 857 if match(fn):
858 858 yield 'm', fn
859 859 ffiles = fdict.keys()
860 860 ffiles.sort()
861 861 for fn in ffiles:
862 862 if badmatch and badmatch(fn):
863 863 if match(fn):
864 864 yield 'b', fn
865 865 else:
866 866 self.ui.warn(_('%s: No such file in rev %s\n')
867 867 % (self.pathto(fn), short(node)))
868 868 else:
869 869 for src, fn in self.dirstate.walk(files, match, badmatch=badmatch):
870 870 yield src, fn
871 871
872 872 def status(self, node1=None, node2=None, files=[], match=util.always,
873 873 list_ignored=False, list_clean=False):
874 874 """return status of files between two nodes or node and working directory
875 875
876 876 If node1 is None, use the first dirstate parent instead.
877 877 If node2 is None, compare node1 with working directory.
878 878 """
879 879
880 880 def fcmp(fn, getnode):
881 881 t1 = self.wread(fn)
882 882 return self.file(fn).cmp(getnode(fn), t1)
883 883
884 884 def mfmatches(node):
885 885 change = self.changelog.read(node)
886 886 mf = self.manifest.read(change[0]).copy()
887 887 for fn in mf.keys():
888 888 if not match(fn):
889 889 del mf[fn]
890 890 return mf
891 891
892 892 modified, added, removed, deleted, unknown = [], [], [], [], []
893 893 ignored, clean = [], []
894 894
895 895 compareworking = False
896 896 if not node1 or (not node2 and node1 == self.dirstate.parents()[0]):
897 897 compareworking = True
898 898
899 899 if not compareworking:
900 900 # read the manifest from node1 before the manifest from node2,
901 901 # so that we'll hit the manifest cache if we're going through
902 902 # all the revisions in parent->child order.
903 903 mf1 = mfmatches(node1)
904 904
905 905 # are we comparing the working directory?
906 906 if not node2:
907 907 (lookup, modified, added, removed, deleted, unknown,
908 908 ignored, clean) = self.dirstate.status(files, match,
909 909 list_ignored, list_clean)
910 910
911 911 # are we comparing working dir against its parent?
912 912 if compareworking:
913 913 if lookup:
914 914 fixup = []
915 915 # do a full compare of any files that might have changed
916 916 ctx = self.changectx()
917 917 for f in lookup:
918 918 if f not in ctx or ctx[f].cmp(self.wread(f)):
919 919 modified.append(f)
920 920 else:
921 921 fixup.append(f)
922 922 if list_clean:
923 923 clean.append(f)
924 924
925 925 # update dirstate for files that are actually clean
926 926 if fixup:
927 927 wlock = None
928 928 try:
929 929 try:
930 930 wlock = self.wlock(False)
931 931 except lock.LockException:
932 932 pass
933 933 if wlock:
934 934 for f in fixup:
935 935 self.dirstate.normal(f)
936 936 finally:
937 937 del wlock
938 938 else:
939 939 # we are comparing working dir against non-parent
940 940 # generate a pseudo-manifest for the working dir
941 941 # XXX: create it in dirstate.py ?
942 942 mf2 = mfmatches(self.dirstate.parents()[0])
943 943 is_exec = util.execfunc(self.root, mf2.execf)
944 944 is_link = util.linkfunc(self.root, mf2.linkf)
945 945 for f in lookup + modified + added:
946 946 mf2[f] = ""
947 947 mf2.set(f, is_exec(f), is_link(f))
948 948 for f in removed:
949 949 if f in mf2:
950 950 del mf2[f]
951 951
952 952 else:
953 953 # we are comparing two revisions
954 954 mf2 = mfmatches(node2)
955 955
956 956 if not compareworking:
957 957 # flush lists from dirstate before comparing manifests
958 958 modified, added, clean = [], [], []
959 959
960 960 # make sure to sort the files so we talk to the disk in a
961 961 # reasonable order
962 962 mf2keys = mf2.keys()
963 963 mf2keys.sort()
964 964 getnode = lambda fn: mf1.get(fn, nullid)
965 965 for fn in mf2keys:
966 966 if mf1.has_key(fn):
967 967 if (mf1.flags(fn) != mf2.flags(fn) or
968 968 (mf1[fn] != mf2[fn] and
969 969 (mf2[fn] != "" or fcmp(fn, getnode)))):
970 970 modified.append(fn)
971 971 elif list_clean:
972 972 clean.append(fn)
973 973 del mf1[fn]
974 974 else:
975 975 added.append(fn)
976 976
977 977 removed = mf1.keys()
978 978
979 979 # sort and return results:
980 980 for l in modified, added, removed, deleted, unknown, ignored, clean:
981 981 l.sort()
982 982 return (modified, added, removed, deleted, unknown, ignored, clean)
983 983
984 984 def add(self, list):
985 985 wlock = self.wlock()
986 986 try:
987 987 for f in list:
988 988 p = self.wjoin(f)
989 989 try:
990 990 st = os.lstat(p)
991 991 except:
992 992 self.ui.warn(_("%s does not exist!\n") % f)
993 993 continue
994 994 if st.st_size > 10000000:
995 995 self.ui.warn(_("%s: files over 10MB may cause memory and"
996 996 " performance problems\n"
997 997 "(use 'hg revert %s' to unadd the file)\n")
998 998 % (f, f))
999 999 if not (stat.S_ISREG(st.st_mode) or stat.S_ISLNK(st.st_mode)):
1000 1000 self.ui.warn(_("%s not added: only files and symlinks "
1001 1001 "supported currently\n") % f)
1002 1002 elif self.dirstate[f] in 'amn':
1003 1003 self.ui.warn(_("%s already tracked!\n") % f)
1004 1004 elif self.dirstate[f] == 'r':
1005 1005 self.dirstate.normallookup(f)
1006 1006 else:
1007 1007 self.dirstate.add(f)
1008 1008 finally:
1009 1009 del wlock
1010 1010
1011 1011 def forget(self, list):
1012 1012 wlock = self.wlock()
1013 1013 try:
1014 1014 for f in list:
1015 1015 if self.dirstate[f] != 'a':
1016 1016 self.ui.warn(_("%s not added!\n") % f)
1017 1017 else:
1018 1018 self.dirstate.forget(f)
1019 1019 finally:
1020 1020 del wlock
1021 1021
1022 1022 def remove(self, list, unlink=False):
1023 1023 wlock = None
1024 1024 try:
1025 1025 if unlink:
1026 1026 for f in list:
1027 1027 try:
1028 1028 util.unlink(self.wjoin(f))
1029 1029 except OSError, inst:
1030 1030 if inst.errno != errno.ENOENT:
1031 1031 raise
1032 1032 wlock = self.wlock()
1033 1033 for f in list:
1034 1034 if unlink and os.path.exists(self.wjoin(f)):
1035 1035 self.ui.warn(_("%s still exists!\n") % f)
1036 1036 elif self.dirstate[f] == 'a':
1037 1037 self.dirstate.forget(f)
1038 1038 elif f not in self.dirstate:
1039 1039 self.ui.warn(_("%s not tracked!\n") % f)
1040 1040 else:
1041 1041 self.dirstate.remove(f)
1042 1042 finally:
1043 1043 del wlock
1044 1044
1045 1045 def undelete(self, list):
1046 1046 wlock = None
1047 1047 try:
1048 1048 manifests = [self.manifest.read(self.changelog.read(p)[0])
1049 1049 for p in self.dirstate.parents() if p != nullid]
1050 1050 wlock = self.wlock()
1051 1051 for f in list:
1052 1052 if self.dirstate[f] != 'r':
1053 1053 self.ui.warn("%s not removed!\n" % f)
1054 1054 else:
1055 1055 m = f in manifests[0] and manifests[0] or manifests[1]
1056 1056 t = self.file(f).read(m[f])
1057 1057 self.wwrite(f, t, m.flags(f))
1058 1058 self.dirstate.normal(f)
1059 1059 finally:
1060 1060 del wlock
1061 1061
1062 1062 def copy(self, source, dest):
1063 1063 wlock = None
1064 1064 try:
1065 1065 p = self.wjoin(dest)
1066 1066 if not (os.path.exists(p) or os.path.islink(p)):
1067 1067 self.ui.warn(_("%s does not exist!\n") % dest)
1068 1068 elif not (os.path.isfile(p) or os.path.islink(p)):
1069 1069 self.ui.warn(_("copy failed: %s is not a file or a "
1070 1070 "symbolic link\n") % dest)
1071 1071 else:
1072 1072 wlock = self.wlock()
1073 1073 if dest not in self.dirstate:
1074 1074 self.dirstate.add(dest)
1075 1075 self.dirstate.copy(source, dest)
1076 1076 finally:
1077 1077 del wlock
1078 1078
1079 1079 def heads(self, start=None):
1080 1080 heads = self.changelog.heads(start)
1081 1081 # sort the output in rev descending order
1082 1082 heads = [(-self.changelog.rev(h), h) for h in heads]
1083 1083 heads.sort()
1084 1084 return [n for (r, n) in heads]
1085 1085
1086 1086 def branchheads(self, branch, start=None):
1087 1087 branches = self.branchtags()
1088 1088 if branch not in branches:
1089 1089 return []
1090 1090 # The basic algorithm is this:
1091 1091 #
1092 1092 # Start from the branch tip since there are no later revisions that can
1093 1093 # possibly be in this branch, and the tip is a guaranteed head.
1094 1094 #
1095 1095 # Remember the tip's parents as the first ancestors, since these by
1096 1096 # definition are not heads.
1097 1097 #
1098 1098 # Step backwards from the brach tip through all the revisions. We are
1099 1099 # guaranteed by the rules of Mercurial that we will now be visiting the
1100 1100 # nodes in reverse topological order (children before parents).
1101 1101 #
1102 1102 # If a revision is one of the ancestors of a head then we can toss it
1103 1103 # out of the ancestors set (we've already found it and won't be
1104 1104 # visiting it again) and put its parents in the ancestors set.
1105 1105 #
1106 1106 # Otherwise, if a revision is in the branch it's another head, since it
1107 1107 # wasn't in the ancestor list of an existing head. So add it to the
1108 1108 # head list, and add its parents to the ancestor list.
1109 1109 #
1110 1110 # If it is not in the branch ignore it.
1111 1111 #
1112 1112 # Once we have a list of heads, use nodesbetween to filter out all the
1113 1113 # heads that cannot be reached from startrev. There may be a more
1114 1114 # efficient way to do this as part of the previous algorithm.
1115 1115
1116 1116 set = util.set
1117 1117 heads = [self.changelog.rev(branches[branch])]
1118 1118 # Don't care if ancestors contains nullrev or not.
1119 1119 ancestors = set(self.changelog.parentrevs(heads[0]))
1120 1120 for rev in xrange(heads[0] - 1, nullrev, -1):
1121 1121 if rev in ancestors:
1122 1122 ancestors.update(self.changelog.parentrevs(rev))
1123 1123 ancestors.remove(rev)
1124 1124 elif self.changectx(rev).branch() == branch:
1125 1125 heads.append(rev)
1126 1126 ancestors.update(self.changelog.parentrevs(rev))
1127 1127 heads = [self.changelog.node(rev) for rev in heads]
1128 1128 if start is not None:
1129 1129 heads = self.changelog.nodesbetween([start], heads)[2]
1130 1130 return heads
1131 1131
1132 1132 def branches(self, nodes):
1133 1133 if not nodes:
1134 1134 nodes = [self.changelog.tip()]
1135 1135 b = []
1136 1136 for n in nodes:
1137 1137 t = n
1138 1138 while 1:
1139 1139 p = self.changelog.parents(n)
1140 1140 if p[1] != nullid or p[0] == nullid:
1141 1141 b.append((t, n, p[0], p[1]))
1142 1142 break
1143 1143 n = p[0]
1144 1144 return b
1145 1145
1146 1146 def between(self, pairs):
1147 1147 r = []
1148 1148
1149 1149 for top, bottom in pairs:
1150 1150 n, l, i = top, [], 0
1151 1151 f = 1
1152 1152
1153 1153 while n != bottom:
1154 1154 p = self.changelog.parents(n)[0]
1155 1155 if i == f:
1156 1156 l.append(n)
1157 1157 f = f * 2
1158 1158 n = p
1159 1159 i += 1
1160 1160
1161 1161 r.append(l)
1162 1162
1163 1163 return r
1164 1164
1165 1165 def findincoming(self, remote, base=None, heads=None, force=False):
1166 1166 """Return list of roots of the subsets of missing nodes from remote
1167 1167
1168 1168 If base dict is specified, assume that these nodes and their parents
1169 1169 exist on the remote side and that no child of a node of base exists
1170 1170 in both remote and self.
1171 1171 Furthermore base will be updated to include the nodes that exists
1172 1172 in self and remote but no children exists in self and remote.
1173 1173 If a list of heads is specified, return only nodes which are heads
1174 1174 or ancestors of these heads.
1175 1175
1176 1176 All the ancestors of base are in self and in remote.
1177 1177 All the descendants of the list returned are missing in self.
1178 1178 (and so we know that the rest of the nodes are missing in remote, see
1179 1179 outgoing)
1180 1180 """
1181 1181 m = self.changelog.nodemap
1182 1182 search = []
1183 1183 fetch = {}
1184 1184 seen = {}
1185 1185 seenbranch = {}
1186 1186 if base == None:
1187 1187 base = {}
1188 1188
1189 1189 if not heads:
1190 1190 heads = remote.heads()
1191 1191
1192 1192 if self.changelog.tip() == nullid:
1193 1193 base[nullid] = 1
1194 1194 if heads != [nullid]:
1195 1195 return [nullid]
1196 1196 return []
1197 1197
1198 1198 # assume we're closer to the tip than the root
1199 1199 # and start by examining the heads
1200 1200 self.ui.status(_("searching for changes\n"))
1201 1201
1202 1202 unknown = []
1203 1203 for h in heads:
1204 1204 if h not in m:
1205 1205 unknown.append(h)
1206 1206 else:
1207 1207 base[h] = 1
1208 1208
1209 1209 if not unknown:
1210 1210 return []
1211 1211
1212 1212 req = dict.fromkeys(unknown)
1213 1213 reqcnt = 0
1214 1214
1215 1215 # search through remote branches
1216 1216 # a 'branch' here is a linear segment of history, with four parts:
1217 1217 # head, root, first parent, second parent
1218 1218 # (a branch always has two parents (or none) by definition)
1219 1219 unknown = remote.branches(unknown)
1220 1220 while unknown:
1221 1221 r = []
1222 1222 while unknown:
1223 1223 n = unknown.pop(0)
1224 1224 if n[0] in seen:
1225 1225 continue
1226 1226
1227 1227 self.ui.debug(_("examining %s:%s\n")
1228 1228 % (short(n[0]), short(n[1])))
1229 1229 if n[0] == nullid: # found the end of the branch
1230 1230 pass
1231 1231 elif n in seenbranch:
1232 1232 self.ui.debug(_("branch already found\n"))
1233 1233 continue
1234 1234 elif n[1] and n[1] in m: # do we know the base?
1235 1235 self.ui.debug(_("found incomplete branch %s:%s\n")
1236 1236 % (short(n[0]), short(n[1])))
1237 1237 search.append(n) # schedule branch range for scanning
1238 1238 seenbranch[n] = 1
1239 1239 else:
1240 1240 if n[1] not in seen and n[1] not in fetch:
1241 1241 if n[2] in m and n[3] in m:
1242 1242 self.ui.debug(_("found new changeset %s\n") %
1243 1243 short(n[1]))
1244 1244 fetch[n[1]] = 1 # earliest unknown
1245 1245 for p in n[2:4]:
1246 1246 if p in m:
1247 1247 base[p] = 1 # latest known
1248 1248
1249 1249 for p in n[2:4]:
1250 1250 if p not in req and p not in m:
1251 1251 r.append(p)
1252 1252 req[p] = 1
1253 1253 seen[n[0]] = 1
1254 1254
1255 1255 if r:
1256 1256 reqcnt += 1
1257 1257 self.ui.debug(_("request %d: %s\n") %
1258 1258 (reqcnt, " ".join(map(short, r))))
1259 1259 for p in xrange(0, len(r), 10):
1260 1260 for b in remote.branches(r[p:p+10]):
1261 1261 self.ui.debug(_("received %s:%s\n") %
1262 1262 (short(b[0]), short(b[1])))
1263 1263 unknown.append(b)
1264 1264
1265 1265 # do binary search on the branches we found
1266 1266 while search:
1267 1267 n = search.pop(0)
1268 1268 reqcnt += 1
1269 1269 l = remote.between([(n[0], n[1])])[0]
1270 1270 l.append(n[1])
1271 1271 p = n[0]
1272 1272 f = 1
1273 1273 for i in l:
1274 1274 self.ui.debug(_("narrowing %d:%d %s\n") % (f, len(l), short(i)))
1275 1275 if i in m:
1276 1276 if f <= 2:
1277 1277 self.ui.debug(_("found new branch changeset %s\n") %
1278 1278 short(p))
1279 1279 fetch[p] = 1
1280 1280 base[i] = 1
1281 1281 else:
1282 1282 self.ui.debug(_("narrowed branch search to %s:%s\n")
1283 1283 % (short(p), short(i)))
1284 1284 search.append((p, i))
1285 1285 break
1286 1286 p, f = i, f * 2
1287 1287
1288 1288 # sanity check our fetch list
1289 1289 for f in fetch.keys():
1290 1290 if f in m:
1291 1291 raise repo.RepoError(_("already have changeset ") + short(f[:4]))
1292 1292
1293 1293 if base.keys() == [nullid]:
1294 1294 if force:
1295 1295 self.ui.warn(_("warning: repository is unrelated\n"))
1296 1296 else:
1297 1297 raise util.Abort(_("repository is unrelated"))
1298 1298
1299 1299 self.ui.debug(_("found new changesets starting at ") +
1300 1300 " ".join([short(f) for f in fetch]) + "\n")
1301 1301
1302 1302 self.ui.debug(_("%d total queries\n") % reqcnt)
1303 1303
1304 1304 return fetch.keys()
1305 1305
1306 1306 def findoutgoing(self, remote, base=None, heads=None, force=False):
1307 1307 """Return list of nodes that are roots of subsets not in remote
1308 1308
1309 1309 If base dict is specified, assume that these nodes and their parents
1310 1310 exist on the remote side.
1311 1311 If a list of heads is specified, return only nodes which are heads
1312 1312 or ancestors of these heads, and return a second element which
1313 1313 contains all remote heads which get new children.
1314 1314 """
1315 1315 if base == None:
1316 1316 base = {}
1317 1317 self.findincoming(remote, base, heads, force=force)
1318 1318
1319 1319 self.ui.debug(_("common changesets up to ")
1320 1320 + " ".join(map(short, base.keys())) + "\n")
1321 1321
1322 1322 remain = dict.fromkeys(self.changelog.nodemap)
1323 1323
1324 1324 # prune everything remote has from the tree
1325 1325 del remain[nullid]
1326 1326 remove = base.keys()
1327 1327 while remove:
1328 1328 n = remove.pop(0)
1329 1329 if n in remain:
1330 1330 del remain[n]
1331 1331 for p in self.changelog.parents(n):
1332 1332 remove.append(p)
1333 1333
1334 1334 # find every node whose parents have been pruned
1335 1335 subset = []
1336 1336 # find every remote head that will get new children
1337 1337 updated_heads = {}
1338 1338 for n in remain:
1339 1339 p1, p2 = self.changelog.parents(n)
1340 1340 if p1 not in remain and p2 not in remain:
1341 1341 subset.append(n)
1342 1342 if heads:
1343 1343 if p1 in heads:
1344 1344 updated_heads[p1] = True
1345 1345 if p2 in heads:
1346 1346 updated_heads[p2] = True
1347 1347
1348 1348 # this is the set of all roots we have to push
1349 1349 if heads:
1350 1350 return subset, updated_heads.keys()
1351 1351 else:
1352 1352 return subset
1353 1353
1354 1354 def pull(self, remote, heads=None, force=False):
1355 1355 lock = self.lock()
1356 1356 try:
1357 1357 fetch = self.findincoming(remote, heads=heads, force=force)
1358 1358 if fetch == [nullid]:
1359 1359 self.ui.status(_("requesting all changes\n"))
1360 1360
1361 1361 if not fetch:
1362 1362 self.ui.status(_("no changes found\n"))
1363 1363 return 0
1364 1364
1365 1365 if heads is None:
1366 1366 cg = remote.changegroup(fetch, 'pull')
1367 1367 else:
1368 1368 if 'changegroupsubset' not in remote.capabilities:
1369 1369 raise util.Abort(_("Partial pull cannot be done because other repository doesn't support changegroupsubset."))
1370 1370 cg = remote.changegroupsubset(fetch, heads, 'pull')
1371 1371 return self.addchangegroup(cg, 'pull', remote.url())
1372 1372 finally:
1373 1373 del lock
1374 1374
1375 1375 def push(self, remote, force=False, revs=None):
1376 1376 # there are two ways to push to remote repo:
1377 1377 #
1378 1378 # addchangegroup assumes local user can lock remote
1379 1379 # repo (local filesystem, old ssh servers).
1380 1380 #
1381 1381 # unbundle assumes local user cannot lock remote repo (new ssh
1382 1382 # servers, http servers).
1383 1383
1384 1384 if remote.capable('unbundle'):
1385 1385 return self.push_unbundle(remote, force, revs)
1386 1386 return self.push_addchangegroup(remote, force, revs)
1387 1387
1388 1388 def prepush(self, remote, force, revs):
1389 1389 base = {}
1390 1390 remote_heads = remote.heads()
1391 1391 inc = self.findincoming(remote, base, remote_heads, force=force)
1392 1392
1393 1393 update, updated_heads = self.findoutgoing(remote, base, remote_heads)
1394 1394 if revs is not None:
1395 1395 msng_cl, bases, heads = self.changelog.nodesbetween(update, revs)
1396 1396 else:
1397 1397 bases, heads = update, self.changelog.heads()
1398 1398
1399 1399 if not bases:
1400 1400 self.ui.status(_("no changes found\n"))
1401 1401 return None, 1
1402 1402 elif not force:
1403 1403 # check if we're creating new remote heads
1404 1404 # to be a remote head after push, node must be either
1405 1405 # - unknown locally
1406 1406 # - a local outgoing head descended from update
1407 1407 # - a remote head that's known locally and not
1408 1408 # ancestral to an outgoing head
1409 1409
1410 1410 warn = 0
1411 1411
1412 1412 if remote_heads == [nullid]:
1413 1413 warn = 0
1414 1414 elif not revs and len(heads) > len(remote_heads):
1415 1415 warn = 1
1416 1416 else:
1417 1417 newheads = list(heads)
1418 1418 for r in remote_heads:
1419 1419 if r in self.changelog.nodemap:
1420 1420 desc = self.changelog.heads(r, heads)
1421 1421 l = [h for h in heads if h in desc]
1422 1422 if not l:
1423 1423 newheads.append(r)
1424 1424 else:
1425 1425 newheads.append(r)
1426 1426 if len(newheads) > len(remote_heads):
1427 1427 warn = 1
1428 1428
1429 1429 if warn:
1430 1430 self.ui.warn(_("abort: push creates new remote branches!\n"))
1431 1431 self.ui.status(_("(did you forget to merge?"
1432 1432 " use push -f to force)\n"))
1433 1433 return None, 1
1434 1434 elif inc:
1435 1435 self.ui.warn(_("note: unsynced remote changes!\n"))
1436 1436
1437 1437
1438 1438 if revs is None:
1439 1439 cg = self.changegroup(update, 'push')
1440 1440 else:
1441 1441 cg = self.changegroupsubset(update, revs, 'push')
1442 1442 return cg, remote_heads
1443 1443
1444 1444 def push_addchangegroup(self, remote, force, revs):
1445 1445 lock = remote.lock()
1446 1446 try:
1447 1447 ret = self.prepush(remote, force, revs)
1448 1448 if ret[0] is not None:
1449 1449 cg, remote_heads = ret
1450 1450 return remote.addchangegroup(cg, 'push', self.url())
1451 1451 return ret[1]
1452 1452 finally:
1453 1453 del lock
1454 1454
1455 1455 def push_unbundle(self, remote, force, revs):
1456 1456 # local repo finds heads on server, finds out what revs it
1457 1457 # must push. once revs transferred, if server finds it has
1458 1458 # different heads (someone else won commit/push race), server
1459 1459 # aborts.
1460 1460
1461 1461 ret = self.prepush(remote, force, revs)
1462 1462 if ret[0] is not None:
1463 1463 cg, remote_heads = ret
1464 1464 if force: remote_heads = ['force']
1465 1465 return remote.unbundle(cg, remote_heads, 'push')
1466 1466 return ret[1]
1467 1467
1468 1468 def changegroupinfo(self, nodes):
1469 1469 self.ui.note(_("%d changesets found\n") % len(nodes))
1470 1470 if self.ui.debugflag:
1471 1471 self.ui.debug(_("List of changesets:\n"))
1472 1472 for node in nodes:
1473 1473 self.ui.debug("%s\n" % hex(node))
1474 1474
1475 1475 def changegroupsubset(self, bases, heads, source):
1476 1476 """This function generates a changegroup consisting of all the nodes
1477 1477 that are descendents of any of the bases, and ancestors of any of
1478 1478 the heads.
1479 1479
1480 1480 It is fairly complex as determining which filenodes and which
1481 1481 manifest nodes need to be included for the changeset to be complete
1482 1482 is non-trivial.
1483 1483
1484 1484 Another wrinkle is doing the reverse, figuring out which changeset in
1485 1485 the changegroup a particular filenode or manifestnode belongs to."""
1486 1486
1487 1487 self.hook('preoutgoing', throw=True, source=source)
1488 1488
1489 1489 # Set up some initial variables
1490 1490 # Make it easy to refer to self.changelog
1491 1491 cl = self.changelog
1492 1492 # msng is short for missing - compute the list of changesets in this
1493 1493 # changegroup.
1494 1494 msng_cl_lst, bases, heads = cl.nodesbetween(bases, heads)
1495 1495 self.changegroupinfo(msng_cl_lst)
1496 1496 # Some bases may turn out to be superfluous, and some heads may be
1497 1497 # too. nodesbetween will return the minimal set of bases and heads
1498 1498 # necessary to re-create the changegroup.
1499 1499
1500 1500 # Known heads are the list of heads that it is assumed the recipient
1501 1501 # of this changegroup will know about.
1502 1502 knownheads = {}
1503 1503 # We assume that all parents of bases are known heads.
1504 1504 for n in bases:
1505 1505 for p in cl.parents(n):
1506 1506 if p != nullid:
1507 1507 knownheads[p] = 1
1508 1508 knownheads = knownheads.keys()
1509 1509 if knownheads:
1510 1510 # Now that we know what heads are known, we can compute which
1511 1511 # changesets are known. The recipient must know about all
1512 1512 # changesets required to reach the known heads from the null
1513 1513 # changeset.
1514 1514 has_cl_set, junk, junk = cl.nodesbetween(None, knownheads)
1515 1515 junk = None
1516 1516 # Transform the list into an ersatz set.
1517 1517 has_cl_set = dict.fromkeys(has_cl_set)
1518 1518 else:
1519 1519 # If there were no known heads, the recipient cannot be assumed to
1520 1520 # know about any changesets.
1521 1521 has_cl_set = {}
1522 1522
1523 1523 # Make it easy to refer to self.manifest
1524 1524 mnfst = self.manifest
1525 1525 # We don't know which manifests are missing yet
1526 1526 msng_mnfst_set = {}
1527 1527 # Nor do we know which filenodes are missing.
1528 1528 msng_filenode_set = {}
1529 1529
1530 1530 junk = mnfst.index[mnfst.count() - 1] # Get around a bug in lazyindex
1531 1531 junk = None
1532 1532
1533 1533 # A changeset always belongs to itself, so the changenode lookup
1534 1534 # function for a changenode is identity.
1535 1535 def identity(x):
1536 1536 return x
1537 1537
1538 1538 # A function generating function. Sets up an environment for the
1539 1539 # inner function.
1540 1540 def cmp_by_rev_func(revlog):
1541 1541 # Compare two nodes by their revision number in the environment's
1542 1542 # revision history. Since the revision number both represents the
1543 1543 # most efficient order to read the nodes in, and represents a
1544 1544 # topological sorting of the nodes, this function is often useful.
1545 1545 def cmp_by_rev(a, b):
1546 1546 return cmp(revlog.rev(a), revlog.rev(b))
1547 1547 return cmp_by_rev
1548 1548
1549 1549 # If we determine that a particular file or manifest node must be a
1550 1550 # node that the recipient of the changegroup will already have, we can
1551 1551 # also assume the recipient will have all the parents. This function
1552 1552 # prunes them from the set of missing nodes.
1553 1553 def prune_parents(revlog, hasset, msngset):
1554 1554 haslst = hasset.keys()
1555 1555 haslst.sort(cmp_by_rev_func(revlog))
1556 1556 for node in haslst:
1557 1557 parentlst = [p for p in revlog.parents(node) if p != nullid]
1558 1558 while parentlst:
1559 1559 n = parentlst.pop()
1560 1560 if n not in hasset:
1561 1561 hasset[n] = 1
1562 1562 p = [p for p in revlog.parents(n) if p != nullid]
1563 1563 parentlst.extend(p)
1564 1564 for n in hasset:
1565 1565 msngset.pop(n, None)
1566 1566
1567 1567 # This is a function generating function used to set up an environment
1568 1568 # for the inner function to execute in.
1569 1569 def manifest_and_file_collector(changedfileset):
1570 1570 # This is an information gathering function that gathers
1571 1571 # information from each changeset node that goes out as part of
1572 1572 # the changegroup. The information gathered is a list of which
1573 1573 # manifest nodes are potentially required (the recipient may
1574 1574 # already have them) and total list of all files which were
1575 1575 # changed in any changeset in the changegroup.
1576 1576 #
1577 1577 # We also remember the first changenode we saw any manifest
1578 1578 # referenced by so we can later determine which changenode 'owns'
1579 1579 # the manifest.
1580 1580 def collect_manifests_and_files(clnode):
1581 1581 c = cl.read(clnode)
1582 1582 for f in c[3]:
1583 1583 # This is to make sure we only have one instance of each
1584 1584 # filename string for each filename.
1585 1585 changedfileset.setdefault(f, f)
1586 1586 msng_mnfst_set.setdefault(c[0], clnode)
1587 1587 return collect_manifests_and_files
1588 1588
1589 1589 # Figure out which manifest nodes (of the ones we think might be part
1590 1590 # of the changegroup) the recipient must know about and remove them
1591 1591 # from the changegroup.
1592 1592 def prune_manifests():
1593 1593 has_mnfst_set = {}
1594 1594 for n in msng_mnfst_set:
1595 1595 # If a 'missing' manifest thinks it belongs to a changenode
1596 1596 # the recipient is assumed to have, obviously the recipient
1597 1597 # must have that manifest.
1598 1598 linknode = cl.node(mnfst.linkrev(n))
1599 1599 if linknode in has_cl_set:
1600 1600 has_mnfst_set[n] = 1
1601 1601 prune_parents(mnfst, has_mnfst_set, msng_mnfst_set)
1602 1602
1603 1603 # Use the information collected in collect_manifests_and_files to say
1604 1604 # which changenode any manifestnode belongs to.
1605 1605 def lookup_manifest_link(mnfstnode):
1606 1606 return msng_mnfst_set[mnfstnode]
1607 1607
1608 1608 # A function generating function that sets up the initial environment
1609 1609 # the inner function.
1610 1610 def filenode_collector(changedfiles):
1611 1611 next_rev = [0]
1612 1612 # This gathers information from each manifestnode included in the
1613 1613 # changegroup about which filenodes the manifest node references
1614 1614 # so we can include those in the changegroup too.
1615 1615 #
1616 1616 # It also remembers which changenode each filenode belongs to. It
1617 1617 # does this by assuming the a filenode belongs to the changenode
1618 1618 # the first manifest that references it belongs to.
1619 1619 def collect_msng_filenodes(mnfstnode):
1620 1620 r = mnfst.rev(mnfstnode)
1621 1621 if r == next_rev[0]:
1622 1622 # If the last rev we looked at was the one just previous,
1623 1623 # we only need to see a diff.
1624 1624 deltamf = mnfst.readdelta(mnfstnode)
1625 1625 # For each line in the delta
1626 1626 for f, fnode in deltamf.items():
1627 1627 f = changedfiles.get(f, None)
1628 1628 # And if the file is in the list of files we care
1629 1629 # about.
1630 1630 if f is not None:
1631 1631 # Get the changenode this manifest belongs to
1632 1632 clnode = msng_mnfst_set[mnfstnode]
1633 1633 # Create the set of filenodes for the file if
1634 1634 # there isn't one already.
1635 1635 ndset = msng_filenode_set.setdefault(f, {})
1636 1636 # And set the filenode's changelog node to the
1637 1637 # manifest's if it hasn't been set already.
1638 1638 ndset.setdefault(fnode, clnode)
1639 1639 else:
1640 1640 # Otherwise we need a full manifest.
1641 1641 m = mnfst.read(mnfstnode)
1642 1642 # For every file in we care about.
1643 1643 for f in changedfiles:
1644 1644 fnode = m.get(f, None)
1645 1645 # If it's in the manifest
1646 1646 if fnode is not None:
1647 1647 # See comments above.
1648 1648 clnode = msng_mnfst_set[mnfstnode]
1649 1649 ndset = msng_filenode_set.setdefault(f, {})
1650 1650 ndset.setdefault(fnode, clnode)
1651 1651 # Remember the revision we hope to see next.
1652 1652 next_rev[0] = r + 1
1653 1653 return collect_msng_filenodes
1654 1654
1655 1655 # We have a list of filenodes we think we need for a file, lets remove
1656 1656 # all those we now the recipient must have.
1657 1657 def prune_filenodes(f, filerevlog):
1658 1658 msngset = msng_filenode_set[f]
1659 1659 hasset = {}
1660 1660 # If a 'missing' filenode thinks it belongs to a changenode we
1661 1661 # assume the recipient must have, then the recipient must have
1662 1662 # that filenode.
1663 1663 for n in msngset:
1664 1664 clnode = cl.node(filerevlog.linkrev(n))
1665 1665 if clnode in has_cl_set:
1666 1666 hasset[n] = 1
1667 1667 prune_parents(filerevlog, hasset, msngset)
1668 1668
1669 1669 # A function generator function that sets up the a context for the
1670 1670 # inner function.
1671 1671 def lookup_filenode_link_func(fname):
1672 1672 msngset = msng_filenode_set[fname]
1673 1673 # Lookup the changenode the filenode belongs to.
1674 1674 def lookup_filenode_link(fnode):
1675 1675 return msngset[fnode]
1676 1676 return lookup_filenode_link
1677 1677
1678 1678 # Now that we have all theses utility functions to help out and
1679 1679 # logically divide up the task, generate the group.
1680 1680 def gengroup():
1681 1681 # The set of changed files starts empty.
1682 1682 changedfiles = {}
1683 1683 # Create a changenode group generator that will call our functions
1684 1684 # back to lookup the owning changenode and collect information.
1685 1685 group = cl.group(msng_cl_lst, identity,
1686 1686 manifest_and_file_collector(changedfiles))
1687 1687 for chnk in group:
1688 1688 yield chnk
1689 1689
1690 1690 # The list of manifests has been collected by the generator
1691 1691 # calling our functions back.
1692 1692 prune_manifests()
1693 1693 msng_mnfst_lst = msng_mnfst_set.keys()
1694 1694 # Sort the manifestnodes by revision number.
1695 1695 msng_mnfst_lst.sort(cmp_by_rev_func(mnfst))
1696 1696 # Create a generator for the manifestnodes that calls our lookup
1697 1697 # and data collection functions back.
1698 1698 group = mnfst.group(msng_mnfst_lst, lookup_manifest_link,
1699 1699 filenode_collector(changedfiles))
1700 1700 for chnk in group:
1701 1701 yield chnk
1702 1702
1703 1703 # These are no longer needed, dereference and toss the memory for
1704 1704 # them.
1705 1705 msng_mnfst_lst = None
1706 1706 msng_mnfst_set.clear()
1707 1707
1708 1708 changedfiles = changedfiles.keys()
1709 1709 changedfiles.sort()
1710 1710 # Go through all our files in order sorted by name.
1711 1711 for fname in changedfiles:
1712 1712 filerevlog = self.file(fname)
1713 1713 # Toss out the filenodes that the recipient isn't really
1714 1714 # missing.
1715 1715 if msng_filenode_set.has_key(fname):
1716 1716 prune_filenodes(fname, filerevlog)
1717 1717 msng_filenode_lst = msng_filenode_set[fname].keys()
1718 1718 else:
1719 1719 msng_filenode_lst = []
1720 1720 # If any filenodes are left, generate the group for them,
1721 1721 # otherwise don't bother.
1722 1722 if len(msng_filenode_lst) > 0:
1723 1723 yield changegroup.chunkheader(len(fname))
1724 1724 yield fname
1725 1725 # Sort the filenodes by their revision #
1726 1726 msng_filenode_lst.sort(cmp_by_rev_func(filerevlog))
1727 1727 # Create a group generator and only pass in a changenode
1728 1728 # lookup function as we need to collect no information
1729 1729 # from filenodes.
1730 1730 group = filerevlog.group(msng_filenode_lst,
1731 1731 lookup_filenode_link_func(fname))
1732 1732 for chnk in group:
1733 1733 yield chnk
1734 1734 if msng_filenode_set.has_key(fname):
1735 1735 # Don't need this anymore, toss it to free memory.
1736 1736 del msng_filenode_set[fname]
1737 1737 # Signal that no more groups are left.
1738 1738 yield changegroup.closechunk()
1739 1739
1740 1740 if msng_cl_lst:
1741 1741 self.hook('outgoing', node=hex(msng_cl_lst[0]), source=source)
1742 1742
1743 1743 return util.chunkbuffer(gengroup())
1744 1744
1745 1745 def changegroup(self, basenodes, source):
1746 1746 """Generate a changegroup of all nodes that we have that a recipient
1747 1747 doesn't.
1748 1748
1749 1749 This is much easier than the previous function as we can assume that
1750 1750 the recipient has any changenode we aren't sending them."""
1751 1751
1752 1752 self.hook('preoutgoing', throw=True, source=source)
1753 1753
1754 1754 cl = self.changelog
1755 1755 nodes = cl.nodesbetween(basenodes, None)[0]
1756 1756 revset = dict.fromkeys([cl.rev(n) for n in nodes])
1757 1757 self.changegroupinfo(nodes)
1758 1758
1759 1759 def identity(x):
1760 1760 return x
1761 1761
1762 1762 def gennodelst(revlog):
1763 1763 for r in xrange(0, revlog.count()):
1764 1764 n = revlog.node(r)
1765 1765 if revlog.linkrev(n) in revset:
1766 1766 yield n
1767 1767
1768 1768 def changed_file_collector(changedfileset):
1769 1769 def collect_changed_files(clnode):
1770 1770 c = cl.read(clnode)
1771 1771 for fname in c[3]:
1772 1772 changedfileset[fname] = 1
1773 1773 return collect_changed_files
1774 1774
1775 1775 def lookuprevlink_func(revlog):
1776 1776 def lookuprevlink(n):
1777 1777 return cl.node(revlog.linkrev(n))
1778 1778 return lookuprevlink
1779 1779
1780 1780 def gengroup():
1781 1781 # construct a list of all changed files
1782 1782 changedfiles = {}
1783 1783
1784 1784 for chnk in cl.group(nodes, identity,
1785 1785 changed_file_collector(changedfiles)):
1786 1786 yield chnk
1787 1787 changedfiles = changedfiles.keys()
1788 1788 changedfiles.sort()
1789 1789
1790 1790 mnfst = self.manifest
1791 1791 nodeiter = gennodelst(mnfst)
1792 1792 for chnk in mnfst.group(nodeiter, lookuprevlink_func(mnfst)):
1793 1793 yield chnk
1794 1794
1795 1795 for fname in changedfiles:
1796 1796 filerevlog = self.file(fname)
1797 1797 nodeiter = gennodelst(filerevlog)
1798 1798 nodeiter = list(nodeiter)
1799 1799 if nodeiter:
1800 1800 yield changegroup.chunkheader(len(fname))
1801 1801 yield fname
1802 1802 lookup = lookuprevlink_func(filerevlog)
1803 1803 for chnk in filerevlog.group(nodeiter, lookup):
1804 1804 yield chnk
1805 1805
1806 1806 yield changegroup.closechunk()
1807 1807
1808 1808 if nodes:
1809 1809 self.hook('outgoing', node=hex(nodes[0]), source=source)
1810 1810
1811 1811 return util.chunkbuffer(gengroup())
1812 1812
1813 1813 def addchangegroup(self, source, srctype, url):
1814 1814 """add changegroup to repo.
1815 1815
1816 1816 return values:
1817 1817 - nothing changed or no source: 0
1818 1818 - more heads than before: 1+added heads (2..n)
1819 1819 - less heads than before: -1-removed heads (-2..-n)
1820 1820 - number of heads stays the same: 1
1821 1821 """
1822 1822 def csmap(x):
1823 1823 self.ui.debug(_("add changeset %s\n") % short(x))
1824 1824 return cl.count()
1825 1825
1826 1826 def revmap(x):
1827 1827 return cl.rev(x)
1828 1828
1829 1829 if not source:
1830 1830 return 0
1831 1831
1832 1832 self.hook('prechangegroup', throw=True, source=srctype, url=url)
1833 1833
1834 1834 changesets = files = revisions = 0
1835 1835
1836 1836 # write changelog data to temp files so concurrent readers will not see
1837 1837 # inconsistent view
1838 1838 cl = self.changelog
1839 1839 cl.delayupdate()
1840 1840 oldheads = len(cl.heads())
1841 1841
1842 1842 tr = self.transaction()
1843 1843 try:
1844 1844 trp = weakref.proxy(tr)
1845 1845 # pull off the changeset group
1846 1846 self.ui.status(_("adding changesets\n"))
1847 1847 cor = cl.count() - 1
1848 1848 chunkiter = changegroup.chunkiter(source)
1849 1849 if cl.addgroup(chunkiter, csmap, trp, 1) is None:
1850 1850 raise util.Abort(_("received changelog group is empty"))
1851 1851 cnr = cl.count() - 1
1852 1852 changesets = cnr - cor
1853 1853
1854 1854 # pull off the manifest group
1855 1855 self.ui.status(_("adding manifests\n"))
1856 1856 chunkiter = changegroup.chunkiter(source)
1857 1857 # no need to check for empty manifest group here:
1858 1858 # if the result of the merge of 1 and 2 is the same in 3 and 4,
1859 1859 # no new manifest will be created and the manifest group will
1860 1860 # be empty during the pull
1861 1861 self.manifest.addgroup(chunkiter, revmap, trp)
1862 1862
1863 1863 # process the files
1864 1864 self.ui.status(_("adding file changes\n"))
1865 1865 while 1:
1866 1866 f = changegroup.getchunk(source)
1867 1867 if not f:
1868 1868 break
1869 1869 self.ui.debug(_("adding %s revisions\n") % f)
1870 1870 fl = self.file(f)
1871 1871 o = fl.count()
1872 1872 chunkiter = changegroup.chunkiter(source)
1873 1873 if fl.addgroup(chunkiter, revmap, trp) is None:
1874 1874 raise util.Abort(_("received file revlog group is empty"))
1875 1875 revisions += fl.count() - o
1876 1876 files += 1
1877 1877
1878 1878 # make changelog see real files again
1879 1879 cl.finalize(trp)
1880 1880
1881 1881 newheads = len(self.changelog.heads())
1882 1882 heads = ""
1883 1883 if oldheads and newheads != oldheads:
1884 1884 heads = _(" (%+d heads)") % (newheads - oldheads)
1885 1885
1886 1886 self.ui.status(_("added %d changesets"
1887 1887 " with %d changes to %d files%s\n")
1888 1888 % (changesets, revisions, files, heads))
1889 1889
1890 1890 if changesets > 0:
1891 1891 self.hook('pretxnchangegroup', throw=True,
1892 1892 node=hex(self.changelog.node(cor+1)), source=srctype,
1893 1893 url=url)
1894 1894
1895 1895 tr.close()
1896 1896 finally:
1897 1897 del tr
1898 1898
1899 1899 if changesets > 0:
1900 1900 self.hook("changegroup", node=hex(self.changelog.node(cor+1)),
1901 1901 source=srctype, url=url)
1902 1902
1903 1903 for i in xrange(cor + 1, cnr + 1):
1904 1904 self.hook("incoming", node=hex(self.changelog.node(i)),
1905 1905 source=srctype, url=url)
1906 1906
1907 1907 # never return 0 here:
1908 1908 if newheads < oldheads:
1909 1909 return newheads - oldheads - 1
1910 1910 else:
1911 1911 return newheads - oldheads + 1
1912 1912
1913 1913
1914 1914 def stream_in(self, remote):
1915 1915 fp = remote.stream_out()
1916 1916 l = fp.readline()
1917 1917 try:
1918 1918 resp = int(l)
1919 1919 except ValueError:
1920 1920 raise util.UnexpectedOutput(
1921 1921 _('Unexpected response from remote server:'), l)
1922 1922 if resp == 1:
1923 1923 raise util.Abort(_('operation forbidden by server'))
1924 1924 elif resp == 2:
1925 1925 raise util.Abort(_('locking the remote repository failed'))
1926 1926 elif resp != 0:
1927 1927 raise util.Abort(_('the server sent an unknown error code'))
1928 1928 self.ui.status(_('streaming all changes\n'))
1929 1929 l = fp.readline()
1930 1930 try:
1931 1931 total_files, total_bytes = map(int, l.split(' ', 1))
1932 1932 except ValueError, TypeError:
1933 1933 raise util.UnexpectedOutput(
1934 1934 _('Unexpected response from remote server:'), l)
1935 1935 self.ui.status(_('%d files to transfer, %s of data\n') %
1936 1936 (total_files, util.bytecount(total_bytes)))
1937 1937 start = time.time()
1938 1938 for i in xrange(total_files):
1939 1939 # XXX doesn't support '\n' or '\r' in filenames
1940 1940 l = fp.readline()
1941 1941 try:
1942 1942 name, size = l.split('\0', 1)
1943 1943 size = int(size)
1944 1944 except ValueError, TypeError:
1945 1945 raise util.UnexpectedOutput(
1946 1946 _('Unexpected response from remote server:'), l)
1947 1947 self.ui.debug('adding %s (%s)\n' % (name, util.bytecount(size)))
1948 1948 ofp = self.sopener(name, 'w')
1949 1949 for chunk in util.filechunkiter(fp, limit=size):
1950 1950 ofp.write(chunk)
1951 1951 ofp.close()
1952 1952 elapsed = time.time() - start
1953 1953 if elapsed <= 0:
1954 1954 elapsed = 0.001
1955 1955 self.ui.status(_('transferred %s in %.1f seconds (%s/sec)\n') %
1956 1956 (util.bytecount(total_bytes), elapsed,
1957 1957 util.bytecount(total_bytes / elapsed)))
1958 1958 self.invalidate()
1959 1959 return len(self.heads()) + 1
1960 1960
1961 1961 def clone(self, remote, heads=[], stream=False):
1962 1962 '''clone remote repository.
1963 1963
1964 1964 keyword arguments:
1965 1965 heads: list of revs to clone (forces use of pull)
1966 1966 stream: use streaming clone if possible'''
1967 1967
1968 1968 # now, all clients that can request uncompressed clones can
1969 1969 # read repo formats supported by all servers that can serve
1970 1970 # them.
1971 1971
1972 1972 # if revlog format changes, client will have to check version
1973 1973 # and format flags on "stream" capability, and use
1974 1974 # uncompressed only if compatible.
1975 1975
1976 1976 if stream and not heads and remote.capable('stream'):
1977 1977 return self.stream_in(remote)
1978 1978 return self.pull(remote, heads)
1979 1979
1980 1980 # used to avoid circular references so destructors work
1981 1981 def aftertrans(files):
1982 1982 renamefiles = [tuple(t) for t in files]
1983 1983 def a():
1984 1984 for src, dest in renamefiles:
1985 1985 util.rename(src, dest)
1986 1986 return a
1987 1987
1988 1988 def instance(ui, path, create):
1989 1989 return localrepository(ui, util.drop_scheme('file', path), create)
1990 1990
1991 1991 def islocal(path):
1992 1992 return True
@@ -1,152 +1,151
1 1 precommit hook: HG_PARENT1=0000000000000000000000000000000000000000
2 2 pretxncommit hook: HG_NODE=29b62aeb769fdf78d8d9c5f28b017f76d7ef824b HG_PARENT1=0000000000000000000000000000000000000000
3 3 0:29b62aeb769f
4 4 commit hook: HG_NODE=29b62aeb769fdf78d8d9c5f28b017f76d7ef824b HG_PARENT1=0000000000000000000000000000000000000000
5 5 commit.b hook: HG_NODE=29b62aeb769fdf78d8d9c5f28b017f76d7ef824b HG_PARENT1=0000000000000000000000000000000000000000
6 6 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
7 7 precommit hook: HG_PARENT1=29b62aeb769fdf78d8d9c5f28b017f76d7ef824b
8 8 pretxncommit hook: HG_NODE=b702efe9688826e3a91283852b328b84dbf37bc2 HG_PARENT1=29b62aeb769fdf78d8d9c5f28b017f76d7ef824b
9 9 1:b702efe96888
10 10 commit hook: HG_NODE=b702efe9688826e3a91283852b328b84dbf37bc2 HG_PARENT1=29b62aeb769fdf78d8d9c5f28b017f76d7ef824b
11 11 commit.b hook: HG_NODE=b702efe9688826e3a91283852b328b84dbf37bc2 HG_PARENT1=29b62aeb769fdf78d8d9c5f28b017f76d7ef824b
12 12 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
13 13 precommit hook: HG_PARENT1=29b62aeb769fdf78d8d9c5f28b017f76d7ef824b
14 14 pretxncommit hook: HG_NODE=1324a5531bac09b329c3845d35ae6a7526874edb HG_PARENT1=29b62aeb769fdf78d8d9c5f28b017f76d7ef824b
15 15 2:1324a5531bac
16 16 commit hook: HG_NODE=1324a5531bac09b329c3845d35ae6a7526874edb HG_PARENT1=29b62aeb769fdf78d8d9c5f28b017f76d7ef824b
17 17 commit.b hook: HG_NODE=1324a5531bac09b329c3845d35ae6a7526874edb HG_PARENT1=29b62aeb769fdf78d8d9c5f28b017f76d7ef824b
18 18 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
19 19 (branch merge, don't forget to commit)
20 20 precommit hook: HG_PARENT1=1324a5531bac09b329c3845d35ae6a7526874edb HG_PARENT2=b702efe9688826e3a91283852b328b84dbf37bc2
21 21 pretxncommit hook: HG_NODE=4c52fb2e402287dd5dc052090682536c8406c321 HG_PARENT1=1324a5531bac09b329c3845d35ae6a7526874edb HG_PARENT2=b702efe9688826e3a91283852b328b84dbf37bc2
22 22 3:4c52fb2e4022
23 23 commit hook: HG_NODE=4c52fb2e402287dd5dc052090682536c8406c321 HG_PARENT1=1324a5531bac09b329c3845d35ae6a7526874edb HG_PARENT2=b702efe9688826e3a91283852b328b84dbf37bc2
24 24 commit.b hook: HG_NODE=4c52fb2e402287dd5dc052090682536c8406c321 HG_PARENT1=1324a5531bac09b329c3845d35ae6a7526874edb HG_PARENT2=b702efe9688826e3a91283852b328b84dbf37bc2
25 25 pre-identify hook: HG_ARGS=id
26 26 warning: pre-identify hook exited with status 1
27 27 pre-cat hook: HG_ARGS=cat b
28 28 post-cat hook: HG_ARGS=cat b HG_RESULT=0
29 29 b
30 30 prechangegroup hook: HG_SOURCE=pull HG_URL=file:
31 31 changegroup hook: HG_NODE=b702efe9688826e3a91283852b328b84dbf37bc2 HG_SOURCE=pull HG_URL=file:
32 32 incoming hook: HG_NODE=b702efe9688826e3a91283852b328b84dbf37bc2 HG_SOURCE=pull HG_URL=file:
33 33 incoming hook: HG_NODE=1324a5531bac09b329c3845d35ae6a7526874edb HG_SOURCE=pull HG_URL=file:
34 34 incoming hook: HG_NODE=4c52fb2e402287dd5dc052090682536c8406c321 HG_SOURCE=pull HG_URL=file:
35 35 pulling from ../a
36 36 searching for changes
37 37 adding changesets
38 38 adding manifests
39 39 adding file changes
40 40 added 3 changesets with 2 changes to 2 files
41 41 (run 'hg update' to get a working copy)
42 42 pretag hook: HG_LOCAL=0 HG_NODE=4c52fb2e402287dd5dc052090682536c8406c321 HG_TAG=a
43 tag hook: HG_LOCAL=0 HG_NODE=4c52fb2e402287dd5dc052090682536c8406c321 HG_TAG=a
44 43 precommit hook: HG_PARENT1=4c52fb2e402287dd5dc052090682536c8406c321
45 44 pretxncommit hook: HG_NODE=8ea2ef7ad3e8cac946c72f1e0c79d6aebc301198 HG_PARENT1=4c52fb2e402287dd5dc052090682536c8406c321
46 45 4:8ea2ef7ad3e8
47 46 commit hook: HG_NODE=8ea2ef7ad3e8cac946c72f1e0c79d6aebc301198 HG_PARENT1=4c52fb2e402287dd5dc052090682536c8406c321
48 47 commit.b hook: HG_NODE=8ea2ef7ad3e8cac946c72f1e0c79d6aebc301198 HG_PARENT1=4c52fb2e402287dd5dc052090682536c8406c321
49 48 tag hook: HG_LOCAL=0 HG_NODE=4c52fb2e402287dd5dc052090682536c8406c321 HG_TAG=a
50 49 pretag hook: HG_LOCAL=1 HG_NODE=8ea2ef7ad3e8cac946c72f1e0c79d6aebc301198 HG_TAG=la
51 50 tag hook: HG_LOCAL=1 HG_NODE=8ea2ef7ad3e8cac946c72f1e0c79d6aebc301198 HG_TAG=la
52 51 pretag hook: HG_LOCAL=0 HG_NODE=8ea2ef7ad3e8cac946c72f1e0c79d6aebc301198 HG_TAG=fa
53 52 pretag.forbid hook: HG_LOCAL=0 HG_NODE=8ea2ef7ad3e8cac946c72f1e0c79d6aebc301198 HG_TAG=fa
54 53 abort: pretag.forbid hook exited with status 1
55 54 pretag hook: HG_LOCAL=1 HG_NODE=8ea2ef7ad3e8cac946c72f1e0c79d6aebc301198 HG_TAG=fla
56 55 pretag.forbid hook: HG_LOCAL=1 HG_NODE=8ea2ef7ad3e8cac946c72f1e0c79d6aebc301198 HG_TAG=fla
57 56 abort: pretag.forbid hook exited with status 1
58 57 4:8ea2ef7ad3e8
59 58 precommit hook: HG_PARENT1=8ea2ef7ad3e8cac946c72f1e0c79d6aebc301198
60 59 pretxncommit hook: HG_NODE=fad284daf8c032148abaffcd745dafeceefceb61 HG_PARENT1=8ea2ef7ad3e8cac946c72f1e0c79d6aebc301198
61 60 5:fad284daf8c0
62 61 pretxncommit.forbid hook: HG_NODE=fad284daf8c032148abaffcd745dafeceefceb61 HG_PARENT1=8ea2ef7ad3e8cac946c72f1e0c79d6aebc301198
63 62 transaction abort!
64 63 rollback completed
65 64 abort: pretxncommit.forbid1 hook exited with status 1
66 65 4:8ea2ef7ad3e8
67 66 precommit hook: HG_PARENT1=8ea2ef7ad3e8cac946c72f1e0c79d6aebc301198
68 67 precommit.forbid hook: HG_PARENT1=8ea2ef7ad3e8cac946c72f1e0c79d6aebc301198
69 68 abort: precommit.forbid hook exited with status 1
70 69 4:8ea2ef7ad3e8
71 70 preupdate hook: HG_PARENT1=b702efe96888
72 71 0 files updated, 0 files merged, 2 files removed, 0 files unresolved
73 72 preupdate hook: HG_PARENT1=8ea2ef7ad3e8
74 73 update hook: HG_ERROR=0 HG_PARENT1=8ea2ef7ad3e8
75 74 2 files updated, 0 files merged, 0 files removed, 0 files unresolved
76 75 3:4c52fb2e4022
77 76 prechangegroup.forbid hook: HG_SOURCE=pull HG_URL=file:
78 77 pulling from ../a
79 78 searching for changes
80 79 abort: prechangegroup.forbid hook exited with status 1
81 80 4:8ea2ef7ad3e8
82 81 pretxnchangegroup.forbid hook: HG_NODE=8ea2ef7ad3e8cac946c72f1e0c79d6aebc301198 HG_SOURCE=pull HG_URL=file:
83 82 pulling from ../a
84 83 searching for changes
85 84 adding changesets
86 85 adding manifests
87 86 adding file changes
88 87 added 1 changesets with 1 changes to 1 files
89 88 transaction abort!
90 89 rollback completed
91 90 abort: pretxnchangegroup.forbid1 hook exited with status 1
92 91 3:4c52fb2e4022
93 92 preoutgoing hook: HG_SOURCE=pull
94 93 outgoing hook: HG_NODE=8ea2ef7ad3e8cac946c72f1e0c79d6aebc301198 HG_SOURCE=pull
95 94 pulling from ../a
96 95 searching for changes
97 96 adding changesets
98 97 adding manifests
99 98 adding file changes
100 99 added 1 changesets with 1 changes to 1 files
101 100 (run 'hg update' to get a working copy)
102 101 rolling back last transaction
103 102 preoutgoing hook: HG_SOURCE=pull
104 103 preoutgoing.forbid hook: HG_SOURCE=pull
105 104 pulling from ../a
106 105 searching for changes
107 106 abort: preoutgoing.forbid hook exited with status 1
108 107 # test python hooks
109 108 error: preoutgoing.broken hook raised an exception: unsupported operand type(s) for +: 'int' and 'dict'
110 109 error: preoutgoing.raise hook raised an exception: exception from hook
111 110 pulling from ../a
112 111 searching for changes
113 112 error: preoutgoing.abort hook failed: raise abort from hook
114 113 abort: raise abort from hook
115 114 pulling from ../a
116 115 searching for changes
117 116 hook args:
118 117 hooktype preoutgoing
119 118 source pull
120 119 abort: preoutgoing.fail hook failed
121 120 pulling from ../a
122 121 searching for changes
123 122 abort: preoutgoing.uncallable hook is invalid ("hooktests.uncallable" is not callable)
124 123 pulling from ../a
125 124 searching for changes
126 125 abort: preoutgoing.nohook hook is invalid ("hooktests.nohook" is not defined)
127 126 pulling from ../a
128 127 searching for changes
129 128 abort: preoutgoing.nomodule hook is invalid ("nomodule" not in a module)
130 129 pulling from ../a
131 130 searching for changes
132 131 abort: preoutgoing.badmodule hook is invalid (import of "nomodule" failed)
133 132 pulling from ../a
134 133 searching for changes
135 134 abort: preoutgoing.unreachable hook is invalid (import of "hooktests.container" failed)
136 135 pulling from ../a
137 136 searching for changes
138 137 hook args:
139 138 hooktype preoutgoing
140 139 source pull
141 140 adding changesets
142 141 adding manifests
143 142 adding file changes
144 143 added 1 changesets with 1 changes to 1 files
145 144 (run 'hg update' to get a working copy)
146 145 # make sure --traceback works
147 146 Traceback (most recent call last):
148 147 Automatically installed hook
149 148 foo
150 149 calling hook commit.auto: <function autohook>
151 150 Automatically installed hook
152 151 hooks.commit.auto=<function autohook>
General Comments 0
You need to be logged in to leave comments. Login now