Browse Source

Allow accessing invalidly named refs, but don't allow setting them.

Jelmer Vernooij 14 years ago
parent
commit
34f38b652e
2 changed files with 12 additions and 3 deletions
  1. 2 1
      dulwich/repo.py
  2. 10 2
      dulwich/tests/test_repository.py

+ 2 - 1
dulwich/repo.py

@@ -243,7 +243,6 @@ class RefsContainer(object):
         :return: a tuple of (refname, sha), where refname is the name of the
             last reference in the symbolic reference chain
         """
-        self._check_refname(name)
         contents = SYMREF + name
         depth = 0
         while contents.startswith(SYMREF):
@@ -360,6 +359,7 @@ class DictRefsContainer(RefsContainer):
         if old_ref is not None and self._refs.get(name, None) != old_ref:
             return False
         realname, _ = self._follow(name)
+        self._check_refname(realname)
         self._refs[realname] = new_ref
         return True
 
@@ -574,6 +574,7 @@ class DiskRefsContainer(RefsContainer):
         :param new_ref: The new sha the refname will refer to.
         :return: True if the set was successful, False otherwise.
         """
+        self._check_refname(name)
         try:
             realname, _ = self._follow(name)
         except KeyError:

+ 10 - 2
dulwich/tests/test_repository.py

@@ -574,6 +574,8 @@ class RefsContainerTests(object):
         self._refs['refs/some/ref'] = '42d06bd4b77fed026b154d16493e5deab78f02ec'
         self.assertEqual('42d06bd4b77fed026b154d16493e5deab78f02ec',
                          self._refs['refs/some/ref'])
+        self.assertRaises(errors.RefFormatError, self._refs.__setitem__,
+                          'notrefs/foo', '42d06bd4b77fed026b154d16493e5deab78f02ec')
 
     def test_set_if_equals(self):
         nines = '9' * 40
@@ -650,6 +652,14 @@ class DictRefsContainerTests(RefsContainerTests, TestCase):
         TestCase.setUp(self)
         self._refs = DictRefsContainer(dict(_TEST_REFS))
 
+    def test_invalid_refname(self):
+        # FIXME: Move this test into RefsContainerTests, but requires
+        # some way of injecting invalid refs.
+        self._refs._refs["refs/stash"] = "00" * 20
+        expected_refs = dict(_TEST_REFS)
+        expected_refs["refs/stash"] = "00" * 20
+        self.assertEquals(expected_refs, self._refs.as_dict())
+
 
 class DiskRefsContainerTests(RefsContainerTests, TestCase):
 
@@ -748,8 +758,6 @@ class DiskRefsContainerTests(RefsContainerTests, TestCase):
         self.assertEquals(
           ('refs/heads/master', '42d06bd4b77fed026b154d16493e5deab78f02ec'),
           self._refs._follow('refs/heads/master'))
-        self.assertRaises(errors.RefFormatError, self._refs._follow,
-                          'notrefs/foo')
         self.assertRaises(KeyError, self._refs._follow, 'refs/heads/loop')
 
     def test_delitem(self):