@unifan/pi-commit-zh 1.0.8 → 1.0.9

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.
Files changed (3) hide show
  1. package/index.ts +8 -5
  2. package/package.json +1 -1
  3. package/src/git.ts +42 -3
package/index.ts CHANGED
@@ -174,13 +174,16 @@ export default function (pi: ExtensionAPI) {
174
174
  let pushText = "";
175
175
  if (andPush) {
176
176
  notify("正在推送到远端仓库 (git push)...", "info");
177
- const pushRes = await pushCurrentBranch(ctx.cwd);
177
+ const pushRes = await pushCurrentBranch(ctx.cwd, (msg) => notify(msg, "info"));
178
178
  if (pushRes.ok) {
179
- notify("🚀 成功推送到远端仓库!", "info");
180
- pushText = "\n\n🚀 **已自动推送到远端分支**";
179
+ const successMsg = pushRes.autoRebased
180
+ ? "🚀 远端有新提交,已自动完成变基 (pull --rebase) 并成功推送!"
181
+ : "🚀 成功推送到远端仓库!";
182
+ notify(successMsg, "info");
183
+ pushText = `\n\n${successMsg}`;
181
184
  } else {
182
- notify(`⚠️ 推送失败: ${pushRes.output}`, "warning");
183
- pushText = `\n\n⚠️ **推送到远端失败**: ${pushRes.output}`;
185
+ notify(`⚠️ 推送失败: ${pushRes.output}`, "error");
186
+ pushText = `\n\n⚠️ **推送到远端失败**:\n${pushRes.output}`;
184
187
  }
185
188
  }
186
189
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@unifan/pi-commit-zh",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "Pi 智能 Git 提交助手(规范化 Conventional Commits 纯中文版,支持一键提审与推流)",
5
5
  "type": "module",
6
6
  "main": "index.ts",
package/src/git.ts CHANGED
@@ -55,7 +55,46 @@ export async function commitWithMsg(cwd: string, message: string): Promise<{ ok:
55
55
  return { ok: res.ok, output: res.ok ? res.stdout : res.stderr };
56
56
  }
57
57
 
58
- export async function pushCurrentBranch(cwd: string): Promise<{ ok: boolean; output: string }> {
59
- const res = await runGit(cwd, ["push"]);
60
- return { ok: res.ok, output: res.ok ? res.stdout : res.stderr };
58
+ export async function pushCurrentBranch(
59
+ cwd: string,
60
+ onLog?: (msg: string) => void,
61
+ ): Promise<{ ok: boolean; output: string; autoRebased?: boolean }> {
62
+ const initialPush = await runGit(cwd, ["push"]);
63
+ if (initialPush.ok) {
64
+ return { ok: true, output: initialPush.stdout || "推送成功" };
65
+ }
66
+
67
+ const pushError = `${initialPush.stderr} ${initialPush.stdout}`.toLowerCase();
68
+ const needsPull =
69
+ pushError.includes("fetch first") ||
70
+ pushError.includes("non-fast-forward") ||
71
+ pushError.includes("rejected") ||
72
+ pushError.includes("behind");
73
+
74
+ if (!needsPull) {
75
+ return { ok: false, output: initialPush.stderr || initialPush.stdout };
76
+ }
77
+
78
+ if (onLog) onLog("检测到远端有新提交,正在自动执行变基拉取 (git pull --rebase)...");
79
+
80
+ const pullRebase = await runGit(cwd, ["pull", "--rebase"]);
81
+ if (!pullRebase.ok) {
82
+ const rebaseErr = `${pullRebase.stderr} ${pullRebase.stdout}`;
83
+ return {
84
+ ok: false,
85
+ output: `远端有新提交,尝试自动变基 (git pull --rebase) 时产生代码冲突。\n${rebaseErr}\n请手动解决冲突后执行 git rebase --continue,或执行 git rebase --abort 撤销变基。`,
86
+ };
87
+ }
88
+
89
+ if (onLog) onLog("变基拉取成功(无代码冲突),正在重新推送到远端...");
90
+ const retryPush = await runGit(cwd, ["push"]);
91
+ if (retryPush.ok) {
92
+ return {
93
+ ok: true,
94
+ output: "远端有新提交,已自动完成 git pull --rebase 变基并成功推送到远端!",
95
+ autoRebased: true,
96
+ };
97
+ }
98
+
99
+ return { ok: false, output: retryPush.stderr || retryPush.stdout };
61
100
  }