Recover branch during rebase

This commit is contained in:
Mohamed Boudra
2026-04-22 18:35:01 +07:00
parent 637109eab0
commit 407b4b68b0
2 changed files with 41 additions and 1 deletions
+23 -1
View File
@@ -1,6 +1,6 @@
import { resolve, dirname, basename } from "path";
import { existsSync, realpathSync } from "fs";
import { open as openFile, stat as statFile } from "fs/promises";
import { open as openFile, readFile, stat as statFile } from "fs/promises";
import { TTLCache } from "@isaacs/ttlcache";
import type { ParsedDiffFile } from "../server/utils/diff-highlighter.js";
import { parseAndHighlightDiff } from "../server/utils/diff-highlighter.js";
@@ -701,12 +701,34 @@ export async function getCurrentBranch(cwd: string): Promise<string | null> {
env: READ_ONLY_GIT_ENV,
});
const branch = stdout.trim();
if (branch === "HEAD") {
return await getRebaseHeadBranch(cwd);
}
return branch.length > 0 ? branch : null;
} catch {
return null;
}
}
async function getRebaseHeadBranch(cwd: string): Promise<string | null> {
for (const path of ["rebase-merge/head-name", "rebase-apply/head-name"]) {
try {
const { stdout } = await runGitCommand(["rev-parse", "--git-path", path], {
cwd,
env: READ_ONLY_GIT_ENV,
});
const headName = (await readFile(resolve(cwd, stdout.trim()), "utf8")).trim();
if (headName.startsWith("refs/heads/")) {
return headName.slice("refs/heads/".length) || null;
}
return headName || null;
} catch {
// Try the other rebase backend.
}
}
return null;
}
async function getWorktreeRoot(cwd: string): Promise<string | null> {
try {
const { stdout } = await runGitCommand(["rev-parse", "--show-toplevel"], {