| 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 | * ツリー(ディレクトリ) |
| 42 | * |
| 43 | * entriesは[mode, name, id](idは16進数)。modeは「100644」「100755」「120000」(シンボリックリンク)、 |
| 44 | * 「40000」(ツリー)、「160000」(サブモジュール)。 |
| 45 | */ |
| 46 | final class Tree { |
| 47 | public const FILE = '100644'; |
| 48 | public const EXEC = '100755'; |
| 49 | public const LINK = '120000'; |
| 50 | public const DIR = '40000'; |
| 51 | public const SUBMODULE = '160000'; |
| 52 | |
| 53 | /** @var array [mode, name, id]の配列(Gitの順番) */ |
| 54 | public array $entries = []; |
| 55 | public ?string $id = null; |
| 56 | |
| 57 | public static function parse(string $data, int $hashLen, ?string $id = null): self { |
| 58 | $t = new self(); |
| 59 | $t->id = $id; |
| 60 | $len = strlen($data); |
| 61 | $pos = 0; |
| 62 | while ($pos < $len) { |
| 63 | $sp = strpos($data, ' ', $pos); |
| 64 | $nul = $sp === false ? false : strpos($data, "\0", $sp); |
| 65 | if ($nul === false || $nul + 1 + $hashLen > $len) throw new GitException('壊れたツリー'.($id ? ":{$id}" : '')); |
| 66 | $t->entries[] = [substr($data, $pos, $sp - $pos), substr($data, $sp + 1, $nul - $sp - 1), bin2hex(substr($data, $nul + 1, $hashLen))]; |
| 67 | $pos = $nul + 1 + $hashLen; |
| 68 | } |
| 69 | return $t; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * エントリーからツリーを作る(Gitの順番に並べる) |
| 74 | * |
| 75 | * @param array $entries [mode, name, id] |
| 76 | */ |
| 77 | public static function create(array $entries): self { |
| 78 | $t = new self(); |
| 79 | foreach ($entries as [$mode, $name, $id]) { |
| 80 | if ($name === '' || $name === '.' || $name === '..' || str_contains($name, '/') || str_contains($name, "\0")) throw new GitException("不正なファイル名:{$name}"); |
| 81 | $t->entries[] = [ltrim((string)$mode, '0') ?: '0', $name, strtolower($id)]; |
| 82 | } |
| 83 | usort($t->entries, fn($a, $b) => strcmp(self::sortKey($a), self::sortKey($b))); |
| 84 | for ($i = 1; $i < count($t->entries); $i++) { |
| 85 | if ($t->entries[$i][1] === $t->entries[$i - 1][1]) throw new GitException("同じ名前が2つ有ります:{$t->entries[$i][1]}"); |
| 86 | } |
| 87 | return $t; |
| 88 | } |
| 89 | |
| 90 | /** ディレクトリは名前の後に「/」が有るとして並べる */ |
| 91 | private static function sortKey(array $e): string { |
| 92 | return $e[0] === self::DIR ? $e[1].'/' : $e[1]; |
| 93 | } |
| 94 | |
| 95 | public function toString(): string { |
| 96 | $out = ''; |
| 97 | foreach ($this->entries as [$mode, $name, $id]) $out .= "{$mode} {$name}\0".hex2bin($id); |
| 98 | return $out; |
| 99 | } |
| 100 | |
| 101 | /** 名前のエントリー */ |
| 102 | public function find(string $name): ?array { |
| 103 | foreach ($this->entries as $e) if ($e[1] === $name) return $e; |
| 104 | return null; |
| 105 | } |
| 106 | |
| 107 | public static function isTree(string $mode): bool { |
| 108 | return $mode === self::DIR; |
| 109 | } |
| 110 | } |