mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
fix(cli): a rename keeps the received-files record listing the moved file (#8112)
* fix(cli): a rename keeps the received-files record listing the moved file On a disk that tells letter case or Unicode forms apart, a Studio rename to a name differing only in case or form followed the old spelling, so the record dropped the file's only line while the file moved to the new name. - onDiskPath folds a requested name onto an existing entry only when the disk opens the same file under both spellings, so the record follows the exact path the rename creates, spelled as the move spells it. - After a move, an old non-media line goes only when the file left its old path, sits at the planned path (same identity or content), and another line lists that path. Otherwise the line stays, as it does for a delete. - A refused move keeps a new line for a file another move already put there. * docs(cli): word a rename helper comment as what it checks now * test(cli): build the rename test folder by its native real path On Windows the temp folder can carry an 8.3 short name, which onDiskPath spells in full, as the native real path does.
This commit is contained in:
@@ -500,7 +500,7 @@ function receivedRecords(projectDir: string): ReceivedRecord[] | null {
|
||||
return records;
|
||||
}
|
||||
|
||||
function fileIdentity(path: string): string | null {
|
||||
export function fileIdentity(path: string): string | null {
|
||||
try {
|
||||
const stat = lstatSync(path, { bigint: true });
|
||||
return stat.ino === 0n ? null : `${stat.dev}:${stat.ino}`;
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
import {
|
||||
existsSync,
|
||||
mkdirSync,
|
||||
mkdtempSync,
|
||||
readFileSync,
|
||||
realpathSync,
|
||||
renameSync,
|
||||
rmSync,
|
||||
writeFileSync,
|
||||
} from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import {
|
||||
dropReceivedArrivals,
|
||||
dropReceivedLeavers,
|
||||
onDiskPath,
|
||||
plannedReceivedChange,
|
||||
recordReceivedArrivals,
|
||||
} from "./receivedRecordFollow.js";
|
||||
|
||||
let projectDir: string;
|
||||
|
||||
afterEach(() => rmSync(projectDir, { recursive: true, force: true }));
|
||||
|
||||
function project(listed: string[]): string {
|
||||
projectDir = realpathSync.native(mkdtempSync(join(tmpdir(), "hf-record-follow-")));
|
||||
mkdirSync(join(projectDir, ".hyperframes"));
|
||||
writeFileSync(
|
||||
join(projectDir, ".hyperframes", "received-files.json"),
|
||||
JSON.stringify({ files: listed }),
|
||||
);
|
||||
for (const line of listed) writeFileSync(join(projectDir, line), "<p>received</p>");
|
||||
return projectDir;
|
||||
}
|
||||
|
||||
const listed = (dir: string): string[] =>
|
||||
JSON.parse(readFileSync(join(dir, ".hyperframes", "received-files.json"), "utf-8")).files;
|
||||
|
||||
async function followRename(dir: string, planned: string, actual: string): Promise<void> {
|
||||
const change = await plannedReceivedChange(dir, join(dir, "intro.html"), join(dir, planned));
|
||||
const started = await recordReceivedArrivals(change);
|
||||
renameSync(join(dir, "intro.html"), join(dir, actual));
|
||||
await dropReceivedLeavers(started);
|
||||
}
|
||||
|
||||
describe("following a rename in the received-files record", () => {
|
||||
it("drops the old line once the file sits at the path the record now lists", async () => {
|
||||
const dir = project(["intro.html"]);
|
||||
await followRename(dir, "opening.html", "opening.html");
|
||||
expect(listed(dir)).toEqual(["opening.html"]);
|
||||
});
|
||||
|
||||
it("keeps the old line when the file is not at the path the record now lists", async () => {
|
||||
const dir = project(["intro.html"]);
|
||||
await followRename(dir, "opening.html", "elsewhere.html");
|
||||
expect(listed(dir)).toEqual(["intro.html", "opening.html"]);
|
||||
});
|
||||
|
||||
it("keeps the old line when the file stays put and a copy of it appears at the planned path", async () => {
|
||||
const dir = project(["intro.html"]);
|
||||
const change = await plannedReceivedChange(
|
||||
dir,
|
||||
join(dir, "intro.html"),
|
||||
join(dir, "opening.html"),
|
||||
);
|
||||
const started = await recordReceivedArrivals(change);
|
||||
writeFileSync(join(dir, "opening.html"), "<p>received</p>");
|
||||
await dropReceivedLeavers(started);
|
||||
expect(listed(dir)).toEqual(["intro.html", "opening.html"]);
|
||||
});
|
||||
|
||||
it("keeps the new line a refused rename listed when another rename moved the file there", async () => {
|
||||
const dir = project(["intro.html"]);
|
||||
const plan = () =>
|
||||
plannedReceivedChange(dir, join(dir, "intro.html"), join(dir, "opening.html"));
|
||||
const refused = await recordReceivedArrivals(await plan());
|
||||
const done = await recordReceivedArrivals(await plan());
|
||||
renameSync(join(dir, "intro.html"), join(dir, "opening.html"));
|
||||
await dropReceivedLeavers(done);
|
||||
await dropReceivedArrivals(refused);
|
||||
expect(listed(dir)).toEqual(["opening.html"]);
|
||||
});
|
||||
|
||||
it("takes back the new line of a refused rename when the file did not move", async () => {
|
||||
const dir = project(["intro.html"]);
|
||||
const change = await plannedReceivedChange(
|
||||
dir,
|
||||
join(dir, "intro.html"),
|
||||
join(dir, "opening.html"),
|
||||
);
|
||||
await dropReceivedArrivals(await recordReceivedArrivals(change));
|
||||
expect(listed(dir)).toEqual(["intro.html"]);
|
||||
});
|
||||
|
||||
it("keeps the old line when the planned path is the old line itself", async () => {
|
||||
const dir = project(["intro.html"]);
|
||||
await followRename(dir, "intro.html", "elsewhere.html");
|
||||
expect(listed(dir)).toEqual(["intro.html"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("onDiskPath", () => {
|
||||
it("spells a path as the folder lists it only when the disk opens one file under both spellings", () => {
|
||||
const dir = project(["intro.html"]);
|
||||
const foldsCase = existsSync(join(dir, "INTRO.html"));
|
||||
expect(onDiskPath(join(dir, "INTRO.html"))).toBe(
|
||||
join(dir, foldsCase ? "intro.html" : "INTRO.html"),
|
||||
);
|
||||
expect(onDiskPath(join(dir, "intro.html"))).toBe(join(dir, "intro.html"));
|
||||
});
|
||||
});
|
||||
@@ -5,19 +5,22 @@ import { realFilePath, resolveWithinProject } from "@hyperframes/studio-server/s
|
||||
import { registryRoot } from "../registry/publication.js";
|
||||
import {
|
||||
changeReceivedRecordAsync,
|
||||
fileIdentity,
|
||||
filesListedInFolder,
|
||||
receivedLinesAtOrAbove,
|
||||
} from "./privateProjectFile.js";
|
||||
import { contentHashAsync, hashable, readReceivedContent } from "./receivedContent.js";
|
||||
import { contentHash, contentHashAsync, hashable, readReceivedContent } from "./receivedContent.js";
|
||||
import { isMediaFontOrImage, isMediaName } from "./receivedRender.js";
|
||||
|
||||
type Lines = Map<string, string[]>;
|
||||
|
||||
/** A listed file a Studio rename, move or delete takes off its path, and its sha256 when its content is checked. */
|
||||
/** A listed file a Studio rename, move or delete takes off its path, its sha256 when checked, and where a move puts it. */
|
||||
interface Leaving {
|
||||
line: string;
|
||||
hash?: string;
|
||||
stays?: boolean;
|
||||
identity?: string | null;
|
||||
landsAt?: string;
|
||||
}
|
||||
|
||||
/** Worked out while the files are on disk. A line whose file is already gone stays, as the sign it is gone. */
|
||||
@@ -36,24 +39,26 @@ function caseInsensitive(folder: string): boolean {
|
||||
return swapped !== folder && existsSync(swapped);
|
||||
}
|
||||
|
||||
/** `path` spelled as its folder lists it, so a request in another letter case or Unicode form names the listed file. */
|
||||
/** `path` spelled as its folder lists it, when the disk opens that same file under the spelling `path` asks for. */
|
||||
export function onDiskPath(path: string): string {
|
||||
const folder = realFilePath(dirname(path));
|
||||
const name = basename(path);
|
||||
const asked = fileIdentity(join(folder, name));
|
||||
if (asked === null) return join(folder, name);
|
||||
const folded = name.normalize("NFC").toLowerCase();
|
||||
const sameFile = (entry: string) =>
|
||||
entry.normalize("NFC").toLowerCase() === folded && fileIdentity(join(folder, entry)) === asked;
|
||||
try {
|
||||
const names = readdirSync(folder);
|
||||
const found =
|
||||
names.find((entry) => entry === name) ??
|
||||
names.find((entry) => entry.normalize("NFC").toLowerCase() === folded);
|
||||
return join(folder, found ?? name);
|
||||
return join(folder, names.includes(name) ? name : (names.find(sameFile) ?? name));
|
||||
} catch {
|
||||
return join(folder, name);
|
||||
}
|
||||
}
|
||||
|
||||
const keyIn = (folder: string, path: string) =>
|
||||
relative(folder, path).split(sep).join("/").normalize("NFC");
|
||||
const pathIn = (folder: string, path: string) => relative(folder, path).split(sep).join("/");
|
||||
|
||||
const keyIn = (folder: string, path: string) => pathIn(folder, path).normalize("NFC");
|
||||
|
||||
const leavesFolder = (key: string) => key === ".." || key.startsWith("../") || isAbsolute(key);
|
||||
|
||||
@@ -118,6 +123,38 @@ async function leaving(line: string, path: string): Promise<Leaving | null> {
|
||||
return hash === null ? null : { line, hash, stays: true };
|
||||
}
|
||||
|
||||
interface Arrival {
|
||||
folder: string;
|
||||
line: string;
|
||||
}
|
||||
|
||||
function arrivalOfMovedLine(
|
||||
root: string,
|
||||
folder: string,
|
||||
fromKey: string,
|
||||
line: string,
|
||||
to: string,
|
||||
): Arrival {
|
||||
const rest = line
|
||||
.split("/")
|
||||
.slice(fromKey.split("/").length)
|
||||
.map((segment) => `/${segment}`)
|
||||
.join("");
|
||||
const toKey = pathIn(folder, to);
|
||||
if (leavesFolder(toKey)) return { folder: root, line: pathIn(root, to) + rest };
|
||||
return { folder, line: toKey + rest };
|
||||
}
|
||||
|
||||
function leaverStayingUntilListedWhereItLands(
|
||||
folder: string,
|
||||
left: Leaving,
|
||||
arrived: Arrival,
|
||||
): Leaving {
|
||||
const landsAt = join(arrived.folder, arrived.line);
|
||||
const stays = left.stays === true && !isMediaName(left.line) && isMediaName(landsAt);
|
||||
return { ...left, stays, identity: fileIdentity(join(folder, left.line)), landsAt };
|
||||
}
|
||||
|
||||
export async function plannedReceivedChange(
|
||||
projectDir: string,
|
||||
from: string,
|
||||
@@ -135,14 +172,9 @@ export async function plannedReceivedChange(
|
||||
push(change.leaving, folder, left);
|
||||
continue;
|
||||
}
|
||||
const rest = line.normalize("NFC").slice(fromKey.length);
|
||||
const toKey = keyIn(folder, to);
|
||||
push(change.leaving, folder, {
|
||||
...left,
|
||||
stays: left.stays === true && !isMediaName(line) && isMediaName(toKey + rest),
|
||||
});
|
||||
if (leavesFolder(toKey)) push(change.arriving, root, keyIn(root, to) + rest);
|
||||
else push(change.arriving, folder, toKey + rest);
|
||||
const arrived = arrivalOfMovedLine(root, folder, fromKey, line, to);
|
||||
push(change.leaving, folder, leaverStayingUntilListedWhereItLands(folder, left, arrived));
|
||||
push(change.arriving, arrived.folder, arrived.line);
|
||||
}
|
||||
}
|
||||
return change;
|
||||
@@ -176,18 +208,54 @@ export async function recordReceivedArrivals(change: ReceivedChange): Promise<Re
|
||||
return { ...change, leaving, arriving };
|
||||
}
|
||||
|
||||
const holdsLeaver = (path: string, left: Leaving) =>
|
||||
(!!left.identity && fileIdentity(path) === left.identity) ||
|
||||
(left.hash !== undefined && contentHash(path) === left.hash);
|
||||
|
||||
/** Whether the file `left` lists has left its old path for the one its move planned. */
|
||||
const movedAsPlanned = (folder: string, left: Leaving): left is Leaving & { landsAt: string } =>
|
||||
left.landsAt !== undefined &&
|
||||
!holdsLeaver(join(folder, left.line), left) &&
|
||||
holdsLeaver(left.landsAt, left);
|
||||
|
||||
/** Whether a non-media file sits at its planned destination and a record lists it there by another line. */
|
||||
function listedWhereItLands(folder: string, left: Leaving): boolean {
|
||||
if (left.landsAt === undefined || left.hash === undefined) return true;
|
||||
if (!movedAsPlanned(folder, left)) return false;
|
||||
const { landsAt } = left;
|
||||
return receivedLinesAtOrAbove(dirname(landsAt)).some(({ folder: at, lines }) =>
|
||||
lines.some((line) => join(at, line) === landsAt && (at !== folder || line !== left.line)),
|
||||
);
|
||||
}
|
||||
|
||||
export const dropReceivedLeavers = (change: ReceivedChange): Promise<void> =>
|
||||
dropLines(
|
||||
new Map(
|
||||
[...change.leaving].map(([folder, lines]) => [
|
||||
folder,
|
||||
lines.flatMap((left) => (left.stays ? [] : [left.line])),
|
||||
lines.flatMap((left) =>
|
||||
!left.stays && listedWhereItLands(folder, left) ? [left.line] : [],
|
||||
),
|
||||
]),
|
||||
),
|
||||
);
|
||||
|
||||
export const dropReceivedArrivals = (change: ReceivedChange): Promise<void> =>
|
||||
dropLines(change.arriving);
|
||||
/** A refused move's new lines go, but for a file another move already put there. */
|
||||
export function dropReceivedArrivals(change: ReceivedChange): Promise<void> {
|
||||
const landed = new Set(
|
||||
[...change.leaving].flatMap(([folder, lines]) =>
|
||||
lines.filter((left) => movedAsPlanned(folder, left)).flatMap((left) => left.landsAt ?? []),
|
||||
),
|
||||
);
|
||||
return dropLines(
|
||||
new Map(
|
||||
[...change.arriving].map(([folder, lines]) => [
|
||||
folder,
|
||||
lines.filter((line) => !landed.has(join(folder, line))),
|
||||
]),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists in the project's own record each of `paths` that no record lists and that holds content kept as received, so
|
||||
|
||||
Reference in New Issue
Block a user