def get_packed_refs(self): """Get contents of the packed-refs file. :return: Dictionary mapping ref names to SHA1s :note: Will return an empty dictionary when no packed-refs file is present. """ # TODO: invalidate the cache on repacking if self._packed_refs is None: # set both to empty because we want _peeled_refs to be # None if and only if _packed_refs is also None. self._packed_refs = {} self._peeled_refs = {} path = os.path.join(self.path, b'packed-refs') try: f = GitFile(path, 'rb') except IOError as e: if e.errno == errno.ENOENT: return {} raise with f: first_line = next(iter(f)).rstrip() if (first_line.startswith(b'# pack-refs') and b' peeled' in first_line): for sha, name, peeled in read_packed_refs_with_peeled(f): self._packed_refs[name] = sha if peeled: self._peeled_refs[name] = peeled else: f.seek(0) for sha, name in read_packed_refs(f): self._packed_refs[name] = sha return self._packed_refs
def test_readonly(self): f = GitFile(self.path('foo'), 'rb') self.assertTrue(isinstance(f, io.IOBase)) self.assertEqual(b'foo contents', f.read()) self.assertEqual(b'', f.read()) f.seek(4) self.assertEqual(b'contents', f.read()) f.close()
def test_write(self): foo = self.path('foo') foo_lock = '%s.lock' % foo orig_f = open(foo, 'rb') self.assertEqual(orig_f.read(), b'foo contents') orig_f.close() self.assertFalse(os.path.exists(foo_lock)) f = GitFile(foo, 'wb') self.assertFalse(f.closed) self.assertRaises(AttributeError, getattr, f, 'not_a_file_property') self.assertTrue(os.path.exists(foo_lock)) f.write(b'new stuff') f.seek(4) f.write(b'contents') f.close() self.assertFalse(os.path.exists(foo_lock)) new_f = open(foo, 'rb') self.assertEqual(b'new contents', new_f.read()) new_f.close()