dsh-git-push 2.15.3 → 2.16.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/lib/app/command-registry.js +1 -0
- package/lib/app/tool-call.js +18 -2
- package/lib/git/index.js +2 -2
- package/lib/git/init-compat.js +45 -1
- package/lib/git/remote.js +51 -16
- package/lib/git/transport.js +36 -0
- package/lib/self/index.js +1 -1
- package/package.json +1 -1
|
@@ -93,6 +93,7 @@ export const TOOL_REGISTRY = [
|
|
|
93
93
|
params: [
|
|
94
94
|
{ flag: 'repo', type: 'string', positional: 0, required: true, desc: '本地 git 仓库绝对路径' },
|
|
95
95
|
{ flag: 'visibility', type: 'string', enum: ['public', 'private'], default: 'private', desc: '可见性' },
|
|
96
|
+
{ flag: 'pushInitial', type: 'boolean', desc: '建仓后做首次推送(本地无提交时先本地提交,再走 Git Data API 上传;CIFS 上不需 git remote add)' },
|
|
96
97
|
{ flag: 'dryRun', type: 'boolean', desc: '只探测预演' },
|
|
97
98
|
],
|
|
98
99
|
},
|
package/lib/app/tool-call.js
CHANGED
|
@@ -18,6 +18,7 @@ import { auditWithScope, auditFull, summarize, isBlockerFinding } from '../audit
|
|
|
18
18
|
import { formatAnalysisCoverageLine } from '../audit/analysis-coverage.js';
|
|
19
19
|
import { scoreQuality } from '../score/index.js';
|
|
20
20
|
import {
|
|
21
|
+
pushInitialContent,
|
|
21
22
|
cloneViaApi, previewClone, resolveMaxCloneFileMB, ensureRemoteRepo, setVisibility, renameRepo, maintainRepoIndex, updateRepoIndex, resolveToken, commitAndPush,
|
|
22
23
|
scanReposAsync, scanRepos, checkGithubAccount, formatGithubAccountBlock, generateSshKey, persistSshPub,
|
|
23
24
|
refreshAccountStatus,
|
|
@@ -597,9 +598,24 @@ export async function callTool(toolName, args = {}, env = {}, cfg = defaultConfi
|
|
|
597
598
|
}
|
|
598
599
|
case 'npm_publish':
|
|
599
600
|
return callNpmPublishJob(args, jobs, exec, log, env, cfg);
|
|
600
|
-
case 'git_remote_create':
|
|
601
|
+
case 'git_remote_create': {
|
|
601
602
|
if (!args.repo) return { ok: false, error: MSG_REPO_REQUIRED };
|
|
602
|
-
|
|
603
|
+
const created = await ensureRemoteRepo({ repoPath: args.repo, visibility: args.visibility || 'private', dryRun: args.dryRun === true });
|
|
604
|
+
// 可选:建仓后做「首次推送」——本地还没提交就先提交,再走 Git Data API 上传。
|
|
605
|
+
// CIFS 上这一整条链不依赖 `git remote add/config`(那些会 chmod 失败),
|
|
606
|
+
// 只有 add/commit 两次本地写,实测在 smb2 挂载上可用。
|
|
607
|
+
if (args.pushInitial === true && created.ok && created.dryRun !== true) {
|
|
608
|
+
const push = await pushInitialContent({ repoPath: args.repo, branch: args.branch || '', token: '' });
|
|
609
|
+
return {
|
|
610
|
+
...created,
|
|
611
|
+
initialPush: {
|
|
612
|
+
ok: push.ok === true, pushed: push.pushed === true, committed: push.committed === true,
|
|
613
|
+
reason: push.reason || '', remoteSha: push.remoteSha || push.commitSha || '',
|
|
614
|
+
},
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
return created;
|
|
618
|
+
}
|
|
603
619
|
case 'git_identity_rewrite':
|
|
604
620
|
// 提交身份历史改写:默认 dryRun 只报告,真改写要先 dryRun:false/write,推送要 push:true
|
|
605
621
|
return callIdentityRewrite(args, env, cfg);
|
package/lib/git/index.js
CHANGED
|
@@ -28,10 +28,10 @@ export { GH_API, githubFetch, parseGithubOwnerRepo, isBadCredentials, detectRepo
|
|
|
28
28
|
export { resolveRepoVisibility } from './visibility.js';
|
|
29
29
|
export { scanSensitiveFiles } from './sensitive.js';
|
|
30
30
|
export { DEFAULT_IGNORE_PATTERNS, ensureGitignore } from './ignore.js';
|
|
31
|
-
export { fetchRemoteHeads, dispatchPush, pushViaSsh, pushViaApi, sshReason, isNonFastForward, liveRemoteHead, liveRemoteHeadAsync } from './transport.js';
|
|
31
|
+
export { fetchRemoteHeads, dispatchPush, pushViaSsh, pushViaApi, pushInitialContent, sshReason, isNonFastForward, liveRemoteHead, liveRemoteHeadAsync } from './transport.js';
|
|
32
32
|
export { readmeCheckHint, commitAndPush, pushCurrentBranch } from './push.js';
|
|
33
33
|
export { cloneViaApi, previewClone, resolveMaxCloneFileMB, isPartialCloneDir } from './clone.js';
|
|
34
|
-
export { ensureRemoteRepo, setVisibility } from './remote.js';
|
|
34
|
+
export { ensureRemoteRepo, setVisibility, prepareLocalRepoForRemote } from './remote.js';
|
|
35
35
|
export { renameRepo } from './rename.js';
|
|
36
36
|
export { scanRepos, scanReposAsync, collectRepoPaths, describeRepo, describeRepoAsync, maskRemoteUrl } from './repos.js';
|
|
37
37
|
export { browseDir } from './browse.js';
|
package/lib/git/init-compat.js
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
* 因此降级路径 = 手写 `.git/{HEAD,config}` + 空 `objects/refs/info` 目录,绕开 git 的
|
|
23
23
|
* config 写入流程;config 里预先写死 `filemode = false`。
|
|
24
24
|
*/
|
|
25
|
-
import { existsSync, mkdirSync, writeFileSync, chmodSync, rmSync } from 'node:fs';
|
|
25
|
+
import { existsSync, mkdirSync, writeFileSync, chmodSync, rmSync, realpathSync } from 'node:fs';
|
|
26
26
|
import { join } from 'node:path';
|
|
27
27
|
import { runGit } from './exec.js';
|
|
28
28
|
|
|
@@ -112,6 +112,50 @@ export function initRepoCompat(dir, { branch = 'master', remoteUrl = '', chmodOk
|
|
|
112
112
|
}
|
|
113
113
|
}
|
|
114
114
|
|
|
115
|
+
/**
|
|
116
|
+
* 现有 .git 是否**真的可用**(不是"目录存在"就算)。
|
|
117
|
+
*
|
|
118
|
+
* 为什么必须实测:CIFS/SMB 上 `git init` 会在写 config 时 chmod 失败而**半途退出**——
|
|
119
|
+
* 留下 `.git/{config,description,hooks,info}` 却**没有 HEAD/objects/refs**。
|
|
120
|
+
* 此时 `existsSync('.git')` 为真,但 `git remote add` 直接报「not a git repository」。
|
|
121
|
+
* 历史实现正是拿 existsSync 当判据,于是「建仓成功、origin 没设上」还报成功。
|
|
122
|
+
*/
|
|
123
|
+
export function isUsableRepo(dir) {
|
|
124
|
+
const r = runGit(['rev-parse', '--git-dir'], { cwd: dir });
|
|
125
|
+
if (!r || !r.ok) return false;
|
|
126
|
+
// 必须确认「这个目录本身」就是仓库根:git 会向上层目录找仓库,
|
|
127
|
+
// 若目标目录位于某个父仓库内部(如插件里的临时目录),
|
|
128
|
+
// `rev-parse --git-dir` 同样成功 —— 会把它误判成"已有仓库"从而跳过初始化(实测踩过)。
|
|
129
|
+
const top = runGit(['rev-parse', '--show-toplevel'], { cwd: dir });
|
|
130
|
+
if (!top || !top.ok) return false;
|
|
131
|
+
const real = (p) => { try { return realpathSync.native(p); } catch { return p; } };
|
|
132
|
+
return real(String(top.stdout || '').trim() || dir) === real(dir);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* 确保目录里有一个**可用**的本地 git 仓库(CIFS 安全)。
|
|
137
|
+
*
|
|
138
|
+
* 三种情形:
|
|
139
|
+
* · 已可用 → action:'keep'(不动它,避免覆盖用户既有配置)
|
|
140
|
+
* · 有残缺 .git(半途而废)→ action:'repair'(走 initRepoCompat 补全/重写)
|
|
141
|
+
* · 没有 .git → action:'create'
|
|
142
|
+
*
|
|
143
|
+
* @param {string} dir 工作树目录
|
|
144
|
+
* @param {{branch?:string, remoteUrl?:string, remoteName?:string, chmodOk?:boolean,
|
|
145
|
+
* gitInit?:Function}} [opts]
|
|
146
|
+
* remoteUrl 给出时,降级路径会把 remote 段一并写进 config —— 因为 `git remote add`
|
|
147
|
+
* 同样是写 config,在 CIFS 上一样会 chmod 失败(实测)。
|
|
148
|
+
* @returns {{ok:boolean, action:'keep'|'repair'|'create'|'failed', via:string, filemode:?boolean, stderr?:string}}
|
|
149
|
+
*/
|
|
150
|
+
export function ensureUsableLocalRepo(dir, { branch = 'master', remoteUrl = '', remoteName = 'origin', chmodOk, gitInit } = {}) {
|
|
151
|
+
const exec = typeof gitInit === 'function' ? gitInit : (args, opts) => runGit(args, opts);
|
|
152
|
+
if (isUsableRepo(dir)) return { ok: true, action: 'keep', via: 'existing', filemode: null };
|
|
153
|
+
const hadPartial = hasGitDir(dir);
|
|
154
|
+
const r = initRepoCompat(dir, { branch, remoteUrl, remoteName, chmodOk, gitInit: exec });
|
|
155
|
+
if (!r.ok) return { ok: false, action: 'failed', via: r.via, filemode: r.filemode, stderr: r.stderr };
|
|
156
|
+
return { ok: true, action: hadPartial ? 'repair' : 'create', via: r.via, filemode: r.filemode };
|
|
157
|
+
}
|
|
158
|
+
|
|
115
159
|
/** .git 目录是否已存在(续跑判断:已 init 过就不再重复写)。 */
|
|
116
160
|
export function hasGitDir(dir) {
|
|
117
161
|
return existsSync(join(dir, '.git'));
|
package/lib/git/remote.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
import { existsSync } from 'node:fs';
|
|
8
8
|
import { basename, join } from 'node:path';
|
|
9
9
|
import { githubFetch } from './api.js';
|
|
10
|
+
import { ensureUsableLocalRepo } from './init-compat.js';
|
|
10
11
|
import { resolveToken } from './credentials.js';
|
|
11
12
|
import { runGit } from './exec.js';
|
|
12
13
|
|
|
@@ -34,19 +35,43 @@ function isApiFormOrigin(url) {
|
|
|
34
35
|
* —— 这样用旧版建过仓的仓库不用手工修。
|
|
35
36
|
* @returns {{action:'add'|'repair'|'keep', url:string, from?:string}}
|
|
36
37
|
*/
|
|
37
|
-
|
|
38
|
+
/**
|
|
39
|
+
* 确保「本地仓库可用 + origin 指向该 GitHub 仓库」,两者都按实际情况回报。
|
|
40
|
+
*
|
|
41
|
+
* 历史缺陷(CIFS 上必现):老实现只判 `existsSync('.git')` 就调 `git remote add`。
|
|
42
|
+
* CIFS/SMB 上 `git init` 会在写 config 时 chmod 失败而半途退出 —— 留下没有 HEAD/objects
|
|
43
|
+
* 的残缺 `.git`;于是 `remote add` 报「not a git repository」,而函数**仍返回 action:'add'**,
|
|
44
|
+
* 调用方据此报「建仓成功」,用户以为好了、其实本地根本不能用。
|
|
45
|
+
*
|
|
46
|
+
* 现在:先 ensureUsableLocalRepo(CIFS 安全 init,与 clone 用的同一套),再校验 origin;
|
|
47
|
+
* 任何一步失败都带 error 返回,不再谎报。
|
|
48
|
+
* @returns {{ok:boolean, action:'create'|'repair'|'keep'|'add'|'failed',
|
|
49
|
+
* url:string, localRepo:object, error?:string}}
|
|
50
|
+
*/
|
|
51
|
+
export function prepareLocalRepoForRemote(repoPath, ownerName, name, { branch = 'main' } = {}) {
|
|
38
52
|
const gitUrl = `${GH_WEB}/${ownerName}/${name}`;
|
|
53
|
+
const local = ensureUsableLocalRepo(repoPath, { branch, remoteUrl: gitUrl });
|
|
54
|
+
if (!local.ok) {
|
|
55
|
+
return { ok: false, action: 'failed', url: gitUrl, localRepo: local, error: `本地仓库不可用:${local.stderr || '初始化失败'}` };
|
|
56
|
+
}
|
|
39
57
|
const cur = runGit(['remote', 'get-url', 'origin'], { cwd: repoPath });
|
|
40
58
|
if (!cur.ok) {
|
|
41
|
-
runGit(['remote', 'add', 'origin', gitUrl], { cwd: repoPath });
|
|
42
|
-
return {
|
|
59
|
+
const add = runGit(['remote', 'add', 'origin', gitUrl], { cwd: repoPath });
|
|
60
|
+
return {
|
|
61
|
+
ok: !!add.ok, action: add.ok ? 'add' : 'failed', url: gitUrl, localRepo: local,
|
|
62
|
+
...(add.ok ? {} : { error: `设置 origin 失败:${String(add.stderr || '').slice(0, 200)}` }),
|
|
63
|
+
};
|
|
43
64
|
}
|
|
44
65
|
const existing = String(cur.stdout || cur.stderr || '').trim();
|
|
45
66
|
if (isApiFormOrigin(existing)) {
|
|
46
|
-
runGit(['remote', 'set-url', 'origin', gitUrl], { cwd: repoPath });
|
|
47
|
-
return {
|
|
67
|
+
const set = runGit(['remote', 'set-url', 'origin', gitUrl], { cwd: repoPath });
|
|
68
|
+
return {
|
|
69
|
+
// 动作名沿用既有语义 'repair'(把 API 形态 origin 修回 git 形态)——测试与外部消费方都按这个名字判断
|
|
70
|
+
ok: !!set.ok, action: set.ok ? 'repair' : 'failed', url: gitUrl, localRepo: local, from: existing,
|
|
71
|
+
...(set.ok ? {} : { error: `修正 origin 失败:${String(set.stderr || '').slice(0, 200)}` }),
|
|
72
|
+
};
|
|
48
73
|
}
|
|
49
|
-
return { action: 'keep', url: existing };
|
|
74
|
+
return { ok: true, action: 'keep', url: existing, localRepo: local };
|
|
50
75
|
}
|
|
51
76
|
|
|
52
77
|
/** 建仓(POST /user/repos)并设置 origin。dryRun 只探测不创建。 */
|
|
@@ -82,20 +107,30 @@ export async function ensureRemoteRepo({ repoPath = '', owner = '', visibility =
|
|
|
82
107
|
if (exists.status === 200) {
|
|
83
108
|
// 仓库已存在也要**确保 origin 可用**(原来这里直接 return,不碰 origin ⇒ 本地缺 origin 或
|
|
84
109
|
// 是旧版写下的 API 形态时会一直坏着)。
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
110
|
+
// 已存在也要确保本地可用 + origin 正确(原实现只要 existsSync('.git') 就往下走,CIFS 上会静默失败)
|
|
111
|
+
const prep = repoPath ? prepareLocalRepoForRemote(repoPath, ownerName, name) : null;
|
|
112
|
+
return {
|
|
113
|
+
ok: true, exists: true, owner: ownerName, name, visibility: vis, reason: '已存在同名仓库',
|
|
114
|
+
origin: `${GH_WEB}/${ownerName}/${name}`,
|
|
115
|
+
originAction: prep ? prep.action : 'skip',
|
|
116
|
+
originOk: prep ? prep.ok : false,
|
|
117
|
+
localRepo: prep ? prep.localRepo : null,
|
|
118
|
+
...(prep && !prep.ok ? { warning: prep.error } : {}),
|
|
119
|
+
};
|
|
90
120
|
}
|
|
91
121
|
if (dryRun) return { ok: true, dryRun: true, wouldCreate: true, owner: ownerName, name, visibility: vis };
|
|
92
122
|
const created = await githubFetch('/user/repos', { token: tok, method: 'POST', body: { name, private: vis === 'private' } });
|
|
93
123
|
if (created.status !== 201) return { ok: false, error: created.json?.message || created.error || `创建失败: HTTP ${created.status}` };
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
124
|
+
// 建仓后:确保本地仓库可用(CIFS 安全 init)+ origin 指向它;失败如实回报
|
|
125
|
+
const prep = repoPath ? prepareLocalRepoForRemote(repoPath, ownerName, name) : null;
|
|
126
|
+
return {
|
|
127
|
+
ok: true, created: true, owner: ownerName, name, visibility: vis,
|
|
128
|
+
origin: prep ? prep.url : `${GH_WEB}/${ownerName}/${name}`,
|
|
129
|
+
originAction: prep ? prep.action : 'skip',
|
|
130
|
+
originOk: prep ? prep.ok : false,
|
|
131
|
+
localRepo: prep ? prep.localRepo : null,
|
|
132
|
+
...(prep && !prep.ok ? { warning: prep.error } : {}),
|
|
133
|
+
};
|
|
99
134
|
}
|
|
100
135
|
|
|
101
136
|
/** 从本地仓库 origin remote 解析 owner/repo(支持 api.github.com/repos/、github.com、ssh:// 三格式)。 */
|
package/lib/git/transport.js
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
import { join } from 'node:path';
|
|
15
15
|
import { githubFetch, isBadCredentials, parseGithubOwnerRepo } from './api.js';
|
|
16
16
|
import { resolveSshKey, resolveSshKeys, resolveToken } from './credentials.js';
|
|
17
|
+
import { resolveCommitIdentity } from './identity.js';
|
|
17
18
|
import { gitRaw, runGit, runGitAsync } from './exec.js';
|
|
18
19
|
// 「推送到 GitHub 时自动指定正确的 remote」:归属 + 本机可用形态(ssh:443)+ 名字优先级统一判定
|
|
19
20
|
import { resolveGithubRemote, parseGithubUrl } from './remote-resolve.js';
|
|
@@ -299,6 +300,41 @@ async function apiOrFallback(api, repoPath, branch, res) {
|
|
|
299
300
|
* token 401 → pushViaSsh 回退。
|
|
300
301
|
* @returns {{ok, pushed, method?, commitSha?, reason?, branchAdjusted?}}
|
|
301
302
|
*/
|
|
303
|
+
/**
|
|
304
|
+
* 首次推送:**本地还没有提交时**先做一次本地提交,再走 Git Data API 上传。
|
|
305
|
+
*
|
|
306
|
+
* 为什么需要:`pushViaApi` 以本地提交为输入(rev-parse HEAD / ls-tree / cat-file),
|
|
307
|
+
* 新建的仓库一个提交都没有 ⇒ 直接调它只会得到「本地无提交」。
|
|
308
|
+
* 而 CIFS/SMB 上恰好「本地提交」这件事需要 CIFS 安全 init 才能成立(见 init-compat.js)。
|
|
309
|
+
*
|
|
310
|
+
* 身份:优先用仓库已配置的 user.name/email;缺失时按 `-c` 临时给出(**不写 config**,
|
|
311
|
+
* 因为写 config 在 CIFS 上会 chmod 失败)。
|
|
312
|
+
* @returns {Promise<object>} pushViaApi 的返回,附加 committed 字段说明本次是否新做了提交
|
|
313
|
+
*/
|
|
314
|
+
export async function pushInitialContent({
|
|
315
|
+
repoPath = '', branch = '', token = '', message = 'init: 初始提交(dsh-git-push)', remote = '',
|
|
316
|
+
} = {}) {
|
|
317
|
+
const head = runGit(['rev-parse', 'HEAD'], { cwd: repoPath });
|
|
318
|
+
let committed = false;
|
|
319
|
+
if (!head.ok || !String(head.stdout || '').trim()) {
|
|
320
|
+
const readCfg = (key) => {
|
|
321
|
+
const r = runGit(['config', key], { cwd: repoPath });
|
|
322
|
+
return r && r.ok ? String(r.stdout || '').trim() : '';
|
|
323
|
+
};
|
|
324
|
+
const ident = resolveCommitIdentity({ configName: readCfg('user.name'), configEmail: readCfg('user.email') });
|
|
325
|
+
const idArgs = ident.complete ? [] : ['-c', `user.name=${ident.name}`, '-c', `user.email=${ident.email}`];
|
|
326
|
+
const add = runGit(['add', '-A'], { cwd: repoPath });
|
|
327
|
+
if (!add.ok) return { ok: false, pushed: false, committed: false, reason: `git add 失败:${String(add.stderr || '').slice(0, 300)}` };
|
|
328
|
+
const commit = runGit([...idArgs, 'commit', '-m', message], { cwd: repoPath });
|
|
329
|
+
if (!commit.ok && !/nothing to commit|no changes added/i.test(String(commit.stderr || ''))) {
|
|
330
|
+
return { ok: false, pushed: false, committed: false, reason: `git commit 失败:${String(commit.stderr || '').slice(0, 300)}` };
|
|
331
|
+
}
|
|
332
|
+
committed = !!commit.ok;
|
|
333
|
+
}
|
|
334
|
+
const r = await pushViaApi({ repoPath, branch, token, remote });
|
|
335
|
+
return { ...r, committed };
|
|
336
|
+
}
|
|
337
|
+
|
|
302
338
|
export async function pushViaApi({ repoPath = '', branch = '', token = '', force = false, remote = '' } = {}) {
|
|
303
339
|
const rr = resolveGithubRemote({ repoPath });
|
|
304
340
|
const origin = runGit(['remote', 'get-url', 'origin'], { cwd: repoPath }).stdout;
|
package/lib/self/index.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
export const VERSION = '2.
|
|
12
|
+
export const VERSION = '2.16.0'; // 2.16.0:建仓路径改用 CIFS 安全初始化 + 可选首推。① 抽公共:init-compat 新增 ensureUsableLocalRepo/isUsableRepo —— 判「.git 是否真可用」(含仓库根校验,避免把父仓库误判成本目录的仓库),不可用且残缺则 repair、没有则 create,复用 clone 已在用的 CIFS 降级(手写 .git 绕 chmod);② 建仓:ensureRemoteRepo 用它替代 existsSync('.git') 判据,origin 设置失败**如实报错**(原来会谎报成功);③ 首推:git_remote_create 新增 pushInitial —— 本地无提交时先本地提交(身份用 -c 传入,不写 config),再走已有 Git Data API 上传(transport.pushInitialContent → pushViaApi),CIFS 上全程不需 git remote add/config。历史:2.15.3 本地 registry 彩排。
|
|
13
13
|
// 实测后果:该目录有 package.json ⇒ npm 去打包整棵 DSH 树、输出 2.1MB > spawnSync 默认 maxBuffer 1MB ⇒ ENOBUFS(npm_publish 的体检与真发全部失败);
|
|
14
14
|
// 该目录没有 package.json 时报 ENOENT。现 publish 的两个调用点显式传 cwd=目标包目录,并给 runNpm 加 16MB maxBuffer。
|
|
15
15
|
// 2.8.3:文案修正——「零改动 / 无新提交」明确为**正常成功路径**(注入用法、工具描述、README、skill 同批改),并修 job 输出自相矛盾(cleanSkipCommit 路径补 committed:false,不再显示「已提交,未推送」)
|