Beispiel #1
0
 def test_alternates(self):
     alternate_dir = tempfile.mkdtemp()
     self.addCleanup(shutil.rmtree, alternate_dir)
     alternate_store = DiskObjectStore(alternate_dir)
     b2 = make_object(Blob, data="yummy data")
     alternate_store.add_object(b2)
     store = DiskObjectStore(self.store_dir)
     self.assertRaises(KeyError, store.__getitem__, b2.id)
     store.add_alternate_path(alternate_dir)
     self.assertEqual(b2, store[b2.id])
Beispiel #2
0
 def test_rel_alternative_path(self):
     alternate_dir = tempfile.mkdtemp()
     self.addCleanup(shutil.rmtree, alternate_dir)
     alternate_store = DiskObjectStore(alternate_dir)
     b2 = make_object(Blob, data=b"yummy data")
     alternate_store.add_object(b2)
     store = DiskObjectStore(self.store_dir)
     self.assertRaises(KeyError, store.__getitem__, b2.id)
     store.add_alternate_path(os.path.relpath(alternate_dir, self.store_dir))
     self.assertEqual(list(alternate_store), list(store.alternates[0]))
     self.assertIn(b2.id, store)
     self.assertEqual(b2, store[b2.id])
Beispiel #3
0
 def test_alternates(self):
     alternate_dir = tempfile.mkdtemp()
     if not isinstance(alternate_dir, bytes):
         alternate_dir = alternate_dir.encode(sys.getfilesystemencoding())
     self.addCleanup(shutil.rmtree, alternate_dir)
     alternate_store = DiskObjectStore(alternate_dir)
     b2 = make_object(Blob, data=b"yummy data")
     alternate_store.add_object(b2)
     store = DiskObjectStore(self.store_dir)
     self.assertRaises(KeyError, store.__getitem__, b2.id)
     store.add_alternate_path(alternate_dir)
     self.assertIn(b2.id, store)
     self.assertEqual(b2, store[b2.id])
Beispiel #4
0
 def test_loose_compression_level(self):
     alternate_dir = tempfile.mkdtemp()
     self.addCleanup(shutil.rmtree, alternate_dir)
     alternate_store = DiskObjectStore(alternate_dir,
                                       loose_compression_level=6)
     b2 = make_object(Blob, data=b"yummy data")
     alternate_store.add_object(b2)
Beispiel #5
0
    def test_add_thin_pack(self):
        o = DiskObjectStore(self.store_dir)
        try:
            blob = make_object(Blob, data=b"yummy data")
            o.add_object(blob)

            f = BytesIO()
            entries = build_pack(
                f,
                [
                    (REF_DELTA, (blob.id, b"more yummy data")),
                ],
                store=o,
            )

            with o.add_thin_pack(f.read, None) as pack:
                packed_blob_sha = sha_to_hex(entries[0][3])
                pack.check_length_and_checksum()
                self.assertEqual(sorted([blob.id, packed_blob_sha]),
                                 list(pack))
                self.assertTrue(o.contains_packed(packed_blob_sha))
                self.assertTrue(o.contains_packed(blob.id))
                self.assertEqual(
                    (Blob.type_num, b"more yummy data"),
                    o.get_raw(packed_blob_sha),
                )
        finally:
            o.close()
    def test_add_thin_pack(self):
        o = DiskObjectStore(self.store_dir)
        blob = make_object(Blob, data='yummy data')
        o.add_object(blob)

        f = StringIO()
        entries = build_pack(f, [
            (REF_DELTA, (blob.id, 'more yummy data')),
        ],
                             store=o)
        pack = o.add_thin_pack(f.read, None)
        try:
            packed_blob_sha = sha_to_hex(entries[0][3])
            pack.check_length_and_checksum()
            self.assertEqual(sorted([blob.id, packed_blob_sha]), list(pack))
            self.assertTrue(o.contains_packed(packed_blob_sha))
            self.assertTrue(o.contains_packed(blob.id))
            self.assertEqual((Blob.type_num, 'more yummy data'),
                             o.get_raw(packed_blob_sha))
        finally:
            # FIXME: DiskObjectStore should have close() which do the following:
            for p in o._pack_cache or []:
                p.close()

            pack.close()
