Ejemplo n.º 1
0
 def test_object_sha1(self):
     """Tests that the correct object offset is returned from the index."""
     p = self.get_pack_index(pack1_sha)
     self.assertRaises(KeyError, p.object_sha1, 876)
     self.assertEqual(p.object_sha1(178), hex_to_sha(a_sha))
     self.assertEqual(p.object_sha1(138), hex_to_sha(tree_sha))
     self.assertEqual(p.object_sha1(12), hex_to_sha(commit_sha))
 def test_simple(self):
     myhexsha = b'd80c186a03f423a81b39df39dc87fd269736ca86'
     x = Tree()
     x[b'myname'] = (0o100755, myhexsha)
     self.assertEqual(b'100755 myname\0' + hex_to_sha(myhexsha),
                      x.as_raw_string())
     self.assertEqual(b'100755 myname\0' + hex_to_sha(myhexsha), bytes(x))
Ejemplo n.º 3
0
 def _parse_refs(self, output):
     refs = {}
     for line in BytesIO(output):
         fields = line.rstrip(b'\n').split(b' ')
         self.assertEqual(3, len(fields))
         refname, type_name, sha = fields
         check_ref_format(refname[5:])
         hex_to_sha(sha)
         refs[refname] = (type_name, sha)
     return refs
Ejemplo n.º 4
0
 def test_multiple_ext_refs(self):
     b1, b2 = self.store_blobs([b'foo', b'bar'])
     f = BytesIO()
     entries = build_pack(f, [
         (REF_DELTA, (b1.id, b'foo1')),
         (REF_DELTA, (b2.id, b'bar2')),
     ],
                          store=self.store)
     pack_iter = self.make_pack_iter(f)
     self.assertEntriesMatch([0, 1], entries, pack_iter)
     self.assertEqual(
         [hex_to_sha(b1.id), hex_to_sha(b2.id)], pack_iter.ext_refs())
def _get_shallow(repo):
    shallow_file = repo.get_named_file('shallow')
    if not shallow_file:
        return []
    shallows = []
    with shallow_file:
        for line in shallow_file:
            sha = line.strip()
            if not sha:
                continue
            hex_to_sha(sha)
            shallows.append(sha)
    return shallows
 def test_add(self):
     myhexsha = b'd80c186a03f423a81b39df39dc87fd269736ca86'
     x = Tree()
     x.add(b'myname', 0o100755, myhexsha)
     self.assertEqual(x[b'myname'], (0o100755, myhexsha))
     self.assertEqual(b'100755 myname\0' + hex_to_sha(myhexsha),
                      x.as_raw_string())
    def get_raw(self, name):
        """Obtain the raw fulltext for an object.

        :param name: sha for the object.
        :return: tuple with numeric type and object contents.
        """
        if len(name) == 40:
            sha = hex_to_sha(name)
            hexsha = name
        elif len(name) == 20:
            sha = name
            hexsha = None
        else:
            raise AssertionError("Invalid object name %r" % name)
        for pack in self.packs:
            try:
                return pack.get_raw(sha)
            except KeyError:
                pass
        if hexsha is None:
            hexsha = sha_to_hex(name)
        ret = self._get_loose_object(hexsha)
        if ret is not None:
            return ret.type_num, ret.as_raw_string()
        for alternate in self.alternates:
            try:
                return alternate.get_raw(hexsha)
            except KeyError:
                pass
        raise KeyError(hexsha)
Ejemplo n.º 8
0
 def test_ext_ref(self):
     blob, = self.store_blobs([b'blob'])
     f = BytesIO()
     entries = build_pack(f, [(REF_DELTA, (blob.id, b'blob1'))],
                          store=self.store)
     pack_iter = self.make_pack_iter(f)
     self.assertEntriesMatch([0], entries, pack_iter)
     self.assertEqual([hex_to_sha(blob.id)], pack_iter.ext_refs())
 def test_add_old_order(self):
     myhexsha = b'd80c186a03f423a81b39df39dc87fd269736ca86'
     x = Tree()
     warnings.simplefilter("ignore", DeprecationWarning)
     try:
         x.add(0o100755, b'myname', myhexsha)
     finally:
         warnings.resetwarnings()
     self.assertEqual(x[b'myname'], (0o100755, myhexsha))
     self.assertEqual(b'100755 myname\0' + hex_to_sha(myhexsha),
                      x.as_raw_string())
