Recover branch during rebase
This commit is contained in:
@@ -194,6 +194,24 @@ describe("checkout git utilities", () => {
|
|||||||
expect(branch).toBeNull();
|
expect(branch).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("returns the branch being rebased when HEAD is detached during a rebase", async () => {
|
||||||
|
execSync("git checkout -b feature/rebase-test", { cwd: repoDir });
|
||||||
|
writeFileSync(join(repoDir, "file.txt"), "feature\n");
|
||||||
|
execSync("git add file.txt", { cwd: repoDir });
|
||||||
|
execSync("git -c commit.gpgsign=false commit -m 'feature change'", { cwd: repoDir });
|
||||||
|
|
||||||
|
execSync("git checkout main", { cwd: repoDir });
|
||||||
|
writeFileSync(join(repoDir, "file.txt"), "main\n");
|
||||||
|
execSync("git add file.txt", { cwd: repoDir });
|
||||||
|
execSync("git -c commit.gpgsign=false commit -m 'main change'", { cwd: repoDir });
|
||||||
|
|
||||||
|
execSync("git checkout feature/rebase-test", { cwd: repoDir });
|
||||||
|
expect(() => execSync("git rebase main", { cwd: repoDir, stdio: "pipe" })).toThrow();
|
||||||
|
|
||||||
|
const branch = await getCurrentBranch(repoDir);
|
||||||
|
expect(branch).toBe("feature/rebase-test");
|
||||||
|
});
|
||||||
|
|
||||||
it("handles status/diff/commit in a normal repo", async () => {
|
it("handles status/diff/commit in a normal repo", async () => {
|
||||||
writeFileSync(join(repoDir, "file.txt"), "updated\n");
|
writeFileSync(join(repoDir, "file.txt"), "updated\n");
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { resolve, dirname, basename } from "path";
|
import { resolve, dirname, basename } from "path";
|
||||||
import { existsSync, realpathSync } from "fs";
|
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 { TTLCache } from "@isaacs/ttlcache";
|
||||||
import type { ParsedDiffFile } from "../server/utils/diff-highlighter.js";
|
import type { ParsedDiffFile } from "../server/utils/diff-highlighter.js";
|
||||||
import { parseAndHighlightDiff } 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,
|
env: READ_ONLY_GIT_ENV,
|
||||||
});
|
});
|
||||||
const branch = stdout.trim();
|
const branch = stdout.trim();
|
||||||
|
if (branch === "HEAD") {
|
||||||
|
return await getRebaseHeadBranch(cwd);
|
||||||
|
}
|
||||||
return branch.length > 0 ? branch : null;
|
return branch.length > 0 ? branch : null;
|
||||||
} catch {
|
} catch {
|
||||||
return null;
|
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> {
|
async function getWorktreeRoot(cwd: string): Promise<string | null> {
|
||||||
try {
|
try {
|
||||||
const { stdout } = await runGitCommand(["rev-parse", "--show-toplevel"], {
|
const { stdout } = await runGitCommand(["rev-parse", "--show-toplevel"], {
|
||||||
|
|||||||
Reference in New Issue
Block a user