openmatrix 0.1.87 → 0.1.88
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.
|
@@ -23,8 +23,13 @@ export interface CommitResult {
|
|
|
23
23
|
*/
|
|
24
24
|
export declare class GitCommitManager {
|
|
25
25
|
private repoPath;
|
|
26
|
+
private gitRoot;
|
|
26
27
|
private enabled;
|
|
27
28
|
constructor(repoPath?: string);
|
|
29
|
+
/**
|
|
30
|
+
* 获取 git 仓库根目录(支持 .git 在父级目录的情况)
|
|
31
|
+
*/
|
|
32
|
+
private getGitRoot;
|
|
28
33
|
/**
|
|
29
34
|
* 设置是否启用自动提交
|
|
30
35
|
*/
|
|
@@ -50,10 +50,27 @@ const execAsync = (0, util_1.promisify)(child_process_1.exec);
|
|
|
50
50
|
*/
|
|
51
51
|
class GitCommitManager {
|
|
52
52
|
repoPath;
|
|
53
|
+
gitRoot = null;
|
|
53
54
|
enabled = true;
|
|
54
55
|
constructor(repoPath = process.cwd()) {
|
|
55
56
|
this.repoPath = repoPath;
|
|
56
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* 获取 git 仓库根目录(支持 .git 在父级目录的情况)
|
|
60
|
+
*/
|
|
61
|
+
async getGitRoot() {
|
|
62
|
+
if (this.gitRoot)
|
|
63
|
+
return this.gitRoot;
|
|
64
|
+
try {
|
|
65
|
+
const { stdout } = await execAsync('git rev-parse --show-toplevel', { cwd: this.repoPath });
|
|
66
|
+
this.gitRoot = stdout.trim();
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
// 不是 git 仓库,回退到 repoPath
|
|
70
|
+
this.gitRoot = this.repoPath;
|
|
71
|
+
}
|
|
72
|
+
return this.gitRoot;
|
|
73
|
+
}
|
|
57
74
|
/**
|
|
58
75
|
* 设置是否启用自动提交
|
|
59
76
|
*/
|
|
@@ -222,22 +239,21 @@ class GitCommitManager {
|
|
|
222
239
|
// 检查是否在 Git 仓库中,如果不是则自动初始化
|
|
223
240
|
if (!await this.isGitRepo()) {
|
|
224
241
|
await execAsync('git init', { cwd: this.repoPath });
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
await
|
|
240
|
-
await execAsync('git commit -m "initial commit" --allow-empty', { cwd: this.repoPath }).catch(() => { });
|
|
242
|
+
this.gitRoot = this.repoPath; // 刚初始化的仓库,根目录就是 repoPath
|
|
243
|
+
}
|
|
244
|
+
// 确保 .gitignore 中包含 .openmatrix(写入到 git 根目录)
|
|
245
|
+
const gitRoot = await this.getGitRoot();
|
|
246
|
+
const gitignorePath = path.join(gitRoot, '.gitignore');
|
|
247
|
+
let gitignoreContent = '';
|
|
248
|
+
try {
|
|
249
|
+
gitignoreContent = await fs.readFile(gitignorePath, 'utf-8');
|
|
250
|
+
}
|
|
251
|
+
catch {
|
|
252
|
+
// 文件不存在
|
|
253
|
+
}
|
|
254
|
+
if (!gitignoreContent.includes('.openmatrix')) {
|
|
255
|
+
const addition = (gitignoreContent && !gitignoreContent.endsWith('\n') ? '\n' : '') + '.openmatrix/\n';
|
|
256
|
+
await fs.writeFile(gitignorePath, gitignoreContent + addition, 'utf-8');
|
|
241
257
|
}
|
|
242
258
|
// 获取未提交的文件
|
|
243
259
|
const files = await this.getUncommittedFiles();
|
|
@@ -267,7 +283,7 @@ class GitCommitManager {
|
|
|
267
283
|
};
|
|
268
284
|
}
|
|
269
285
|
// 使用临时文件传递 commit message(避免 Windows 下多行消息转义问题)
|
|
270
|
-
const tmpFile = path.join(
|
|
286
|
+
const tmpFile = path.join(gitRoot, '.git', 'COMMIT_MSG_TMP');
|
|
271
287
|
await fs.writeFile(tmpFile, commitMessage, 'utf-8');
|
|
272
288
|
try {
|
|
273
289
|
const { stdout } = await execAsync(`git commit -F "${tmpFile}"`, { cwd: this.repoPath });
|
package/dist/utils/gitignore.js
CHANGED
|
@@ -38,13 +38,30 @@ exports.ensureOpenmatrixGitignore = ensureOpenmatrixGitignore;
|
|
|
38
38
|
// src/utils/gitignore.ts
|
|
39
39
|
const fs = __importStar(require("fs/promises"));
|
|
40
40
|
const path = __importStar(require("path"));
|
|
41
|
+
const child_process_1 = require("child_process");
|
|
42
|
+
const util_1 = require("util");
|
|
43
|
+
const execAsync = (0, util_1.promisify)(child_process_1.exec);
|
|
44
|
+
/**
|
|
45
|
+
* 获取 git 仓库根目录(支持 .git 在父级目录的情况)
|
|
46
|
+
*/
|
|
47
|
+
async function getGitRoot(basePath) {
|
|
48
|
+
try {
|
|
49
|
+
const { stdout } = await execAsync('git rev-parse --show-toplevel', { cwd: basePath });
|
|
50
|
+
return stdout.trim();
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return basePath;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
41
56
|
/**
|
|
42
57
|
* 确保指定目录被 git 忽略
|
|
43
|
-
* @param basePath
|
|
58
|
+
* @param basePath 项目目录(可以是 git 仓库的子目录)
|
|
44
59
|
* @param ignorePattern 要忽略的模式 (默认 .openmatrix/)
|
|
45
60
|
*/
|
|
46
61
|
async function ensureGitignore(basePath, ignorePattern = '.openmatrix/') {
|
|
47
|
-
|
|
62
|
+
// 写入到 git 根目录的 .gitignore
|
|
63
|
+
const gitRoot = await getGitRoot(basePath);
|
|
64
|
+
const gitignorePath = path.join(gitRoot, '.gitignore');
|
|
48
65
|
try {
|
|
49
66
|
const content = await fs.readFile(gitignorePath, 'utf-8');
|
|
50
67
|
// 检查是否已经包含该模式
|