Beispiel #7
0
    def __init__(self, root):
        if os.path.isdir(os.path.join(root, ".git", OBJECTDIR)):
            self.bare = False
            self._controldir = os.path.join(root, ".git")
        elif (os.path.isdir(os.path.join(root, OBJECTDIR))
              and os.path.isdir(os.path.join(root, REFSDIR))):
            self.bare = True
            self._controldir = root
        elif (os.path.isfile(os.path.join(root, ".git"))):
            import re
            f = open(os.path.join(root, ".git"), 'r')
            try:
                _, path = re.match('(gitdir: )(.+$)', f.read()).groups()
            finally:
                f.close()
            self.bare = False
            self._controldir = os.path.join(root, path)
        else:
            raise NotGitRepository("No git repository was found at %(path)s" %
                                   dict(path=root))
        self.path = root
        object_store = DiskObjectStore(
            os.path.join(self.controldir(), OBJECTDIR))
        refs = DiskRefsContainer(self.controldir())
        BaseRepo.__init__(self, object_store, refs)

        graft_file = self.get_named_file(os.path.join("info", "grafts"))
        if graft_file:
            self._graftpoints = parse_graftpoints(graft_file)

        self.hooks['pre-commit'] = PreCommitShellHook(self.controldir())
        self.hooks['commit-msg'] = CommitMsgShellHook(self.controldir())
        self.hooks['post-commit'] = PostCommitShellHook(self.controldir())
Beispiel #8
0
    def test_add_thin_pack_empty(self):
        o = DiskObjectStore(self.store_dir)

        f = BytesIO()
        entries = build_pack(f, [], store=o)
        self.assertEqual([], entries)
        o.add_thin_pack(f.read, None)
Beispiel #9
0
 def test_add_alternate_path(self):
     store = DiskObjectStore(self.store_dir)
     self.assertEqual([], list(store._read_alternate_paths()))
     store.add_alternate_path(b'/foo/path')
     self.assertEqual([b'/foo/path'], list(store._read_alternate_paths()))
     store.add_alternate_path(b'/bar/path')
     self.assertEqual([b'/foo/path', b'/bar/path'],
                      list(store._read_alternate_paths()))
 def test_add_alternate_path(self):
     store = DiskObjectStore(self.store_dir)
     self.assertEqual([], list(store._read_alternate_paths()))
     store.add_alternate_path("/foo/path")
     self.assertEqual(["/foo/path"], list(store._read_alternate_paths()))
     store.add_alternate_path("/bar/path")
     self.assertEqual(["/foo/path", "/bar/path"],
                      list(store._read_alternate_paths()))
Beispiel #11
0
 def test_add_pack(self):
     o = DiskObjectStore(self.store_dir)
     f, commit, abort = o.add_pack()
     try:
         b = make_object(Blob, data=b"more yummy data")
         write_pack_objects(f, [(b, None)])
     except:
         abort()
         raise
     else:
         commit()
Beispiel #12
0
 def __init__(self, root):
     if os.path.isdir(os.path.join(root, ".git", OBJECTDIR)):
         self.bare = False
         self._controldir = os.path.join(root, ".git")
     elif (os.path.isdir(os.path.join(root, OBJECTDIR))
           and os.path.isdir(os.path.join(root, REFSDIR))):
         self.bare = True
         self._controldir = root
     else:
         raise NotGitRepository(root)
     self.path = root
     self.refs = DiskRefsContainer(self.controldir())
     self.object_store = DiskObjectStore(
         os.path.join(self.controldir(), OBJECTDIR))
