Commits

Fredrik Håård committed 96ab4b9

Indexing and slicing

Comments (0)

Files changed (3)

  hg revert
 
 You also have access to the configuration (config, configbool,
-configlist) just as in the internal Mercurial API.
+configlist) just as in the internal Mercurial API. The repository 
+supports slicing and indexing notation.
 
 Example usage::
     >>> import hgapi
     >>> repo.hg_commit("Adding file.txt", user="me")
     >>> str(repo['tip'].desc)
     'Adding file.txt'
+    
 
 Installation
 ============
         self.cfg = False
         self.user = user
  
-    def __getitem__(self, rev=None):
+    def __getitem__(self, rev=slice(0, 'tip')):
         """Get a Revision object for the revision identifed by rev
            rev can be a range (6c31a9f7be7ac58686f0610dd3c4ba375db2472c:tip)
            a single changeset id
            or it can be left blank to indicate the entire history
         """
-        if rev == None:
-            rev = slice(0, 'tip') #'0:tip'
+        if isinstance(rev, slice):
+        
+            return self.revisions(rev)
         return self.revision(rev)
 
     def hg_command(self, *args):
 
     def revision(self, identifier):
         """Get the identified revision as a Revision object"""
+
         out = self.hg_log(identifier=str(identifier), 
-                          template=self.rev_log_tpl)
-       
+                                     template=self.rev_log_tpl)
+                
+        return Revision(out)   
+
+    def revisions(self, slice_):
+        """Retruns a list of Revision objects for the given slice"""
+        out = self.hg_log(identifier=":".join([str(x)for x in (slice_.start, slice_.stop)]), 
+                                             template=self.rev_log_tpl)
+                        
         revs = []
         for entry in out.split('\n')[:-1]:
             revs.append(Revision(entry))
-        if isinstance(identifier, slice):
-            return revs
-        return revs[0] 
 
+        return revs      
+    
     def read_config(self):
         """Read the configuration as seen with 'hg showconfig'
         Is called by __init__ - only needs to be called explicitly

hgapi/testhgapi.py

 
     def test_160_CommitFiles(self):
         with open("test/file2.txt", "w") as out:
-            out.write("newstuff")
+                    out.write("newstuff")        	
         with open("test/file3.txt", "w") as out:
             out.write("this is even more stuff")
         self.repo.hg_commit("only committing file2.txt", user="test", files=["file2.txt"])
         self.assertTrue("file3.txt" in self.repo.hg_status()["M"])
-
+        
+    def test_170_Indexing(self):
+        with open("test/file2.txt", "a+") as out:
+            out.write("newstuff")
+        self.repo.hg_commit("indexing", user="test", files=["file2.txt"])
+        #Compare tip and current revision number
+        self.assertEquals(self.repo['tip'], self.repo[self.repo.hg_rev()])
+        self.assertEquals(self.repo['tip'].desc, "indexing")
+        
+    def test_180_Slicing(self):
+        with open("test/file2.txt", "a+") as out:
+            out.write("newstuff")
+        self.repo.hg_commit("indexing", user="test", files=["file2.txt"])
+        
+        all_revs = self.repo[0:'tip']
+        self.assertEquals(len(all_revs), 12)
+        self.assertEquals(all_revs[-1].desc, all_revs[-2].desc)
+        self.assertNotEquals(all_revs[-2].desc, all_revs[-3].desc)
+        
+        
 
 def test_doc():
     #Prepare for doctest
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.