mirror of
https://github.com/getpaseo/paseo.git
synced 2026-11-19 23:05:21 +00:00
fix(archive): key a saved HEAD by the folder's real path
Archive and recovery key the saved HEAD by the recorded worktree root, which is already a real path. Worktree placement checked the path as written, so with a symlink in the Paseo home or worktrees root a new worktree missed the saved HEAD and took the archived folder's path. archived-head.ts now resolves symlinks itself when it builds the key, through the nearest existing ancestor when the folder is gone, so every spelling of one folder reaches the same ref. Co-authored-by: Okan YILDIRIM <63511519+ragokan@users.noreply.github.com>
This commit is contained in:
co-authored by
Okan YILDIRIM
parent
de77c82889
commit
ce637139e0
@@ -282,7 +282,7 @@ does not flip it back. Tabs and layout are purged only once the undo window clos
|
||||
archive committed.
|
||||
|
||||
Before deleting the directory, the archive saves HEAD as one ref,
|
||||
`refs/paseo/archived/<hash of the directory path>/<name>`, whose value is HEAD's commit and
|
||||
`refs/paseo/archived/<hash of the directory's real path>/<name>`, whose value is HEAD's commit and
|
||||
whose `<name>` is `git rev-parse --abbrev-ref HEAD` (`feature`, or `HEAD` when detached). The ref
|
||||
keeps git's cleanup from deleting commits only that HEAD held. Recovery checks out the saved name
|
||||
when it still resolves to the saved commit and the saved commit otherwise, so git decides
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
import { execFileSync } from "node:child_process";
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { mkdtempSync, rmSync, symlinkSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { basename, dirname, join } from "node:path";
|
||||
|
||||
import { afterEach, expect, test } from "vitest";
|
||||
|
||||
import { readArchivedHead, releaseArchivedHead, saveArchivedHead } from "./archived-head.js";
|
||||
import {
|
||||
isHeldByArchivedWorktree,
|
||||
readArchivedHead,
|
||||
releaseArchivedHead,
|
||||
saveArchivedHead,
|
||||
} from "./archived-head.js";
|
||||
|
||||
const tempDirectories: string[] = [];
|
||||
|
||||
@@ -57,6 +62,22 @@ test("restore checks out the saved branch while it still points at the saved com
|
||||
});
|
||||
});
|
||||
|
||||
test("a removed folder reached through a symlinked parent finds the HEAD saved under its real path", async () => {
|
||||
const { repoRoot, worktreePath } = createRepositoryWithWorktree();
|
||||
const linkedParent = `${dirname(worktreePath)}-link`;
|
||||
symlinkSync(dirname(worktreePath), linkedParent);
|
||||
tempDirectories.push(linkedParent);
|
||||
const linkedWorktreePath = join(linkedParent, basename(worktreePath));
|
||||
const commit = git(worktreePath, ["rev-parse", "HEAD"]);
|
||||
await saveArchivedHead({ worktreePath, repoRoot, hadActiveWorkspace: true });
|
||||
removeWorktree(repoRoot, worktreePath);
|
||||
|
||||
expect({
|
||||
held: await isHeldByArchivedWorktree({ repoRoot, worktreePath: linkedWorktreePath }),
|
||||
head: await readArchivedHead({ repoRoot, worktreePath: linkedWorktreePath }),
|
||||
}).toEqual({ held: true, head: { checkout: "feature", commit } });
|
||||
});
|
||||
|
||||
test("a HEAD left behind by a failed release is not read as current after the folder disappears", async () => {
|
||||
const { repoRoot, worktreePath } = createRepositoryWithWorktree();
|
||||
// An earlier archive saved HEAD, and the unarchive that followed could not delete it.
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { realpath } from "node:fs";
|
||||
import { stat } from "node:fs/promises";
|
||||
import { join, resolve } from "node:path";
|
||||
import { basename, dirname, join, resolve } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
import { runGitCommand } from "../../../utils/run-git-command.js";
|
||||
|
||||
// Removing a worktree folder at archive deletes its HEAD, and with it the only hold on commits
|
||||
// that no branch reaches. Before the folder goes, archive saves HEAD as one ref:
|
||||
//
|
||||
// refs/paseo/archived/<sha256 of the folder path>/<git's name for HEAD> -> HEAD's commit
|
||||
// refs/paseo/archived/<sha256 of the folder's real path>/<git's name for HEAD> -> HEAD's commit
|
||||
//
|
||||
// The name is `git rev-parse --abbrev-ref HEAD`, stored as git prints it and never parsed here.
|
||||
// The ref keeps the commit alive, and recovery hands the name back to git when it still means
|
||||
@@ -25,8 +27,26 @@ async function exists(path: string): Promise<boolean> {
|
||||
);
|
||||
}
|
||||
|
||||
function savedHeadPrefix(worktreePath: string): string {
|
||||
const folder = createHash("sha256").update(resolve(worktreePath)).digest("hex");
|
||||
// fs.realpath rather than the native resolver, which can return a Windows 8.3 alias.
|
||||
const resolveRealPath = promisify(realpath);
|
||||
|
||||
// The folder's path with symlinks resolved, even after the folder is gone: archive, recovery and
|
||||
// worktree placement can each spell one folder differently, and all must reach the same key.
|
||||
async function canonicalFolderPath(path: string): Promise<string> {
|
||||
const absolute = resolve(path);
|
||||
const parent = dirname(absolute);
|
||||
if (parent === absolute) return absolute;
|
||||
try {
|
||||
return await resolveRealPath(absolute);
|
||||
} catch {
|
||||
return join(await canonicalFolderPath(parent), basename(absolute));
|
||||
}
|
||||
}
|
||||
|
||||
async function savedHeadPrefix(worktreePath: string): Promise<string> {
|
||||
const folder = createHash("sha256")
|
||||
.update(await canonicalFolderPath(worktreePath))
|
||||
.digest("hex");
|
||||
return `refs/paseo/archived/${folder}/`;
|
||||
}
|
||||
|
||||
@@ -74,7 +94,7 @@ export async function saveArchivedHead(input: {
|
||||
repoRoot: string | null;
|
||||
hadActiveWorkspace: boolean;
|
||||
}): Promise<void> {
|
||||
const prefix = savedHeadPrefix(input.worktreePath);
|
||||
const prefix = await savedHeadPrefix(input.worktreePath);
|
||||
const name = await readHeadName(input.worktreePath);
|
||||
if (name) {
|
||||
const cwd = input.worktreePath;
|
||||
@@ -98,7 +118,7 @@ export async function readArchivedHead(input: {
|
||||
worktreePath: string;
|
||||
}): Promise<ArchivedHead | null> {
|
||||
if (!(await isRepository(input.repoRoot))) return null;
|
||||
const prefix = savedHeadPrefix(input.worktreePath);
|
||||
const prefix = await savedHeadPrefix(input.worktreePath);
|
||||
const [saved] = await listSavedHeads(input.repoRoot, prefix);
|
||||
if (!saved) return null;
|
||||
const separator = saved.indexOf(" ");
|
||||
@@ -118,7 +138,7 @@ export async function releaseArchivedHead(input: {
|
||||
}): Promise<void> {
|
||||
// Without a repository there is no ref to delete.
|
||||
if (!(await isRepository(input.repoRoot))) return;
|
||||
await discardSavedHeads(input.repoRoot, savedHeadPrefix(input.worktreePath));
|
||||
await discardSavedHeads(input.repoRoot, await savedHeadPrefix(input.worktreePath));
|
||||
}
|
||||
|
||||
/** Whether an archived worktree still holds this path, so a new worktree must not take it. */
|
||||
@@ -126,6 +146,6 @@ export async function isHeldByArchivedWorktree(input: {
|
||||
repoRoot: string;
|
||||
worktreePath: string;
|
||||
}): Promise<boolean> {
|
||||
const saved = await listSavedHeads(input.repoRoot, savedHeadPrefix(input.worktreePath));
|
||||
const saved = await listSavedHeads(input.repoRoot, await savedHeadPrefix(input.worktreePath));
|
||||
return saved.length > 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user