master
ブランチ master
LittleBeast / src / Std / Lib / Git / Maintenance.php
5 KB、110行 生 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 * リポジトリの整理(git gc の基本):到達出来るオブジェクトを1つのパックにまとめ、古い不要なファイルを消す
42 *
43 * プッシュで増えたルーズオブジェクトと小さいパックをまとめる。cronで定期的に実行すると良い。
44 * 新しい差分は作らない(既存のパックの差分は再利用する)。
45 */
46final class Maintenance {
47 /**
48 * @param int $pruneAge 到達出来ないオブジェクト・パックを消すまでの秒数(既定は2週間、受信中の物を消さない様に)
49 * @return array objects(パックにしたオブジェクトの数)、removedLoose、removedPacks
50 */
51 public static function gc(Repository $repo, int $pruneAge = 1209600): array {
52 $lock = $repo->path.'/gc.pid';
53 $fh = @fopen($lock, 'c');
54 if ($fh === false || !flock($fh, LOCK_EX | LOCK_NB)) throw new GitException('別のgcが実行中です');
55 try {
56 $tips = array_values($repo->refs->all());
57 $head = $repo->refs->resolve('HEAD');
58 if ($head !== null) $tips[] = $head;
59 $tips = array_values(array_unique($tips));
60 $objects = empty($tips) ? [] : (new Walk($repo))->objects($tips, []);
61 $old = $repo->objects->packs(true);
62 $newPack = null;
63 if (!empty($objects)) {
64 $tmp = $repo->objects->dir().'/pack/tmp_gc_'.bin2hex(random_bytes(6));
65 $out = fopen($tmp, 'w+b');
66 try {
67 (new PackWriter($repo))->write($objects, fn(string $b) => fwrite($out, $b));
68 rewind($out);
69 $res = (new PackIndexer($repo))->receive($out, 0);
70 $newPack = $res['pack'];
71 } finally {
72 fclose($out);
73 @unlink($tmp);
74 }
75 }
76 $cutoff = time() - $pruneAge;
77 $removedPacks = 0;
78 foreach ($old as $path => $pack) {
79 if ($path === $newPack || !str_starts_with($path, $repo->objects->dir().'/')) continue;
80 // 全部が新しいパックに有るか、古い(到達出来ない物だけ)なら消す
81 $all = true;
82 foreach ($pack->ids() as $id) if (!isset($objects[$id])) {
83 $all = false;
84 break;
85 }
86 if (!$all && filemtime($path) >= $cutoff) continue;
87 if (is_file(substr($path, 0, -5).'.keep')) continue;
88 unset($pack);
89 foreach (['.idx', '.rev', '.pack'] as $ext) @unlink(substr($path, 0, -5).$ext);
90 $removedPacks++;
91 }
92 $removedLoose = 0;
93 foreach (glob($repo->objects->dir().'/[0-9a-f][0-9a-f]/*') ?: [] as $f) {
94 $id = basename(dirname($f)).basename($f);
95 if (!$repo->isId($id)) continue;
96 if (isset($objects[$id]) || filemtime($f) < $cutoff) {
97 if (@unlink($f)) $removedLoose++;
98 }
99 }
100 foreach (glob($repo->objects->dir().'/[0-9a-f][0-9a-f]', GLOB_ONLYDIR) ?: [] as $d) @rmdir($d);
101 // 残った一時ファイル
102 foreach (glob($repo->objects->dir().'/pack/tmp_*') ?: [] as $f) if (filemtime($f) < $cutoff) @unlink($f);
103 return ['objects' => count($objects), 'removedLoose' => $removedLoose, 'removedPacks' => $removedPacks];
104 } finally {
105 flock($fh, LOCK_UN);
106 fclose($fh);
107 @unlink($lock);
108 }
109 }
110}