| 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 | * 行毎の最後に変更したコミット(git blame) |
| 42 | * |
| 43 | * 名前の変更は辿らない。マージは、同じ行が有る最初の親から辿る。 |
| 44 | */ |
| 45 | final class Blame { |
| 46 | /** |
| 47 | * @param string $rev リビジョン |
| 48 | * @param string $path ファイルのパス |
| 49 | * @param int $maxCommits 調べるコミットの最大(超えたら残りの行はそのコミット) |
| 50 | * @return array 行毎に ['commit' => ID, 'line' => そのコミットでの行の番号(1から), 'text' => 行] |
| 51 | */ |
| 52 | public static function file(Repository $repo, string $rev, string $path, int $maxCommits = 10000): array { |
| 53 | $start = $repo->resolve($rev); |
| 54 | $start = $start === null ? null : $repo->peel($start, 'commit'); |
| 55 | if ($start === null) throw new GitException("リビジョンが有りません:{$rev}"); |
| 56 | $path = trim($path, '/'); |
| 57 | $entry = $repo->entry($start, $path); |
| 58 | if ($entry === null || $entry[0] === Tree::DIR) throw new GitException("ファイルが有りません:{$path}"); |
| 59 | $lines = Diff::split($repo->blob($entry[2])); |
| 60 | $result = array_fill(0, count($lines), null); |
| 61 | // コミット => [ブロブのID, [そのコミットでの行の番号 => 最後の行の番号]] |
| 62 | $pending = [$start => [$entry[2], array_combine(array_keys($lines), array_keys($lines))]]; |
| 63 | $queue = new \SplPriorityQueue(); |
| 64 | $seq = PHP_INT_MAX; |
| 65 | $queue->insert($start, [$repo->commit($start)->committer?->time ?? 0, $seq--]); |
| 66 | $blobs = []; |
| 67 | $split = function(string $id) use ($repo, &$blobs): array { |
| 68 | return $blobs[$id] ??= Diff::split($repo->blob($id)); |
| 69 | }; |
| 70 | $done = 0; |
| 71 | while (!$queue->isEmpty()) { |
| 72 | $id = $queue->extract(); |
| 73 | if (!isset($pending[$id])) continue; |
| 74 | [$blob, $map] = $pending[$id]; |
| 75 | unset($pending[$id]); |
| 76 | $c = $repo->commit($id); |
| 77 | if (++$done > $maxCommits) { |
| 78 | foreach ($map as $cur => $final) $result[$final] = [$id, $cur]; |
| 79 | continue; |
| 80 | } |
| 81 | foreach ($c->parents as $p) { |
| 82 | if (empty($map)) break; |
| 83 | if (!$repo->objects->exists($p)) continue; |
| 84 | $pe = $repo->entry($p, $path); |
| 85 | if ($pe === null || $pe[0] === Tree::DIR) continue; |
| 86 | if ($pe[2] === $blob) { |
| 87 | // 同じ内容:全部を親に |
| 88 | self::pass($repo, $pending, $queue, $seq, $p, $pe[2], $map); |
| 89 | $map = []; |
| 90 | break; |
| 91 | } |
| 92 | $mine = $split($blob); |
| 93 | $theirs = $split($pe[2]); |
| 94 | $passed = []; |
| 95 | foreach (Diff::match($theirs, $mine) as [$pi, $ci]) { |
| 96 | if (isset($map[$ci])) { |
| 97 | $passed[$pi] = $map[$ci]; |
| 98 | unset($map[$ci]); |
| 99 | } |
| 100 | } |
| 101 | if (!empty($passed)) self::pass($repo, $pending, $queue, $seq, $p, $pe[2], $passed); |
| 102 | } |
| 103 | foreach ($map as $cur => $final) $result[$final] = [$id, $cur]; |
| 104 | } |
| 105 | $out = []; |
| 106 | foreach ($lines as $i => $text) $out[] = ['commit' => $result[$i][0], 'line' => $result[$i][1] + 1, 'text' => $text]; |
| 107 | return $out; |
| 108 | } |
| 109 | |
| 110 | private static function pass(Repository $repo, array &$pending, \SplPriorityQueue $queue, int &$seq, string $p, string $blob, array $map): void { |
| 111 | if (isset($pending[$p])) { |
| 112 | $pending[$p][1] += $map; |
| 113 | return; |
| 114 | } |
| 115 | $pending[$p] = [$blob, $map]; |
| 116 | $queue->insert($p, [$repo->commit($p)->committer?->time ?? 0, $seq--]); |
| 117 | } |
| 118 | } |