Forráskód Böngészése

Add methods to repo to get objects of a certain type.

Now get_blob etc. will ensure that you get an object of the type you want.
There needs to be the addition to get the type by going via a higher type
where possible, e.g. tree from commit, but it's not done yet.
James Westby 18 éve
szülő
commit
1699681bfb
4 módosított fájl, 115 hozzáadás és 7 törlés
  1. 46 0
      git/errors.py
  2. 11 3
      git/objects.py
  3. 20 4
      git/repository.py
  4. 38 0
      git/tests/test_repository.py

+ 46 - 0
git/errors.py

@@ -0,0 +1,46 @@
+# errors.py -- errors for python-git
+# Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
+# 
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; version 2
+# of the License.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA  02110-1301, USA.
+
+class WrongObjectException(Exception):
+  """Baseclass for all the _ is not a _ exceptions on objects.
+
+  Do not instantiate directly.
+
+  Subclasses should define a _type attribute that indicates what
+  was expected if they were raised.
+  """
+
+  def __init__(self, sha, *args, **kwargs):
+    string = "%s is not a %s" % (sha, self._type)
+    Exception.__init__(self, string)
+
+class NotCommitError(WrongObjectException):
+  """Indicates that the sha requested does not point to a commit."""
+
+  _type = 'commit'
+
+class NotTreeError(WrongObjectException):
+  """Indicates that the sha requested does not point to a tree."""
+
+  _type = 'tree'
+
+class NotBlobError(WrongObjectException):
+  """Indicates that the sha requested does not point to a blob."""
+
+  _type = 'blob'
+

+ 11 - 3
git/objects.py

@@ -24,6 +24,11 @@ import os
 import sha
 import zlib
 
+from errors import (NotCommitError,
+                    NotTreeError,
+                    NotBlobError,
+                    )
+
 blob_id = "blob"
 tree_id = "tree"
 commit_id = "commit"
@@ -156,7 +161,8 @@ class Blob(ShaFile):
   @classmethod
   def from_file(cls, filename):
     blob = ShaFile.from_file(filename)
-    assert blob._type == cls._type, "%s is not a blob object" % filename
+    if blob._type != cls._type:
+      raise NotBlobError(filename)
     return blob
 
   @classmethod
@@ -175,7 +181,8 @@ class Tree(ShaFile):
   @classmethod
   def from_file(cls, filename):
     tree = ShaFile.from_file(filename)
-    assert tree._type == cls._type, "%s is not a tree object" % filename
+    if tree._type != cls._type:
+      raise NotTreeError(filename)
     return tree
 
   def entries(self):
@@ -216,7 +223,8 @@ class Commit(ShaFile):
   @classmethod
   def from_file(cls, filename):
     commit = ShaFile.from_file(filename)
-    assert commit._type == cls._type, "%s is not a commit object" % filename
+    if commit._type != cls._type:
+      raise NotCommitError(filename)
     return commit
 
   def _parse_text(self):

+ 20 - 4
git/repository.py

@@ -18,7 +18,11 @@
 
 import os
 
-from objects import ShaFile
+from objects import (ShaFile,
+                     Commit,
+                     Tree,
+                     Blob,
+                     )
 
 objectdir = 'objects'
 symref = 'ref: '
@@ -59,12 +63,24 @@ class Repository(object):
   def head(self):
     return self.ref('HEAD')
 
-  def get_object(self, sha):
-    assert len(sha) == 40, "Incorrect sha length"
+  def _get_object(self, sha, cls):
+    assert len(sha) == 40, "Incorrect length sha: %s" % str(sha)
     dir = sha[:2]
     file = sha[2:]
     path = os.path.join(self.object_dir(), dir, file)
     if not os.path.exists(path):
       return None
-    return ShaFile.from_file(path)
+    return cls.from_file(path)
+
+  def get_object(self, sha):
+    return self._get_object(sha, ShaFile)
+
+  def get_commit(self, sha):
+    return self._get_object(sha, Commit)
+
+  def get_tree(self, sha):
+    return self._get_object(sha, Tree)
+
+  def get_blob(self, sha):
+    return self._get_object(sha, Blob)
 

+ 38 - 0
git/tests/test_repository.py

@@ -19,6 +19,10 @@
 import os
 import unittest
 
+from git.errors import (NotCommitError,
+                        NotTreeError,
+                        NotBlobError,
+                        )
 from git.repository import Repository
 
 class RepositoryTests(unittest.TestCase):
@@ -52,3 +56,37 @@ class RepositoryTests(unittest.TestCase):
     obj = r.get_object('b91fa4d900g17e99b433218e988c4eb4a3e9a097')
     self.assertEqual(obj, None)
 
+  def test_get_commit(self):
+    r = self.open_repo('a')
+    obj = r.get_commit(r.head())
+    self.assertEqual(obj._type, 'commit')
+
+  def test_get_commit_not_commit(self):
+    r = self.open_repo('a')
+    self.assertRaises(NotCommitError,
+                      r.get_commit, '4f2e6529203aa6d44b5af6e3292c837ceda003f9')
+
+  def test_get_tree(self):
+    r = self.open_repo('a')
+    commit = r.get_commit(r.head())
+    tree = r.get_tree(commit.tree())
+    self.assertEqual(tree._type, 'tree')
+    self.assertEqual(tree.sha().hexdigest(), commit.tree())
+
+  def test_get_tree_not_tree(self):
+    r = self.open_repo('a')
+    self.assertRaises(NotTreeError, r.get_tree, r.head())
+
+  def test_get_blob(self):
+    r = self.open_repo('a')
+    commit = r.get_commit(r.head())
+    tree = r.get_tree(commit.tree())
+    blob_sha = tree.entries()[0][2]
+    blob = r.get_blob(blob_sha)
+    self.assertEqual(blob._type, 'blob')
+    self.assertEqual(blob.sha().hexdigest(), blob_sha)
+
+  def test_get_blob(self):
+    r = self.open_repo('a')
+    self.assertRaises(NotBlobError, r.get_blob, r.head())
+