changeset 36:da28286bf6b7

Add smart node lookup by substring or by rev number
author mpm@selenic.com
date Sat, 07 May 2005 16:11:36 -0800
parents 9197c26a414b
children a8811676c85a
files mercurial/revlog.py
diffstat 1 files changed, 21 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/revlog.py	Sat May 07 16:10:50 2005 -0800
+++ b/mercurial/revlog.py	Sat May 07 16:11:36 2005 -0800
@@ -8,9 +8,12 @@
 # This software may be used and distributed according to the terms
 # of the GNU General Public License, incorporated herein by reference.
 
-import zlib, struct, sha, os, tempfile
+import zlib, struct, sha, os, tempfile, binascii
 from mercurial import mdiff
 
+def hex(node): return binascii.hexlify(node)
+def bin(node): return binascii.unhexlify(node)
+
 def compress(text):
     return zlib.compress(text)
 
@@ -32,7 +35,7 @@
         self.index = []
         self.opener = opener
         self.cache = None
-        self.nodemap = { -1: nullid, nullid: -1 }
+        self.nodemap = {nullid: -1}
         # read the whole index for now, handle on-demand later
         try:
             n = 0
@@ -60,6 +63,21 @@
     def end(self, rev): return self.start(rev) + self.length(rev)
     def base(self, rev): return self.index[rev][2]
 
+    def lookup(self, id):
+        try:
+            rev = int(id)
+            return self.node(rev)
+        except ValueError:
+            c = []
+            for n in self.nodemap:
+                if id in hex(n):
+                    c.append(n)
+            if len(c) > 1: raise KeyError("Ambiguous identifier")
+            if len(c) < 1: raise KeyError
+            return c[0]
+                
+        return None
+
     def revisions(self, list):
         # this can be optimized to do spans, etc
         # be stupid for now
@@ -73,7 +91,7 @@
         return mdiff.patch(text, patch)
 
     def revision(self, node):
-        if node is nullid: return ""
+        if node == nullid: return ""
         if self.cache and self.cache[0] == node: return self.cache[2]
 
         text = None