Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ When `patchbook track` failed after its sync, such as when `.patchbook/config.to

`patchbook sync` used to stop with a stack trace when a change's `base/<file>.pb` or `ours/<file>.pb` was missing. Now it fails with a `patchbook:` message that names each change, file and missing copy, both copies when both are gone, and changes nothing. Restore the copies, or remove the edit from the change, and sync again (#10).

### An edited copy keeps following upstream

Copy an upstream file, commit so `record` writes `copy skills/a -> skills/b`, then edit `skills/b/SKILL.md` and commit. `record` and the commit hook used to write `replace skills/b/SKILL.md`, a frozen copy, so `skills/b` missed every later upstream change to `skills/a`, and `status` said nothing. Now they write `file skills/b/SKILL.md`, a file edit on the copy's own path whose base is upstream's `skills/a/SKILL.md`. `sync` merges upstream's changes to `skills/a` into it. An upstream change that overlaps your edit is a conflict, which `sync` reports and `status`, `build` and the commit hook refuse, as for any file edit. `skills/a` still builds from upstream, without your edit. The file edit applies where the `replace` did in the series, so a later hand-written `copy` or `move` of `skills/b` still builds your edit, and `record` writes it only into the change with the `copy` or a later one. An edited rename of a file that a copy still builds from is now `move` plus a `file` edit on the new path, where it was `delete` plus `add`. Once a copy has its own file edit, an edit to its source is a `file` edit as well, where it was a `replace`. Deleting the edited copy takes its file edit out of the change and records a `delete`, unless a later copy or move reads it, when the edit stays. An edited copy that isn't text, or whose source upstream makes binary, keeps your bytes and builds, as the `replace` did, since `sync` merges only text. `sync` then moves its base to upstream's file, lists it in `replacedUpstreamChanged` and says on stderr that it kept yours. To take upstream's file instead, write it over the copy and run `patchbook record`. A copy's file edit now needs its base copy, and `status` and `build` refuse and name the change and the missing copy when it is gone, where the `replace` built the frozen copy without one. A copy recorded as a `replace` before this release stays one (#317).

### A partial commit can't move the upstream pin without its vendored copy

`git commit -- note.txt .patchbook/upstream.lock` after a `patchbook sync` used to commit the new pin and leave the vendored copy at the old one, and a clone's `status` then failed with `vendor/extra (tree ...) differs from the pin`. The hook now refuses that commit before it records or builds anything, and names the upstream, the lock and the vendor folder. Add the lock and the folder to the paths you name, or run `git add` on the lock, and on the file it points to when it is a link, and make a full commit, where the hook stages the pinned copy. When the lock is a link to a file in the repo, name that file too. When git doesn't know a path yet, such as the folder of a first vendoring, the message says to `git add` it first, since `git commit -- <path>` fails on a path git doesn't know. A refused commit leaves HEAD, your index and your working files as they were. A partial commit that doesn't move the pin, such as one that only names `note.txt`, is not refused by this check, including when HEAD's lock is a link that leads out of the repo. A copy that differs from the pin still gets the message that it differs from the pin. The message for an unstaged `upstream.lock` gives the same advice. It names the lock, the file a linked lock points to, the copies, and any other unstaged build input, where it said that naming the lock alone was enough (#227).
Expand Down
14 changes: 7 additions & 7 deletions DESIGN.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type Problem =
| ({ problem: "invalid" } & (InFile | InEdit | InChange | InOutput | InBlock) & Detail)
| ({ problem: "invalid" } & (InFile | InEdit | InChange) & Detail & Refuse)
| ({ problem: "gone" } & (InEdit | InChange) & Detail)
| ({ problem: "stale" } & InChange & Detail)
| ({ problem: "stale" } & (InEdit | InChange) & Detail)
| ({ problem: "conflict" } & (InFile | InChange) & Detail)
| ({ problem: "conflict" } & (InFile | InChange) & Detail & Refuse)
| ({ problem: "placeholder" } & (InOutput | InBlock) & Detail)
Expand Down
68 changes: 49 additions & 19 deletions src/assemble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "./blocks.ts";
import { ATTRIBUTES_FILE, Book, buildInputs, GIT, PB, type Reserved, under } from "./book.ts";
import {
Change, CHANGE_NAME, editText, isMergeMarker, readChanges, replaceIntent, strayProblems,
Change, CHANGE_NAME, type Edit, editText, isMergeMarker, readChanges, replaceIntent, strayProblems,
} from "./changes.ts";
import { installHookOrWarn, stageModes, upstreamModes, warnUnhooked } from "./githook.ts";
import { Fail } from "./lib/fail.ts";
Expand Down Expand Up @@ -112,19 +112,30 @@ function intoReserved(where: InEdit, path: string, { place, refuse }: Reserved):
return refuse ? { problem: "invalid", ...where, detail, refuse: true } : { problem: "invalid", ...where, detail };
}

/** Apply add, replace, delete, move and copy to a map of staged path to source, and count the edits that applied. */
/** Apply add, replace, delete, move and copy, and each `file` edit on a path upstream doesn't have, to a map of staged path to source,
* and count the structural edits that applied. */
export function applyStructure(
changes: readonly Change[],
upstream: ReadonlyMap<string, File>,
problems: Problem[],
reservedHolding: (staged: string) => Reserved | null,
dropped = new Set<string>(),
): { tree: Map<string, Source>; applied: number } {
): { tree: Map<string, Source>; applied: number; upstreamOf: Map<string, string>; unread: Set<string> } {
const tree = new Map<string, Source>(Array.from(upstream.keys(), (p) => [p, { kind: "up", path: p }]));
const upstreamOf = new Map<string, string>();
const placed: string[] = [];
let applied = 0;
for (const c of changes) {
for (const e of c.edits) {
if (e.kind === "file") continue;
if (e.kind === "file") {
const src = upstream.has(e.path) ? undefined : tree.get(e.path);
if (src?.kind !== "up") continue;
upstreamOf.set(e.path, src.path);
if (c.read("ours", e.path) === null) continue;
tree.set(e.path, { kind: "ours", change: c.name, path: e.path });
placed.push(`${c.name}\0${e.path}`);
continue;
}
const where: InEdit = { file: `changes/${c.name}`, edit: editText(e) };
const matched = Array.from(tree.keys()).filter((p) => under(p, e.path));
if (e.kind === "add") {
Expand Down Expand Up @@ -170,7 +181,8 @@ export function applyStructure(
}
}
}
return { tree, applied };
const read = new Set(Array.from(tree.values()).flatMap((s) => (s.kind === "ours" ? [`${s.change}\0${s.path}`] : [])));
return { tree, applied, upstreamOf, unread: new Set(placed.filter((k) => !read.has(k))) };
}

export function blockFiles(book: Book): string[] {
Expand All @@ -183,12 +195,16 @@ export function assemble(book: Book): Assembly {
let applied = 0;
const changes = readChanges(book, problems);
const byName = new Map(changes.map((c) => [c.name, c]));
const structure: Problem[] = [];
const lost = new Set<string>();
const { tree: staged, applied: structural, upstreamOf, unread } = applyStructure(changes, upstream, structure, (p) => book.reservedHolding(p), lost);

const conflicted = syncConflicts(book);
for (const name of foldersWith(book.data("changes"), "CHANGE.md").filter((name) => CHANGE_NAME.test(name))) {
const c = byName.get(name) ?? new Change(name, book.data("changes", name));
for (const copy of nonFolders(join(c.dir, "ours")).filter((p) => p.endsWith(".pb"))) {
const path = copy.slice(0, -".pb".length);
if (unread.has(`${c.name}\0${path}`)) continue;
const adds = conflicted.has(`${c.name}\0${path}`) ? addsConflictBlock : addsConflictMarker;
if (adds(textOf(c.file("ours", path)), textOf(c.file("base", path)))) {
problems.push({ problem: "conflict", file: path, change: c.name, detail: "ours has conflict markers", refuse: true });
Expand All @@ -199,6 +215,23 @@ export function assemble(book: Book): Assembly {
// Content edits, keyed by upstream path. One owner per path.
const content = new Map(upstream);
const owner = new Map<string, string>();
const copiesOver = (c: Change, path: string, up: File, at?: InEdit): { base: File; ours: File } | null => {
const where: InChange | InEdit = at ?? { file: path, change: c.name };
const base = c.file("base", path);
const ours = c.file("ours", path);
if (base === null || ours === null) {
const lost = [base === null ? "base" : "", ours === null ? "ours" : ""].filter(Boolean);
const detail = !at ? "its base or ours copy is missing" : `its ${lost.join(" and ")} ${lost.length > 1 ? "copies are" : "copy is"} missing`;
problems.push({ problem: "invalid", ...where, detail });
return null;
}
if (!bytesEqual(base.data, up.data)) {
problems.push({ problem: "stale", ...where, detail: "its base differs from upstream at the pin; run sync" });
}
applied++;
return { base, ours };
};
const destinations: { c: Change; e: Edit }[] = [];
for (const c of changes) {
for (const e of c.edits) {
if (e.kind !== "file") continue;
Expand All @@ -211,20 +244,11 @@ export function assemble(book: Book): Assembly {
owner.set(e.path, `change ${c.name}`);
const up = upstream.get(e.path);
if (!up) {
problems.push({ problem: "gone", ...where, detail: "upstream no longer has it" });
destinations.push({ c, e });
continue;
}
const base = c.file("base", e.path);
const ours = c.file("ours", e.path);
if (base === null || ours === null) {
problems.push({ problem: "invalid", ...where, detail: "its base or ours copy is missing" });
continue;
}
if (!bytesEqual(base.data, up.data)) {
problems.push({ problem: "stale", ...where, detail: "its base differs from upstream at the pin; run sync" });
}
content.set(e.path, { data: ours.data, mode: outputMode(ours, base, up) });
applied++;
const copies = copiesOver(c, e.path, up);
if (copies) content.set(e.path, { data: copies.ours.data, mode: outputMode(copies.ours, copies.base, up) });
}
}
const blocks = new Map<string, Map<string, Entry>>();
Expand All @@ -241,9 +265,15 @@ export function assemble(book: Book): Assembly {
blocks.set(rel, entries);
}

const lost = new Set<string>();
const { tree: staged, applied: structural } = applyStructure(changes, upstream, problems, (p) => book.reservedHolding(p), lost);
problems.push(...structure);
applied += structural;
for (const { c, e } of destinations) {
const at: InEdit = { file: `changes/${c.name}`, edit: editText(e) };
const from = upstreamOf.get(e.path);
if (from === undefined) problems.push({ problem: "gone", ...at, detail: "no earlier copy or move of an upstream file reaches it" });
else if (unread.has(`${c.name}\0${e.path}`)) applied++;
else copiesOver(c, e.path, upstream.get(from)!, at);
}
const prefix = book.layout.kind === "folder" ? book.layout.from : "";
const atOutput = (p: string) => (!prefix ? [p] : p.startsWith(prefix + "/") ? [p.slice(prefix.length + 1)] : []);
const selected = new Map(Array.from(staged).flatMap(([p, s]) => atOutput(p).map((at) => [at, s] as const)));
Expand Down
Loading
Loading