Add non-bare repository tests.
authorDave Borowitz <dborowitz@google.com>
Wed, 28 Apr 2010 17:41:39 +0000 (10:41 -0700)
committerDave Borowitz <dborowitz@google.com>
Wed, 12 May 2010 16:40:21 +0000 (09:40 -0700)
These tests test building non-bare repos from scratch by manually
adding, staging, and committing files. Also fixed various bugs that the
tests exercise.

Change-Id: Ic5458b28f2e5e4534a7855f5be9562f4b59f5839

dulwich/errors.py
dulwich/index.py
dulwich/repo.py
dulwich/tests/test_repository.py

index 0514d4d0e8a9e0089c2021adb2ab1bc0c8c2b2e9..80f54b380ce2c537a7d4da6037dcae886e1b5264 100644 (file)
@@ -151,3 +151,7 @@ class ObjectFormatException(FileFormatException):
 
 class NoIndexPresent(Exception):
     """No index is present."""
+
+
+class CommitError(Exception):
+    """An error occurred while performing a commit."""
index 077b2a7166b3dfd6f713d63d07da0ede54571861..b6649a8bf9863a85c40df215ac7176af5df5ef94 100644 (file)
@@ -256,6 +256,10 @@ class Index(object):
         # Remove the old entry if any
         self._byname[name] = x
 
+    def __delitem__(self, name):
+        assert isinstance(name, str)
+        del self._byname[name]
+
     def iteritems(self):
         return self._byname.iteritems()
 
