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 = {} try: f = self.transport.get("packed-refs") except NoSuchFile: return {} try: 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 finally: f.close() return self._packed_refs
def test_read_with_peeled(self): f = StringIO('%s ref/1\n%s ref/2\n^%s\n%s ref/4' % (ONES, TWOS, THREES, FOURS)) self.assertEqual([ (ONES, 'ref/1', None), (TWOS, 'ref/2', THREES), (FOURS, 'ref/4', None), ], list(read_packed_refs_with_peeled(f)))
def test_read_with_peeled(self): f = StringIO('%s ref/1\n%s ref/2\n^%s\n%s ref/4' % ( ONES, TWOS, THREES, FOURS)) self.assertEqual([ (ONES, 'ref/1', None), (TWOS, 'ref/2', THREES), (FOURS, 'ref/4', None), ], list(read_packed_refs_with_peeled(f)))
def test_read_with_peeled(self): with BytesIO(ONES.hex_bytes + b' ref/1\n' + TWOS.hex_bytes + b' ref/2\n^' + THREES.hex_bytes + b'\n' + FOURS.hex_bytes + b' ref/4') as f: self.assertEqual([ (ONES, b'ref/1', None), (TWOS, b'ref/2', THREES), (FOURS, b'ref/4', None), ], list(read_packed_refs_with_peeled(f)))
def test_read_with_peeled(self): f = StringIO("%s ref/1\n%s ref/2\n^%s\n%s ref/4" % (ONES, TWOS, THREES, FOURS)) self.assertEqual( [(ONES, "ref/1", None), (TWOS, "ref/2", THREES), (FOURS, "ref/4", None)], list(read_packed_refs_with_peeled(f)), )