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

Error when a commit isn't found to avoid problems later.

A missing commit while doing revision_history will ow throw MissingCommitError
rather than falling over.

With no support for pack files and alternate object dirs then this may merely
indicate that they are in use.

With shallow repos and the like maybe a marker would want to be inserted
instead.
James Westby 18 éve
szülő
commit
f83f2e4dc2
3 módosított fájl, 21 hozzáadás és 8 törlés
  1. 6 0
      git/errors.py
  2. 4 0
      git/repository.py
  3. 11 8
      git/tests/test_repository.py

+ 6 - 0
git/errors.py

@@ -44,3 +44,9 @@ class NotBlobError(WrongObjectException):
 
   _type = 'blob'
 
+class MissingCommitError(Exception):
+  """Indicates that a commit was not found in the repository"""
+
+  def __init__(self, sha, *args, **kwargs):
+    Exception.__init__(self, "%s is not in the revision store" % sha)
+

+ 4 - 0
git/repository.py

@@ -18,6 +18,7 @@
 
 import os
 
+from errors import MissingCommitError
 from objects import (ShaFile,
                      Commit,
                      Tree,
@@ -69,6 +70,7 @@ class Repository(object):
     file = sha[2:]
     path = os.path.join(self.object_dir(), dir, file)
     if not os.path.exists(path):
+      # Should this raise instead?
       return None
     return cls.from_file(path)
 
@@ -96,6 +98,8 @@ class Repository(object):
     XXX: work out how to handle merges.
     """
     commit = self.get_commit(head)
+    if commit is None:
+      raise MissingCommitError(head)
     history = [commit]
     parents = commit.parents()
     parent_commits = [[]] * len(parents)

+ 11 - 8
git/tests/test_repository.py

@@ -19,12 +19,11 @@
 import os
 import unittest
 
-from git.errors import (NotCommitError,
-                        NotTreeError,
-                        NotBlobError,
-                        )
+from git import errors
 from git.repository import Repository
 
+missing_sha = 'b91fa4d900g17e99b433218e988c4eb4a3e9a097'
+
 class RepositoryTests(unittest.TestCase):
 
   def open_repo(self, name):
@@ -53,7 +52,7 @@ class RepositoryTests(unittest.TestCase):
 
   def test_get_object_non_existant(self):
     r = self.open_repo('a')
-    obj = r.get_object('b91fa4d900g17e99b433218e988c4eb4a3e9a097')
+    obj = r.get_object(missing_sha)
     self.assertEqual(obj, None)
 
   def test_get_commit(self):
@@ -63,7 +62,7 @@ class RepositoryTests(unittest.TestCase):
 
   def test_get_commit_not_commit(self):
     r = self.open_repo('a')
-    self.assertRaises(NotCommitError,
+    self.assertRaises(errors.NotCommitError,
                       r.get_commit, '4f2e6529203aa6d44b5af6e3292c837ceda003f9')
 
   def test_get_tree(self):
@@ -75,7 +74,7 @@ class RepositoryTests(unittest.TestCase):
 
   def test_get_tree_not_tree(self):
     r = self.open_repo('a')
-    self.assertRaises(NotTreeError, r.get_tree, r.head())
+    self.assertRaises(errors.NotTreeError, r.get_tree, r.head())
 
   def test_get_blob(self):
     r = self.open_repo('a')
@@ -88,7 +87,7 @@ class RepositoryTests(unittest.TestCase):
 
   def test_get_blob(self):
     r = self.open_repo('a')
-    self.assertRaises(NotBlobError, r.get_blob, r.head())
+    self.assertRaises(errors.NotBlobError, r.get_blob, r.head())
 
   def test_linear_history(self):
     r = self.open_repo('a')
@@ -107,4 +106,8 @@ class RepositoryTests(unittest.TestCase):
                             '60dacdc733de308bb77bb76ce0fb0f9b44c9769e',
                             '0d89f20333fbb1d2f3a94da77f4981373d8f4310'])
 
+  def test_revision_history_missing_commit(self):
+    r = self.open_repo('simple_merge')
+    self.assertRaises(errors.MissingCommitError, r.revision_history,
+                      missing_sha)