Beispiel #13
0
    def __init__(self, root):
        hidden_path = os.path.join(root, CONTROLDIR)
        if os.path.isdir(os.path.join(hidden_path, OBJECTDIR)):
            self.bare = False
            self._controldir = hidden_path
        elif (os.path.isdir(os.path.join(root, OBJECTDIR)) and
              os.path.isdir(os.path.join(root, REFSDIR))):
            self.bare = True
            self._controldir = root
        elif os.path.isfile(hidden_path):
            self.bare = False
            with open(hidden_path, 'r') as f:
                path = read_gitfile(f)
            self.bare = False
            self._controldir = os.path.join(root, path)
        else:
            raise NotGitRepository(
                "No git repository was found at %(path)s" % dict(path=root)
            )
        commondir = self.get_named_file(COMMONDIR)
        if commondir is not None:
            with commondir:
                self._commondir = os.path.join(
                    self.controldir(),
                    commondir.read().rstrip(b"\r\n").decode(
                        sys.getfilesystemencoding()))
        else:
            self._commondir = self._controldir
        self.path = root
        object_store = DiskObjectStore(
            os.path.join(self.commondir(), OBJECTDIR))
        refs = DiskRefsContainer(self.commondir(), self._controldir,
                                 logger=self._write_reflog)
        BaseRepo.__init__(self, object_store, refs)

        self._graftpoints = {}
        graft_file = self.get_named_file(os.path.join("info", "grafts"),
                                         basedir=self.commondir())
        if graft_file:
            with graft_file:
                self._graftpoints.update(parse_graftpoints(graft_file))
        graft_file = self.get_named_file("shallow",
                                         basedir=self.commondir())
        if graft_file:
            with graft_file:
                self._graftpoints.update(parse_graftpoints(graft_file))

        self.hooks['pre-commit'] = PreCommitShellHook(self.controldir())
        self.hooks['commit-msg'] = CommitMsgShellHook(self.controldir())
        self.hooks['post-commit'] = PostCommitShellHook(self.controldir())
    def test_read_alternate_paths(self):
        store = DiskObjectStore(self.store_dir)

        abs_path = os.path.abspath(os.path.normpath('/abspath'))
        # ensures in particular existence of the alternates file
        store.add_alternate_path(abs_path)
        self.assertEqual(set(store._read_alternate_paths()), {abs_path})

        store.add_alternate_path("relative-path")
        self.assertIn(os.path.join(store.path, "relative-path"),
                      set(store._read_alternate_paths()))

        # arguably, add_alternate_path() could strip comments.
        # Meanwhile it's more convenient to use it than to import INFODIR
        store.add_alternate_path("# comment")
        for alt_path in store._read_alternate_paths():
            self.assertNotIn("#", alt_path)
Beispiel #15
0
    def test_add_thin_pack(self):
        o = DiskObjectStore(self.store_dir)
        blob = make_object(Blob, data='yummy data')
        o.add_object(blob)

        f = StringIO()
        entries = build_pack(f, [
            (REF_DELTA, (blob.id, 'more yummy data')),
        ],
                             store=o)
        pack = o.add_thin_pack(f.read, None)

        packed_blob_sha = sha_to_hex(entries[0][3])
        pack.check_length_and_checksum()
        self.assertEqual(sorted([blob.id, packed_blob_sha]), list(pack))
        self.assertTrue(o.contains_packed(packed_blob_sha))
        self.assertTrue(o.contains_packed(blob.id))
        self.assertEqual((Blob.type_num, 'more yummy data'),
                         o.get_raw(packed_blob_sha))
