Mercurial > public > mercurial-scm > hg
comparison mercurial/localrepo.py @ 6750:fb42030d79d6
add __len__ and __iter__ methods to repo and revlog
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Thu, 26 Jun 2008 14:35:50 -0500 |
parents | f6c00b17387c |
children | ed5ffb2c12f3 |
comparison
equal
deleted
inserted
replaced
6749:51b0e799352f | 6750:fb42030d79d6 |
---|---|
120 def __getitem__(self, changeid): | 120 def __getitem__(self, changeid): |
121 if changeid == None: | 121 if changeid == None: |
122 return context.workingctx(self) | 122 return context.workingctx(self) |
123 return context.changectx(self, changeid) | 123 return context.changectx(self, changeid) |
124 | 124 |
125 def __nonzero__(self): | |
126 return True | |
127 | |
128 def __len__(self): | |
129 return len(self.changelog) | |
130 | |
131 def __iter__(self): | |
132 for i in xrange(len(self)): | |
133 yield i | |
134 | |
125 def url(self): | 135 def url(self): |
126 return 'file:' + self.root | 136 return 'file:' + self.root |
127 | 137 |
128 def hook(self, name, throw=False, **args): | 138 def hook(self, name, throw=False, **args): |
129 return hook.hook(self.ui, self, name, throw, **args) | 139 return hook.hook(self.ui, self, name, throw, **args) |
366 for t, n in self.tags().items(): | 376 for t, n in self.tags().items(): |
367 self.nodetagscache.setdefault(n, []).append(t) | 377 self.nodetagscache.setdefault(n, []).append(t) |
368 return self.nodetagscache.get(node, []) | 378 return self.nodetagscache.get(node, []) |
369 | 379 |
370 def _branchtags(self, partial, lrev): | 380 def _branchtags(self, partial, lrev): |
371 tiprev = self.changelog.count() - 1 | 381 tiprev = len(self) - 1 |
372 if lrev != tiprev: | 382 if lrev != tiprev: |
373 self._updatebranchcache(partial, lrev+1, tiprev+1) | 383 self._updatebranchcache(partial, lrev+1, tiprev+1) |
374 self._writebranchcache(partial, self.changelog.tip(), tiprev) | 384 self._writebranchcache(partial, self.changelog.tip(), tiprev) |
375 | 385 |
376 return partial | 386 return partial |
411 return {}, nullid, nullrev | 421 return {}, nullid, nullrev |
412 | 422 |
413 try: | 423 try: |
414 last, lrev = lines.pop(0).split(" ", 1) | 424 last, lrev = lines.pop(0).split(" ", 1) |
415 last, lrev = bin(last), int(lrev) | 425 last, lrev = bin(last), int(lrev) |
416 if not (lrev < self.changelog.count() and | 426 if lrev >= len(self) or self[lrev].node() != last: |
417 self.changelog.node(lrev) == last): # sanity check | |
418 # invalidate the cache | 427 # invalidate the cache |
419 raise ValueError('invalidating branch cache (tip differs)') | 428 raise ValueError('invalidating branch cache (tip differs)') |
420 for l in lines: | 429 for l in lines: |
421 if not l: continue | 430 if not l: continue |
422 node, label = l.split(" ", 1) | 431 node, label = l.split(" ", 1) |
832 trp = weakref.proxy(tr) | 841 trp = weakref.proxy(tr) |
833 | 842 |
834 # check in files | 843 # check in files |
835 new = {} | 844 new = {} |
836 changed = [] | 845 changed = [] |
837 linkrev = self.changelog.count() | 846 linkrev = len(self) |
838 commit.sort() | 847 commit.sort() |
839 for f in commit: | 848 for f in commit: |
840 self.ui.note(f + "\n") | 849 self.ui.note(f + "\n") |
841 try: | 850 try: |
842 fctx = wctx.filectx(f) | 851 fctx = wctx.filectx(f) |
1636 # We don't know which manifests are missing yet | 1645 # We don't know which manifests are missing yet |
1637 msng_mnfst_set = {} | 1646 msng_mnfst_set = {} |
1638 # Nor do we know which filenodes are missing. | 1647 # Nor do we know which filenodes are missing. |
1639 msng_filenode_set = {} | 1648 msng_filenode_set = {} |
1640 | 1649 |
1641 junk = mnfst.index[mnfst.count() - 1] # Get around a bug in lazyindex | 1650 junk = mnfst.index[len(mnfst) - 1] # Get around a bug in lazyindex |
1642 junk = None | 1651 junk = None |
1643 | 1652 |
1644 # A changeset always belongs to itself, so the changenode lookup | 1653 # A changeset always belongs to itself, so the changenode lookup |
1645 # function for a changenode is identity. | 1654 # function for a changenode is identity. |
1646 def identity(x): | 1655 def identity(x): |
1836 changedfiles = changedfiles.keys() | 1845 changedfiles = changedfiles.keys() |
1837 changedfiles.sort() | 1846 changedfiles.sort() |
1838 # Go through all our files in order sorted by name. | 1847 # Go through all our files in order sorted by name. |
1839 for fname in changedfiles: | 1848 for fname in changedfiles: |
1840 filerevlog = self.file(fname) | 1849 filerevlog = self.file(fname) |
1841 if filerevlog.count() == 0: | 1850 if not len(filerevlog): |
1842 raise util.Abort(_("empty or missing revlog for %s") % fname) | 1851 raise util.Abort(_("empty or missing revlog for %s") % fname) |
1843 # Toss out the filenodes that the recipient isn't really | 1852 # Toss out the filenodes that the recipient isn't really |
1844 # missing. | 1853 # missing. |
1845 if fname in msng_filenode_set: | 1854 if fname in msng_filenode_set: |
1846 prune_filenodes(fname, filerevlog) | 1855 prune_filenodes(fname, filerevlog) |
1887 self.changegroupinfo(nodes, source) | 1896 self.changegroupinfo(nodes, source) |
1888 | 1897 |
1889 def identity(x): | 1898 def identity(x): |
1890 return x | 1899 return x |
1891 | 1900 |
1892 def gennodelst(revlog): | 1901 def gennodelst(log): |
1893 for r in xrange(0, revlog.count()): | 1902 for r in log: |
1894 n = revlog.node(r) | 1903 n = log.node(r) |
1895 if revlog.linkrev(n) in revset: | 1904 if log.linkrev(n) in revset: |
1896 yield n | 1905 yield n |
1897 | 1906 |
1898 def changed_file_collector(changedfileset): | 1907 def changed_file_collector(changedfileset): |
1899 def collect_changed_files(clnode): | 1908 def collect_changed_files(clnode): |
1900 c = cl.read(clnode) | 1909 c = cl.read(clnode) |
1922 for chnk in mnfst.group(nodeiter, lookuprevlink_func(mnfst)): | 1931 for chnk in mnfst.group(nodeiter, lookuprevlink_func(mnfst)): |
1923 yield chnk | 1932 yield chnk |
1924 | 1933 |
1925 for fname in changedfiles: | 1934 for fname in changedfiles: |
1926 filerevlog = self.file(fname) | 1935 filerevlog = self.file(fname) |
1927 if filerevlog.count() == 0: | 1936 if not len(filerevlog): |
1928 raise util.Abort(_("empty or missing revlog for %s") % fname) | 1937 raise util.Abort(_("empty or missing revlog for %s") % fname) |
1929 nodeiter = gennodelst(filerevlog) | 1938 nodeiter = gennodelst(filerevlog) |
1930 nodeiter = list(nodeiter) | 1939 nodeiter = list(nodeiter) |
1931 if nodeiter: | 1940 if nodeiter: |
1932 yield changegroup.chunkheader(len(fname)) | 1941 yield changegroup.chunkheader(len(fname)) |
1951 - less heads than before: -1-removed heads (-2..-n) | 1960 - less heads than before: -1-removed heads (-2..-n) |
1952 - number of heads stays the same: 1 | 1961 - number of heads stays the same: 1 |
1953 """ | 1962 """ |
1954 def csmap(x): | 1963 def csmap(x): |
1955 self.ui.debug(_("add changeset %s\n") % short(x)) | 1964 self.ui.debug(_("add changeset %s\n") % short(x)) |
1956 return cl.count() | 1965 return len(cl) |
1957 | 1966 |
1958 def revmap(x): | 1967 def revmap(x): |
1959 return cl.rev(x) | 1968 return cl.rev(x) |
1960 | 1969 |
1961 if not source: | 1970 if not source: |
1974 tr = self.transaction() | 1983 tr = self.transaction() |
1975 try: | 1984 try: |
1976 trp = weakref.proxy(tr) | 1985 trp = weakref.proxy(tr) |
1977 # pull off the changeset group | 1986 # pull off the changeset group |
1978 self.ui.status(_("adding changesets\n")) | 1987 self.ui.status(_("adding changesets\n")) |
1979 cor = cl.count() - 1 | 1988 cor = len(cl) - 1 |
1980 chunkiter = changegroup.chunkiter(source) | 1989 chunkiter = changegroup.chunkiter(source) |
1981 if cl.addgroup(chunkiter, csmap, trp) is None and not emptyok: | 1990 if cl.addgroup(chunkiter, csmap, trp) is None and not emptyok: |
1982 raise util.Abort(_("received changelog group is empty")) | 1991 raise util.Abort(_("received changelog group is empty")) |
1983 cnr = cl.count() - 1 | 1992 cnr = len(cl) - 1 |
1984 changesets = cnr - cor | 1993 changesets = cnr - cor |
1985 | 1994 |
1986 # pull off the manifest group | 1995 # pull off the manifest group |
1987 self.ui.status(_("adding manifests\n")) | 1996 self.ui.status(_("adding manifests\n")) |
1988 chunkiter = changegroup.chunkiter(source) | 1997 chunkiter = changegroup.chunkiter(source) |
1998 f = changegroup.getchunk(source) | 2007 f = changegroup.getchunk(source) |
1999 if not f: | 2008 if not f: |
2000 break | 2009 break |
2001 self.ui.debug(_("adding %s revisions\n") % f) | 2010 self.ui.debug(_("adding %s revisions\n") % f) |
2002 fl = self.file(f) | 2011 fl = self.file(f) |
2003 o = fl.count() | 2012 o = len(fl) |
2004 chunkiter = changegroup.chunkiter(source) | 2013 chunkiter = changegroup.chunkiter(source) |
2005 if fl.addgroup(chunkiter, revmap, trp) is None: | 2014 if fl.addgroup(chunkiter, revmap, trp) is None: |
2006 raise util.Abort(_("received file revlog group is empty")) | 2015 raise util.Abort(_("received file revlog group is empty")) |
2007 revisions += fl.count() - o | 2016 revisions += len(fl) - o |
2008 files += 1 | 2017 files += 1 |
2009 | 2018 |
2010 # make changelog see real files again | 2019 # make changelog see real files again |
2011 cl.finalize(trp) | 2020 cl.finalize(trp) |
2012 | 2021 |