@cocorograph/hub-agent 0.5.4 → 0.5.5
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/package.json +1 -1
- package/src/main.mjs +48 -0
- package/src/tmux.mjs +66 -0
package/package.json
CHANGED
package/src/main.mjs
CHANGED
|
@@ -24,6 +24,7 @@ import { listSkills } from "./skills.mjs"
|
|
|
24
24
|
import { listSessionStates } from "./state.mjs"
|
|
25
25
|
import {
|
|
26
26
|
createSession as createTmuxSession,
|
|
27
|
+
createWorktreeDir,
|
|
27
28
|
execTmux,
|
|
28
29
|
killManySessions,
|
|
29
30
|
killSession as killTmuxSession,
|
|
@@ -313,6 +314,53 @@ async function dispatch(msg, ctx) {
|
|
|
313
314
|
}
|
|
314
315
|
return
|
|
315
316
|
}
|
|
317
|
+
case "worktree.create": {
|
|
318
|
+
// body: { request_id, dir, branch, claude_cmd? }
|
|
319
|
+
const dir = (msg.dir || "").trim()
|
|
320
|
+
const branch = (msg.branch || "").trim()
|
|
321
|
+
if (!dir || !branch) {
|
|
322
|
+
ctx.client.send({
|
|
323
|
+
type: "worktree.create.result",
|
|
324
|
+
request_id: msg.request_id,
|
|
325
|
+
ok: false,
|
|
326
|
+
error: "dir and branch required",
|
|
327
|
+
})
|
|
328
|
+
return
|
|
329
|
+
}
|
|
330
|
+
if (!/^[A-Za-z0-9._\-/]+$/.test(branch) || branch.startsWith("-")) {
|
|
331
|
+
ctx.client.send({
|
|
332
|
+
type: "worktree.create.result",
|
|
333
|
+
request_id: msg.request_id,
|
|
334
|
+
ok: false,
|
|
335
|
+
error: "invalid branch name",
|
|
336
|
+
})
|
|
337
|
+
return
|
|
338
|
+
}
|
|
339
|
+
try {
|
|
340
|
+
const { wtName, wtPath, createdBranch } = await createWorktreeDir(dir, branch)
|
|
341
|
+
// worktree dir で tmux session を作成 (claude_cmd は createSession の default)
|
|
342
|
+
await createTmuxSession(wtName, wtPath, {
|
|
343
|
+
claudeCmd: typeof msg.claude_cmd === "string" ? msg.claude_cmd : undefined,
|
|
344
|
+
})
|
|
345
|
+
ctx.client.send({
|
|
346
|
+
type: "worktree.create.result",
|
|
347
|
+
request_id: msg.request_id,
|
|
348
|
+
ok: true,
|
|
349
|
+
wt_name: wtName,
|
|
350
|
+
wt_path: wtPath,
|
|
351
|
+
branch,
|
|
352
|
+
created_branch: createdBranch,
|
|
353
|
+
})
|
|
354
|
+
} catch (err) {
|
|
355
|
+
ctx.client.send({
|
|
356
|
+
type: "worktree.create.result",
|
|
357
|
+
request_id: msg.request_id,
|
|
358
|
+
ok: false,
|
|
359
|
+
error: err.message,
|
|
360
|
+
})
|
|
361
|
+
}
|
|
362
|
+
return
|
|
363
|
+
}
|
|
316
364
|
case "tmux.kill_session": {
|
|
317
365
|
const names = Array.isArray(msg.session_names)
|
|
318
366
|
? msg.session_names
|
package/src/tmux.mjs
CHANGED
|
@@ -33,6 +33,72 @@ function sanitizeTmuxName(s) {
|
|
|
33
33
|
return s.replace(/[.:]/g, "-")
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
/**
|
|
37
|
+
* git branch 名から worktree dir 名 (= tmux session 名) を導出する。
|
|
38
|
+
* 例: "feature/multi-area" -> "feature-multi-area"
|
|
39
|
+
* "fix.typo:v2" -> "fix-typo-v2"
|
|
40
|
+
*
|
|
41
|
+
* 移植元: D00000_cockpit/webapp/lib/worktrees.ts (sanitizeBranchToWtName)
|
|
42
|
+
*/
|
|
43
|
+
export function sanitizeBranchToWtName(branch) {
|
|
44
|
+
return branch
|
|
45
|
+
.replace(/[\s/:.]+/g, "-")
|
|
46
|
+
.replace(/[^A-Za-z0-9_-]/g, "")
|
|
47
|
+
.replace(/^-+|-+$/g, "")
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async function branchExists(repoDir, branch) {
|
|
51
|
+
try {
|
|
52
|
+
await execFileP("git", [
|
|
53
|
+
"-C",
|
|
54
|
+
repoDir,
|
|
55
|
+
"show-ref",
|
|
56
|
+
"--verify",
|
|
57
|
+
"--quiet",
|
|
58
|
+
`refs/heads/${branch}`,
|
|
59
|
+
])
|
|
60
|
+
return true
|
|
61
|
+
} catch {
|
|
62
|
+
return false
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* `<repoDir>/.claude/worktrees/<wtName>` に worktree を作成する。
|
|
68
|
+
* branch が既存ならそれを checkout、無ければ `-b` で新規作成。
|
|
69
|
+
*
|
|
70
|
+
* 移植元: D00000_cockpit/webapp/lib/worktrees.ts (createWorktree)
|
|
71
|
+
*/
|
|
72
|
+
export async function createWorktreeDir(parentDir, branch) {
|
|
73
|
+
const repoDir = path.resolve(HUB_PROJECTS_BASE, parentDir)
|
|
74
|
+
if (repoDir !== HUB_PROJECTS_BASE && !repoDir.startsWith(HUB_PROJECTS_BASE + path.sep)) {
|
|
75
|
+
throw new Error("dir outside projects base")
|
|
76
|
+
}
|
|
77
|
+
// .git の存在チェック (file or dir)
|
|
78
|
+
try {
|
|
79
|
+
await fs.stat(path.join(repoDir, ".git"))
|
|
80
|
+
} catch {
|
|
81
|
+
throw new Error("not a git repository")
|
|
82
|
+
}
|
|
83
|
+
const wtName = sanitizeBranchToWtName(branch)
|
|
84
|
+
if (!wtName) throw new Error("invalid branch name")
|
|
85
|
+
const wtPath = path.join(repoDir, ".claude", "worktrees", wtName)
|
|
86
|
+
try {
|
|
87
|
+
await fs.stat(wtPath)
|
|
88
|
+
throw new Error("worktree already exists")
|
|
89
|
+
} catch (err) {
|
|
90
|
+
if (err.message === "worktree already exists") throw err
|
|
91
|
+
// ENOENT は期待動作
|
|
92
|
+
}
|
|
93
|
+
const exists = await branchExists(repoDir, branch)
|
|
94
|
+
if (exists) {
|
|
95
|
+
await execFileP("git", ["-C", repoDir, "worktree", "add", wtPath, branch])
|
|
96
|
+
} else {
|
|
97
|
+
await execFileP("git", ["-C", repoDir, "worktree", "add", "-b", branch, wtPath])
|
|
98
|
+
}
|
|
99
|
+
return { wtName, wtPath, createdBranch: !exists }
|
|
100
|
+
}
|
|
101
|
+
|
|
36
102
|
/**
|
|
37
103
|
* `~/hub/projects/<project>/.claude/worktrees/<wt>` を走査し、
|
|
38
104
|
* worktree_session_name → parent_session_name の Map を返す。
|