2
0
Эх сурвалжийг харах

Raise exception when no git repository is found.

Jelmer Vernooij 16 жил өмнө
parent
commit
0bfd90c747
2 өөрчлөгдсөн 13 нэмэгдсэн , 5 устгасан
  1. 7 0
      dulwich/errors.py
  2. 6 5
      dulwich/repo.py

+ 7 - 0
dulwich/errors.py

@@ -63,3 +63,10 @@ class ApplyDeltaError(Exception):
     
     def __init__(self, *args, **kwargs):
         Exception.__init__(self, *args, **kwargs)
+
+
+class NotGitRepository(Exception):
+    """Indicates that no Git repository was found."""
+
+    def __init__(self, *args, **kwargs):
+        Exception.__init__(self, *args, **kwargs)

+ 6 - 5
dulwich/repo.py

@@ -20,7 +20,7 @@
 import os
 
 from commit import Commit
-from errors import MissingCommitError, NotBlobError, NotTreeError, NotCommitError
+from errors import MissingCommitError, NotBlobError, NotTreeError, NotCommitError, NotGitRepository
 from objects import (ShaFile,
                      Commit,
                      Tree,
@@ -46,13 +46,14 @@ class Repo(object):
   ref_locs = ['', 'refs', 'refs/tags', 'refs/heads', 'refs/remotes']
 
   def __init__(self, root):
-    controldir = os.path.join(root, ".git")
-    if os.path.exists(os.path.join(controldir, "objects")):
+    if os.path.isdir(os.path.join(root, ".git", "objects")):
       self.bare = False
-      self._controldir = controldir
-    else:
+      self._controldir = os.path.join(root, ".git")
+    elif os.path.isdir(os.path.join(root, "objects")):
       self.bare = True
       self._controldir = root
+    else:
+      raise NotGitRepository(root)
     self.path = root
     self.tags = [Tag(name, ref) for name, ref in self.get_tags().items()]
     self._object_store = None