master
ブランチ master
LittleBeast / src / Std / Lib / Git / PackWriter.php
4.6 KB、116行 生 Blame 履歴
1<?php
2/*************************************************************
3# 076 License
4
5Copyright (c) テクニカル諏訪子
6
7Permission is hereby granted to any person obtaining a copy of the software
8Little Beast (the "Software") to use, modify, merge, copy, publish, distribute,
9sublicense, and/or sell copies of the Software, subject to the following conditions:
10
11 1. **Origin Attribution**:
12 - You must not misrepresent the origin of the Software; you must not claim
13 you created the original Software.
14 - If the Software is used in a product, you must either:
15 a. Provide clear attribution in the product's documentation, user interface,
16 or other visible areas, **OR**
17 b. Pay the original developers a fee they specify in writing.
18 2. **Usage Restriction**:
19 - The Software, or any derivative works, dependencies, or libraries
20 incorporating it, must not be used for censorship or to suppress freedom of
21 speech, expression, or creativity. Prohibited uses include, but are not
22 limited to:
23 - Censorship of so-called "hate speech", visuals, non-mainstream opinions,
24 ideas, or objective reality.
25 - Tools or systems designed to restrict access to information or
26 artistic works.
27 3. **Notice Preservation**:
28 - This license and the above copyright notice must remain intact in all copies
29 of the source code.
30
31THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
32INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
33PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
34HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
35CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
36OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 */
38namespace Std\Lib\Git;
39
40/**
41 * パックを作る(クローン・フェッチで送る)
42 *
43 * 既存のパックの中のオブジェクトは圧縮されたまま再利用する(差分の元も送る場合は差分のまま)。
44 */
45final class PackWriter {
46 private const CHUNK = 65536;
47
48 public function __construct(private Repository $repo) {}
49
50 /** パックのエントリーのヘッダー */
51 public static function entryHeader(int $type, int $size): string {
52 $b = ($type << 4) | ($size & 15);
53 $size >>= 4;
54 $out = '';
55 while ($size > 0) {
56 $out .= chr($b | 0x80);
57 $b = $size & 0x7f;
58 $size >>= 7;
59 }
60 return $out.chr($b);
61 }
62
63 /**
64 * パックを書く
65 *
66 * @param array $objects ID => 種類
67 * @param callable $out fn(string $bytes)
68 * @param callable|null $progress fn(int $done, int $total)
69 * @return string パックのチェックサム(16進数)
70 */
71 public function write(array $objects, callable $out, ?callable $progress = null): string {
72 $ctx = hash_init($this->repo->hashAlgo);
73 $buf = 'PACK'.pack('NN', 2, count($objects));
74 $flush = function(bool $force = false) use (&$buf, $ctx, $out): void {
75 if ($buf === '' || (!$force && strlen($buf) < self::CHUNK)) return;
76 hash_update($ctx, $buf);
77 $out($buf);
78 $buf = '';
79 };
80 $codes = array_flip(Pack::TYPES);
81 $done = 0;
82 $total = count($objects);
83 foreach ($objects as $id => $type) {
84 $loc = $this->repo->objects->locate($id);
85 if ($loc === null) throw new GitException("オブジェクトが有りません:{$id}");
86 $written = false;
87 if ($loc[0] === 'pack') {
88 [$pack, $off] = [$loc[1], $loc[2]];
89 [$t, $size, $dataAt, $baseOff, $baseId] = $pack->header($off);
90 if (isset(Pack::TYPES[$t])) {
91 // そのままコピー
92 $buf .= $pack->raw($off, $pack->entryEnd($off));
93 $written = true;
94 } else {
95 $base = $baseOff !== null ? $pack->idAtOffset($baseOff) : $baseId;
96 if ($base !== null && isset($objects[bin2hex($base)])) {
97 // 元も送るので差分のまま(REF_DELTA)
98 $buf .= self::entryHeader(Pack::REF_DELTA, $size).$base.$pack->raw($dataAt, $pack->entryEnd($off));
99 $written = true;
100 }
101 }
102 }
103 if (!$written) {
104 [$t, $data] = $this->repo->objects->read($id) ?? throw new GitException("オブジェクトが有りません:{$id}");
105 $buf .= self::entryHeader($codes[$t], strlen($data)).gzcompress($data, 6);
106 }
107 $flush();
108 $done++;
109 if ($progress && ($done % 1000 === 0 || $done === $total)) $progress($done, $total);
110 }
111 $flush(true);
112 $sum = hash_final($ctx, true);
113 $out($sum);
114 return bin2hex($sum);
115 }
116}