Ejemplo n.º 10
0
 def test_large(self):
     entry1_sha = hex_to_sha('4e6388232ec39792661e2e75db8fb117fc869ce6')
     entry2_sha = hex_to_sha('e98f071751bd77f59967bfa671cd2caebdccc9a2')
     entries = [(entry1_sha, 0xf2972d0830529b87, 24),
                (entry2_sha, (~0xf2972d0830529b87) & (2**64 - 1), 92)]
     if not self._supports_large:
         self.assertRaises(TypeError, self.index, 'single.idx', entries,
                           pack_checksum)
         return
     idx = self.index('single.idx', entries, pack_checksum)
     self.assertEqual(idx.get_pack_checksum(), pack_checksum)
     self.assertEqual(2, len(idx))
     actual_entries = list(idx.iterentries())
     self.assertEqual(len(entries), len(actual_entries))
     for mine, actual in zip(entries, actual_entries):
         my_sha, my_offset, my_crc = mine
         actual_sha, actual_offset, actual_crc = actual
         self.assertEqual(my_sha, actual_sha)
         self.assertEqual(my_offset, actual_offset)
         if self._has_crc32_checksum:
             self.assertEqual(my_crc, actual_crc)
         else:
             self.assertTrue(actual_crc is None)
    def test_check(self):
        t = Tree
        sha = hex_to_sha(a_sha)

        # filenames
        self.assertCheckSucceeds(t, b'100644 .a\0' + sha)
        self.assertCheckFails(t, b'100644 \0' + sha)
        self.assertCheckFails(t, b'100644 .\0' + sha)
        self.assertCheckFails(t, b'100644 a/a\0' + sha)
        self.assertCheckFails(t, b'100644 ..\0' + sha)
        self.assertCheckFails(t, b'100644 .git\0' + sha)

        # modes
        self.assertCheckSucceeds(t, b'100644 a\0' + sha)
        self.assertCheckSucceeds(t, b'100755 a\0' + sha)
        self.assertCheckSucceeds(t, b'160000 a\0' + sha)
        # TODO more whitelisted modes
        self.assertCheckFails(t, b'123456 a\0' + sha)
        self.assertCheckFails(t, b'123abc a\0' + sha)
        # should fail check, but parses ok
        self.assertCheckFails(t, b'0100644 foo\0' + sha)

        # shas
        self.assertCheckFails(t, b'100644 a\0' + (b'x' * 5))
        self.assertCheckFails(t, b'100644 a\0' + (b'x' * 18) + b'\0')
        self.assertCheckFails(
            t, b'100644 a\0' + (b'x' * 21) + b'\n100644 b\0' + sha)

        # ordering
        sha2 = hex_to_sha(b_sha)
        self.assertCheckSucceeds(t,
                                 b'100644 a\0' + sha + b'\n100644 b\0' + sha)
        self.assertCheckSucceeds(t,
                                 b'100644 a\0' + sha + b'\n100644 b\0' + sha2)
        self.assertCheckFails(t, b'100644 a\0' + sha + b'\n100755 a\0' + sha2)
        self.assertCheckFails(t, b'100644 b\0' + sha2 + b'\n100644 a\0' + sha)
Ejemplo n.º 12
0
    def test_ext_ref_chain_degenerate(self):
        # Test a degenerate case where the sender is sending a REF_DELTA
        # object that expands to an object already in the repository.
        blob, = self.store_blobs([b'blob'])
        blob2, = self.store_blobs([b'blob2'])
        assert blob.id < blob2.id

        f = BytesIO()
        entries = build_pack(f, [
            (REF_DELTA, (blob.id, b'blob2')),
            (REF_DELTA, (0, b'blob3')),
        ],
                             store=self.store)
        pack_iter = self.make_pack_iter(f)
        self.assertEntriesMatch([0, 1], entries, pack_iter)
        self.assertEqual([hex_to_sha(blob.id)], pack_iter.ext_refs())
    def _do_test_parse_tree(self, parse_tree):
        dir = os.path.join(os.path.dirname(__file__), 'data', 'trees')
        o = Tree.from_path(hex_to_filename(dir, tree_sha))
        self.assertEqual([(b'a', 0o100644, a_sha), (b'b', 0o100644, b_sha)],
                         list(parse_tree(o.as_raw_string())))
        # test a broken tree that has a leading 0 on the file mode
        broken_tree = b'0100644 foo\0' + hex_to_sha(a_sha)

        def eval_parse_tree(*args, **kwargs):
            return list(parse_tree(*args, **kwargs))

        self.assertEqual([(b'foo', 0o100644, a_sha)],
                         eval_parse_tree(broken_tree))
        self.assertRaises(ObjectFormatException,
                          eval_parse_tree,
                          broken_tree,
                          strict=True)
