@rethunk/mcp-multi-root-git 2.1.0 → 2.3.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/AGENTS.md +10 -8
- package/CHANGELOG.md +95 -0
- package/README.md +1 -1
- package/dist/server/batch-commit-tool.js +61 -13
- package/dist/server/git-cherry-pick-tool.js +285 -0
- package/dist/server/git-diff-summary-tool.js +5 -12
- package/dist/server/git-inventory-tool.js +4 -1
- package/dist/server/git-log-tool.js +16 -18
- package/dist/server/git-merge-tool.js +378 -0
- package/dist/server/git-parity-tool.js +4 -1
- package/dist/server/git-push-tool.js +109 -0
- package/dist/server/git-refs.js +192 -0
- package/dist/server/git-reset-soft-tool.js +82 -0
- package/dist/server/git-status-tool.js +4 -1
- package/dist/server/git-worktree-tool.js +188 -0
- package/dist/server/list-presets-tool.js +4 -1
- package/dist/server/roots.js +17 -0
- package/dist/server/schemas.js +11 -2
- package/dist/server/test-harness.js +27 -0
- package/dist/server/tools.js +17 -2
- package/docs/mcp-tools.md +261 -10
- package/package.json +5 -4
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { spawnGitAsync } from "./git.js";
|
|
2
|
+
// ---------------------------------------------------------------------------
|
|
3
|
+
// Merge conflict helpers (shared between git_merge and git_cherry_pick)
|
|
4
|
+
// ---------------------------------------------------------------------------
|
|
5
|
+
/** Paths with unresolved merge conflicts (`--diff-filter=U`). */
|
|
6
|
+
export async function conflictPaths(gitTop) {
|
|
7
|
+
const r = await spawnGitAsync(gitTop, ["diff", "--name-only", "--diff-filter=U"]);
|
|
8
|
+
if (!r.ok)
|
|
9
|
+
return [];
|
|
10
|
+
return r.stdout
|
|
11
|
+
.split("\n")
|
|
12
|
+
.map((l) => l.trim())
|
|
13
|
+
.filter((l) => l.length > 0);
|
|
14
|
+
}
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
// Protected branch names — never auto-delete, never cascade destructive ops onto
|
|
17
|
+
// ---------------------------------------------------------------------------
|
|
18
|
+
const PROTECTED_EXACT = new Set([
|
|
19
|
+
"main",
|
|
20
|
+
"master",
|
|
21
|
+
"dev",
|
|
22
|
+
"develop",
|
|
23
|
+
"stable",
|
|
24
|
+
"trunk",
|
|
25
|
+
"prod",
|
|
26
|
+
"production",
|
|
27
|
+
"HEAD",
|
|
28
|
+
]);
|
|
29
|
+
const PROTECTED_PATTERN = /^(release|hotfix)[-/].+$/i;
|
|
30
|
+
/** True when a branch name is on the protected list and must not be auto-deleted. */
|
|
31
|
+
export function isProtectedBranch(name) {
|
|
32
|
+
const t = name.trim();
|
|
33
|
+
if (t === "")
|
|
34
|
+
return true;
|
|
35
|
+
if (PROTECTED_EXACT.has(t))
|
|
36
|
+
return true;
|
|
37
|
+
return PROTECTED_PATTERN.test(t);
|
|
38
|
+
}
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
// Ref/branch name validation (argv-safe subset of git's ref-format rules)
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
/**
|
|
43
|
+
* Conservative check for branch/ref names passed to git argv.
|
|
44
|
+
* Rejects anything outside the ASCII subset `A-Z a-z 0-9 _ . / + -`,
|
|
45
|
+
* sequences git itself rejects (`..`, `@{`, leading `-`, trailing `.lock`/`/`),
|
|
46
|
+
* and pathological tokens.
|
|
47
|
+
*/
|
|
48
|
+
export function isSafeGitRefToken(s) {
|
|
49
|
+
const t = s.trim();
|
|
50
|
+
if (t.length === 0 || t.length > 256)
|
|
51
|
+
return false;
|
|
52
|
+
if (t.startsWith("-"))
|
|
53
|
+
return false;
|
|
54
|
+
if (t.endsWith("/") || t.endsWith(".lock") || t.endsWith("."))
|
|
55
|
+
return false;
|
|
56
|
+
if (t.includes(".."))
|
|
57
|
+
return false;
|
|
58
|
+
if (t.includes("@{"))
|
|
59
|
+
return false;
|
|
60
|
+
if (t.includes("//"))
|
|
61
|
+
return false;
|
|
62
|
+
return /^[A-Za-z0-9_./+-]+$/.test(t);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Same as `isSafeGitRefToken` but also allows `~N` / `^N` ancestor notation used
|
|
66
|
+
* by `git reset --soft HEAD~3`. Permits `~` and `^` suffix characters.
|
|
67
|
+
*/
|
|
68
|
+
export function isSafeGitAncestorRef(s) {
|
|
69
|
+
const t = s.trim();
|
|
70
|
+
if (t.length === 0 || t.length > 256)
|
|
71
|
+
return false;
|
|
72
|
+
if (t.startsWith("-"))
|
|
73
|
+
return false;
|
|
74
|
+
return /^[A-Za-z0-9_./+~^-]+$/.test(t);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Same as `isSafeGitRefToken` but also allows the `A..B` / `A...B` range forms
|
|
78
|
+
* used by `git log` / `git cherry-pick`. Splits once and validates each side.
|
|
79
|
+
*/
|
|
80
|
+
export function isSafeGitRangeToken(s) {
|
|
81
|
+
const t = s.trim();
|
|
82
|
+
if (t.includes("...")) {
|
|
83
|
+
const parts = t.split("...");
|
|
84
|
+
return parts.length === 2 && parts.every((p) => isSafeGitRefToken(p));
|
|
85
|
+
}
|
|
86
|
+
if (t.includes("..")) {
|
|
87
|
+
const parts = t.split("..");
|
|
88
|
+
return parts.length === 2 && parts.every((p) => isSafeGitRefToken(p));
|
|
89
|
+
}
|
|
90
|
+
return isSafeGitRefToken(t);
|
|
91
|
+
}
|
|
92
|
+
// ---------------------------------------------------------------------------
|
|
93
|
+
// Async helpers
|
|
94
|
+
// ---------------------------------------------------------------------------
|
|
95
|
+
/** Current branch name; `null` if detached HEAD. */
|
|
96
|
+
export async function getCurrentBranch(cwd) {
|
|
97
|
+
const r = await spawnGitAsync(cwd, ["symbolic-ref", "--short", "-q", "HEAD"]);
|
|
98
|
+
if (!r.ok)
|
|
99
|
+
return null;
|
|
100
|
+
const name = r.stdout.trim();
|
|
101
|
+
return name === "" ? null : name;
|
|
102
|
+
}
|
|
103
|
+
/** Resolve a ref to its full SHA; `null` if unknown. */
|
|
104
|
+
export async function resolveRef(cwd, ref) {
|
|
105
|
+
if (!isSafeGitRefToken(ref))
|
|
106
|
+
return null;
|
|
107
|
+
const r = await spawnGitAsync(cwd, ["rev-parse", "--verify", "--quiet", `${ref}^{commit}`]);
|
|
108
|
+
if (!r.ok)
|
|
109
|
+
return null;
|
|
110
|
+
const sha = r.stdout.trim();
|
|
111
|
+
return sha === "" ? null : sha;
|
|
112
|
+
}
|
|
113
|
+
/** Working tree clean (no staged, no unstaged, no untracked). */
|
|
114
|
+
export async function isWorkingTreeClean(cwd) {
|
|
115
|
+
const r = await spawnGitAsync(cwd, ["status", "--porcelain"]);
|
|
116
|
+
if (!r.ok)
|
|
117
|
+
return false;
|
|
118
|
+
return r.stdout.trim() === "";
|
|
119
|
+
}
|
|
120
|
+
/** True when every commit on `branch` is reachable from `target`. */
|
|
121
|
+
export async function isFullyMergedInto(cwd, branch, target) {
|
|
122
|
+
if (!isSafeGitRefToken(branch) || !isSafeGitRefToken(target))
|
|
123
|
+
return false;
|
|
124
|
+
const r = await spawnGitAsync(cwd, ["merge-base", "--is-ancestor", branch, target]);
|
|
125
|
+
return r.ok;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* SHAs of commits in `exclude..include`, oldest-first (cherry-pick feed order).
|
|
129
|
+
* Returns `null` on git failure.
|
|
130
|
+
*/
|
|
131
|
+
export async function commitListBetween(cwd, excludeRef, includeRef) {
|
|
132
|
+
if (!isSafeGitRefToken(excludeRef) || !isSafeGitRefToken(includeRef))
|
|
133
|
+
return null;
|
|
134
|
+
const r = await spawnGitAsync(cwd, ["rev-list", "--reverse", `${excludeRef}..${includeRef}`]);
|
|
135
|
+
if (!r.ok)
|
|
136
|
+
return null;
|
|
137
|
+
return r.stdout
|
|
138
|
+
.split("\n")
|
|
139
|
+
.map((l) => l.trim())
|
|
140
|
+
.filter((l) => l.length > 0);
|
|
141
|
+
}
|
|
142
|
+
/** Parse `git worktree list --porcelain` into structured entries. */
|
|
143
|
+
export async function listWorktrees(cwd) {
|
|
144
|
+
const r = await spawnGitAsync(cwd, ["worktree", "list", "--porcelain"]);
|
|
145
|
+
if (!r.ok)
|
|
146
|
+
return [];
|
|
147
|
+
const out = [];
|
|
148
|
+
let cur = {};
|
|
149
|
+
for (const line of r.stdout.split("\n")) {
|
|
150
|
+
if (line.startsWith("worktree ")) {
|
|
151
|
+
if (cur.path)
|
|
152
|
+
out.push({ path: cur.path, branch: cur.branch ?? null, head: cur.head ?? null });
|
|
153
|
+
cur = { path: line.slice("worktree ".length).trim() };
|
|
154
|
+
}
|
|
155
|
+
else if (line.startsWith("HEAD ")) {
|
|
156
|
+
cur.head = line.slice("HEAD ".length).trim();
|
|
157
|
+
}
|
|
158
|
+
else if (line.startsWith("branch ")) {
|
|
159
|
+
// e.g. `branch refs/heads/foo`
|
|
160
|
+
const ref = line.slice("branch ".length).trim();
|
|
161
|
+
cur.branch = ref.startsWith("refs/heads/") ? ref.slice("refs/heads/".length) : ref;
|
|
162
|
+
}
|
|
163
|
+
else if (line === "detached") {
|
|
164
|
+
cur.branch = null;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (cur.path)
|
|
168
|
+
out.push({ path: cur.path, branch: cur.branch ?? null, head: cur.head ?? null });
|
|
169
|
+
return out;
|
|
170
|
+
}
|
|
171
|
+
/** Path of the worktree currently checked out on `branch`; `null` if none. */
|
|
172
|
+
export async function worktreeForBranch(cwd, branch) {
|
|
173
|
+
const trees = await listWorktrees(cwd);
|
|
174
|
+
const hit = trees.find((t) => t.branch === branch);
|
|
175
|
+
return hit?.path ?? null;
|
|
176
|
+
}
|
|
177
|
+
// ---------------------------------------------------------------------------
|
|
178
|
+
// Push helpers
|
|
179
|
+
// ---------------------------------------------------------------------------
|
|
180
|
+
/**
|
|
181
|
+
* Probe `@{u}` and extract the remote name from the tracking ref.
|
|
182
|
+
* Returns an error payload when no upstream is configured.
|
|
183
|
+
*/
|
|
184
|
+
export async function inferRemoteFromUpstream(cwd) {
|
|
185
|
+
const r = await spawnGitAsync(cwd, ["rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"]);
|
|
186
|
+
if (!r.ok)
|
|
187
|
+
return { ok: false, detail: (r.stderr || r.stdout).trim() };
|
|
188
|
+
const upstream = r.stdout.trim();
|
|
189
|
+
const slash = upstream.indexOf("/");
|
|
190
|
+
const remote = slash > 0 ? upstream.slice(0, slash) : "origin";
|
|
191
|
+
return { ok: true, remote, upstream };
|
|
192
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { spawnGitAsync } from "./git.js";
|
|
3
|
+
import { isSafeGitAncestorRef, isWorkingTreeClean } from "./git-refs.js";
|
|
4
|
+
import { jsonRespond, spreadDefined } from "./json.js";
|
|
5
|
+
import { requireSingleRepo } from "./roots.js";
|
|
6
|
+
import { WorkspacePickSchema } from "./schemas.js";
|
|
7
|
+
export function registerGitResetSoftTool(server) {
|
|
8
|
+
server.addTool({
|
|
9
|
+
name: "git_reset_soft",
|
|
10
|
+
description: "Soft-reset the current branch to a reference (`git reset --soft <ref>`). " +
|
|
11
|
+
"Moves the branch pointer back while keeping all changes from the rewound commits " +
|
|
12
|
+
"in the staging index — use this to re-split an already-committed chunk. " +
|
|
13
|
+
"Refuses when the working tree has any uncommitted or unstaged changes (run on a clean tree).",
|
|
14
|
+
annotations: {
|
|
15
|
+
readOnlyHint: false,
|
|
16
|
+
destructiveHint: false,
|
|
17
|
+
idempotentHint: false,
|
|
18
|
+
},
|
|
19
|
+
parameters: WorkspacePickSchema.extend({
|
|
20
|
+
ref: z
|
|
21
|
+
.string()
|
|
22
|
+
.min(1)
|
|
23
|
+
.describe("Commit to reset to. Accepts ancestor notation (`HEAD~1`, `HEAD~3`), " +
|
|
24
|
+
"branch names, or full SHAs."),
|
|
25
|
+
}),
|
|
26
|
+
execute: async (args) => {
|
|
27
|
+
const pre = requireSingleRepo(server, args);
|
|
28
|
+
if (!pre.ok)
|
|
29
|
+
return jsonRespond(pre.error);
|
|
30
|
+
const { gitTop } = pre;
|
|
31
|
+
// Validate ref — allow ancestor notation (~N, ^N).
|
|
32
|
+
if (!isSafeGitAncestorRef(args.ref)) {
|
|
33
|
+
return jsonRespond({ error: "unsafe_ref_token", ref: args.ref });
|
|
34
|
+
}
|
|
35
|
+
// Refuse when the working tree is dirty (unstaged or untracked changes).
|
|
36
|
+
if (!(await isWorkingTreeClean(gitTop))) {
|
|
37
|
+
return jsonRespond({
|
|
38
|
+
error: "working_tree_dirty",
|
|
39
|
+
detail: "git_reset_soft requires a clean working tree. " +
|
|
40
|
+
"Commit or stash pending changes first.",
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
// Probe HEAD before reset for the response.
|
|
44
|
+
const preSha = await spawnGitAsync(gitTop, ["rev-parse", "HEAD"]);
|
|
45
|
+
const beforeSha = preSha.ok ? preSha.stdout.trim() : undefined;
|
|
46
|
+
// Run the reset.
|
|
47
|
+
const r = await spawnGitAsync(gitTop, ["reset", "--soft", args.ref]);
|
|
48
|
+
if (!r.ok) {
|
|
49
|
+
return jsonRespond({
|
|
50
|
+
error: "reset_failed",
|
|
51
|
+
detail: (r.stderr || r.stdout).trim(),
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
// Probe HEAD after reset.
|
|
55
|
+
const postSha = await spawnGitAsync(gitTop, ["rev-parse", "HEAD"]);
|
|
56
|
+
const afterSha = postSha.ok ? postSha.stdout.trim() : undefined;
|
|
57
|
+
// Count staged changes after reset.
|
|
58
|
+
const stagedResult = await spawnGitAsync(gitTop, ["diff", "--cached", "--name-only"]);
|
|
59
|
+
const stagedFiles = stagedResult.ok
|
|
60
|
+
? stagedResult.stdout
|
|
61
|
+
.split("\n")
|
|
62
|
+
.map((l) => l.trim())
|
|
63
|
+
.filter((l) => l.length > 0)
|
|
64
|
+
: [];
|
|
65
|
+
if (args.format === "json") {
|
|
66
|
+
return jsonRespond({
|
|
67
|
+
ok: true,
|
|
68
|
+
ref: args.ref,
|
|
69
|
+
...spreadDefined("beforeSha", beforeSha),
|
|
70
|
+
...spreadDefined("afterSha", afterSha),
|
|
71
|
+
stagedCount: stagedFiles.length,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
const beforeShort = beforeSha?.slice(0, 7) ?? "?";
|
|
75
|
+
const afterShort = afterSha?.slice(0, 7) ?? "?";
|
|
76
|
+
return [
|
|
77
|
+
"# Reset (soft)",
|
|
78
|
+
`✓ ${beforeShort} → ${afterShort} (${stagedFiles.length} file(s) staged)`,
|
|
79
|
+
].join("\n");
|
|
80
|
+
},
|
|
81
|
+
});
|
|
82
|
+
}
|
|
@@ -8,7 +8,10 @@ import { WorkspacePickSchema } from "./schemas.js";
|
|
|
8
8
|
export function registerGitStatusTool(server) {
|
|
9
9
|
server.addTool({
|
|
10
10
|
name: "git_status",
|
|
11
|
-
description: "Read-only `git status --short -b` per root + submodules.
|
|
11
|
+
description: "Read-only `git status --short -b` per root + submodules.",
|
|
12
|
+
annotations: {
|
|
13
|
+
readOnlyHint: true,
|
|
14
|
+
},
|
|
12
15
|
parameters: WorkspacePickSchema.extend({
|
|
13
16
|
includeSubmodules: z.boolean().optional().default(true),
|
|
14
17
|
}),
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { spawnGitAsync } from "./git.js";
|
|
4
|
+
import { isProtectedBranch, isSafeGitRefToken, listWorktrees, } from "./git-refs.js";
|
|
5
|
+
import { jsonRespond, spreadDefined, spreadWhen } from "./json.js";
|
|
6
|
+
import { requireSingleRepo } from "./roots.js";
|
|
7
|
+
import { WorkspacePickSchema } from "./schemas.js";
|
|
8
|
+
// ---------------------------------------------------------------------------
|
|
9
|
+
// git_worktree_list
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
export function registerGitWorktreeListTool(server) {
|
|
12
|
+
server.addTool({
|
|
13
|
+
name: "git_worktree_list",
|
|
14
|
+
description: "List all git worktrees for the repository (`git worktree list --porcelain`).",
|
|
15
|
+
annotations: {
|
|
16
|
+
readOnlyHint: true,
|
|
17
|
+
},
|
|
18
|
+
parameters: WorkspacePickSchema.pick({
|
|
19
|
+
workspaceRoot: true,
|
|
20
|
+
rootIndex: true,
|
|
21
|
+
format: true,
|
|
22
|
+
}),
|
|
23
|
+
execute: async (args) => {
|
|
24
|
+
const pre = requireSingleRepo(server, args);
|
|
25
|
+
if (!pre.ok)
|
|
26
|
+
return jsonRespond(pre.error);
|
|
27
|
+
const { gitTop } = pre;
|
|
28
|
+
const trees = await listWorktrees(gitTop);
|
|
29
|
+
if (args.format === "json") {
|
|
30
|
+
return jsonRespond({ worktrees: trees });
|
|
31
|
+
}
|
|
32
|
+
if (trees.length === 0) {
|
|
33
|
+
return "# Worktrees\n_(none)_";
|
|
34
|
+
}
|
|
35
|
+
const lines = ["# Worktrees", ""];
|
|
36
|
+
for (const t of trees) {
|
|
37
|
+
const branchPart = t.branch ?? "(detached)";
|
|
38
|
+
const headPart = t.head ? ` @ ${t.head.slice(0, 7)}` : "";
|
|
39
|
+
lines.push(`- \`${t.path}\` ${branchPart}${headPart}`);
|
|
40
|
+
}
|
|
41
|
+
return lines.join("\n");
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
// ---------------------------------------------------------------------------
|
|
46
|
+
// git_worktree_add
|
|
47
|
+
// ---------------------------------------------------------------------------
|
|
48
|
+
export function registerGitWorktreeAddTool(server) {
|
|
49
|
+
server.addTool({
|
|
50
|
+
name: "git_worktree_add",
|
|
51
|
+
description: "Add a new git worktree. If `branch` does not exist it is created from `baseRef` " +
|
|
52
|
+
"(defaults to HEAD). Refuses to create on a protected branch name.",
|
|
53
|
+
annotations: {
|
|
54
|
+
readOnlyHint: false,
|
|
55
|
+
destructiveHint: false,
|
|
56
|
+
idempotentHint: false,
|
|
57
|
+
},
|
|
58
|
+
parameters: WorkspacePickSchema.extend({
|
|
59
|
+
path: z
|
|
60
|
+
.string()
|
|
61
|
+
.min(1)
|
|
62
|
+
.describe("Filesystem path for the new worktree. Relative paths are resolved from the git toplevel."),
|
|
63
|
+
branch: z
|
|
64
|
+
.string()
|
|
65
|
+
.min(1)
|
|
66
|
+
.describe("Branch to check out in the new worktree. Created from `baseRef` if absent."),
|
|
67
|
+
baseRef: z
|
|
68
|
+
.string()
|
|
69
|
+
.optional()
|
|
70
|
+
.describe("Commit-ish to base the new branch on when it does not yet exist. Default: HEAD."),
|
|
71
|
+
}),
|
|
72
|
+
execute: async (args) => {
|
|
73
|
+
const pre = requireSingleRepo(server, args);
|
|
74
|
+
if (!pre.ok)
|
|
75
|
+
return jsonRespond(pre.error);
|
|
76
|
+
const { gitTop } = pre;
|
|
77
|
+
// Validate branch
|
|
78
|
+
if (!isSafeGitRefToken(args.branch)) {
|
|
79
|
+
return jsonRespond({ error: "unsafe_ref_token", ref: args.branch });
|
|
80
|
+
}
|
|
81
|
+
if (isProtectedBranch(args.branch)) {
|
|
82
|
+
return jsonRespond({ error: "protected_branch", branch: args.branch });
|
|
83
|
+
}
|
|
84
|
+
if (args.baseRef !== undefined && !isSafeGitRefToken(args.baseRef)) {
|
|
85
|
+
return jsonRespond({ error: "unsafe_ref_token", ref: args.baseRef });
|
|
86
|
+
}
|
|
87
|
+
// Resolve path
|
|
88
|
+
const wtPath = resolve(gitTop, args.path);
|
|
89
|
+
// Check if branch already exists
|
|
90
|
+
const branchCheck = await spawnGitAsync(gitTop, [
|
|
91
|
+
"show-ref",
|
|
92
|
+
"--verify",
|
|
93
|
+
"--quiet",
|
|
94
|
+
`refs/heads/${args.branch}`,
|
|
95
|
+
]);
|
|
96
|
+
const branchExists = branchCheck.ok;
|
|
97
|
+
let gitArgs;
|
|
98
|
+
if (branchExists) {
|
|
99
|
+
gitArgs = ["worktree", "add", wtPath, args.branch];
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
gitArgs = ["worktree", "add", "-b", args.branch, wtPath];
|
|
103
|
+
if (args.baseRef)
|
|
104
|
+
gitArgs.push(args.baseRef);
|
|
105
|
+
}
|
|
106
|
+
const r = await spawnGitAsync(gitTop, gitArgs);
|
|
107
|
+
if (!r.ok) {
|
|
108
|
+
return jsonRespond({
|
|
109
|
+
ok: false,
|
|
110
|
+
error: "worktree_add_failed",
|
|
111
|
+
detail: (r.stderr || r.stdout).trim(),
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
if (args.format === "json") {
|
|
115
|
+
return jsonRespond({
|
|
116
|
+
ok: true,
|
|
117
|
+
path: wtPath,
|
|
118
|
+
branch: args.branch,
|
|
119
|
+
created: !branchExists,
|
|
120
|
+
...spreadDefined("baseRef", !branchExists ? (args.baseRef ?? "HEAD") : undefined),
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
const createdNote = branchExists ? "" : ` (new branch from ${args.baseRef ?? "HEAD"})`;
|
|
124
|
+
return `# Worktree added\n✓ ${wtPath} → ${args.branch}${createdNote}`;
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
// ---------------------------------------------------------------------------
|
|
129
|
+
// git_worktree_remove
|
|
130
|
+
// ---------------------------------------------------------------------------
|
|
131
|
+
export function registerGitWorktreeRemoveTool(server) {
|
|
132
|
+
server.addTool({
|
|
133
|
+
name: "git_worktree_remove",
|
|
134
|
+
description: "Remove a git worktree (`git worktree remove`). " +
|
|
135
|
+
"Pass `force: true` to remove a worktree with uncommitted changes. " +
|
|
136
|
+
"Refuses to remove the main worktree.",
|
|
137
|
+
annotations: {
|
|
138
|
+
readOnlyHint: false,
|
|
139
|
+
destructiveHint: true,
|
|
140
|
+
idempotentHint: false,
|
|
141
|
+
},
|
|
142
|
+
parameters: WorkspacePickSchema.extend({
|
|
143
|
+
path: z
|
|
144
|
+
.string()
|
|
145
|
+
.min(1)
|
|
146
|
+
.describe("Path of the worktree to remove. Must not be the main worktree."),
|
|
147
|
+
force: z
|
|
148
|
+
.boolean()
|
|
149
|
+
.optional()
|
|
150
|
+
.default(false)
|
|
151
|
+
.describe("Pass `--force` to allow removal of worktrees with uncommitted changes."),
|
|
152
|
+
}),
|
|
153
|
+
execute: async (args) => {
|
|
154
|
+
const pre = requireSingleRepo(server, args);
|
|
155
|
+
if (!pre.ok)
|
|
156
|
+
return jsonRespond(pre.error);
|
|
157
|
+
const { gitTop } = pre;
|
|
158
|
+
// Refuse to remove the main worktree
|
|
159
|
+
const wtPath = resolve(gitTop, args.path);
|
|
160
|
+
if (wtPath === gitTop) {
|
|
161
|
+
return jsonRespond({ error: "cannot_remove_main_worktree", path: wtPath });
|
|
162
|
+
}
|
|
163
|
+
// Verify it exists in the worktree list
|
|
164
|
+
const trees = await listWorktrees(gitTop);
|
|
165
|
+
const isRegistered = trees.some((t) => t.path === wtPath || t.path === args.path);
|
|
166
|
+
if (!isRegistered) {
|
|
167
|
+
return jsonRespond({ error: "worktree_not_found", path: args.path });
|
|
168
|
+
}
|
|
169
|
+
const removeArgs = ["worktree", "remove", wtPath];
|
|
170
|
+
if (args.force)
|
|
171
|
+
removeArgs.push("--force");
|
|
172
|
+
const r = await spawnGitAsync(gitTop, removeArgs);
|
|
173
|
+
if (!r.ok) {
|
|
174
|
+
return jsonRespond({
|
|
175
|
+
ok: false,
|
|
176
|
+
error: "worktree_remove_failed",
|
|
177
|
+
detail: (r.stderr || r.stdout).trim(),
|
|
178
|
+
...spreadWhen((r.stderr || r.stdout).includes("contains modified") ||
|
|
179
|
+
(r.stderr || r.stdout).includes("is not empty"), { hint: "Pass force: true to remove a worktree with uncommitted changes." }),
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
if (args.format === "json") {
|
|
183
|
+
return jsonRespond({ ok: true, path: wtPath });
|
|
184
|
+
}
|
|
185
|
+
return `# Worktree removed\n✓ ${wtPath}`;
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
}
|
|
@@ -7,7 +7,10 @@ import { WorkspacePickSchema } from "./schemas.js";
|
|
|
7
7
|
export function registerListPresetsTool(server) {
|
|
8
8
|
server.addTool({
|
|
9
9
|
name: "list_presets",
|
|
10
|
-
description: "List presets from .rethunk/git-mcp-presets.json.
|
|
10
|
+
description: "List presets from .rethunk/git-mcp-presets.json.",
|
|
11
|
+
annotations: {
|
|
12
|
+
readOnlyHint: true,
|
|
13
|
+
},
|
|
11
14
|
parameters: WorkspacePickSchema.pick({
|
|
12
15
|
workspaceRoot: true,
|
|
13
16
|
rootIndex: true,
|
package/dist/server/roots.js
CHANGED
|
@@ -118,3 +118,20 @@ export function requireGitAndRoots(server, args, presetName) {
|
|
|
118
118
|
}
|
|
119
119
|
return { ok: true, roots: rootsRes.roots };
|
|
120
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* Convenience wrapper for single-repo tools: gate git, resolve roots, pick the first
|
|
123
|
+
* root, and resolve its git toplevel. Returns `{ ok: true, gitTop }` or a structured
|
|
124
|
+
* error payload ready for `jsonRespond`.
|
|
125
|
+
*/
|
|
126
|
+
export function requireSingleRepo(server, args, presetName = undefined) {
|
|
127
|
+
const pre = requireGitAndRoots(server, args, presetName);
|
|
128
|
+
if (!pre.ok)
|
|
129
|
+
return pre;
|
|
130
|
+
const rootInput = pre.roots[0];
|
|
131
|
+
if (!rootInput)
|
|
132
|
+
return { ok: false, error: { error: "no_workspace_root" } };
|
|
133
|
+
const top = gitTopLevel(rootInput);
|
|
134
|
+
if (!top)
|
|
135
|
+
return { ok: false, error: { error: "not_a_git_repository", path: rootInput } };
|
|
136
|
+
return { ok: true, gitTop: top };
|
|
137
|
+
}
|
package/dist/server/schemas.js
CHANGED
|
@@ -3,8 +3,17 @@ import { MAX_INVENTORY_ROOTS_DEFAULT } from "./inventory.js";
|
|
|
3
3
|
const FormatSchema = z.enum(["markdown", "json"]).optional().default("markdown");
|
|
4
4
|
export const WorkspacePickSchema = z.object({
|
|
5
5
|
workspaceRoot: z.string().optional().describe("Highest-priority override."),
|
|
6
|
-
rootIndex: z
|
|
7
|
-
|
|
6
|
+
rootIndex: z
|
|
7
|
+
.number()
|
|
8
|
+
.int()
|
|
9
|
+
.min(0)
|
|
10
|
+
.optional()
|
|
11
|
+
.describe("0-based index into the MCP file roots list; ignored when workspaceRoot is set."),
|
|
12
|
+
allWorkspaceRoots: z
|
|
13
|
+
.boolean()
|
|
14
|
+
.optional()
|
|
15
|
+
.default(false)
|
|
16
|
+
.describe("Fan out across all MCP file roots."),
|
|
8
17
|
format: FormatSchema,
|
|
9
18
|
});
|
|
10
19
|
export { MAX_INVENTORY_ROOTS_DEFAULT };
|
|
@@ -16,6 +16,9 @@
|
|
|
16
16
|
* const result = await tool({ workspaceRoot: dir, commits: [...] });
|
|
17
17
|
* // result is string (markdown) or JSON-parseable string
|
|
18
18
|
*/
|
|
19
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
20
|
+
import { tmpdir } from "node:os";
|
|
21
|
+
import { join } from "node:path";
|
|
19
22
|
// Stub context — no tool currently uses context
|
|
20
23
|
const STUB_CONTEXT = {
|
|
21
24
|
log: {
|
|
@@ -28,6 +31,30 @@ const STUB_CONTEXT = {
|
|
|
28
31
|
session: undefined,
|
|
29
32
|
};
|
|
30
33
|
// ---------------------------------------------------------------------------
|
|
34
|
+
// Tmp-dir lifecycle — prevents accumulating thousands of leaked test dirs.
|
|
35
|
+
// Each test file must register the hook itself:
|
|
36
|
+
// afterEach(cleanupTmpPaths);
|
|
37
|
+
// Module-scope afterEach(...) would only register once (first-importer wins)
|
|
38
|
+
// because the module is cached across test files in the same bun test run.
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
const tmpPaths = [];
|
|
41
|
+
export function mkTmpDir(prefix) {
|
|
42
|
+
const dir = mkdtempSync(join(tmpdir(), prefix));
|
|
43
|
+
tmpPaths.push(dir);
|
|
44
|
+
return dir;
|
|
45
|
+
}
|
|
46
|
+
export function trackTmpPath(path) {
|
|
47
|
+
tmpPaths.push(path);
|
|
48
|
+
return path;
|
|
49
|
+
}
|
|
50
|
+
export function cleanupTmpPaths() {
|
|
51
|
+
while (tmpPaths.length > 0) {
|
|
52
|
+
const p = tmpPaths.pop();
|
|
53
|
+
if (p)
|
|
54
|
+
rmSync(p, { recursive: true, force: true });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
// ---------------------------------------------------------------------------
|
|
31
58
|
// Fake server
|
|
32
59
|
// ---------------------------------------------------------------------------
|
|
33
60
|
function makeFakeServer() {
|
package/dist/server/tools.js
CHANGED
|
@@ -1,18 +1,33 @@
|
|
|
1
1
|
import { registerBatchCommitTool } from "./batch-commit-tool.js";
|
|
2
|
+
import { registerGitCherryPickTool } from "./git-cherry-pick-tool.js";
|
|
2
3
|
import { registerGitDiffSummaryTool } from "./git-diff-summary-tool.js";
|
|
3
4
|
import { registerGitInventoryTool } from "./git-inventory-tool.js";
|
|
4
5
|
import { registerGitLogTool } from "./git-log-tool.js";
|
|
6
|
+
import { registerGitMergeTool } from "./git-merge-tool.js";
|
|
5
7
|
import { registerGitParityTool } from "./git-parity-tool.js";
|
|
8
|
+
import { registerGitPushTool } from "./git-push-tool.js";
|
|
9
|
+
import { registerGitResetSoftTool } from "./git-reset-soft-tool.js";
|
|
6
10
|
import { registerGitStatusTool } from "./git-status-tool.js";
|
|
11
|
+
import { registerGitWorktreeAddTool, registerGitWorktreeListTool, registerGitWorktreeRemoveTool, } from "./git-worktree-tool.js";
|
|
7
12
|
import { registerListPresetsTool } from "./list-presets-tool.js";
|
|
8
13
|
import { registerPresetsResource } from "./presets-resource.js";
|
|
9
14
|
export function registerRethunkGitTools(server) {
|
|
15
|
+
// Read-only tools
|
|
10
16
|
registerGitStatusTool(server);
|
|
11
17
|
registerGitInventoryTool(server);
|
|
12
18
|
registerGitParityTool(server);
|
|
13
19
|
registerListPresetsTool(server);
|
|
14
|
-
registerBatchCommitTool(server);
|
|
15
|
-
registerGitDiffSummaryTool(server);
|
|
16
20
|
registerGitLogTool(server);
|
|
21
|
+
registerGitDiffSummaryTool(server);
|
|
22
|
+
registerGitWorktreeListTool(server);
|
|
23
|
+
// Mutating tools
|
|
24
|
+
registerBatchCommitTool(server);
|
|
25
|
+
registerGitPushTool(server);
|
|
26
|
+
registerGitMergeTool(server);
|
|
27
|
+
registerGitCherryPickTool(server);
|
|
28
|
+
registerGitResetSoftTool(server);
|
|
29
|
+
registerGitWorktreeAddTool(server);
|
|
30
|
+
registerGitWorktreeRemoveTool(server);
|
|
31
|
+
// Resources
|
|
17
32
|
registerPresetsResource(server);
|
|
18
33
|
}
|