Beispiel #16
0
    def __init__(self, path):
        self.path = path
        if not isinstance(path, bytes):
            self._path_bytes = path.encode(sys.getfilesystemencoding())
        else:
            self._path_bytes = path
        if os.path.isdir(os.path.join(self._path_bytes, b'.git', OBJECTDIR)):
            self.bare = False
            self._controldir = os.path.join(self._path_bytes, b'.git')
        elif (os.path.isdir(os.path.join(self._path_bytes, OBJECTDIR))
              and os.path.isdir(os.path.join(self._path_bytes, REFSDIR))):
            self.bare = True
            self._controldir = self._path_bytes
        elif (os.path.isfile(os.path.join(self._path_bytes, b'.git'))):
            import re
            with open(os.path.join(self._path_bytes, b'.git'), 'rb') as f:
                _, gitdir = re.match(b'(gitdir: )(.+$)', f.read()).groups()
            self.bare = False
            self._controldir = os.path.join(self._path_bytes, gitdir)
        else:
            raise NotGitRepository("No git repository was found at %(path)s" %
                                   dict(path=path))
        object_store = DiskObjectStore(
            os.path.join(self.controldir(), OBJECTDIR))
        refs = DiskRefsContainer(self.controldir())
        BaseRepo.__init__(self, object_store, refs)

        self._graftpoints = {}
        graft_file = self.get_named_file(os.path.join(b'info', b'grafts'))
        if graft_file:
            with graft_file:
                self._graftpoints.update(parse_graftpoints(graft_file))
        graft_file = self.get_named_file(b'shallow')
        if graft_file:
            with graft_file:
                self._graftpoints.update(parse_graftpoints(graft_file))

        self.hooks['pre-commit'] = PreCommitShellHook(self.controldir())
        self.hooks['commit-msg'] = CommitMsgShellHook(self.controldir())
        self.hooks['post-commit'] = PostCommitShellHook(self.controldir())
Beispiel #17
0
    def __init__(self, root):
        hidden_path = os.path.join(root, CONTROLDIR)
        if os.path.isdir(os.path.join(hidden_path, OBJECTDIR)):
            self.bare = False
            self._controldir = hidden_path
        elif (os.path.isdir(os.path.join(root, OBJECTDIR)) and
              os.path.isdir(os.path.join(root, REFSDIR))):
            self.bare = True
            self._controldir = root
        elif os.path.isfile(hidden_path):
            self.bare = False
            with open(hidden_path, 'r') as f:
                path = read_gitfile(f)
            self.bare = False
            self._controldir = os.path.join(root, path)
        else:
            raise NotGitRepository(
                "No git repository was found at %(path)s" % dict(path=root)
            )
        self.path = root
        object_store = DiskObjectStore(os.path.join(self.controldir(),
                                                    OBJECTDIR))
        refs = DiskRefsContainer(self.controldir())
        BaseRepo.__init__(self, object_store, refs)

        self._graftpoints = {}
        graft_file = self.get_named_file(os.path.join("info", "grafts"))
        if graft_file:
            with graft_file:
                self._graftpoints.update(parse_graftpoints(graft_file))
        graft_file = self.get_named_file("shallow")
        if graft_file:
            with graft_file:
                self._graftpoints.update(parse_graftpoints(graft_file))

        self.hooks['pre-commit'] = PreCommitShellHook(self.controldir())
        self.hooks['commit-msg'] = CommitMsgShellHook(self.controldir())
        self.hooks['post-commit'] = PostCommitShellHook(self.controldir())
Beispiel #18
0
 def test_pack_dir(self):
     o = DiskObjectStore(self.store_dir)
     self.assertEqual(os.path.join(self.store_dir, "pack"), o.pack_dir)
 def test_add_pack(self):
     o = DiskObjectStore(self.store_dir)
     f, commit = o.add_pack()
     b = make_object(Blob, data="more yummy data")
     write_pack_objects(f, [(b, None)])
     commit()
Beispiel #20
0
 def test_pack_dir(self):
     o = DiskObjectStore("foo")
     self.assertEquals(os.path.join("foo", "pack"), o.pack_dir)
Beispiel #21
0
 def test_empty_packs(self):
     o = DiskObjectStore("foo")
     self.assertEquals([], o.packs)
Beispiel #22
0
 def setUp(self):
     TestCase.setUp(self)
     if os.path.exists("foo"):
         shutil.rmtree("foo")
     os.makedirs(os.path.join("foo", "pack"))
     self.store = DiskObjectStore("foo")