-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-refs.test.ts
More file actions
236 lines (197 loc) · 8.31 KB
/
git-refs.test.ts
File metadata and controls
236 lines (197 loc) · 8.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
* Pure-helper tests for src/server/git-refs.ts.
*
* Async helpers (getCurrentBranch, isWorkingTreeClean, etc.) are exercised
* through the integration tests for git_merge and git_cherry_pick.
*/
import { afterEach, describe, expect, test } from "bun:test";
import { writeFileSync } from "node:fs";
import { join } from "node:path";
import {
isContentEquivalentlyMergedInto,
isProtectedBranch,
isSafeGitAncestorRef,
isSafeGitRangeToken,
isSafeGitRefToken,
} from "./git-refs.js";
import { cleanupTmpPaths, gitCmd, makeRepoWithSeed } from "./test-harness.js";
afterEach(cleanupTmpPaths);
// ---------------------------------------------------------------------------
// Integration: isContentEquivalentlyMergedInto
// ---------------------------------------------------------------------------
function makeRepo(): string {
return makeRepoWithSeed("mcp-git-refs-test-");
}
function addFile(dir: string, name: string, content: string, msg: string): void {
writeFileSync(join(dir, name), content);
gitCmd(dir, "add", name);
gitCmd(dir, "commit", "-m", msg);
}
describe("isContentEquivalentlyMergedInto", () => {
test("returns true when branch is a direct ancestor of target (fast-forward merged)", async () => {
const dir = makeRepo();
addFile(dir, "a.ts", "const a = 1;\n", "feat: a");
// Create feature branch at same commit, then add another commit on main.
gitCmd(dir, "checkout", "-b", "feature");
addFile(dir, "b.ts", "const b = 2;\n", "feat: b");
gitCmd(dir, "checkout", "main");
gitCmd(dir, "merge", "--ff-only", "feature");
expect(await isContentEquivalentlyMergedInto(dir, "feature", "main")).toBe(true);
});
test("returns false when branch has commits not on target", async () => {
const dir = makeRepo();
addFile(dir, "a.ts", "const a = 1;\n", "feat: a");
gitCmd(dir, "checkout", "-b", "feature");
addFile(dir, "b.ts", "const b = 2;\n", "feat: b");
gitCmd(dir, "checkout", "main");
// Do NOT merge — feature is ahead of main.
expect(await isContentEquivalentlyMergedInto(dir, "feature", "main")).toBe(false);
});
test("returns true when cherry-picked commits are content-equivalent (different SHA)", async () => {
const dir = makeRepo();
addFile(dir, "a.ts", "const a = 1;\n", "feat: base");
// Feature branch adds a commit.
gitCmd(dir, "checkout", "-b", "feature");
addFile(dir, "b.ts", "const b = 2;\n", "feat: b");
const featureSha = gitCmd(dir, "rev-parse", "HEAD").trim();
// Main advances so cherry-pick produces a different SHA.
gitCmd(dir, "checkout", "main");
addFile(dir, "c.ts", "const c = 3;\n", "feat: unrelated");
gitCmd(dir, "cherry-pick", featureSha);
expect(await isContentEquivalentlyMergedInto(dir, "feature", "main")).toBe(true);
});
test("returns false for unsafe ref tokens", async () => {
const dir = makeRepo();
addFile(dir, "a.ts", "const a = 1;\n", "feat: a");
expect(await isContentEquivalentlyMergedInto(dir, "-bad-branch", "main")).toBe(false);
expect(await isContentEquivalentlyMergedInto(dir, "main", "-bad-target")).toBe(false);
});
});
describe("isProtectedBranch", () => {
test("exact protected names are protected", () => {
for (const name of [
"main",
"master",
"dev",
"develop",
"stable",
"trunk",
"prod",
"production",
"HEAD",
]) {
expect(isProtectedBranch(name)).toBe(true);
}
});
test("release/* and hotfix/* are protected (case-insensitive)", () => {
expect(isProtectedBranch("release/1.2.3")).toBe(true);
expect(isProtectedBranch("release-2024-04")).toBe(true);
expect(isProtectedBranch("hotfix/security-patch")).toBe(true);
expect(isProtectedBranch("Release/foo")).toBe(true);
});
test("feature/agent branches are not protected", () => {
expect(isProtectedBranch("feature/auth")).toBe(false);
expect(isProtectedBranch("worktree-agent-abc")).toBe(false);
expect(isProtectedBranch("fix/login")).toBe(false);
});
test("empty / whitespace treated as protected (refuse)", () => {
expect(isProtectedBranch("")).toBe(true);
expect(isProtectedBranch(" ")).toBe(true);
});
test("release on its own (no suffix) is not matched by pattern", () => {
// Pattern requires at least one char after release[-/]
expect(isProtectedBranch("release")).toBe(false);
});
test("refs/heads/ prefix is stripped before checking", () => {
expect(isProtectedBranch("refs/heads/main")).toBe(true);
expect(isProtectedBranch("refs/heads/master")).toBe(true);
expect(isProtectedBranch("refs/heads/develop")).toBe(true);
expect(isProtectedBranch("refs/heads/feature/auth")).toBe(false);
});
test("case-insensitive matching for exact names", () => {
expect(isProtectedBranch("Main")).toBe(true);
expect(isProtectedBranch("MAIN")).toBe(true);
expect(isProtectedBranch("MASTER")).toBe(true);
expect(isProtectedBranch("Master")).toBe(true);
expect(isProtectedBranch("HEAD")).toBe(true);
expect(isProtectedBranch("head")).toBe(true);
expect(isProtectedBranch("Prod")).toBe(true);
});
});
describe("isSafeGitRefToken", () => {
test("accepts simple branch names", () => {
expect(isSafeGitRefToken("main")).toBe(true);
expect(isSafeGitRefToken("feature/auth")).toBe(true);
expect(isSafeGitRefToken("v1.2.3")).toBe(true);
expect(isSafeGitRefToken("release-2024-04")).toBe(true);
});
test("rejects leading dash, double dots, @{, trailing slash / .lock", () => {
expect(isSafeGitRefToken("-evil")).toBe(false);
expect(isSafeGitRefToken("a..b")).toBe(false);
expect(isSafeGitRefToken("HEAD@{1}")).toBe(false);
expect(isSafeGitRefToken("refs/heads/main/")).toBe(false);
expect(isSafeGitRefToken("main.lock")).toBe(false);
});
test("rejects shell metacharacters and spaces", () => {
expect(isSafeGitRefToken("a;b")).toBe(false);
expect(isSafeGitRefToken("a b")).toBe(false);
expect(isSafeGitRefToken("a$(b)")).toBe(false);
expect(isSafeGitRefToken("a|b")).toBe(false);
});
test("rejects empty and too-long", () => {
expect(isSafeGitRefToken("")).toBe(false);
expect(isSafeGitRefToken("a".repeat(257))).toBe(false);
});
});
describe("isSafeGitRangeToken", () => {
test("accepts plain refs", () => {
expect(isSafeGitRangeToken("main")).toBe(true);
expect(isSafeGitRangeToken("HEAD")).toBe(true);
});
test("accepts A..B and A...B with valid sides", () => {
expect(isSafeGitRangeToken("main..feature")).toBe(true);
expect(isSafeGitRangeToken("main...feature")).toBe(true);
expect(isSafeGitRangeToken("v1.0.0..v1.1.0")).toBe(true);
});
test("rejects ranges with more than two endpoints", () => {
expect(isSafeGitRangeToken("a..b..c")).toBe(false);
});
test("rejects ranges with invalid endpoint", () => {
expect(isSafeGitRangeToken("main..-evil")).toBe(false);
expect(isSafeGitRangeToken("-a..b")).toBe(false);
expect(isSafeGitRangeToken("a..b$(c)")).toBe(false);
});
test("rejects empty endpoints", () => {
expect(isSafeGitRangeToken("..b")).toBe(false);
expect(isSafeGitRangeToken("a..")).toBe(false);
});
});
// ---------------------------------------------------------------------------
// isSafeGitAncestorRef
// ---------------------------------------------------------------------------
describe("isSafeGitAncestorRef", () => {
test("accepts HEAD~N ancestor notation", () => {
expect(isSafeGitAncestorRef("HEAD~1")).toBe(true);
expect(isSafeGitAncestorRef("HEAD~10")).toBe(true);
expect(isSafeGitAncestorRef("HEAD^1")).toBe(true);
});
test("accepts plain branch names and full SHAs", () => {
expect(isSafeGitAncestorRef("main")).toBe(true);
expect(isSafeGitAncestorRef("feature/auth")).toBe(true);
expect(isSafeGitAncestorRef("a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2")).toBe(true);
});
test("rejects leading dash", () => {
expect(isSafeGitAncestorRef("-ref")).toBe(false);
});
test("rejects shell metacharacters", () => {
expect(isSafeGitAncestorRef("HEAD;evil")).toBe(false);
expect(isSafeGitAncestorRef("HEAD$(cmd)")).toBe(false);
expect(isSafeGitAncestorRef("HEAD ref")).toBe(false);
});
test("rejects empty string", () => {
expect(isSafeGitAncestorRef("")).toBe(false);
});
test("rejects string longer than 256 chars", () => {
expect(isSafeGitAncestorRef("a".repeat(257))).toBe(false);
});
});