Implement set_symbolic_ref separately in Disk/DictRefsContainer.
authorDave Borowitz <dborowitz@google.com>
Mon, 3 May 2010 18:36:56 +0000 (11:36 -0700)
committerDave Borowitz <dborowitz@google.com>
Wed, 12 May 2010 16:40:22 +0000 (09:40 -0700)
When writing tests, it became clear that the old implementation was broken.

Change-Id: Id98d7af0c92568d1faf555784214d0c1eb298feb

dulwich/repo.py
dulwich/tests/test_repository.py

index f4e9034b9959d9b3eda3bff34079e2fb94b7c10a..087ad56fa124ccce56635bf580d196f228783117 100644 (file)
@@ -127,7 +127,7 @@ class RefsContainer(object):
         :param name: Name of the ref to set
         :param other: Name of the ref to point at
         """
-        self[name] = SYMREF + other
+        raise NotImplementedError(self.set_symbolic_ref)
 
     def get_packed_refs(self):
         """Get contents of the packed-refs file.
@@ -351,13 +351,14 @@ class DictRefsContainer(RefsContainer):
     def get_packed_refs(self):
         return {}
 
-    def __getitem__(self, name):
-        return self._refs[name]
+    def set_symbolic_ref(self, name, other):
+        self._refs[name] = SYMREF + other
 
     def set_if_equals(self, name, old_ref, new_ref):
         if old_ref is not None and self._refs.get(name, None) != old_ref:
             return False
-        self._refs[name] = new_ref
+        realname, _ = self._follow(name)
+        self._refs[realname] = new_ref
         return True
 
     def add_if_new(self, name, ref):
@@ -525,6 +526,25 @@ class DiskRefsContainer(RefsContainer):
         finally:
             f.abort()
 
+    def set_symbolic_ref(self, name, other):
+        """Make a ref point at another ref.
+
+        :param name: Name of the ref to set
+        :param other: Name of the ref to point at
+        """
+        self._check_refname(name)
+        self._check_refname(other)
+        filename = self.refpath(name)
+        try:
+            f = GitFile(filename, 'wb')
+            try:
+                f.write(SYMREF + other + '\n')
+            except (IOError, OSError):
+                f.abort()
+                raise
+        finally:
+            f.close()
+
     def set_if_equals(self, name, old_ref, new_ref):
         """Set a refname to new_ref only if it currently equals old_ref.
 
index 07dd3477f24a2067858e1cf8361e796bacc83c5d..4aa0e1e4b63c8757224491cd8127a2ebe9fc434d 100644 (file)
@@ -539,6 +539,24 @@ class RefsContainerTests(object):
         self.assertTrue(self._refs.add_if_new('refs/some/ref', nines))
         self.assertEqual(nines, self._refs['refs/some/ref'])
 
+    def test_set_symbolic_ref(self):
+        self._refs.set_symbolic_ref('refs/heads/symbolic', 'refs/heads/master')
+        self.assertEqual('ref: refs/heads/master',
+                         self._refs.read_loose_ref('refs/heads/symbolic'))
+        self.assertEqual('42d06bd4b77fed026b154d16493e5deab78f02ec',
+                         self._refs['refs/heads/symbolic'])
+
+    def test_set_symbolic_ref_overwrite(self):
+        nines = '9' * 40
+        self.assertFalse('refs/heads/symbolic' in self._refs)
+        self._refs['refs/heads/symbolic'] = nines
+        self.assertEqual(nines, self._refs.read_loose_ref('refs/heads/symbolic'))
+        self._refs.set_symbolic_ref('refs/heads/symbolic', 'refs/heads/master')
+        self.assertEqual('ref: refs/heads/master',
+                         self._refs.read_loose_ref('refs/heads/symbolic'))
+        self.assertEqual('42d06bd4b77fed026b154d16493e5deab78f02ec',
+                         self._refs['refs/heads/symbolic'])
+
     def test_check_refname(self):
         try:
             self._refs._check_refname('HEAD')