| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9729ed3 commit 93677a0
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,10 +5,14 @@ | |||
| 5 | 5 | ||
| 6 | 6 | __all__ = ["GitCmdObjectDB", "GitDB"] | |
| 7 | 7 | ||
| 8 | - from gitdb.base import OInfo, OStream | ||
| 8 | + from subprocess import PIPE | ||
| 9 | + | ||
| 10 | + from gitdb.base import IStream, OInfo, OStream | ||
| 9 | 11 | from gitdb.db import GitDB, LooseObjectDB | |
| 10 | 12 | from gitdb.exc import BadObject | |
| 13 | + from gitdb.fun import stream_copy | ||
| 11 | 14 | ||
| 15 | + from git.compat import force_text | ||
| 12 | 16 | from git.util import bin_to_hex, hex_to_bin | |
| 13 | 17 | from git.exc import GitCommandError | |
| 14 | 18 | ||
@@ -46,6 +50,25 @@ def stream(self, binsha: bytes) -> OStream: | |||
| 46 | 50 | hexsha, typename, size, stream = self._git.stream_object_data(bin_to_hex(binsha)) | |
| 47 | 51 | return OStream(hex_to_bin(hexsha), typename, size, stream) | |
| 48 | 52 | ||
| 53 | + def store(self, istream: IStream) -> IStream: | ||
| 54 | + """Store an object using git itself.""" | ||
| 55 | + if istream.binsha is not None or self.ostream() is not None: | ||
| 56 | + return super().store(istream) | ||
| 57 | + | ||
| 58 | + proc = self._git.hash_object( | ||
| 59 | + "-t", force_text(istream.type), "-w", "--stdin", "--literally", as_process=True, istream=PIPE | ||
| 60 | + ) | ||
| 61 | + assert proc.stdin is not None | ||
| 62 | + try: | ||
| 63 | + stream_copy(istream.read, proc.stdin.write, istream.size, self.stream_chunk_size) | ||
| 64 | + finally: | ||
| 65 | + proc.stdin.close() | ||
| 66 | + assert proc.stdout is not None | ||
| 67 | + hexsha = proc.stdout.read().strip() | ||
| 68 | + proc.wait() | ||
| 69 | + istream.binsha = hex_to_bin(hexsha) | ||
| 70 | + return istream | ||
| 71 | + | ||
| 49 | 72 | # { Interface | |
| 50 | 73 | ||
| 51 | 74 | def partial_to_complete_sha_hex(self, partial_hexsha: str) -> bytes: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,16 +3,30 @@ | |||
| 3 | 3 | # This module is part of GitPython and is released under the | |
| 4 | 4 | # 3-Clause BSD License: https://opensource.org/license/bsd-3-clause/ | |
| 5 | 5 | ||
| 6 | + from io import BytesIO | ||
| 6 | 7 | import os.path as osp | |
| 8 | + from unittest import mock | ||
| 9 | + | ||
| 10 | + from gitdb import IStream | ||
| 11 | + from gitdb.db import LooseObjectDB | ||
| 12 | + from gitdb.typ import str_blob_type | ||
| 7 | 13 | ||
| 8 | 14 | from git.db import GitCmdObjectDB | |
| 9 | 15 | from git.exc import BadObject | |
| 10 | 16 | from git.util import bin_to_hex | |
| 11 | 17 | ||
| 12 | - from test.lib import TestBase | ||
| 18 | + from test.lib import TestBase, with_rw_repo | ||
| 13 | 19 | ||
| 14 | 20 | ||
| 15 | 21 | class TestDB(TestBase): | |
| 22 | + @with_rw_repo("HEAD") | ||
| 23 | + def test_store_uses_hash_object(self, rw_repo): | ||
| 24 | + data = b"hello world" | ||
| 25 | + with mock.patch.object(LooseObjectDB, "store", side_effect=AssertionError("unexpected loose-object write")): | ||
| 26 | + istream = rw_repo.odb.store(IStream(str_blob_type, len(data), BytesIO(data))) | ||
| 27 | + | ||
| 28 | + assert rw_repo.odb.stream(istream.binsha).read() == data | ||
| 29 | + | ||
| 16 | 30 | def test_base(self): | |
| 17 | 31 | gdb = GitCmdObjectDB(osp.join(self.rorepo.git_dir, "objects"), self.rorepo.git) | |
| 18 | 32 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,7 +26,7 @@ | |||
| 26 | 26 | from git.exc import UnsafeOptionError | |
| 27 | 27 | from git.objects.tag import TagObject | |
| 28 | 28 | import git.refs as refs | |
| 29 | - from git.util import Actor | ||
| 29 | + from git.util import Actor, rmtree | ||
| 30 | 30 | ||
| 31 | 31 | from test.lib import TestBase, requires_symlinks, with_rw_repo, PathLikeMock | |
| 32 | 32 | ||
@@ -43,6 +43,7 @@ def _repo_with_initial_commit(self, base_dir): | |||
| 43 | 43 | yield repo | |
| 44 | 44 | finally: | |
| 45 | 45 | repo.git.clear_cache() | |
| 46 | + rmtree(repo_dir) | ||
| 46 | 47 | ||
| 47 | 48 | def test_from_path(self): | |
| 48 | 49 | # Should be able to create any reference directly. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments