Browse Source

Move do_commit() and has_index() from Repo to BaseRepo.

Jelmer Vernooij 15 years ago
parent
commit
d7302c7954
2 changed files with 61 additions and 46 deletions
  1. 4 0
      dulwich/errors.py
  2. 57 46
      dulwich/repo.py

+ 4 - 0
dulwich/errors.py

@@ -116,3 +116,7 @@ class FileFormatException(Exception):
 
 class PackedRefsException(FileFormatException):
     """Indicates an error parsing a packed-refs file."""
+
+
+class NoIndexPresent(Exception):
+    """No index is present."""

+ 57 - 46
dulwich/repo.py

@@ -27,6 +27,7 @@ import os
 
 from dulwich.errors import (
     MissingCommitError, 
+    NoIndexPresent,
     NotBlobError, 
     NotCommitError, 
     NotGitRepository,
@@ -548,6 +549,14 @@ class BaseRepo(object):
         """
         raise NotImplementedError(self.put_named_file)
 
+    def open_index(self):
+        """Open the index for this repository.
+        
+        :raises NoIndexPresent: If no index is present
+        :return: Index instance
+        """
+        raise NotImplementedError(self.open_index)
+
     def fetch(self, target, determine_wants=None, progress=None):
         """Fetch objects into another repository.
 
@@ -687,6 +696,52 @@ class BaseRepo(object):
             del self.refs[name]
         raise ValueError(name)
 
+    def do_commit(self, committer, message,
+                  author=None, commit_timestamp=None,
+                  commit_timezone=None, author_timestamp=None, 
+                  author_timezone=None, tree=None):
+        """Create a new commit.
+
+        :param committer: Committer fullname
+        :param message: Commit message
+        :param author: Author fullname (defaults to committer)
+        :param commit_timestamp: Commit timestamp (defaults to now)
+        :param commit_timezone: Commit timestamp timezone (defaults to GMT)
+        :param author_timestamp: Author timestamp (defaults to commit timestamp)
+        :param author_timezone: Author timestamp timezone 
+            (defaults to commit timestamp timezone)
+        :param tree: SHA1 of the tree root to use (if not specified the current index will be committed).
+        :return: New commit SHA1
+        """
+        from dulwich.index import commit_index
+        import time
+        index = self.open_index()
+        c = Commit()
+        if tree is None:
+            c.tree = commit_index(self.object_store, index)
+        else:
+            c.tree = tree
+        c.committer = committer
+        if commit_timestamp is None:
+            commit_timestamp = time.time()
+        c.commit_time = int(commit_timestamp)
+        if commit_timezone is None:
+            commit_timezone = 0
+        c.commit_timezone = commit_timezone
+        if author is None:
+            author = committer
+        c.author = author
+        if author_timestamp is None:
+            author_timestamp = commit_timestamp
+        c.author_time = int(author_timestamp)
+        if author_timezone is None:
+            author_timezone = commit_timezone
+        c.author_timezone = author_timezone
+        c.message = message
+        self.object_store.add_object(c)
+        self.refs["HEAD"] = c.id
+        return c.id
+
 
 class Repo(BaseRepo):
     """A git repository backed by local disk."""
@@ -744,6 +799,8 @@ class Repo(BaseRepo):
     def open_index(self):
         """Open the index for this repository."""
         from dulwich.index import Index
+        if not self.has_index():
+            raise NoIndexPresent()
         return Index(self.index_path())
 
     def has_index(self):
@@ -753,52 +810,6 @@ class Repo(BaseRepo):
     def __repr__(self):
         return "<Repo at %r>" % self.path
 
-    def do_commit(self, committer, message,
-                  author=None, commit_timestamp=None,
-                  commit_timezone=None, author_timestamp=None, 
-                  author_timezone=None, tree=None):
-        """Create a new commit.
-
-        :param committer: Committer fullname
-        :param message: Commit message
-        :param author: Author fullname (defaults to committer)
-        :param commit_timestamp: Commit timestamp (defaults to now)
-        :param commit_timezone: Commit timestamp timezone (defaults to GMT)
-        :param author_timestamp: Author timestamp (defaults to commit timestamp)
-        :param author_timezone: Author timestamp timezone 
-            (defaults to commit timestamp timezone)
-        :param tree: SHA1 of the tree root to use (if not specified the current index will be committed).
-        :return: New commit SHA1
-        """
-        from dulwich.index import commit_index
-        import time
-        index = self.open_index()
-        c = Commit()
-        if tree is None:
-            c.tree = commit_index(self.object_store, index)
-        else:
-            c.tree = tree
-        c.committer = committer
-        if commit_timestamp is None:
-            commit_timestamp = time.time()
-        c.commit_time = int(commit_timestamp)
-        if commit_timezone is None:
-            commit_timezone = 0
-        c.commit_timezone = commit_timezone
-        if author is None:
-            author = committer
-        c.author = author
-        if author_timestamp is None:
-            author_timestamp = commit_timestamp
-        c.author_time = int(author_timestamp)
-        if author_timezone is None:
-            author_timezone = commit_timezone
-        c.author_timezone = author_timezone
-        c.message = message
-        self.object_store.add_object(c)
-        self.refs["HEAD"] = c.id
-        return c.id
-
     @classmethod
     def init(cls, path, mkdir=True):
         controldir = os.path.join(path, ".git")