Skip to content

Commit 709fe16

Browse files
committed
fix(Branches): 修复无上游分支 Push 失败并暴露 git 真实错误;
hyperGit.push 原以零参数调用 repo.push(),当前分支无上游时 git push (push.default=simple)必然失败,vscode.git 包装为 GitError,其 .message 为通用串「Failed to execute git」,真实 stderr 被吞没。 改为以 HEAD.upstream 是否存在为分支:有上游沿用 repo.push();无上游时 选定 remote(单 remote 直用、多 remote 弹选择)并以 repo.push(remote, branch, true) 建立 -u 追踪。同时增强 errMsg 优先暴露 GitError.stderr, 使「无上游」「non-fast-forward」等失败可读(push/pull/fetch 等同享)。 🤖 Generated with [Claude Code](https://github.com/claude), [CodeX](https://openai.com), [Gemini](https://github.com/apps/gemini-code-assist) Co-Authored-By: Aurelius Huang<threefish.ai@gmail.com>
1 parent 219c185 commit 709fe16

1 file changed

Lines changed: 41 additions & 2 deletions

File tree

src/adapter/history-commands.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,20 @@ export function registerHistoryCommands(
1616
favorites: BranchFavorites,
1717
): vscode.Disposable[] {
1818
const subs: vscode.Disposable[] = [];
19-
const errMsg = (e: unknown): string => (e instanceof Error ? e.message : String(e));
19+
// vscode.git 在 git 非零退出时抛出 GitError,其 .message 为通用串「Failed to execute git」,
20+
// 真实原因落在 .stderr。优先暴露 stderr,使「无上游」「non-fast-forward」等失败可读。
21+
const errMsg = (e: unknown): string => {
22+
if (e && typeof e === 'object') {
23+
const ge = e as { stderr?: unknown; message?: unknown };
24+
if (typeof ge.stderr === 'string' && ge.stderr.trim()) {
25+
return ge.stderr.trim();
26+
}
27+
if (typeof ge.message === 'string' && ge.message) {
28+
return ge.message;
29+
}
30+
}
31+
return String(e);
32+
};
2033

2134
subs.push(vscode.commands.registerCommand('hyperGit.refreshLog', () => logTree.refresh()));
2235
subs.push(vscode.commands.registerCommand('hyperGit.refreshBranches', () => branchesTree.refresh()));
@@ -212,9 +225,35 @@ export function registerHistoryCommands(
212225
if (!repo) {
213226
return;
214227
}
228+
const head = repo.state.HEAD;
229+
if (!head?.name) {
230+
void vscode.window.showWarningMessage('当前处于 detached HEAD,无法推送当前分支');
231+
return;
232+
}
215233
try {
216-
await repo.push();
234+
if (head.upstream) {
235+
// 已配置上游:按 push.default 推送到追踪分支(正确处理本地名/上游名不一致)。
236+
await repo.push();
237+
} else {
238+
// 无上游:选定 remote 并以 -u 建立追踪(修复「Failed to execute git」根因)。
239+
const remotes = repo.state.remotes.map((r) => r.name);
240+
if (remotes.length === 0) {
241+
void vscode.window.showWarningMessage('未配置远程仓库(remote),无法推送');
242+
return;
243+
}
244+
const remote =
245+
remotes.length === 1
246+
? remotes[0]
247+
: await vscode.window.showQuickPick(remotes, {
248+
placeHolder: `为「${head.name}」选择推送目标 remote(将建立上游追踪 -u)`,
249+
});
250+
if (!remote) {
251+
return;
252+
}
253+
await repo.push(remote, head.name, true);
254+
}
217255
branchesTree.refresh();
256+
void vscode.window.showInformationMessage(`已推送 ${head.name}`);
218257
} catch (e) {
219258
void vscode.window.showErrorMessage(`Push 失败:${errMsg(e)}`);
220259
}

0 commit comments

Comments
 (0)