Move do_commit() and has_index() from Repo to BaseRepo.
authorJelmer Vernooij <jelmer@samba.org>
Thu, 11 Feb 2010 21:42:46 +0000 (22:42 +0100)
committerJelmer Vernooij <jelmer@samba.org>
Thu, 11 Feb 2010 21:42:46 +0000 (22:42 +0100)
dulwich/errors.py
dulwich/repo.py

index 5b999eef637ccfc4599f5b7e490f3c52f4a6459f..2bafdd36dbf0a11e4bc27c49bd74751672f57774 100644 (file)
@@ -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."""
index d766abd3a65c0586a5b6abf3995bb324b626ac3e..29b5b98717ece93cd5b758a838b655b08ff471cd 100644 (file)
@@ -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")