index 61d905ec7f202fa0dcf37af6732967f55b4fe8b0..f4e9034b9959d9b3eda3bff34079e2fb94b7c10a 100644 (file)
@@ -34,6 +34,7 @@ from dulwich.errors import (
     NotTreeError,
     NotTagError,
     PackedRefsException,
+    CommitError,
     )
 from dulwich.file import (
     ensure_dir_exists,
@@ -1008,8 +1009,20 @@ class BaseRepo(object):
             author_timezone = commit_timezone
         c.author_timezone = author_timezone
         c.message = message
-        self.object_store.add_object(c)
-        self.refs["HEAD"] = c.id
+        try:
+            old_head = self.refs["HEAD"]
+            c.parents = [old_head]
+            self.object_store.add_object(c)
+            ok = self.refs.set_if_equals("HEAD", old_head, c.id)
+        except KeyError:
+            c.parents = []
+            self.object_store.add_object(c)
+            ok = self.refs.add_if_new("HEAD", c.id)
+        if not ok:
+            # Fail if the atomic compare-and-swap failed, leaving the commit and
+            # all its objects as garbage.
+            raise CommitError("HEAD changed during commit")
+
         return c.id
 
 
@@ -1075,7 +1088,9 @@ class Repo(BaseRepo):
 
     def has_index(self):
         """Check if an index is present."""
-        return os.path.exists(self.index_path())
+        # Bare repos must never have index files; non-bare repos may have a
+        # missing index file, which is treated as empty.
+        return not self.bare
 
     def stage(self, paths):
         """Stage a set of paths.
@@ -1085,14 +1100,15 @@ class Repo(BaseRepo):
         from dulwich.index import cleanup_mode
         index = self.open_index()
         for path in paths:
+            full_path = os.path.join(self.path, path)
             blob = Blob()
             try:
-                st = os.stat(path)
+                st = os.stat(full_path)
             except OSError:
                 # File no longer exists
                 del index[path]
             else:
-                f = open(path, 'rb')
+                f = open(full_path, 'rb')
                 try:
                     blob.data = f.read()
                 finally:
index 69bc3e150b6647d72cf22b8d41d6c89ee9945aa4..07dd3477f24a2067858e1cf8361e796bacc83c5d 100644 (file)
@@ -28,6 +28,9 @@ import unittest
 import warnings
 
 from dulwich import errors
+from dulwich.object_store import (
+    tree_lookup_path,
+    )
 from dulwich import objects
 from dulwich.repo import (
     check_ref_format,
@@ -303,6 +306,101 @@ class RepositoryTests(unittest.TestCase):
             shutil.rmtree(r1_dir)
             shutil.rmtree(r2_dir)
 
+    def _build_initial_repo(self):
+        repo_dir = os.path.join(tempfile.mkdtemp(), 'test')
+        os.makedirs(repo_dir)
+        r = self._repo = Repo.init(repo_dir)
+        self.assertFalse(r.bare)
+        self.assertEqual('ref: refs/heads/master', r.refs.read_ref('HEAD'))
+        self.assertRaises(KeyError, lambda: r.refs['refs/heads/master'])
+
+        f = open(os.path.join(r.path, 'a'), 'wb')
+        try:
+            f.write('file contents')
+        finally:
+            f.close()
+        r.stage(['a'])
+        commit_sha = r.do_commit('msg',
+                                 committer='Test Committer <test@nodomain.com>',
+                                 author='Test Author <test@nodomain.com>',
+                                 commit_timestamp=12345, commit_timezone=0,
+                                 author_timestamp=12345, author_timezone=0)
+        self.assertEqual([], r[commit_sha].parents)
+        return commit_sha
+
+    def test_build_repo(self):
+        commit_sha = self._build_initial_repo()
+        r = self._repo
+        self.assertEqual('ref: refs/heads/master', r.refs.read_ref('HEAD'))
+        self.assertEqual(commit_sha, r.refs['refs/heads/master'])
+        expected_blob = objects.Blob.from_string('file contents')
+        self.assertEqual(expected_blob.data, r[expected_blob.id].data)
+        actual_commit = r[commit_sha]
+        self.assertEqual('msg', actual_commit.message)
+
+    def test_commit_modified(self):
+        parent_sha = self._build_initial_repo()
+        r = self._repo
+        f = open(os.path.join(r.path, 'a'), 'wb')
+        try:
+            f.write('new contents')
+        finally:
+            f.close()
+        r.stage(['a'])
+        commit_sha = r.do_commit('modified a',
+                                 committer='Test Committer <test@nodomain.com>',
+                                 author='Test Author <test@nodomain.com>',
+                                 commit_timestamp=12395, commit_timezone=0,
+                                 author_timestamp=12395, author_timezone=0)
+        self.assertEqual([parent_sha], r[commit_sha].parents)
+        _, blob_id = tree_lookup_path(r.get_object, r[commit_sha].tree, 'a')
+        self.assertEqual('new contents', r[blob_id].data)
+
+    def test_commit_deleted(self):
+        parent_sha = self._build_initial_repo()
+        r = self._repo
+        os.remove(os.path.join(r.path, 'a'))
+        r.stage(['a'])
+        commit_sha = r.do_commit('deleted a',
+                                 committer='Test Committer <test@nodomain.com>',
+                                 author='Test Author <test@nodomain.com>',
+                                 commit_timestamp=12395, commit_timezone=0,
+                                 author_timestamp=12395, author_timezone=0)
+        self.assertEqual([parent_sha], r[commit_sha].parents)
+        self.assertEqual([], list(r.open_index()))
+        tree = r[r[commit_sha].tree]
+        self.assertEqual([], tree.iteritems())
+
+    def test_commit_fail_ref(self):
+        repo_dir = os.path.join(tempfile.mkdtemp(), 'test')
+        os.makedirs(repo_dir)
+        r = self._repo = Repo.init(repo_dir)
+
+        def set_if_equals(name, old_ref, new_ref):
+            self.fail('Unexpected call to set_if_equals')
+        r.refs.set_if_equals = set_if_equals
+
+        def add_if_new(name, new_ref):
+            return False
+        r.refs.add_if_new = add_if_new
+
+        self.assertRaises(errors.CommitError, r.do_commit, 'failed commit',
+                          committer='Test Committer <test@nodomain.com>',
+                          author='Test Author <test@nodomain.com>',
+                          commit_timestamp=12345, commit_timezone=0,
+                          author_timestamp=12345, author_timezone=0)
+        shas = list(r.object_store)
+        self.assertEqual(2, len(shas))
+        for sha in shas:
+            obj = r[sha]
+            if isinstance(obj, objects.Commit):
+                commit = obj
+            elif isinstance(obj, objects.Tree):
+                tree = obj
+            else:
+                self.fail('Unexpected object found: %s' % sha)
+        self.assertEqual(tree.id, commit.tree)
+
 
 class CheckRefFormatTests(unittest.TestCase):
     """Tests for the check_ref_format function.