Skip to content

Commit bbaca6a

Browse files
committed
fix: ammend issues if branch is not existing upstream
1 parent 292c179 commit bbaca6a

1 file changed

Lines changed: 102 additions & 24 deletions

File tree

‎src/extension.ts‎

Lines changed: 102 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import * as vscode from "vscode";
21
import * as fs from "fs/promises";
32
import * as path from "path";
3+
import * as vscode from "vscode";
44

55
import { normalizeRepoRelPath, toRepoRelPath } from "./utils/paths";
66
import { runGit, runGitCapture } from "./utils/process";
@@ -56,6 +56,51 @@ async function getHeadMessage(repoRoot: string): Promise<string> {
5656
return msg.trim();
5757
}
5858

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+
59104
export async function activate(context: vscode.ExtensionContext) {
60105
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
61106
if (!workspaceFolder) {
@@ -119,28 +164,45 @@ export async function activate(context: vscode.ExtensionContext) {
119164
const staged = await getStagedPaths(repoRoot);
120165

121166
if (amend) {
167+
const oldMsg = await getHeadMessage(repoRoot);
168+
169+
// If nothing is staged, only allow amend when the message changes.
122170
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);
136173
throw new Error(
137174
headEmpty
138175
? "Nothing to amend: last commit is empty and message is unchanged."
139176
: "Nothing staged to amend. Stage files or disable Amend.",
140177
);
141178
}
179+
180+
// message-only amend
181+
await runGit(repoRoot, [
182+
"commit",
183+
"--amend",
184+
"--allow-empty",
185+
"-m",
186+
newMsg,
187+
]);
142188
} 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+
}
144206
}
145207
} else {
146208
if (staged.size === 0) {
@@ -170,11 +232,7 @@ export async function activate(context: vscode.ExtensionContext) {
170232
return;
171233
}
172234

173-
if (amend) {
174-
await runGit(repoRoot, ["push", "--force-with-lease"]);
175-
} else {
176-
await runGit(repoRoot, ["push"]);
177-
}
235+
await pushWithUpstreamFallback(repoRoot, { amend });
178236
},
179237
);
180238

@@ -221,12 +279,32 @@ export async function activate(context: vscode.ExtensionContext) {
221279
// ----------------------------
222280
async function getStagedPaths(repoRoot: string): Promise<Set<string>> {
223281
const out = await runGitCapture(repoRoot, [
224-
"diff",
225-
"--cached",
226-
"--name-only",
282+
"status",
283+
"--porcelain=v1",
227284
"-z",
228285
]);
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;
230308
}
231309

232310
async function stagePaths(paths: string[]) {

0 commit comments

Comments
 (0)