tazuna 0.23.0 → 0.27.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -3
- package/dist/assets/protocol.js +9 -4
- package/dist/cli.js +6 -4
- package/dist/commands/apply.js +32 -7
- package/dist/commands/commit-apply.js +65 -29
- package/dist/commands/commit.js +24 -6
- package/dist/commands/show.js +59 -7
- package/dist/commands/watch-pack.js +145 -0
- package/dist/commands/watch.js +283 -13
- package/dist/core/commit-message.js +42 -2
- package/dist/core/diff.js +16 -0
- package/dist/core/watch-keys.js +30 -0
- package/dist/infra/git.js +60 -5
- package/dist/infra/history.js +25 -11
- package/dist/infra/watch-keys.js +46 -0
- package/dist/infra/watch-lock.js +180 -0
- package/package.json +1 -1
package/dist/infra/git.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { execFile, spawnSync } from "node:child_process";
|
|
2
2
|
import { randomBytes } from "node:crypto";
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
|
-
import { join } from "node:path";
|
|
4
|
+
import { join, posix } from "node:path";
|
|
5
5
|
import { promisify } from "node:util";
|
|
6
6
|
import { TOOL_NAME } from "../brand.js";
|
|
7
7
|
const execFileP = promisify(execFile);
|
|
@@ -78,6 +78,23 @@ function runGit(cwd, args, noHooksDir, input) {
|
|
|
78
78
|
};
|
|
79
79
|
}
|
|
80
80
|
const nulList = (out) => out.toString("utf8").split("\0").filter((p) => p !== "");
|
|
81
|
+
/** 存在しないディレクトリ (乱数入り)。フックの置き場に指定して、どのフックも見つからないようにする */
|
|
82
|
+
const noHooksDirectory = () => join(tmpdir(), `${TOOL_NAME}-no-hooks-${randomBytes(8).toString("hex")}`);
|
|
83
|
+
/**
|
|
84
|
+
* manifest のパスを git が返す形にそろえる (changes.md に `./a.txt`・`sub//a.txt` と書かれていても、git は `a.txt`・
|
|
85
|
+
* `sub/a.txt` と返す)
|
|
86
|
+
*/
|
|
87
|
+
const gitPathName = (path) => posix.normalize(path);
|
|
88
|
+
/**
|
|
89
|
+
* git が返した一覧のうち、指定したファイルそのものだけ。manifest は非信頼入力で、`.` やディレクトリの名前を
|
|
90
|
+
* 書けば git はその下のファイルすべてに当てはめる (未追跡のものまで git add してしまう) ため、名前の一致で絞る。
|
|
91
|
+
* 大文字小文字は、リポジトリが区別しない (core.ignorecase。Windows・macOS の既定) ときだけ同一視する
|
|
92
|
+
*/
|
|
93
|
+
function onlyNamed(listed, paths, ignoreCase) {
|
|
94
|
+
const key = (p) => (ignoreCase ? p.toLowerCase() : p);
|
|
95
|
+
const wanted = new Set(paths.map((p) => key(gitPathName(p))));
|
|
96
|
+
return listed.filter((p) => wanted.has(key(p)));
|
|
97
|
+
}
|
|
81
98
|
/** git の出力の先頭の数行 (失敗の理由として見せる) */
|
|
82
99
|
const firstLines = (text, n = 8) => text
|
|
83
100
|
.split(/\r?\n/)
|
|
@@ -96,8 +113,7 @@ const firstLines = (text, n = 8) => text
|
|
|
96
113
|
export function commitPaths(rootDir, paths, message) {
|
|
97
114
|
if (paths.length === 0)
|
|
98
115
|
return { kind: "nothing" };
|
|
99
|
-
|
|
100
|
-
const noHooksDir = join(tmpdir(), `${TOOL_NAME}-no-hooks-${randomBytes(8).toString("hex")}`);
|
|
116
|
+
const noHooksDir = noHooksDirectory();
|
|
101
117
|
const git = (args, input) => runGit(rootDir, args, noHooksDir, input);
|
|
102
118
|
const inside = git(["rev-parse", "--is-inside-work-tree"]);
|
|
103
119
|
if (inside.error !== undefined) {
|
|
@@ -114,8 +130,9 @@ export function commitPaths(rootDir, paths, message) {
|
|
|
114
130
|
if (tracked.status !== 0 || untracked.status !== 0) {
|
|
115
131
|
return { kind: "failed", message: firstLines(tracked.stderr || untracked.stderr) || "git ls-files に失敗しました" };
|
|
116
132
|
}
|
|
117
|
-
const
|
|
118
|
-
const
|
|
133
|
+
const ignoreCase = git(["config", "--bool", "core.ignorecase"]).stdout.toString("utf8").trim() === "true";
|
|
134
|
+
const added = onlyNamed(nulList(untracked.stdout), paths, ignoreCase);
|
|
135
|
+
const targets = [...new Set([...onlyNamed(nulList(tracked.stdout), paths, ignoreCase), ...added])];
|
|
119
136
|
if (targets.length === 0)
|
|
120
137
|
return { kind: "nothing" };
|
|
121
138
|
if (added.length > 0) {
|
|
@@ -140,6 +157,44 @@ export function commitPaths(rootDir, paths, message) {
|
|
|
140
157
|
const head = git(["rev-parse", "HEAD"]);
|
|
141
158
|
return { kind: "committed", sha: head.stdout.toString("utf8").trim(), files: targets };
|
|
142
159
|
}
|
|
160
|
+
/**
|
|
161
|
+
* 指定したファイルのうち、HEAD と内容の違うもの (未追跡で .gitignore に一致しないものを含む) を見分ける関数。
|
|
162
|
+
* まとめてコミットするときに、git で元に戻した回を見分ける。git が無い・リポジトリでなければ null。
|
|
163
|
+
* 大文字小文字は区別しない (違うと見る側に倒れるだけ = 回を外さない)
|
|
164
|
+
*/
|
|
165
|
+
export function changedPaths(rootDir, paths) {
|
|
166
|
+
const key = (p) => gitPathName(p).toLowerCase();
|
|
167
|
+
if (paths.length === 0)
|
|
168
|
+
return () => false;
|
|
169
|
+
const noHooksDir = noHooksDirectory();
|
|
170
|
+
// git status はリポジトリの最上位からのパスで返すので、ルートがその下のフォルダならその分を外す
|
|
171
|
+
const prefix = runGit(rootDir, ["rev-parse", "--show-prefix"], noHooksDir);
|
|
172
|
+
if (prefix.error !== undefined || prefix.status !== 0)
|
|
173
|
+
return null;
|
|
174
|
+
const base = prefix.stdout.toString("utf8").trim();
|
|
175
|
+
// --untracked-files=all: 利用者の設定 (status.showUntrackedFiles=no など) によらず、作ったファイルも見る
|
|
176
|
+
const status = runGit(rootDir, ["status", "--porcelain", "-z", "--untracked-files=all", "--", ...paths], noHooksDir);
|
|
177
|
+
if (status.error !== undefined || status.status !== 0)
|
|
178
|
+
return null;
|
|
179
|
+
const changed = new Set();
|
|
180
|
+
const add = (fromTop) => {
|
|
181
|
+
if (fromTop.startsWith(base))
|
|
182
|
+
changed.add(key(fromTop.slice(base.length)));
|
|
183
|
+
};
|
|
184
|
+
const entries = nulList(status.stdout);
|
|
185
|
+
for (let i = 0; i < entries.length; i++) {
|
|
186
|
+
const entry = entries[i];
|
|
187
|
+
add(entry.slice(3));
|
|
188
|
+
// 名前の変更 (R) と写し (C) は、次の項目が元の名前 (索引側・作業ツリー側のどちらの欄でも)
|
|
189
|
+
if (/[RC]/.test(entry.slice(0, 2))) {
|
|
190
|
+
const from = entries[i + 1];
|
|
191
|
+
if (from !== undefined)
|
|
192
|
+
add(from);
|
|
193
|
+
i++;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return (path) => changed.has(key(path));
|
|
197
|
+
}
|
|
143
198
|
/**
|
|
144
199
|
* 今の HEAD のコミット。コミットがまだ無いリポジトリでは null、git が無い・リポジトリでなければ undefined。
|
|
145
200
|
* 適用のときに manifest に残し、次の適用の前のコミットで「その間に利用者がコミットしたか」を見分ける
|
package/dist/infra/history.js
CHANGED
|
@@ -66,12 +66,17 @@ export function listHistoryIds(root) {
|
|
|
66
66
|
.map((e) => e.name)
|
|
67
67
|
.sort();
|
|
68
68
|
}
|
|
69
|
-
/**
|
|
69
|
+
/**
|
|
70
|
+
* historyLimit を超えた古い履歴を削除する (§10)。null = 無制限。
|
|
71
|
+
* まだコミットしていない適用 (tazuna commit がまとめる分。findUncommittedApplies) は、上限を超えてもコミットするまで
|
|
72
|
+
* 残す (消すと、その回で書いたファイルが tazuna commit から黙って漏れる)
|
|
73
|
+
*/
|
|
70
74
|
export function pruneHistory(root, limit) {
|
|
71
75
|
if (limit === null || limit < 1)
|
|
72
76
|
return [];
|
|
73
77
|
const ids = listHistoryIds(root);
|
|
74
|
-
const
|
|
78
|
+
const keep = new Set(findUncommittedApplies(root).map((f) => f.id));
|
|
79
|
+
const excess = ids.slice(0, Math.max(0, ids.length - limit)).filter((id) => !keep.has(id));
|
|
75
80
|
for (const id of excess) {
|
|
76
81
|
rmSync(join(historyRoot(root), id), { recursive: true, force: true });
|
|
77
82
|
}
|
|
@@ -98,30 +103,39 @@ export function markCommitted(root, id, sha, at) {
|
|
|
98
103
|
writeFileSync(p, JSON.stringify(manifest, null, 2) + "\n", "utf8");
|
|
99
104
|
}
|
|
100
105
|
/**
|
|
101
|
-
*
|
|
102
|
-
* (
|
|
103
|
-
*
|
|
106
|
+
* まだコミットしていない適用 (古い順)。巻き戻していない直近の履歴から遡り、同じコミットの上で当てたもの
|
|
107
|
+
* (適用したときの HEAD が直近の適用と同じ) を集める。コミット済みの履歴・HEAD が違う履歴 (その間に利用者が
|
|
108
|
+
* コミットした・ブランチを変えた)・HEAD の記録がない履歴 (0.23.0 より前の版で当てた・git の外) の手前で止める
|
|
109
|
+
* (直近の 1 件は HEAD の記録がなくても含める)。manifest は非信頼入力なので、読めない・形が違う履歴に当たったら
|
|
110
|
+
* そこで止める (推測で含めない)
|
|
104
111
|
*/
|
|
105
|
-
export function
|
|
112
|
+
export function findUncommittedApplies(root) {
|
|
113
|
+
const found = [];
|
|
106
114
|
for (const id of listHistoryIds(root).reverse()) {
|
|
107
115
|
let manifest;
|
|
108
116
|
try {
|
|
109
117
|
manifest = readManifest(root, id);
|
|
110
118
|
}
|
|
111
119
|
catch {
|
|
112
|
-
|
|
120
|
+
break;
|
|
113
121
|
}
|
|
114
122
|
if (manifest === null || typeof manifest !== "object" || !Array.isArray(manifest.files))
|
|
115
|
-
|
|
123
|
+
break;
|
|
116
124
|
if (manifest.undoneAt !== undefined)
|
|
117
125
|
continue;
|
|
118
126
|
if (manifest.committed !== undefined)
|
|
119
|
-
|
|
127
|
+
break;
|
|
120
128
|
if (!manifest.files.some((f) => f?.applied === true))
|
|
121
129
|
continue;
|
|
122
|
-
|
|
130
|
+
const newest = found[0];
|
|
131
|
+
if (newest !== undefined) {
|
|
132
|
+
const base = newest.manifest.gitHead;
|
|
133
|
+
if (base === undefined || manifest.gitHead !== base)
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
found.push({ id, manifest });
|
|
123
137
|
}
|
|
124
|
-
return
|
|
138
|
+
return found.reverse();
|
|
125
139
|
}
|
|
126
140
|
/**
|
|
127
141
|
* 同じ指紋の changes.md を適用した履歴 (巻き戻し済み・書き込みなしを除く) を新しい順に
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tazuna watch の画面で打ったコマンドを読む (1 文字 + Enter。解釈は core/watch-keys.ts)。
|
|
3
|
+
*
|
|
4
|
+
* readline を terminal: false で使い、端末の行入力 (Windows のコンソールの行編集・エコー、Unix の canonical モード) に
|
|
5
|
+
* 任せる。raw モードにしないので Ctrl+C はこれまでどおり SIGINT になり、Git Bash (mintty) のように標準入力が
|
|
6
|
+
* パイプの端末でも同じに動く。短い間隔でまとめて届いた行は 1 回の入力として渡す (画面への貼り付けを見分けるため)。
|
|
7
|
+
*/
|
|
8
|
+
import { createInterface } from "node:readline";
|
|
9
|
+
import { interpretWatchInput } from "../core/watch-keys.js";
|
|
10
|
+
/** 貼り付けを 1 回の入力とみなす間隔 (ミリ秒)。人が打つ Enter の間隔より十分短く、貼り付けの行の間隔より長い */
|
|
11
|
+
const BURST_MS = 50;
|
|
12
|
+
export function listenWatchInput(input, onInput, burstMs = BURST_MS) {
|
|
13
|
+
const rl = createInterface({ input, terminal: false, crlfDelay: Infinity });
|
|
14
|
+
let pending = [];
|
|
15
|
+
let timer;
|
|
16
|
+
const flush = () => {
|
|
17
|
+
timer = undefined;
|
|
18
|
+
const lines = pending;
|
|
19
|
+
pending = [];
|
|
20
|
+
onInput(interpretWatchInput(lines));
|
|
21
|
+
};
|
|
22
|
+
rl.on("line", (line) => {
|
|
23
|
+
pending.push(line);
|
|
24
|
+
if (timer !== undefined)
|
|
25
|
+
clearTimeout(timer);
|
|
26
|
+
timer = setTimeout(flush, burstMs);
|
|
27
|
+
});
|
|
28
|
+
// 標準入力が閉じても (パイプの終わり・/dev/null) watch は続ける。コマンドが打てなくなるだけ
|
|
29
|
+
rl.on("close", () => {
|
|
30
|
+
if (timer !== undefined) {
|
|
31
|
+
clearTimeout(timer);
|
|
32
|
+
flush();
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
return {
|
|
36
|
+
close: () => {
|
|
37
|
+
if (timer !== undefined)
|
|
38
|
+
clearTimeout(timer);
|
|
39
|
+
timer = undefined;
|
|
40
|
+
pending = [];
|
|
41
|
+
rl.removeAllListeners("line");
|
|
42
|
+
rl.removeAllListeners("close");
|
|
43
|
+
rl.close();
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tazuna watch の印 (同じダウンロードフォルダを見る watch を 1 つにする。§4.7)。
|
|
3
|
+
*
|
|
4
|
+
* 同じ Downloads を 2 つの watch が見ていると、届いた 1 つの changes.md を両方が同時に読み、
|
|
5
|
+
* 両方のリポジトリに当ててしまう (2026-11-14 利用者の事例。同じプロジェクトの 2 か所で watch を起動していた)。
|
|
6
|
+
* そこで watch は始めるときに一時フォルダへ印 (リポジトリ・PID・開始時刻・その watch だけが知る合言葉) を書き、
|
|
7
|
+
* 前の印を置き換える。前の watch は約 1 秒ごとに印を読み、自分の合言葉でなくなっていたら終了する
|
|
8
|
+
* (後から始めた watch に切り替わる)。
|
|
9
|
+
*
|
|
10
|
+
* - 印は置き換えるだけなので、ウィンドウを閉じて残った印のせいで watch を始められなくなることはない
|
|
11
|
+
* - 印の場所は一時フォルダ (Windows は %TEMP%)。Downloads のフォルダごとに 1 つ (名前はフォルダのパスのハッシュ)
|
|
12
|
+
* - 動いている watch は約 10 秒ごとに印の更新時刻を新しくする。前の watch がまだ動いているか (切り替えたことを
|
|
13
|
+
* 表示するか) は、PID の存在と更新時刻で見る (PID は使い回されることがあるため)
|
|
14
|
+
* - 置き換えは別名に書いてから rename (途中の印を読ませない。既にあるものが symlink でもその先を書き換えない)
|
|
15
|
+
*/
|
|
16
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
17
|
+
import { lstatSync, readFileSync, realpathSync, renameSync, rmSync, unlinkSync, utimesSync, writeFileSync } from "node:fs";
|
|
18
|
+
import { tmpdir } from "node:os";
|
|
19
|
+
import { join, resolve } from "node:path";
|
|
20
|
+
import { TOOL_NAME } from "../brand.js";
|
|
21
|
+
/** 動いている watch が印の更新時刻を新しくする間隔 */
|
|
22
|
+
const HEARTBEAT_MS = 10_000;
|
|
23
|
+
/** これより長く更新されていない印は、PID があっても止まった watch のものとみなす */
|
|
24
|
+
const STALE_MS = 60_000;
|
|
25
|
+
/** 同じフォルダを同じ文字列にする (Windows・macOS は大文字小文字を区別しないのが普通) */
|
|
26
|
+
function normalizeDir(dir, platform) {
|
|
27
|
+
let path = resolve(dir);
|
|
28
|
+
try {
|
|
29
|
+
path = realpathSync(path);
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
// まだ無いフォルダは、そのままのパスで見る
|
|
33
|
+
}
|
|
34
|
+
return platform === "win32" || platform === "darwin" ? path.toLowerCase() : path;
|
|
35
|
+
}
|
|
36
|
+
/** Downloads のフォルダごとの印のパス。共有の /tmp でほかのユーザーと重ならないよう uid も混ぜる */
|
|
37
|
+
export function watchLockPath(downloadsDir, lockDir = tmpdir(), platform = process.platform) {
|
|
38
|
+
const uid = typeof process.getuid === "function" ? String(process.getuid()) : "";
|
|
39
|
+
const key = createHash("sha256").update(`${uid}\0${normalizeDir(downloadsDir, platform)}`).digest("hex").slice(0, 16);
|
|
40
|
+
return join(lockDir, `${TOOL_NAME}-watch-${key}.json`);
|
|
41
|
+
}
|
|
42
|
+
/** 印を読む。通常のファイルでない・自分のものでない (共有の /tmp)・形の違うものは invalid */
|
|
43
|
+
function readLock(path) {
|
|
44
|
+
let st;
|
|
45
|
+
try {
|
|
46
|
+
st = lstatSync(path);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return { kind: "missing" };
|
|
50
|
+
}
|
|
51
|
+
if (!st.isFile())
|
|
52
|
+
return { kind: "invalid" };
|
|
53
|
+
if (typeof process.getuid === "function" && st.uid !== process.getuid())
|
|
54
|
+
return { kind: "invalid" };
|
|
55
|
+
let value;
|
|
56
|
+
try {
|
|
57
|
+
value = JSON.parse(readFileSync(path, "utf8"));
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return { kind: "invalid" };
|
|
61
|
+
}
|
|
62
|
+
if (typeof value !== "object" || value === null)
|
|
63
|
+
return { kind: "invalid" };
|
|
64
|
+
const v = value;
|
|
65
|
+
if (typeof v["token"] !== "string" ||
|
|
66
|
+
typeof v["pid"] !== "number" ||
|
|
67
|
+
!Number.isInteger(v["pid"]) ||
|
|
68
|
+
typeof v["root"] !== "string" ||
|
|
69
|
+
typeof v["startedAt"] !== "string") {
|
|
70
|
+
return { kind: "invalid" };
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
kind: "ok",
|
|
74
|
+
info: { token: v["token"], pid: v["pid"], root: v["root"], startedAt: v["startedAt"] },
|
|
75
|
+
mtimeMs: st.mtimeMs,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/** PID のプロセスがあるか (別のユーザーのプロセスなら EPERM で、あるとみなす) */
|
|
79
|
+
function processExists(pid) {
|
|
80
|
+
if (pid === process.pid)
|
|
81
|
+
return true;
|
|
82
|
+
if (pid <= 0)
|
|
83
|
+
return false;
|
|
84
|
+
try {
|
|
85
|
+
process.kill(pid, 0);
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
catch (e) {
|
|
89
|
+
return e.code === "EPERM";
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/** 短く待つ (印の置き換えで、相手がちょうど読んでいて rename できなかったときの再試行用) */
|
|
93
|
+
function pause(ms) {
|
|
94
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
95
|
+
}
|
|
96
|
+
/** 別名に書いてから rename で置き換える。Windows で相手が読んでいる瞬間は rename できないことがあるので少し再試行する */
|
|
97
|
+
function replaceLock(path, info) {
|
|
98
|
+
const temp = `${path}.${process.pid}.${randomBytes(6).toString("hex")}.tmp`;
|
|
99
|
+
// wx: 既にあれば失敗する (推測できない名前なので、先に置かれた symlink を辿らない)
|
|
100
|
+
writeFileSync(temp, JSON.stringify(info), { encoding: "utf8", flag: "wx", mode: 0o600 });
|
|
101
|
+
for (let attempt = 0;; attempt++) {
|
|
102
|
+
try {
|
|
103
|
+
renameSync(temp, path);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
catch (e) {
|
|
107
|
+
const code = e.code;
|
|
108
|
+
if (attempt < 10 && (code === "EPERM" || code === "EBUSY" || code === "EACCES")) {
|
|
109
|
+
pause(20);
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
rmSync(temp, { force: true });
|
|
113
|
+
throw e;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* 印を書き (前の印を置き換え)、前に動いていた watch の情報を返す。
|
|
119
|
+
* 書けなければ例外 (呼び出し側は警告して印なしで続ける)
|
|
120
|
+
*/
|
|
121
|
+
export function acquireWatchLock(path, me, now = Date.now()) {
|
|
122
|
+
const before = readLock(path);
|
|
123
|
+
const previous = before.kind === "ok"
|
|
124
|
+
? {
|
|
125
|
+
pid: before.info.pid,
|
|
126
|
+
root: before.info.root,
|
|
127
|
+
startedAt: before.info.startedAt,
|
|
128
|
+
alive: processExists(before.info.pid) && now - before.mtimeMs < STALE_MS,
|
|
129
|
+
}
|
|
130
|
+
: null;
|
|
131
|
+
const token = randomBytes(16).toString("hex");
|
|
132
|
+
replaceLock(path, { ...me, token });
|
|
133
|
+
return { lock: { path, token }, previous };
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* 印がまだ自分のものか。別の watch の印に置き換わっていれば "taken"。
|
|
137
|
+
* 無くなっていれば (一時フォルダの掃除など) 書き直して "mine"。読めないときは "unknown" (次の観測で見直す)。
|
|
138
|
+
* 自分の印なら、ときどき更新時刻を新しくする (まだ動いていることを後から始めた watch に示す)
|
|
139
|
+
*/
|
|
140
|
+
export function checkWatchLock(lock, me, now = Date.now()) {
|
|
141
|
+
const current = readLock(lock.path);
|
|
142
|
+
if (current.kind === "missing") {
|
|
143
|
+
try {
|
|
144
|
+
// wx: その間に別の watch が書いていたら、そちらを残す (次の観測で切り替わったと分かる)
|
|
145
|
+
writeFileSync(lock.path, JSON.stringify({ ...me, token: lock.token }), { encoding: "utf8", flag: "wx", mode: 0o600 });
|
|
146
|
+
return { state: "mine" };
|
|
147
|
+
}
|
|
148
|
+
catch {
|
|
149
|
+
return { state: "unknown" };
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (current.kind === "invalid")
|
|
153
|
+
return { state: "unknown" };
|
|
154
|
+
if (current.info.token !== lock.token) {
|
|
155
|
+
const { token: _token, ...by } = current.info;
|
|
156
|
+
return { state: "taken", by };
|
|
157
|
+
}
|
|
158
|
+
if (now - current.mtimeMs >= HEARTBEAT_MS) {
|
|
159
|
+
try {
|
|
160
|
+
const at = new Date(now);
|
|
161
|
+
utimesSync(lock.path, at, at);
|
|
162
|
+
}
|
|
163
|
+
catch {
|
|
164
|
+
// 更新できなくても困らない (後から始めた watch の表示が変わるだけ)
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return { state: "mine" };
|
|
168
|
+
}
|
|
169
|
+
/** 終了時に印を消す (自分の印のときだけ。切り替わった後は、新しい watch の印を残す) */
|
|
170
|
+
export function releaseWatchLock(lock) {
|
|
171
|
+
const current = readLock(lock.path);
|
|
172
|
+
if (current.kind !== "ok" || current.info.token !== lock.token)
|
|
173
|
+
return;
|
|
174
|
+
try {
|
|
175
|
+
unlinkSync(lock.path);
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
// 消せなくても、次の watch が置き換える
|
|
179
|
+
}
|
|
180
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tazuna",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.27.0",
|
|
4
4
|
"description": "Build with AI chat, without handing over the reins: pack your codebase for the chat, then verify and apply its patches locally (zero dependencies; formerly petari and slnmix)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cli",
|