Ejemplo n.º 14
0
 def test_single(self):
     entry_sha = hex_to_sha('6f670c0fb53f9463760b7295fbb814e965fb20c8')
     my_entries = [(entry_sha, 178, 42)]
     idx = self.index('single.idx', my_entries, pack_checksum)
     self.assertEqual(idx.get_pack_checksum(), pack_checksum)
     self.assertEqual(1, len(idx))
     actual_entries = list(idx.iterentries())
     self.assertEqual(len(my_entries), len(actual_entries))
     for mine, actual in zip(my_entries, actual_entries):
         my_sha, my_offset, my_crc = mine
         actual_sha, actual_offset, actual_crc = actual
         self.assertEqual(my_sha, actual_sha)
         self.assertEqual(my_offset, actual_offset)
         if self._has_crc32_checksum:
             self.assertEqual(my_crc, actual_crc)
         else:
             self.assertTrue(actual_crc is None)
Ejemplo n.º 15
0
 def test_iterobjects(self):
     with self.get_pack_data(pack1_sha) as p:
         commit_data = (
             b'tree b2a2766a2879c209ab1176e7e778b81ae422eeaa\n'
             b'author James Westby <*****@*****.**> '
             b'1174945067 +0100\n'
             b'committer James Westby <*****@*****.**> '
             b'1174945067 +0100\n'
             b'\n'
             b'Test commit\n')
         blob_sha = b'6f670c0fb53f9463760b7295fbb814e965fb20c8'
         tree_data = b'100644 a\0' + hex_to_sha(blob_sha)
         actual = []
         for offset, type_num, chunks, crc32 in p.iterobjects():
             actual.append((offset, type_num, b''.join(chunks), crc32))
         self.assertEqual([(12, 1, commit_data, 3775879613),
                           (138, 2, tree_data, 912998690),
                           (178, 3, b'test 1\n', 1373561701)], actual)
Ejemplo n.º 16
0
def write_cache_entry(f, entry):
    """Write an index entry to a file.

    :param f: File object
    :param entry: Entry to write, tuple with:
        (name, ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags)
    """
    beginoffset = f.tell()
    (name, ctime, mtime, dev, ino, mode, uid, gid, size, sha, flags) = entry
    write_cache_time(f, ctime)
    write_cache_time(f, mtime)
    flags = len(name) | (flags & ~0x0fff)
    f.write(
        struct.pack(b'>LLLLLL20sH', dev & 0xFFFFFFFF, ino & 0xFFFFFFFF, mode,
                    uid, gid, size, hex_to_sha(sha), flags))
    f.write(name)
    real_size = ((f.tell() - beginoffset + 8) & ~7)
    f.write(b'\0' * ((beginoffset + real_size) - f.tell()))
 def test_simple(self):
     self.assertEqual(b'\xab\xcd' * 10, hex_to_sha(b'abcd' * 10))
Ejemplo n.º 18
0
 def test_head(self):
     output = self._run_git(['rev-parse', 'HEAD'])
     head_sha = output.rstrip(b'\n')
     hex_to_sha(head_sha)
     self.assertEqual(head_sha, self._repo.refs[b'HEAD'])
Ejemplo n.º 19
0
        self.assertEqual(crc32, unpacked.crc32)
        self.assertEqual(b'x', unused)

    def test_write_pack_object_sha(self):
        f = BytesIO()
        f.write(b'header')
        offset = f.tell()
        sha_a = sha1(b'foo')
        sha_b = sha_a.copy()
        write_pack_object(f, Blob.type_num, b'blob', sha=sha_a)
        self.assertNotEqual(sha_a.digest(), sha_b.digest())
        sha_b.update(f.getvalue()[offset:])
        self.assertEqual(sha_a.digest(), sha_b.digest())


pack_checksum = hex_to_sha('721980e866af9a5f93ad674144e1459b8ba3e7b7')


class BaseTestPackIndexWriting(object):
    def assertSucceeds(self, func, *args, **kwargs):
        try:
            func(*args, **kwargs)
        except ChecksumMismatch as e:
            self.fail(e)

    def index(self, filename, entries, pack_checksum):
        raise NotImplementedError(self.index)

    def test_empty(self):
        idx = self.index('empty.idx', [], pack_checksum)
        self.assertEqual(idx.get_pack_checksum(), pack_checksum)