|
1 | | -import * as vscode from "vscode"; |
2 | 1 | import * as fs from "fs/promises"; |
3 | 2 | import * as path from "path"; |
| 3 | +import * as vscode from "vscode"; |
4 | 4 |
|
5 | 5 | import { normalizeRepoRelPath, toRepoRelPath } from "./utils/paths"; |
6 | 6 | import { runGit, runGitCapture } from "./utils/process"; |
@@ -56,6 +56,51 @@ async function getHeadMessage(repoRoot: string): Promise<string> { |
56 | 56 | return msg.trim(); |
57 | 57 | } |
58 | 58 |
|
| 59 | +async function getCurrentBranch(repoRoot: string): Promise<string> { |
| 60 | + const out = await runGitCapture(repoRoot, [ |
| 61 | + "rev-parse", |
| 62 | + "--abbrev-ref", |
| 63 | + "HEAD", |
| 64 | + ]); |
| 65 | + const branch = out.trim(); |
| 66 | + |
| 67 | + if (!branch || branch === "HEAD") { |
| 68 | + throw new Error("Detached HEAD: cannot push without a branch."); |
| 69 | + } |
| 70 | + return branch; |
| 71 | +} |
| 72 | + |
| 73 | +function looksLikeNoUpstream(err: unknown): boolean { |
| 74 | + const msg = String(err ?? ""); |
| 75 | + return ( |
| 76 | + msg.includes("no upstream branch") || |
| 77 | + msg.includes("has no upstream branch") || |
| 78 | + msg.includes("set the remote as upstream") || |
| 79 | + (msg.includes("The current branch") && msg.includes("has no upstream")) |
| 80 | + ); |
| 81 | +} |
| 82 | + |
| 83 | +async function pushWithUpstreamFallback( |
| 84 | + repoRoot: string, |
| 85 | + { amend }: { amend: boolean }, |
| 86 | +): Promise<void> { |
| 87 | + try { |
| 88 | + await runGit(repoRoot, amend ? ["push", "--force-with-lease"] : ["push"]); |
| 89 | + return; |
| 90 | + } catch (e) { |
| 91 | + if (!looksLikeNoUpstream(e)) { |
| 92 | + throw e; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + const branch = await getCurrentBranch(repoRoot); |
| 97 | + |
| 98 | + const baseArgs = ["push", "-u", "origin", branch]; |
| 99 | + const args = amend ? [...baseArgs, "--force-with-lease"] : baseArgs; |
| 100 | + |
| 101 | + await runGit(repoRoot, args); |
| 102 | +} |
| 103 | + |
59 | 104 | export async function activate(context: vscode.ExtensionContext) { |
60 | 105 | const workspaceFolder = vscode.workspace.workspaceFolders?.[0]; |
61 | 106 | if (!workspaceFolder) { |
@@ -119,28 +164,45 @@ export async function activate(context: vscode.ExtensionContext) { |
119 | 164 | const staged = await getStagedPaths(repoRoot); |
120 | 165 |
|
121 | 166 | if (amend) { |
| 167 | + const oldMsg = await getHeadMessage(repoRoot); |
| 168 | + |
| 169 | + // If nothing is staged, only allow amend when the message changes. |
122 | 170 | if (staged.size === 0) { |
123 | | - const headEmpty = await isHeadEmptyVsParent(repoRoot); |
124 | | - const oldMsg = await getHeadMessage(repoRoot); |
125 | | - |
126 | | - // allow empty amend ONLY if message changes |
127 | | - if (newMsg !== oldMsg) { |
128 | | - await runGit(repoRoot, [ |
129 | | - "commit", |
130 | | - "--amend", |
131 | | - "--allow-empty", |
132 | | - "-m", |
133 | | - newMsg, |
134 | | - ]); |
135 | | - } else { |
| 171 | + if (newMsg === oldMsg) { |
| 172 | + const headEmpty = await isHeadEmptyVsParent(repoRoot); |
136 | 173 | throw new Error( |
137 | 174 | headEmpty |
138 | 175 | ? "Nothing to amend: last commit is empty and message is unchanged." |
139 | 176 | : "Nothing staged to amend. Stage files or disable Amend.", |
140 | 177 | ); |
141 | 178 | } |
| 179 | + |
| 180 | + // message-only amend |
| 181 | + await runGit(repoRoot, [ |
| 182 | + "commit", |
| 183 | + "--amend", |
| 184 | + "--allow-empty", |
| 185 | + "-m", |
| 186 | + newMsg, |
| 187 | + ]); |
142 | 188 | } else { |
143 | | - await runGit(repoRoot, ["commit", "--amend", "-m", newMsg]); |
| 189 | + // staged amend (but Git may still complain it would become empty) |
| 190 | + try { |
| 191 | + await runGit(repoRoot, ["commit", "--amend", "-m", newMsg]); |
| 192 | + } catch (e) { |
| 193 | + const msg = String((e as any)?.message ?? e); |
| 194 | + if (msg.includes("would make it empty")) { |
| 195 | + await runGit(repoRoot, [ |
| 196 | + "commit", |
| 197 | + "--amend", |
| 198 | + "--allow-empty", |
| 199 | + "-m", |
| 200 | + newMsg, |
| 201 | + ]); |
| 202 | + } else { |
| 203 | + throw e; |
| 204 | + } |
| 205 | + } |
144 | 206 | } |
145 | 207 | } else { |
146 | 208 | if (staged.size === 0) { |
@@ -170,11 +232,7 @@ export async function activate(context: vscode.ExtensionContext) { |
170 | 232 | return; |
171 | 233 | } |
172 | 234 |
|
173 | | - if (amend) { |
174 | | - await runGit(repoRoot, ["push", "--force-with-lease"]); |
175 | | - } else { |
176 | | - await runGit(repoRoot, ["push"]); |
177 | | - } |
| 235 | + await pushWithUpstreamFallback(repoRoot, { amend }); |
178 | 236 | }, |
179 | 237 | ); |
180 | 238 |
|
@@ -221,12 +279,32 @@ export async function activate(context: vscode.ExtensionContext) { |
221 | 279 | // ---------------------------- |
222 | 280 | async function getStagedPaths(repoRoot: string): Promise<Set<string>> { |
223 | 281 | const out = await runGitCapture(repoRoot, [ |
224 | | - "diff", |
225 | | - "--cached", |
226 | | - "--name-only", |
| 282 | + "status", |
| 283 | + "--porcelain=v1", |
227 | 284 | "-z", |
228 | 285 | ]); |
229 | | - return new Set(out.split("\0").filter(Boolean)); |
| 286 | + |
| 287 | + const staged = new Set<string>(); |
| 288 | + const entries = out.split("\0").filter(Boolean); |
| 289 | + |
| 290 | + for (const e of entries) { |
| 291 | + // v1 format: XY<space>path |
| 292 | + // "M file" => staged (index) |
| 293 | + // " M file" => not staged (working tree) |
| 294 | + // "?? file" => untracked |
| 295 | + const xy = e.slice(0, 2); |
| 296 | + const p = e.slice(3); |
| 297 | + if (!p) { |
| 298 | + continue; |
| 299 | + } |
| 300 | + |
| 301 | + const x = xy[0]; // index status |
| 302 | + if (x !== " " && x !== "?") { |
| 303 | + staged.add(normalizeRepoRelPath(p)); |
| 304 | + } |
| 305 | + } |
| 306 | + |
| 307 | + return staged; |
230 | 308 | } |
231 | 309 |
|
232 | 310 | async function stagePaths(paths: string[]) { |
|
0 commit comments