Mercurial > public > mercurial-scm > hg
comparison mercurial/revlog.py @ 3438:b17f9d3eda74
revlog.lookup tweaks
- fast path for binary node ids
- direct lookup for full hex ids
- silly change to bin_id
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Tue, 17 Oct 2006 22:07:54 -0500 |
parents | a74addddd092 |
children | dba3cadef789 c6773b7ebee8 |
comparison
equal
deleted
inserted
replaced
3437:d96429ddc8e2 | 3438:b17f9d3eda74 |
---|---|
752 - nodeid or subset of hex nodeid | 752 - nodeid or subset of hex nodeid |
753 """ | 753 """ |
754 if isinstance(id, (long, int)): | 754 if isinstance(id, (long, int)): |
755 # rev | 755 # rev |
756 return self.node(id) | 756 return self.node(id) |
757 if len(id) == 20: | |
758 # possibly a binary node | |
759 # odds of a binary node being all hex in ASCII are 1 in 10**25 | |
760 try: | |
761 node = id | |
762 r = self.rev(node) # quick search the index | |
763 return node | |
764 except RevlogError: | |
765 pass # may be partial hex id | |
757 try: | 766 try: |
758 # str(rev) | 767 # str(rev) |
759 rev = int(id) | 768 rev = int(id) |
760 if str(rev) != id: raise ValueError | 769 if str(rev) != id: raise ValueError |
761 if rev < 0: rev = self.count() + rev | 770 if rev < 0: rev = self.count() + rev |
762 if rev < 0 or rev >= self.count(): raise ValueError | 771 if rev < 0 or rev >= self.count(): raise ValueError |
763 return self.node(rev) | 772 return self.node(rev) |
764 except (ValueError, OverflowError): | 773 except (ValueError, OverflowError): |
765 pass | 774 pass |
766 try: | 775 try: |
767 # hex(node)[:...] | 776 if len(id) == 40: |
768 if len(id) % 2 == 0: | 777 # a full hex nodeid? |
769 bin_id = bin(id) | 778 node = bin(id) |
770 else: | 779 r = self.rev(node) |
771 bin_id = bin(id[:-1]) | |
772 node = None | |
773 for n in self.nodemap: | |
774 if n.startswith(bin_id) and hex(n).startswith(id): | |
775 if node is not None: | |
776 raise RevlogError(_("Ambiguous identifier")) | |
777 node = n | |
778 if node is not None: | |
779 return node | 780 return node |
781 elif len(id) < 40: | |
782 # hex(node)[:...] | |
783 bin_id = bin(id[:len(id) & ~1]) # grab an even number of digits | |
784 node = None | |
785 for n in self.nodemap: | |
786 if n.startswith(bin_id) and hex(n).startswith(id): | |
787 if node is not None: | |
788 raise RevlogError(_("Ambiguous identifier")) | |
789 node = n | |
790 if node is not None: | |
791 return node | |
780 except TypeError: | 792 except TypeError: |
781 pass | 793 pass |
782 | |
783 # might need fixing if we change hash lengths | |
784 if len(id) == 20 and id in self.nodemap: | |
785 # node | |
786 return id | |
787 | 794 |
788 raise RevlogError(_("No match found")) | 795 raise RevlogError(_("No match found")) |
789 | 796 |
790 def cmp(self, node, text): | 797 def cmp(self, node, text): |
791 """compare text with a given file revision""" | 798 """compare text with a given file revision""" |