| 1 | <?php |
| 2 | /************************************************************* |
| 3 | # 076 License |
| 4 | |
| 5 | Copyright (c) テクニカル諏訪子 |
| 6 | |
| 7 | Permission is hereby granted to any person obtaining a copy of the software |
| 8 | Little Beast (the "Software") to use, modify, merge, copy, publish, distribute, |
| 9 | sublicense, 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 | |
| 31 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 32 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 33 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 34 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 35 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE |
| 36 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 37 | */ |
| 38 | namespace Std\Lib\Git; |
| 39 | |
| 40 | /** |
| 41 | * オブジェクトの保存(objects/):ルーズオブジェクトとパック、objects/info/alternates |
| 42 | * |
| 43 | * IDは16進数の文字列。読むと[種類, データ](種類はcommit、tree、blob、tag)。 |
| 44 | */ |
| 45 | final class ObjectDb { |
| 46 | /** @var string[] オブジェクトのディレクトリ(自分とalternates) */ |
| 47 | private array $dirs; |
| 48 | /** @var Pack[] パス => パック */ |
| 49 | private array $packs = []; |
| 50 | private int $scanned = 0; |
| 51 | /** 差分の元のキャッシュ(キー => [種類, データ]) */ |
| 52 | private array $cache = []; |
| 53 | private int $cacheBytes = 0; |
| 54 | private const CACHE_LIMIT = 33554432; |
| 55 | |
| 56 | public function __construct(private Repository $repo) { |
| 57 | $this->dirs = [$repo->path.'/objects']; |
| 58 | $alt = $repo->path.'/objects/info/alternates'; |
| 59 | if (is_file($alt)) { |
| 60 | foreach (preg_split('/\r?\n/', (string)file_get_contents($alt)) as $line) { |
| 61 | $line = trim($line); |
| 62 | if ($line === '' || $line[0] === '#') continue; |
| 63 | $dir = preg_match('#^(/|[A-Za-z]:[/\\\\])#', $line) ? $line : $repo->path.'/objects/'.$line; |
| 64 | if (is_dir($dir)) $this->dirs[] = rtrim($dir, '/\\'); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** @return Pack[] */ |
| 70 | public function packs(bool $rescan = false): array { |
| 71 | if ($rescan || $this->scanned === 0) { |
| 72 | foreach ($this->dirs as $dir) { |
| 73 | foreach (glob($dir.'/pack/pack-*.idx') ?: [] as $idx) { |
| 74 | $pack = substr($idx, 0, -4).'.pack'; |
| 75 | if (!isset($this->packs[$pack]) && is_file($pack)) $this->packs[$pack] = new Pack($pack, $this->repo->hashLen); |
| 76 | } |
| 77 | } |
| 78 | $this->scanned++; |
| 79 | } |
| 80 | return $this->packs; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * オブジェクトの場所 |
| 85 | * |
| 86 | * @return array|null ['loose', パス] 又は ['pack', Pack, オフセット] |
| 87 | */ |
| 88 | public function locate(string $id): ?array { |
| 89 | $id = strtolower($id); |
| 90 | if (!$this->repo->isId($id)) return null; |
| 91 | foreach ($this->dirs as $dir) { |
| 92 | $p = $dir.'/'.substr($id, 0, 2).'/'.substr($id, 2); |
| 93 | if (is_file($p)) return ['loose', $p]; |
| 94 | } |
| 95 | $bin = hex2bin($id); |
| 96 | foreach ([false, true] as $rescan) { |
| 97 | foreach ($this->packs($rescan) as $pack) { |
| 98 | $off = $pack->find($bin); |
| 99 | if ($off !== null) return ['pack', $pack, $off]; |
| 100 | } |
| 101 | } |
| 102 | return null; |
| 103 | } |
| 104 | |
| 105 | public function exists(string $id): bool { |
| 106 | return $this->locate($id) !== null; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * オブジェクトを読む |
| 111 | * |
| 112 | * @return array|null [種類, データ] |
| 113 | */ |
| 114 | public function read(string $id): ?array { |
| 115 | $loc = $this->locate($id); |
| 116 | if ($loc === null) return null; |
| 117 | if ($loc[0] === 'loose') return $this->readLoose($loc[1], $id); |
| 118 | return $this->readPacked($loc[1], $loc[2]); |
| 119 | } |
| 120 | |
| 121 | /** 必ず有るオブジェクト */ |
| 122 | public function get(string $id, ?string $type = null): string { |
| 123 | $o = $this->read($id); |
| 124 | if ($o === null) throw new GitException("オブジェクトが有りません:{$id}"); |
| 125 | if ($type !== null && $o[0] !== $type) throw new GitException("{$id}は{$type}ではなく{$o[0]}です"); |
| 126 | return $o[1]; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * 種類と大きさだけ(データを全部は読まない) |
| 131 | * |
| 132 | * @return array|null [種類, 大きさ] |
| 133 | */ |
| 134 | public function info(string $id): ?array { |
| 135 | $loc = $this->locate($id); |
| 136 | if ($loc === null) return null; |
| 137 | if ($loc[0] === 'loose') { |
| 138 | $ctx = inflate_init(ZLIB_ENCODING_DEFLATE); |
| 139 | $fh = fopen($loc[1], 'rb'); |
| 140 | $head = ''; |
| 141 | while (!str_contains($head, "\0") && !feof($fh)) $head .= (string)@inflate_add($ctx, (string)fread($fh, 256), ZLIB_SYNC_FLUSH); |
| 142 | fclose($fh); |
| 143 | if (!preg_match('/^(commit|tree|blob|tag) (\d+)\0/', $head, $m)) throw new GitException("壊れたオブジェクト:{$id}"); |
| 144 | return [$m[1], (int)$m[2]]; |
| 145 | } |
| 146 | [$type, $size, $dataAt, $baseOff, $baseId] = $loc[1]->header($loc[2]); |
| 147 | if (isset(Pack::TYPES[$type])) return [Pack::TYPES[$type], $size]; |
| 148 | // 差分:大きさは差分の頭、種類は元を辿る |
| 149 | $head = $loc[1]->inflate($dataAt, $size); |
| 150 | $target = Delta::targetSize($head); |
| 151 | $pack = $loc[1]; |
| 152 | $off = $loc[2]; |
| 153 | for ($depth = 0; $depth < 10000; $depth++) { |
| 154 | [$t, , , $bo, $bi] = $pack->header($off); |
| 155 | if (isset(Pack::TYPES[$t])) return [Pack::TYPES[$t], $target]; |
| 156 | if ($bo !== null) { |
| 157 | $off = $bo; |
| 158 | continue; |
| 159 | } |
| 160 | $l = $this->locate(bin2hex($bi)); |
| 161 | if ($l === null) throw new GitException('差分の元が有りません:'.bin2hex($bi)); |
| 162 | if ($l[0] === 'loose') return [$this->info(bin2hex($bi))[0], $target]; |
| 163 | [$pack, $off] = [$l[1], $l[2]]; |
| 164 | } |
| 165 | throw new GitException("差分が深過ぎます:{$id}"); |
| 166 | } |
| 167 | |
| 168 | private function readLoose(string $path, string $id): array { |
| 169 | $raw = @file_get_contents($path); |
| 170 | $data = $raw === false ? false : @zlib_decode($raw); |
| 171 | if ($data === false) throw new GitException("壊れたオブジェクト:{$id}"); |
| 172 | $nul = strpos($data, "\0"); |
| 173 | if ($nul === false || !preg_match('/^(commit|tree|blob|tag) (\d+)$/', substr($data, 0, $nul), $m) || (int)$m[2] !== strlen($data) - $nul - 1) { |
| 174 | throw new GitException("壊れたオブジェクト:{$id}"); |
| 175 | } |
| 176 | return [$m[1], substr($data, $nul + 1)]; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * パックの中のオブジェクト(差分は元を辿って当てる) |
| 181 | * |
| 182 | * @return array [種類, データ] |
| 183 | */ |
| 184 | public function readPacked(Pack $pack, int $offset): array { |
| 185 | $key = $pack->path.':'.$offset; |
| 186 | if (isset($this->cache[$key])) return $this->cache[$key]; |
| 187 | // 差分の連鎖を集める |
| 188 | $chain = []; |
| 189 | $p = $pack; |
| 190 | $off = $offset; |
| 191 | while (true) { |
| 192 | $k = $p->path.':'.$off; |
| 193 | if (isset($this->cache[$k])) { |
| 194 | $base = $this->cache[$k]; |
| 195 | break; |
| 196 | } |
| 197 | [$type, $size, $dataAt, $baseOff, $baseId] = $p->header($off); |
| 198 | if (isset(Pack::TYPES[$type])) { |
| 199 | $base = [Pack::TYPES[$type], $p->inflate($dataAt, $size)]; |
| 200 | $this->remember($k, $base); |
| 201 | break; |
| 202 | } |
| 203 | $chain[] = [$p, $off, $dataAt, $size]; |
| 204 | if (count($chain) > 10000) throw new GitException('差分が深過ぎます'); |
| 205 | if ($baseOff !== null) { |
| 206 | $off = $baseOff; |
| 207 | continue; |
| 208 | } |
| 209 | $hex = bin2hex($baseId); |
| 210 | $o = $p->find($baseId); |
| 211 | if ($o !== null) { |
| 212 | $off = $o; |
| 213 | continue; |
| 214 | } |
| 215 | $l = $this->locate($hex); |
| 216 | if ($l === null) throw new GitException("差分の元が有りません:{$hex}"); |
| 217 | if ($l[0] === 'loose') { |
| 218 | $base = $this->readLoose($l[1], $hex); |
| 219 | break; |
| 220 | } |
| 221 | [$p, $off] = [$l[1], $l[2]]; |
| 222 | } |
| 223 | for ($i = count($chain) - 1; $i >= 0; $i--) { |
| 224 | [$cp, $coff, $dataAt, $size] = $chain[$i]; |
| 225 | $base = [$base[0], Delta::apply($base[1], $cp->inflate($dataAt, $size))]; |
| 226 | $this->remember($cp->path.':'.$coff, $base); |
| 227 | } |
| 228 | return $base; |
| 229 | } |
| 230 | |
| 231 | private function remember(string $key, array $obj): void { |
| 232 | $n = strlen($obj[1]); |
| 233 | if ($n > self::CACHE_LIMIT / 4) return; |
| 234 | while ($this->cacheBytes + $n > self::CACHE_LIMIT && !empty($this->cache)) { |
| 235 | $k = array_key_first($this->cache); |
| 236 | $this->cacheBytes -= strlen($this->cache[$k][1]); |
| 237 | unset($this->cache[$k]); |
| 238 | } |
| 239 | $this->cache[$key] = $obj; |
| 240 | $this->cacheBytes += $n; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * ルーズオブジェクトとして書く(既に有れば何もしない) |
| 245 | * |
| 246 | * @return string ID |
| 247 | */ |
| 248 | public function write(string $type, string $data): string { |
| 249 | if (!in_array($type, ['commit', 'tree', 'blob', 'tag'], true)) throw new GitException("不正な種類:{$type}"); |
| 250 | $id = $this->repo->hashObject($type, $data); |
| 251 | if ($this->exists($id)) return $id; |
| 252 | $dir = $this->dirs[0].'/'.substr($id, 0, 2); |
| 253 | $this->repo->mkdir($dir); |
| 254 | $level = $this->repo->config->getInt('core.loosecompression', $this->repo->config->getInt('core.compression', -1)); |
| 255 | $this->repo->writeFile($dir.'/'.substr($id, 2), (string)gzcompress($type.' '.strlen($data)."\0".$data, max(-1, min(9, $level))), true); |
| 256 | return $id; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * 短いIDを解決する |
| 261 | * |
| 262 | * @return string|null 一意に決まれば完全なID |
| 263 | */ |
| 264 | public function resolvePrefix(string $prefix): ?string { |
| 265 | $prefix = strtolower($prefix); |
| 266 | if (!preg_match('/^[0-9a-f]{4,}$/', $prefix) || strlen($prefix) > $this->repo->hexLen) return null; |
| 267 | if (strlen($prefix) === $this->repo->hexLen) return $this->exists($prefix) ? $prefix : null; |
| 268 | $found = []; |
| 269 | foreach ($this->dirs as $dir) { |
| 270 | foreach (glob($dir.'/'.substr($prefix, 0, 2).'/'.substr($prefix, 2).'*') ?: [] as $f) $found[substr($prefix, 0, 2).basename($f)] = true; |
| 271 | } |
| 272 | foreach ($this->packs(true) as $pack) foreach ($pack->findPrefix($prefix) as $id) $found[$id] = true; |
| 273 | return count($found) === 1 ? (string)array_key_first($found) : null; |
| 274 | } |
| 275 | |
| 276 | /** 全部のID(重複あり得る) */ |
| 277 | public function ids(): \Generator { |
| 278 | foreach ($this->dirs as $dir) { |
| 279 | foreach (glob($dir.'/[0-9a-f][0-9a-f]/*') ?: [] as $f) { |
| 280 | $id = basename(dirname($f)).basename($f); |
| 281 | if ($this->repo->isId($id)) yield $id; |
| 282 | } |
| 283 | } |
| 284 | foreach ($this->packs(true) as $pack) yield from $pack->ids(); |
| 285 | } |
| 286 | |
| 287 | public function dir(): string { |
| 288 | return $this->dirs[0]; |
| 289 | } |
| 290 | } |