plugin-git-manager 1.3.3 → 1.4.1
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/dist/client/{156.c27c2392746e7866.js → 156.8e36af3e2cee173e.js} +1 -1
- package/dist/client/317.e7b1d59371812c30.js +10 -0
- package/dist/client/434.05d0ba2b821105dc.js +10 -0
- package/dist/client/453.cdcba6d2d0c9522b.js +10 -0
- package/dist/client/454.625da741a7e1161f.js +10 -0
- package/dist/client/index.js +1 -1
- package/dist/client-v2/317.4e116f84ed9081b6.js +10 -0
- package/dist/client-v2/434.764113d20743a45b.js +10 -0
- package/dist/client-v2/453.5ab80c06d8e61a7e.js +10 -0
- package/dist/client-v2/454.b885b1d35ef7ff72.js +10 -0
- package/dist/client-v2/index.js +1 -1
- package/dist/externalVersion.js +10 -9
- package/dist/locale/en-US.json +38 -1
- package/dist/locale/vi-VN.json +38 -1
- package/dist/locale/zh-CN.json +41 -1
- package/dist/node_modules/simple-git/package.json +1 -1
- package/dist/node_modules/zod/package.json +1 -1
- package/dist/server/actions/chat-context.js +282 -0
- package/dist/server/actions/git-actions.js +387 -80
- package/dist/server/actions/gitlab-api.js +101 -15
- package/dist/server/actions/subtree.js +20 -10
- package/dist/server/chat-context-admission.js +187 -0
- package/dist/server/plugin.js +142 -10
- package/dist/server/repository-access.js +2 -2
- package/dist/server/request-method-policy.js +4 -1
- package/dist/server/services/review-source.js +2 -1
- package/dist/server/utils/cluster-git.js +181 -0
- package/package.json +2 -2
- package/src/client/__tests__/FileExplorerAndManage.test.tsx +236 -0
- package/src/client/__tests__/GitContextPicker.test.tsx +120 -0
- package/src/client/__tests__/ai-context.test.tsx +119 -0
- package/src/client/__tests__/workflowRoutes.test.ts +3 -1
- package/src/client/ai-context.tsx +3 -113
- package/src/client/components/CommitHistory.tsx +19 -7
- package/src/client/components/FileExplorer.tsx +80 -34
- package/src/client/components/GitManagerSettings.tsx +21 -5
- package/src/client/components/GitOperations.tsx +25 -4
- package/src/client/components/GitRuntimeBanner.tsx +97 -0
- package/src/client/components/RepositoryConfig.tsx +28 -6
- package/src/client/context/GitManagerContext.tsx +174 -15
- package/src/client/index.tsx +3 -5
- package/src/client/workflowRoutes.ts +11 -0
- package/src/client-v2/ai-context.tsx +135 -72
- package/src/client-v2/components/GitContextPicker.tsx +418 -0
- package/src/client-v2/git-context-api.ts +59 -0
- package/src/client-v2/plugin.tsx +2 -4
- package/src/locale/en-US.json +38 -1
- package/src/locale/vi-VN.json +38 -1
- package/src/locale/zh-CN.json +41 -1
- package/src/server/__tests__/chat-context-admission.test.ts +276 -0
- package/src/server/__tests__/chat-context-options.test.ts +144 -0
- package/src/server/__tests__/git-actions.test.ts +329 -4
- package/src/server/__tests__/git-chat-context.test.ts +138 -0
- package/src/server/__tests__/git-security.test.ts +52 -0
- package/src/server/__tests__/smoke.test.ts +232 -1
- package/src/server/actions/chat-context.ts +302 -0
- package/src/server/actions/git-actions.ts +428 -89
- package/src/server/actions/gitlab-api.ts +113 -14
- package/src/server/actions/subtree.ts +23 -6
- package/src/server/chat-context-admission.ts +182 -0
- package/src/server/plugin.ts +163 -3
- package/src/server/repository-access.ts +2 -2
- package/src/server/request-method-policy.ts +3 -0
- package/src/server/services/review-source.ts +3 -2
- package/src/server/utils/cluster-git.ts +209 -0
- package/dist/client/317.17ed0018e3e7db00.js +0 -10
- package/dist/client/434.b2eeaaef33aae6e0.js +0 -10
- package/dist/client/453.fc3042c9740d8fab.js +0 -10
- package/dist/client-v2/317.d2466574d00f94d0.js +0 -10
- package/dist/client-v2/434.19c7579c06b9077c.js +0 -10
- package/dist/client-v2/453.5e507c94c59277b1.js +0 -10
|
@@ -37,6 +37,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
37
37
|
var git_actions_exports = {};
|
|
38
38
|
__export(git_actions_exports, {
|
|
39
39
|
acquireLock: () => acquireLock,
|
|
40
|
+
assertGitAvailable: () => assertGitAvailable,
|
|
41
|
+
assertLocalGitReady: () => assertLocalGitReady,
|
|
40
42
|
branches: () => branches,
|
|
41
43
|
checkout: () => checkout,
|
|
42
44
|
clone: () => clone,
|
|
@@ -47,20 +49,31 @@ __export(git_actions_exports, {
|
|
|
47
49
|
fetch: () => fetch,
|
|
48
50
|
fileContent: () => fileContent,
|
|
49
51
|
fileTree: () => fileTree,
|
|
52
|
+
getGit: () => getGit,
|
|
50
53
|
getGitBinaryPath: () => getGitBinaryPath,
|
|
54
|
+
getGitMissingMessage: () => getGitMissingMessage,
|
|
51
55
|
getRepo: () => getRepo,
|
|
56
|
+
installGit: () => installGit,
|
|
57
|
+
isGitAvailable: () => isGitAvailable,
|
|
52
58
|
log: () => log,
|
|
53
59
|
pull: () => pull,
|
|
54
60
|
push: () => push,
|
|
61
|
+
readChatCommit: () => readChatCommit,
|
|
62
|
+
resetGitBinaryCacheForTests: () => resetGitBinaryCacheForTests,
|
|
63
|
+
resolveGitBinary: () => resolveGitBinary,
|
|
64
|
+
runtimeStatus: () => runtimeStatus,
|
|
55
65
|
status: () => status,
|
|
56
66
|
validateBranch: () => validateBranch,
|
|
57
67
|
validateLocalPath: () => validateLocalPath,
|
|
58
68
|
validateRef: () => validateRef,
|
|
69
|
+
validateRepoRelativePath: () => validateRepoRelativePath,
|
|
59
70
|
validateRepoUrl: () => validateRepoUrl,
|
|
60
|
-
withAuth: () => withAuth
|
|
71
|
+
withAuth: () => withAuth,
|
|
72
|
+
withRepoLock: () => withRepoLock
|
|
61
73
|
});
|
|
62
74
|
module.exports = __toCommonJS(git_actions_exports);
|
|
63
75
|
var import_simple_git = __toESM(require("simple-git"));
|
|
76
|
+
var import_crypto = require("crypto");
|
|
64
77
|
var path = __toESM(require("path"));
|
|
65
78
|
var fs = __toESM(require("fs"));
|
|
66
79
|
var import_child_process = require("child_process");
|
|
@@ -68,10 +81,12 @@ var import_repository_access = require("../repository-access");
|
|
|
68
81
|
var import_redact = require("../utils/redact");
|
|
69
82
|
var import_get_repo_account = require("../utils/get-repo-account");
|
|
70
83
|
var import_url_security = require("../utils/url-security");
|
|
84
|
+
var import_cluster_git = require("../utils/cluster-git");
|
|
71
85
|
const REF_PATTERN = /^(?!-)[a-zA-Z0-9._/-]+$/;
|
|
86
|
+
const FALLBACK_GIT_PATHS = ["git", "/usr/bin/git", "/usr/local/bin/git", "/bin/git"];
|
|
87
|
+
const REPO_LOCK_TTL_MS = 5 * 60 * 1e3;
|
|
72
88
|
const repoLocks = /* @__PURE__ */ new Map();
|
|
73
|
-
|
|
74
|
-
let gitAvailabilityChecked = false;
|
|
89
|
+
let cachedGitBinary = null;
|
|
75
90
|
function getGitActionParams(ctx) {
|
|
76
91
|
return (0, import_repository_access.getEffectiveActionParams)(ctx);
|
|
77
92
|
}
|
|
@@ -118,24 +133,57 @@ function validateRepoUrl(repoUrl) {
|
|
|
118
133
|
throw new Error("Only HTTPS repository URLs are allowed");
|
|
119
134
|
}
|
|
120
135
|
}
|
|
121
|
-
async function
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
136
|
+
async function withRepoLock(localPath, fn, lockManager) {
|
|
137
|
+
const runLocal = async () => {
|
|
138
|
+
const lock = acquireLock(localPath);
|
|
139
|
+
await lock.promise;
|
|
140
|
+
try {
|
|
141
|
+
return await fn();
|
|
142
|
+
} finally {
|
|
143
|
+
lock.release();
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
if (lockManager == null ? void 0 : lockManager.runExclusive) {
|
|
147
|
+
const lockKey = `git-repo:${(0, import_crypto.createHash)("sha256").update(localPath).digest("hex")}`;
|
|
148
|
+
return lockManager.runExclusive(lockKey, runLocal, REPO_LOCK_TTL_MS);
|
|
149
|
+
}
|
|
150
|
+
return runLocal();
|
|
151
|
+
}
|
|
152
|
+
function validateRepoRelativePath(rawPath, label = "file path") {
|
|
153
|
+
if (rawPath.includes("\0")) {
|
|
154
|
+
throw new Error(`Invalid ${label}`);
|
|
155
|
+
}
|
|
156
|
+
const normalized = rawPath.replace(/\\/g, "/").replace(/^\/+|\/+$/g, "");
|
|
157
|
+
if (!normalized || /^[a-zA-Z]:\//.test(normalized)) {
|
|
158
|
+
throw new Error(`Invalid ${label}`);
|
|
159
|
+
}
|
|
160
|
+
if (normalized.split("/").some((segment) => !segment || segment === "." || segment === "..")) {
|
|
161
|
+
throw new Error(`Invalid ${label}`);
|
|
162
|
+
}
|
|
163
|
+
return normalized;
|
|
164
|
+
}
|
|
165
|
+
async function runAuthenticatedGitUnlocked(git, repoUrl, pat, fn, username, remoteName = "origin") {
|
|
125
166
|
try {
|
|
126
167
|
git.env(credentialEnvironment(repoUrl, pat, username));
|
|
127
|
-
await git.
|
|
168
|
+
const currentUrl = typeof git.raw === "function" ? (await git.raw(["config", "--get", `remote.${remoteName}.url`]).catch(() => "")).trim() : "";
|
|
169
|
+
if (currentUrl !== repoUrl) {
|
|
170
|
+
await git.remote(["set-url", remoteName, repoUrl]);
|
|
171
|
+
}
|
|
128
172
|
return await fn();
|
|
129
173
|
} catch (error) {
|
|
130
174
|
throw redactGitCredentialError(error, pat, username);
|
|
131
175
|
} finally {
|
|
132
|
-
|
|
133
|
-
git.env(gitProcessEnvironment());
|
|
134
|
-
} finally {
|
|
135
|
-
lock.release();
|
|
136
|
-
}
|
|
176
|
+
git.env(gitProcessEnvironment());
|
|
137
177
|
}
|
|
138
178
|
}
|
|
179
|
+
async function withAuth(git, localPath, repoUrl, pat, fn, username, remoteName = "origin", lockManager) {
|
|
180
|
+
validateRepoUrl(repoUrl);
|
|
181
|
+
return withRepoLock(
|
|
182
|
+
localPath,
|
|
183
|
+
() => runAuthenticatedGitUnlocked(git, repoUrl, pat, fn, username, remoteName),
|
|
184
|
+
lockManager
|
|
185
|
+
);
|
|
186
|
+
}
|
|
139
187
|
function gitProcessEnvironment() {
|
|
140
188
|
const env = { ...process.env };
|
|
141
189
|
for (const key of Object.keys(env)) {
|
|
@@ -174,16 +222,82 @@ function redactGitCredentialError(error, pat, username) {
|
|
|
174
222
|
}
|
|
175
223
|
return (0, import_redact.redactError)(new Error(safeMessage));
|
|
176
224
|
}
|
|
177
|
-
function
|
|
178
|
-
return
|
|
225
|
+
function configuredGitBinary() {
|
|
226
|
+
return (process.env.GIT_BINARY_PATH || process.env.GIT_EXECUTABLE || "").trim();
|
|
227
|
+
}
|
|
228
|
+
function canExecuteGit(binary) {
|
|
229
|
+
if (path.isAbsolute(binary) && !fs.existsSync(binary)) {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
const check = (0, import_child_process.spawnSync)(binary, ["--version"], { stdio: "ignore" });
|
|
233
|
+
return !check.error && check.status === 0;
|
|
234
|
+
}
|
|
235
|
+
function resolveGitBinary() {
|
|
236
|
+
const configured = configuredGitBinary();
|
|
237
|
+
if (cachedGitBinary && cachedGitBinary.envValue === configured && (!path.isAbsolute(cachedGitBinary.binary) || fs.existsSync(cachedGitBinary.binary)) && canExecuteGit(cachedGitBinary.binary)) {
|
|
238
|
+
return cachedGitBinary.binary;
|
|
239
|
+
}
|
|
240
|
+
cachedGitBinary = null;
|
|
241
|
+
const candidates = configured ? [configured] : FALLBACK_GIT_PATHS;
|
|
242
|
+
for (const candidate of candidates) {
|
|
243
|
+
if (canExecuteGit(candidate)) {
|
|
244
|
+
cachedGitBinary = { envValue: configured, binary: candidate };
|
|
245
|
+
return candidate;
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
function resetGitBinaryCacheForTests() {
|
|
251
|
+
cachedGitBinary = null;
|
|
252
|
+
}
|
|
253
|
+
function isGitAvailable() {
|
|
254
|
+
return resolveGitBinary() !== null;
|
|
255
|
+
}
|
|
256
|
+
function getGitMissingMessage(clusterStatus) {
|
|
257
|
+
const binary = configuredGitBinary() || "git";
|
|
258
|
+
if ((clusterStatus == null ? void 0 : clusterStatus.available) && clusterStatus.status === "running") {
|
|
259
|
+
return `Git executable "${binary}" is currently being installed via Cluster Manager. Please wait a moment and retry.`;
|
|
260
|
+
}
|
|
261
|
+
if (clusterStatus == null ? void 0 : clusterStatus.available) {
|
|
262
|
+
return `Git executable "${binary}" was not found on the server. Install git via Cluster Manager (Package Installer), or set GIT_BINARY_PATH to the absolute git binary path.`;
|
|
263
|
+
}
|
|
264
|
+
return `Git executable "${binary}" was not found on the server. Install git in the app/worker container, or set GIT_BINARY_PATH to the absolute git binary path.`;
|
|
179
265
|
}
|
|
180
|
-
function assertGitAvailable(ctx) {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
266
|
+
async function assertGitAvailable(ctx) {
|
|
267
|
+
const binary = resolveGitBinary();
|
|
268
|
+
if (binary) return binary;
|
|
269
|
+
const app = ctx.app;
|
|
270
|
+
const clusterStatus = await (0, import_cluster_git.readClusterGitStatus)(app).catch(() => void 0);
|
|
271
|
+
if ((clusterStatus == null ? void 0 : clusterStatus.available) && clusterStatus.gitConfigured && clusterStatus.previouslyInitialized) {
|
|
272
|
+
(0, import_cluster_git.dispatchClusterGitInstall)(app).catch((err) => {
|
|
273
|
+
var _a, _b;
|
|
274
|
+
(_b = (_a = app == null ? void 0 : app.log) == null ? void 0 : _a.warn) == null ? void 0 : _b.call(_a, "plugin-git-manager: background git restore via Cluster Manager failed", err);
|
|
275
|
+
});
|
|
276
|
+
const refreshedStatus = { ...clusterStatus, status: "running" };
|
|
277
|
+
return ctx.throw(503, getGitMissingMessage(refreshedStatus));
|
|
278
|
+
}
|
|
279
|
+
return ctx.throw(503, getGitMissingMessage(clusterStatus));
|
|
280
|
+
}
|
|
281
|
+
async function assertLocalGitReady(localPath, app) {
|
|
282
|
+
if (!fs.existsSync(localPath)) {
|
|
283
|
+
const missingDirError = Object.assign(
|
|
284
|
+
new Error("Repository directory does not exist. Please clone the repository first."),
|
|
285
|
+
{ status: 400 }
|
|
286
|
+
);
|
|
287
|
+
throw missingDirError;
|
|
288
|
+
}
|
|
289
|
+
const binary = resolveGitBinary();
|
|
290
|
+
if (!binary) {
|
|
291
|
+
const clusterStatus = await (0, import_cluster_git.readClusterGitStatus)(app).catch(() => void 0);
|
|
292
|
+
if ((clusterStatus == null ? void 0 : clusterStatus.available) && clusterStatus.gitConfigured && clusterStatus.previouslyInitialized) {
|
|
293
|
+
(0, import_cluster_git.dispatchClusterGitInstall)(app).catch((err) => {
|
|
294
|
+
var _a, _b;
|
|
295
|
+
(_b = (_a = app == null ? void 0 : app.log) == null ? void 0 : _a.warn) == null ? void 0 : _b.call(_a, "plugin-git-manager: background git restore via Cluster Manager failed", err);
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
throw Object.assign(new Error(getGitMissingMessage(clusterStatus)), { status: 503 });
|
|
185
299
|
}
|
|
186
|
-
|
|
300
|
+
return createGit(localPath);
|
|
187
301
|
}
|
|
188
302
|
function isMissingGitError(error) {
|
|
189
303
|
const message = error instanceof Error ? error.message : String(error);
|
|
@@ -192,22 +306,62 @@ function isMissingGitError(error) {
|
|
|
192
306
|
function createGit(baseDir) {
|
|
193
307
|
return (0, import_simple_git.default)({
|
|
194
308
|
baseDir,
|
|
195
|
-
binary:
|
|
309
|
+
binary: getGitBinaryPath(),
|
|
196
310
|
unsafe: { allowUnsafeConfigEnvCount: true, allowUnsafeCredentialHelper: true }
|
|
197
311
|
}).env(gitProcessEnvironment());
|
|
198
312
|
}
|
|
199
313
|
function getGitBinaryPath() {
|
|
200
|
-
return
|
|
314
|
+
return resolveGitBinary() || configuredGitBinary() || "git";
|
|
201
315
|
}
|
|
202
|
-
function getGit(ctx, localPath) {
|
|
316
|
+
async function getGit(ctx, localPath) {
|
|
203
317
|
if (!fs.existsSync(localPath)) {
|
|
204
318
|
ctx.throw(400, "Repository directory does not exist. Please clone the repository first.");
|
|
205
319
|
}
|
|
206
|
-
assertGitAvailable(ctx);
|
|
320
|
+
await assertGitAvailable(ctx);
|
|
207
321
|
return createGit(localPath);
|
|
208
322
|
}
|
|
209
|
-
async function
|
|
210
|
-
const
|
|
323
|
+
async function runtimeStatus(ctx, next) {
|
|
324
|
+
const binary = resolveGitBinary();
|
|
325
|
+
const app = ctx.app;
|
|
326
|
+
const clusterStatus = await (0, import_cluster_git.readClusterGitStatus)(app);
|
|
327
|
+
if (!binary && clusterStatus.available && clusterStatus.gitConfigured && clusterStatus.previouslyInitialized) {
|
|
328
|
+
const restore = await (0, import_cluster_git.dispatchClusterGitInstall)(app).catch(() => ({ dispatched: false }));
|
|
329
|
+
if (restore.dispatched) {
|
|
330
|
+
clusterStatus.status = "running";
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
ctx.body = {
|
|
334
|
+
success: true,
|
|
335
|
+
data: {
|
|
336
|
+
gitAvailable: binary !== null,
|
|
337
|
+
gitBinary: binary || configuredGitBinary() || "git",
|
|
338
|
+
clusterManager: {
|
|
339
|
+
available: clusterStatus.available,
|
|
340
|
+
gitConfigured: clusterStatus.gitConfigured,
|
|
341
|
+
status: clusterStatus.status,
|
|
342
|
+
progressPercent: clusterStatus.progressPercent
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
await next();
|
|
347
|
+
}
|
|
348
|
+
async function installGit(ctx, next) {
|
|
349
|
+
const binary = resolveGitBinary();
|
|
350
|
+
if (binary) {
|
|
351
|
+
ctx.body = { success: true, data: { alreadyInstalled: true, gitBinary: binary } };
|
|
352
|
+
await next();
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
const app = ctx.app;
|
|
356
|
+
const result = await (0, import_cluster_git.dispatchClusterGitInstall)(app, { ensureGitInConfig: true });
|
|
357
|
+
if (!result.dispatched && result.reason !== "Cluster package installation is already running") {
|
|
358
|
+
ctx.throw(400, result.reason || "Unable to dispatch git installation via Cluster Manager");
|
|
359
|
+
}
|
|
360
|
+
ctx.body = { success: true, data: { dispatched: true } };
|
|
361
|
+
await next();
|
|
362
|
+
}
|
|
363
|
+
async function getRepo(ctx, requestedRepositoryId) {
|
|
364
|
+
const repositoryId = requestedRepositoryId ?? getGitActionParams(ctx).repositoryId;
|
|
211
365
|
const repo = await ctx.db.getRepository("gitRepositories").findOne({
|
|
212
366
|
filterByTk: repositoryId
|
|
213
367
|
});
|
|
@@ -257,65 +411,94 @@ async function clone(ctx, next) {
|
|
|
257
411
|
const { pat, username } = account;
|
|
258
412
|
validateRepoUrl(repoUrl);
|
|
259
413
|
validateBranch(defaultBranch);
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
414
|
+
await assertGitAvailable(ctx);
|
|
415
|
+
const app = ctx.app;
|
|
416
|
+
await withRepoLock(
|
|
417
|
+
localPath,
|
|
418
|
+
async () => {
|
|
419
|
+
if (fs.existsSync(localPath)) {
|
|
420
|
+
const entries = fs.readdirSync(localPath);
|
|
421
|
+
if (entries.length > 0) {
|
|
422
|
+
if (fs.existsSync(path.join(localPath, ".git"))) {
|
|
423
|
+
const existingGit = createGit(localPath);
|
|
424
|
+
const existingOrigin = (await existingGit.raw(["config", "--get", "remote.origin.url"]).catch(() => "")).trim();
|
|
425
|
+
if (existingOrigin === repoUrl) {
|
|
426
|
+
await runAuthenticatedGitUnlocked(existingGit, repoUrl, pat, () => existingGit.fetch(), username);
|
|
427
|
+
await ctx.db.getRepository("gitRepositories").update({
|
|
428
|
+
filterByTk: repo.get("id"),
|
|
429
|
+
values: { status: "connected" }
|
|
430
|
+
});
|
|
431
|
+
ctx.body = { success: true, message: "Repository reconnected successfully" };
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
ctx.throw(400, "Directory already exists. Remove it before cloning again.");
|
|
436
|
+
}
|
|
437
|
+
} else if (!fs.existsSync(path.dirname(localPath))) {
|
|
438
|
+
fs.mkdirSync(path.dirname(localPath), { recursive: true });
|
|
439
|
+
}
|
|
440
|
+
try {
|
|
441
|
+
await createGit().env(credentialEnvironment(repoUrl, pat, username)).clone(repoUrl, localPath, ["--branch", defaultBranch]);
|
|
442
|
+
await ctx.db.getRepository("gitRepositories").update({
|
|
443
|
+
filterByTk: repo.get("id"),
|
|
444
|
+
values: { status: "connected" }
|
|
445
|
+
});
|
|
446
|
+
ctx.body = { success: true, message: "Repository cloned successfully" };
|
|
447
|
+
} catch (err) {
|
|
448
|
+
await ctx.db.getRepository("gitRepositories").update({
|
|
449
|
+
filterByTk: repo.get("id"),
|
|
450
|
+
values: { status: "error" }
|
|
451
|
+
});
|
|
452
|
+
if (isMissingGitError(err) && !isGitAvailable()) {
|
|
453
|
+
const clusterStatus = await (0, import_cluster_git.readClusterGitStatus)(app).catch(() => void 0);
|
|
454
|
+
ctx.throw(503, getGitMissingMessage(clusterStatus));
|
|
455
|
+
}
|
|
456
|
+
throw redactGitCredentialError(err, pat, username);
|
|
457
|
+
}
|
|
458
|
+
},
|
|
459
|
+
app == null ? void 0 : app.lockManager
|
|
460
|
+
);
|
|
284
461
|
await next();
|
|
285
462
|
}
|
|
286
463
|
async function pull(ctx, next) {
|
|
464
|
+
var _a;
|
|
287
465
|
const repo = await getRepo(ctx);
|
|
288
466
|
const localPath = validateLocalPath(repo.get("localPath"));
|
|
289
467
|
const repoUrl = (repo.get("repoUrl") || "").trim();
|
|
290
468
|
const account = await (0, import_get_repo_account.getRepoAccount)(ctx.db, repo);
|
|
291
469
|
if (!account) return ctx.throw(400, "Repository has no Git account configured. Please assign a Git account first.");
|
|
292
470
|
const { pat, username } = account;
|
|
293
|
-
const git = getGit(ctx, localPath);
|
|
294
|
-
const
|
|
471
|
+
const git = await getGit(ctx, localPath);
|
|
472
|
+
const lockManager = (_a = ctx.app) == null ? void 0 : _a.lockManager;
|
|
473
|
+
const result = await withAuth(git, localPath, repoUrl, pat, () => git.pull(), username, "origin", lockManager);
|
|
295
474
|
ctx.body = { success: true, data: result };
|
|
296
475
|
await next();
|
|
297
476
|
}
|
|
298
477
|
async function push(ctx, next) {
|
|
478
|
+
var _a;
|
|
299
479
|
const repo = await getRepo(ctx);
|
|
300
480
|
const localPath = validateLocalPath(repo.get("localPath"));
|
|
301
481
|
const repoUrl = (repo.get("repoUrl") || "").trim();
|
|
302
482
|
const account = await (0, import_get_repo_account.getRepoAccount)(ctx.db, repo);
|
|
303
483
|
if (!account) return ctx.throw(400, "Repository has no Git account configured. Please assign a Git account first.");
|
|
304
484
|
const { pat, username } = account;
|
|
305
|
-
const git = getGit(ctx, localPath);
|
|
306
|
-
const
|
|
485
|
+
const git = await getGit(ctx, localPath);
|
|
486
|
+
const lockManager = (_a = ctx.app) == null ? void 0 : _a.lockManager;
|
|
487
|
+
const result = await withAuth(git, localPath, repoUrl, pat, () => git.push(), username, "origin", lockManager);
|
|
307
488
|
ctx.body = { success: true, data: result };
|
|
308
489
|
await next();
|
|
309
490
|
}
|
|
310
491
|
async function fetch(ctx, next) {
|
|
492
|
+
var _a;
|
|
311
493
|
const repo = await getRepo(ctx);
|
|
312
494
|
const localPath = validateLocalPath(repo.get("localPath"));
|
|
313
495
|
const repoUrl = (repo.get("repoUrl") || "").trim();
|
|
314
496
|
const account = await (0, import_get_repo_account.getRepoAccount)(ctx.db, repo);
|
|
315
497
|
if (!account) return ctx.throw(400, "Repository has no Git account configured. Please assign a Git account first.");
|
|
316
498
|
const { pat, username } = account;
|
|
317
|
-
const git = getGit(ctx, localPath);
|
|
318
|
-
const
|
|
499
|
+
const git = await getGit(ctx, localPath);
|
|
500
|
+
const lockManager = (_a = ctx.app) == null ? void 0 : _a.lockManager;
|
|
501
|
+
const result = await withAuth(git, localPath, repoUrl, pat, () => git.fetch(), username, "origin", lockManager);
|
|
319
502
|
ctx.body = { success: true, data: result };
|
|
320
503
|
await next();
|
|
321
504
|
}
|
|
@@ -323,7 +506,7 @@ async function diff(ctx, next) {
|
|
|
323
506
|
const repo = await getRepo(ctx);
|
|
324
507
|
const localPath = validateLocalPath(repo.get("localPath"));
|
|
325
508
|
const { file, commitHash, compareHash } = getGitActionParams(ctx);
|
|
326
|
-
const git = getGit(ctx, localPath);
|
|
509
|
+
const git = await getGit(ctx, localPath);
|
|
327
510
|
const args = [];
|
|
328
511
|
if (commitHash && compareHash) {
|
|
329
512
|
args.push(validateRef(commitHash), validateRef(compareHash));
|
|
@@ -331,8 +514,13 @@ async function diff(ctx, next) {
|
|
|
331
514
|
args.push(validateRef(commitHash) + "^", validateRef(commitHash));
|
|
332
515
|
}
|
|
333
516
|
if (file) {
|
|
334
|
-
|
|
335
|
-
|
|
517
|
+
let safeFile = "";
|
|
518
|
+
try {
|
|
519
|
+
safeFile = validateRepoRelativePath(file, "file path");
|
|
520
|
+
} catch {
|
|
521
|
+
ctx.throw(400, "Invalid file path");
|
|
522
|
+
}
|
|
523
|
+
args.push("--", safeFile);
|
|
336
524
|
}
|
|
337
525
|
const result = await git.diff(args);
|
|
338
526
|
ctx.body = { success: true, data: result };
|
|
@@ -341,38 +529,57 @@ async function diff(ctx, next) {
|
|
|
341
529
|
async function status(ctx, next) {
|
|
342
530
|
const repo = await getRepo(ctx);
|
|
343
531
|
const localPath = validateLocalPath(repo.get("localPath"));
|
|
344
|
-
const
|
|
532
|
+
const git = await getGit(ctx, localPath);
|
|
533
|
+
const result = await git.status();
|
|
345
534
|
ctx.body = { success: true, data: result };
|
|
346
535
|
await next();
|
|
347
536
|
}
|
|
348
537
|
async function log(ctx, next) {
|
|
349
538
|
const repo = await getRepo(ctx);
|
|
350
539
|
const localPath = validateLocalPath(repo.get("localPath"));
|
|
351
|
-
const { maxCount = 50, file } = getGitActionParams(ctx);
|
|
540
|
+
const { maxCount = 50, file, branch, skip } = getGitActionParams(ctx);
|
|
352
541
|
const parsed = Number.parseInt(String(maxCount), 10);
|
|
353
|
-
const
|
|
542
|
+
const boundedMaxCount = Math.min(Math.max(parsed || 50, 1), 500);
|
|
543
|
+
let safeFile;
|
|
354
544
|
if (file) {
|
|
355
|
-
|
|
356
|
-
|
|
545
|
+
try {
|
|
546
|
+
safeFile = validateRepoRelativePath(file, "file path");
|
|
547
|
+
} catch {
|
|
548
|
+
ctx.throw(400, "Invalid file path");
|
|
549
|
+
}
|
|
357
550
|
}
|
|
358
|
-
const
|
|
551
|
+
const args = [];
|
|
552
|
+
if (branch) args.push(validateBranch(branch));
|
|
553
|
+
if (skip !== void 0) {
|
|
554
|
+
const parsedSkip = Number(skip);
|
|
555
|
+
if (!Number.isSafeInteger(parsedSkip) || parsedSkip < 0 || parsedSkip > 1e6) {
|
|
556
|
+
ctx.throw(400, "Invalid history offset");
|
|
557
|
+
}
|
|
558
|
+
args.push(`--skip=${parsedSkip}`);
|
|
559
|
+
}
|
|
560
|
+
const git = await getGit(ctx, localPath);
|
|
561
|
+
const result = args.length || safeFile ? await git.log([...args, `--max-count=${boundedMaxCount}`, ...safeFile ? ["--follow", "--", safeFile] : []]) : await git.log({ maxCount: boundedMaxCount });
|
|
359
562
|
ctx.body = { success: true, data: result };
|
|
360
563
|
await next();
|
|
361
564
|
}
|
|
362
565
|
async function branches(ctx, next) {
|
|
363
566
|
const repo = await getRepo(ctx);
|
|
364
567
|
const localPath = validateLocalPath(repo.get("localPath"));
|
|
365
|
-
const
|
|
568
|
+
const git = await getGit(ctx, localPath);
|
|
569
|
+
const result = await git.branch();
|
|
366
570
|
ctx.body = { success: true, data: result };
|
|
367
571
|
await next();
|
|
368
572
|
}
|
|
369
573
|
async function checkout(ctx, next) {
|
|
574
|
+
var _a;
|
|
370
575
|
const repo = await getRepo(ctx);
|
|
371
576
|
const localPath = validateLocalPath(repo.get("localPath"));
|
|
372
577
|
const { branch } = getGitActionParams(ctx);
|
|
373
578
|
if (!branch) ctx.throw(400, "branch is required");
|
|
374
579
|
validateBranch(branch);
|
|
375
|
-
await getGit(ctx, localPath)
|
|
580
|
+
const git = await getGit(ctx, localPath);
|
|
581
|
+
const lockManager = (_a = ctx.app) == null ? void 0 : _a.lockManager;
|
|
582
|
+
await withRepoLock(localPath, () => git.checkout(branch), lockManager);
|
|
376
583
|
ctx.body = { success: true, message: `Switched to branch ${branch}` };
|
|
377
584
|
await next();
|
|
378
585
|
}
|
|
@@ -380,15 +587,20 @@ async function fileTree(ctx, next) {
|
|
|
380
587
|
const repo = await getRepo(ctx);
|
|
381
588
|
const localPath = validateLocalPath(repo.get("localPath"));
|
|
382
589
|
const { ref = "HEAD", treePath = "", recursive } = getGitActionParams(ctx);
|
|
383
|
-
const git = getGit(ctx, localPath);
|
|
590
|
+
const git = await getGit(ctx, localPath);
|
|
384
591
|
validateRef(ref);
|
|
385
|
-
|
|
386
|
-
|
|
592
|
+
let safeTreePath = "";
|
|
593
|
+
if (treePath) {
|
|
594
|
+
try {
|
|
595
|
+
safeTreePath = validateRepoRelativePath(treePath, "tree path");
|
|
596
|
+
} catch {
|
|
597
|
+
ctx.throw(400, "Invalid tree path");
|
|
598
|
+
}
|
|
387
599
|
}
|
|
388
|
-
const detailArgs = ["ls-tree", "-l"];
|
|
600
|
+
const detailArgs = ["-c", "core.quotePath=false", "ls-tree", "-l"];
|
|
389
601
|
if (recursive === true || recursive === "true") detailArgs.push("-r");
|
|
390
|
-
detailArgs.push(ref);
|
|
391
|
-
if (
|
|
602
|
+
detailArgs.push(ref, "--");
|
|
603
|
+
if (safeTreePath) detailArgs.push(`${safeTreePath}/`);
|
|
392
604
|
const detailedResult = await git.raw(detailArgs);
|
|
393
605
|
const items = detailedResult.trim().split("\n").filter(Boolean).map((line) => {
|
|
394
606
|
const match = line.match(/^(\d+)\s+(blob|tree)\s+([a-f0-9]+)\s+(-|\d+)\t(.+)$/);
|
|
@@ -419,13 +631,16 @@ async function fileContent(ctx, next) {
|
|
|
419
631
|
if (!filePath) {
|
|
420
632
|
ctx.throw(400, "filePath is required");
|
|
421
633
|
}
|
|
422
|
-
|
|
634
|
+
let safeFilePath = "";
|
|
635
|
+
try {
|
|
636
|
+
safeFilePath = validateRepoRelativePath(filePath, "file path");
|
|
637
|
+
} catch {
|
|
423
638
|
ctx.throw(400, "Invalid file path");
|
|
424
639
|
}
|
|
425
640
|
validateRef(ref);
|
|
426
|
-
const git = getGit(ctx, localPath);
|
|
427
|
-
const content = await git.
|
|
428
|
-
ctx.body = { success: true, data: { content, filePath, ref } };
|
|
641
|
+
const git = await getGit(ctx, localPath);
|
|
642
|
+
const content = await git.raw(["-c", "core.quotePath=false", "show", `${ref}:${safeFilePath}`]);
|
|
643
|
+
ctx.body = { success: true, data: { content, filePath: safeFilePath, ref } };
|
|
429
644
|
await next();
|
|
430
645
|
}
|
|
431
646
|
async function commitDetail(ctx, next) {
|
|
@@ -435,7 +650,7 @@ async function commitDetail(ctx, next) {
|
|
|
435
650
|
if (!commitHash) {
|
|
436
651
|
ctx.throw(400, "commitHash is required");
|
|
437
652
|
}
|
|
438
|
-
const git = getGit(ctx, localPath);
|
|
653
|
+
const git = await getGit(ctx, localPath);
|
|
439
654
|
validateRef(commitHash);
|
|
440
655
|
const DELIM_ARG = "%x00";
|
|
441
656
|
const DELIM_OUT = "\0";
|
|
@@ -470,9 +685,91 @@ async function commitDetail(ctx, next) {
|
|
|
470
685
|
};
|
|
471
686
|
await next();
|
|
472
687
|
}
|
|
688
|
+
async function readLimitedGitOutput(localPath, args, maxBytes) {
|
|
689
|
+
return new Promise((resolve, reject) => {
|
|
690
|
+
const child = (0, import_child_process.spawn)(getGitBinaryPath(), args, {
|
|
691
|
+
cwd: localPath,
|
|
692
|
+
env: gitProcessEnvironment(),
|
|
693
|
+
windowsHide: true,
|
|
694
|
+
stdio: ["ignore", "pipe", "ignore"]
|
|
695
|
+
});
|
|
696
|
+
const chunks = [];
|
|
697
|
+
let size = 0;
|
|
698
|
+
let truncated = false;
|
|
699
|
+
child.stdout.on("data", (chunk) => {
|
|
700
|
+
const remaining = maxBytes - size;
|
|
701
|
+
if (remaining > 0) {
|
|
702
|
+
const data = chunk.subarray(0, remaining);
|
|
703
|
+
chunks.push(data);
|
|
704
|
+
size += data.length;
|
|
705
|
+
}
|
|
706
|
+
if (chunk.length > remaining) {
|
|
707
|
+
truncated = true;
|
|
708
|
+
child.kill();
|
|
709
|
+
}
|
|
710
|
+
});
|
|
711
|
+
child.on("error", reject);
|
|
712
|
+
child.on("close", (code) => {
|
|
713
|
+
if (code !== 0 && !truncated) {
|
|
714
|
+
reject(new Error("Unable to read commit diff"));
|
|
715
|
+
return;
|
|
716
|
+
}
|
|
717
|
+
const text = Buffer.concat(chunks).toString("utf8").replace(/\uFFFD$/, "");
|
|
718
|
+
resolve({ text, truncated });
|
|
719
|
+
});
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
async function readChatCommit(ctx, commitHash, maxFiles, maxBytes, repositoryId) {
|
|
723
|
+
if (!/^[a-f\d]{4,64}$/i.test(commitHash)) ctx.throw(400, "Invalid commit hash");
|
|
724
|
+
const repo = await getRepo(ctx, repositoryId);
|
|
725
|
+
const localPath = validateLocalPath(repo.get("localPath"));
|
|
726
|
+
const git = await getGit(ctx, localPath);
|
|
727
|
+
const hash = (await git.raw(["rev-parse", "--verify", `${commitHash}^{commit}`])).trim();
|
|
728
|
+
const [metadata, parentLine] = await Promise.all([
|
|
729
|
+
git.raw(["show", "-s", "--format=%H%x00%an%x00%aI%x00%s%x00%b", hash]),
|
|
730
|
+
git.raw(["rev-list", "--parents", "-n", "1", hash])
|
|
731
|
+
]);
|
|
732
|
+
const [, author, date, subject, body] = metadata.split("\0");
|
|
733
|
+
const parent = parentLine.trim().split(/\s+/)[1];
|
|
734
|
+
const namesArgs = parent ? ["diff", "--name-status", "-z", "-M", parent, hash, "--"] : ["diff-tree", "--root", "--no-commit-id", "-r", "--name-status", "-z", "-M", hash, "--"];
|
|
735
|
+
const tokens = (await git.raw(namesArgs)).split("\0");
|
|
736
|
+
const files = [];
|
|
737
|
+
for (let index = 0; index < tokens.length && tokens[index]; ) {
|
|
738
|
+
const status2 = tokens[index++];
|
|
739
|
+
const oldPath = tokens[index++];
|
|
740
|
+
const newPath = /^[RC]/.test(status2) ? tokens[index++] : oldPath;
|
|
741
|
+
if (!oldPath || !newPath) break;
|
|
742
|
+
files.push({ oldPath, newPath, status: status2 });
|
|
743
|
+
}
|
|
744
|
+
const selectedFiles = files.slice(0, maxFiles);
|
|
745
|
+
const paths = Array.from(new Set(selectedFiles.flatMap((file) => [file.oldPath, file.newPath]))).map(
|
|
746
|
+
(file) => `:(literal)${file}`
|
|
747
|
+
);
|
|
748
|
+
const patchArgs = parent ? ["diff", "--no-ext-diff", "--no-textconv", "--no-color", parent, hash, "--", ...paths] : ["show", "--format=", "--no-ext-diff", "--no-textconv", "--no-color", hash, "--", ...paths];
|
|
749
|
+
const patch = paths.length ? await readLimitedGitOutput(localPath, patchArgs, maxBytes) : { text: "", truncated: false };
|
|
750
|
+
const limitations = [];
|
|
751
|
+
if (files.length > maxFiles) limitations.push("Additional changed files were omitted.");
|
|
752
|
+
if (patch.truncated) limitations.push("The diff was truncated to the configured limit.");
|
|
753
|
+
if (/^Binary files .* differ$|^GIT binary patch$/m.test(patch.text)) {
|
|
754
|
+
limitations.push("Binary file contents are not included in the diff.");
|
|
755
|
+
}
|
|
756
|
+
return {
|
|
757
|
+
commitSha: hash,
|
|
758
|
+
author,
|
|
759
|
+
date,
|
|
760
|
+
subject,
|
|
761
|
+
body: body == null ? void 0 : body.trim(),
|
|
762
|
+
files: selectedFiles,
|
|
763
|
+
diff: patch.text,
|
|
764
|
+
truncated: patch.truncated || files.length > maxFiles,
|
|
765
|
+
limitations
|
|
766
|
+
};
|
|
767
|
+
}
|
|
473
768
|
// Annotate the CommonJS export names for ESM import in node:
|
|
474
769
|
0 && (module.exports = {
|
|
475
770
|
acquireLock,
|
|
771
|
+
assertGitAvailable,
|
|
772
|
+
assertLocalGitReady,
|
|
476
773
|
branches,
|
|
477
774
|
checkout,
|
|
478
775
|
clone,
|
|
@@ -483,15 +780,25 @@ async function commitDetail(ctx, next) {
|
|
|
483
780
|
fetch,
|
|
484
781
|
fileContent,
|
|
485
782
|
fileTree,
|
|
783
|
+
getGit,
|
|
486
784
|
getGitBinaryPath,
|
|
785
|
+
getGitMissingMessage,
|
|
487
786
|
getRepo,
|
|
787
|
+
installGit,
|
|
788
|
+
isGitAvailable,
|
|
488
789
|
log,
|
|
489
790
|
pull,
|
|
490
791
|
push,
|
|
792
|
+
readChatCommit,
|
|
793
|
+
resetGitBinaryCacheForTests,
|
|
794
|
+
resolveGitBinary,
|
|
795
|
+
runtimeStatus,
|
|
491
796
|
status,
|
|
492
797
|
validateBranch,
|
|
493
798
|
validateLocalPath,
|
|
494
799
|
validateRef,
|
|
800
|
+
validateRepoRelativePath,
|
|
495
801
|
validateRepoUrl,
|
|
496
|
-
withAuth
|
|
802
|
+
withAuth,
|
|
803
|
+
withRepoLock
|
|
497
804
|
});
|