|
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: |
|
0 commit comments