Dulwich.io dulwich / 0b655db
Make sure none of the ancestor folders of a new refname is in packed-refs KS Chan authored 3 months ago Jelmer Vernooń≥ committed 2 months ago
2 changed file(s) with 31 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
686686 except (KeyError, IndexError):
687687 realname = name
688688 filename = self.refpath(realname)
689
690 # make sure none of the ancestor folders is in packed refs
691 probe_ref = os.path.dirname(realname)
692 packed_refs = self.get_packed_refs()
693 while probe_ref:
694 if packed_refs.get(probe_ref, None) is not None:
695 raise OSError(errno.ENOTDIR,
696 'Not a directory: {}'.format(filename))
697 probe_ref = os.path.dirname(probe_ref)
698
689699 ensure_dir_exists(os.path.dirname(filename))
690700 with GitFile(filename, 'wb') as f:
691701 if old_ref is not None:
339339 f.read()[:40])
340340 f.close()
341341
342 self.assertRaises(
343 OSError, self._refs.__setitem__,
344 b'refs/some/ref/sub', b'42d06bd4b77fed026b154d16493e5deab78f02ec')
345
346 def test_setitem_packed(self):
347 # It's allowed to set a new ref on a packed ref, the new ref will be placed outside on refs/
348 self._refs[b'refs/heads/packed'] = (
349 b'3ec9c43c84ff242e3ef4a9fc5bc111fd780a76a8'
350 )
351 f = open(os.path.join(self._refs.path, b'refs', b'heads', b'packed'),
352 'rb')
353 self.assertEqual(b'3ec9c43c84ff242e3ef4a9fc5bc111fd780a76a8',
354 f.read()[:40])
355 f.close()
356
357 self._refs._packed_refs[b'refs/some/packed'] = b'42d06bd4b77fed026b154d16493e5deab78f02ec'
358 self.assertRaises(
359 OSError, self._refs.__setitem__,
360 b'refs/some/packed/sub',
361 b'42d06bd4b77fed026b154d16493e5deab78f02ec')
362
342363 def test_setitem_symbolic(self):
343364 ones = b'1' * 40
344365 self._refs[b'HEAD'] = ones