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
38 changes: 27 additions & 11 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ README doesn't: conventions, environment and pitfalls.
with more of its pads and track pieces; even = stay), and the 3D view
glides to the Top or Bottom preset around it (`Board3D.focusOn(box, side)`,
`onView` presses the preset button). Clicks inside a view never move it.
- **Forges** (`src/forge/`): every GitHub request (REST, GraphQL, raw files,
search) and github.com link is in `src/forge/github.js`; the rest of the app
talks to a forge object only (`forge()` / `forgeInHash()` / `parseRepoSpec()`
from `src/forge/index.js`, whose JSDoc typedefs are the interface: `Forge`,
`Source`, `RepoInfo`, `PullRequest`, `ChangedFile`, `Comparison`,
`SearchItem`, `ReviewThreads`) and gets forge-neutral shapes back, never
GitHub JSON. Forge sources carry `source.forge` (app code tests that, not
`kind === "github"`); page hashes use `forge.hashKey` (`gh=`); review
stores and diff specs carry `forge` (an id, GitHub by default). A new forge
= a new adapter added to `FORGES` (`tests/review.js` checks each one has
every member). Not forge-neutral yet: tokens and sign-in (settings `token`,
the GitHub App device flow in tauri.js, which uses `github` directly), the
Rust git commands (github.com URLs in git.rs/git_http.rs, `git_link_github`)
and most UI wording ("GitHub"). No DOM or Tauri in `src/forge/`: it runs
under gjs.
- `src/desktop/` holds the Tauri integration; it's a no-op when
`window.__TAURI__` is missing (`isDesktop`), which is what lets the pages run
in a plain headless browser for tests.
Expand Down Expand Up @@ -174,10 +189,10 @@ README doesn't: conventions, environment and pitfalls.
netclasses: a missing field inherits from Default in KiCad 9+, netclass
patterns and assignments) and `.kicad_dru` (`#` comments, several top-level
rules). Everything else in a `.kicad_pro` is ignored. `RULE_FILES`
(sources.js) files are fetched by the diff page, shown as a table
(project.js) files are fetched by the diff page, shown as a table
(`#rules-view`, `.rules-mode`) and dropped when unchanged
(`dropUnchangedRules`). The inbox badge counts the same files
(`src/desktop/pr-files.js`): a modified `.kicad_pro` is read at the PR head
(`src/desktop/pr-files.js`, on `forge.pullFiles` ChangedFiles): a modified `.kicad_pro` is read at the PR head
and its base rebuilt from the PR's `patch` (`textBeforePatch`, sources.js),
so no extra API request for the base; unverifiable files count.
- Changes may carry `boxesBefore`/`boxesAfter` (per-side marks) and `rows`
Expand All @@ -197,8 +212,9 @@ README doesn't: conventions, environment and pitfalls.
- The user's design: issues pinned like review comments, outside PRs, listed
in a home view; only issues labelled `kivi` (`ISSUE_LABEL`, the user's
choice over listing every issue).
- `src/issues.js` `IssueComments` extends `ReviewComments` (same request and
error code) and turns issues into the same thread shape: `rootId` = issue
- `GitHubIssues` extends `GitHubReviews` (forge/github.js; same request and
error code) and turns issues into the same thread shape (`issueThread`,
review.js; `ISSUE_LABEL` is there too): `rootId` = issue
number (so `thread=12` is issue #12), resolved = closed, the body is the
first comment (never deletable). The marker carries `file` (repository path)
and `commit`. Signed in: one GraphQL query (issues + comments); signed out:
Expand All @@ -213,7 +229,7 @@ README doesn't: conventions, environment and pitfalls.
- In PR mode the store holds both (`api` = review comments, `issueApi` =
issues; `apiFor(t)`, `wordsFor(t)`), keys keep them apart (`t.key`: comment
id, or "i12"), only review threads are numbered 1…n, issue pins are green
(`.cm-pin.issue`), `openCounts` skips issues. `closingIssues()` (review.js:
(`.cm-pin.issue`), `openCounts` skips issues. `closingIssues()` (forge/github.js:
GraphQL `closingIssuesReferences` signed in, else `closingNumbers` of the
description) marks the ones the PR closes; **Close with this PR**
(`addClosingIssue`) appends "Fixes #N" to the description, re-read first. An
Expand Down Expand Up @@ -258,7 +274,7 @@ README doesn't: conventions, environment and pitfalls.
on a blocking thread.
- Branch menu `#branch-menu` (`src/app/branch-menu.js`, shared, the user's
design): the same button and branch list for GitHub projects (main.js
`mountGitHubBranches`: `/branches` API, switching rewrites the URL's `gh=`
`mountForgeBranches`: `forge.branches`, switching rewrites the URL's `gh=`
ref so `restoreFromHash` reopens the project, same tab) and local copies
(git.js `mountGitMenu`: `git_branches` after a best-effort fetch,
`git_switch_branch` = safe checkout like `git switch`, plus New branch…,
Expand All @@ -268,12 +284,12 @@ README doesn't: conventions, environment and pitfalls.
nothing to do); a folder without a GitHub remote gets no Push (no sign-in
needed; the user's request), and before its first
commit `git_info.branch` is the unborn branch and a new branch replaces it). `data-kind` tells which side owns it: main.js
removes only a "github" one, desktop.js mounts the "local" one after a folder
removes only a "forge" one, desktop.js mounts the "local" one after a folder
opens and on live reload/focus. The left of the bar shows the repository
(GitHub) or folder (local), no longer the ref.
- History menu `#history-menu` (`src/app/history-menu.js`, shared, next to
the branch menu; `data-kind` like it): the branch's commits, 50 per
**Show more**. GitHub (main.js `mountGitHubHistory`, hidden in PR mode):
**Show more**. GitHub (main.js `mountForgeHistory`, `forge.commits`, hidden in PR mode):
`/commits?sha=<ref>` (one API request per page per page load); picking one
sets `commit=<SHA>` in the URL, which `openGitHub` turns into the source's
`sha`/`commit` while `ref` (and the branch menu) keep the branch. Local
Expand All @@ -283,7 +299,7 @@ README doesn't: conventions, environment and pitfalls.
repository's root). While a `GitRevSource` is open, live reload is skipped
and Open in KiCad is greyed out; switching branch returns to the working files.
- Visual diff button `#visual-diff` (index.html, set by `setDiffButton` in
`src/app/diff-button.js` from main.js `setGitHubDiff` or git.js
`src/app/diff-button.js` from main.js `setForgeDiff` or git.js
`mountGitMenu`; it replaced the branch menu's "Visual diff of changes", the
user's request). The side pickers, in the Before/After pane labels and the
Overlay one (`.pick-slot`, the user's design; the bar shows only the
Expand All @@ -296,7 +312,7 @@ README doesn't: conventions, environment and pitfalls.
takes a base branch (from the merge base) or revision (that commit itself,
`base_tip`) and an optional head (`rev_tip`: local branch, GitHub's, then any
revision; none = working files); `git_log` takes `rev`. GitHub
(`resolveComparison`): no base = default branch, base = head → that commit
(`forge.compare`, `compare` in forge/github.js): no base = default branch, base = head → that commit
vs its parent, a full-SHA base that isn't the merge base → `treeDiff` (two
recursive trees; refuses truncated ones).
- `src/desktop/git.js`: card menu actions (projects.js), the local branch
Expand Down Expand Up @@ -458,7 +474,7 @@ python3 tools/fetch_demos.py # KiCad demo files → tests/kicad-
gjs -m tests/run.js # parse+render all KiCad demos
gjs -m tests/diff.js
gjs -m tests/nets.js
gjs -m tests/review.js # GitHub API layer, mocked
gjs -m tests/review.js # GitHub adapter and forge interface, mocked
gjs -m tests/models.js # 3D model path resolution order
cargo test --manifest-path src-tauri/Cargo.toml --lib # Rust: model folders, git (needs dist/web)
tests/smoke.sh # pages load cleanly, viewer opens pic_programmer (~15 s)
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,13 @@ src/geom.js matrices, arcs, bounding boxes
src/text.js text layout (KiCad metrics, markup, escapes)
src/drawing-sheet.js .kicad_wks parser/renderer, built-in default sheet
src/project.js project discovery, hierarchy walk, page numbering
src/sources.js GitHub (API, PR/commit comparison), 3D model path resolution
src/forge/ code hosts: the forge interface (index.js) and the GitHub
adapter (github.js: every API request, comparisons, review
comments, issues, search, github.com links)
src/sources.js text before a patch, 3D model path resolution
src/diff/ semantic schematic, net, board and design rule diffs, comment anchors
src/review.js review comments on GitHub (threads, replies, resolve)
src/issues.js change requests as GitHub issues (same threads, labelled kivi)
src/review.js comment markers and the thread shape (review comments and
change requests), the same on every forge
src/sch/ schematic model, renderer, themes
src/pcb/ board model, renderer, shapes, hit testing,
outline extraction, face textures, three.js scene,
Expand Down
2 changes: 1 addition & 1 deletion src/app/branch-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const BRANCH_ICON = `<svg viewBox="0 0 16 16" width="14" height="14" aria
/**
* Put the branch menu in the project bar (replacing any earlier one).
* @param {object} o
* @param {string} o.kind "github" | "local", so each side removes only its own menu
* @param {string} o.kind "forge" | "local", so each side removes only its own menu
* @param {string|null} o.branch the branch shown on the button
* @param {string} [o.title] the button's tooltip
* @param {number} [o.count] a badge after the name (local: changed files)
Expand Down
53 changes: 22 additions & 31 deletions src/app/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// (desktop app, diff.html#local=<folder>) a local git copy's working files with
// the point where its branch left main.

import { DIFFABLE, RULE_FILES, GitHubSource, resolveComparison } from "../sources.js";
import { DIFFABLE, RULE_FILES } from "../project.js";
import { forge, forgeInHash } from "../forge/index.js";
import { parseSchematic } from "../sch/model.js";
import { renderSchematic } from "../sch/render.js";
import { parsePcb } from "../pcb/model.js";
Expand Down Expand Up @@ -69,7 +70,7 @@ class DiffApp {
this.pendingLocal = null;
}

/** A local copy (its working files, or `spec.head`) against main or `spec.base`, in the shape resolveComparison returns. */
/** A local copy (its working files, or `spec.head`) against main or `spec.base`, in the shape a forge's compare() returns. */
async localComparison(spec) {
if (!this.localGit) throw new Error("Local changes can only be shown in the KiVi desktop app");
const lc = await this.localGit.changes(spec.local, spec.base, spec.head);
Expand Down Expand Up @@ -119,13 +120,14 @@ class DiffApp {

specFromHash(p) {
if (p.get("local")) return { local: p.get("local"), base: p.get("base"), head: p.get("head") };
const gh = p.get("gh");
if (!gh) return null;
const [owner, repo] = gh.split("/");
if (p.get("pr")) return { owner, repo, pr: Number(p.get("pr")) };
if (p.get("commit")) return { owner, repo, commit: p.get("commit") };
// Without a base: the default branch (resolveComparison).
if (p.get("head")) return { owner, repo, base: p.get("base"), head: p.get("head") };
const at = forgeInHash(p);
if (!at) return null;
const [owner, repo] = at.value.split("/");
const f = at.forge.id;
if (p.get("pr")) return { forge: f, owner, repo, pr: Number(p.get("pr")) };
if (p.get("commit")) return { forge: f, owner, repo, commit: p.get("commit") };
// Without a base: the default branch (the forge's compare).
if (p.get("head")) return { forge: f, owner, repo, base: p.get("base"), head: p.get("head") };
return null;
}

Expand All @@ -137,7 +139,7 @@ class DiffApp {
if (s.base) p.set("base", s.base);
if (s.head) p.set("head", s.head);
} else {
p.set("gh", `${s.owner}/${s.repo}`);
p.set(forge(s.forge).hashKey, `${s.owner}/${s.repo}`);
if (s.pr) p.set("pr", String(s.pr));
else if (s.commit) p.set("commit", s.commit);
else {
Expand All @@ -160,8 +162,8 @@ class DiffApp {
this.cache.clear();
this.proCache = new Map(); // the sides may be other commits now
try {
this.busy(spec.local ? "Reading local changes…" : "Reading changes from GitHub…");
const cmp = spec.local ? await this.localComparison(spec) : await resolveComparison(spec, settings().token);
this.busy(spec.local ? "Reading local changes…" : `Reading changes from ${forge(spec.forge).name}…`);
const cmp = spec.local ? await this.localComparison(spec) : await forge(spec.forge).compare(spec, settings().token);
this.cmp = cmp;
$("#diff-bar").hidden = false;
$("#diff-main").hidden = false;
Expand Down Expand Up @@ -193,7 +195,7 @@ class DiffApp {
cmp.files.sort((a, b) => order(a.path) - order(b.path) || a.path.localeCompare(b.path));
this.renderFileList();
if (spec.pr) {
this.store.open({ owner: spec.owner, repo: spec.repo, number: spec.pr, headSha: cmp.head.sha });
this.store.open({ forge: spec.forge, owner: spec.owner, repo: spec.repo, number: spec.pr, headSha: cmp.head.sha });
if (focus) this.review.setFocus(focus);
} else this.store.close();
const first = cmp.files.find((f) => f.path === initialFile) ?? cmp.files[0];
Expand Down Expand Up @@ -235,35 +237,24 @@ class DiffApp {
for (const slot of document.querySelectorAll(".pick-slot")) slot.replaceChildren();
if (spec.pr) return;
const local = !!spec.local;
const gh = local ? null : spec;
const f = local ? null : forge(spec.forge);
// Both pickers list the same branches, read once per comparison.
let branchList = null;
const branches = () =>
(branchList ??= local
? this.localGit.branches(cmp.root).then((bs) => bs.map((b) => ({ name: b.name, note: b.current ? "checked out" : b.remote ? "on GitHub" : "" })))
: (async () => {
const all = [];
for (let page = 1; page <= 5; page++) {
const batch = await GitHubSource.api(`/repos/${gh.owner}/${gh.repo}/branches?per_page=100&page=${page}`, settings().token);
all.push(...batch);
if (batch.length < 100) break;
}
return all.map((b) => ({ name: b.name }));
})());
: f.branches(spec.owner, spec.repo, settings().token));
const commits = local
? (branch, offset, count) => this.localGit.commits(cmp.root, branch, offset, count)
: async (branch, offset, count) => {
const batch = await GitHubSource.api(`/repos/${gh.owner}/${gh.repo}/commits?sha=${encodeURIComponent(branch)}&per_page=${count}&page=${Math.floor(offset / count) + 1}`, settings().token);
return batch.map((c) => ({ sha: c.sha, summary: c.commit.message.split("\n")[0], author: c.commit.author?.name ?? c.author?.login ?? "", time: Date.parse(c.commit.author?.date) / 1000 }));
};
: (branch, offset, count) => f.commits(spec.owner, spec.repo, branch, offset, count, settings().token);
const sha = /^[0-9a-f]{7,40}\^?$/i;
// What each side is now: the URL's choice, else what it was resolved to
// (a local copy's default base is e.g. origin/main, listed as main).
const baseValue = spec.base ?? (local ? cmp.base.ref.split("/").pop() : !cmp.base.sha ? null : cmp.base.sha.startsWith(cmp.base.ref) ? cmp.base.sha : cmp.base.ref); // null: a first commit has no parent
const headValue = local ? (spec.head ?? "") : (spec.head ?? cmp.head.sha);
const headBranch = local ? (spec.head && !sha.test(spec.head) ? spec.head : cmp.branch) : !sha.test(headValue) ? headValue : null;
const pick = (side) => async (value) => {
const next = local ? { local: spec.local, base: spec.base, head: spec.head } : { owner: gh.owner, repo: gh.repo, base: baseValue, head: headValue };
const next = local ? { local: spec.local, base: spec.base, head: spec.head } : { forge: spec.forge, owner: spec.owner, repo: spec.repo, base: baseValue, head: headValue };
next[side] = value || null;
await this.open(next, this.file?.path);
this.writeHash();
Expand Down Expand Up @@ -323,7 +314,7 @@ class DiffApp {
const ref = this.cmp[side];
if (ref.src) return ref.src.readText(path); // a local copy
if (!ref.sha) return null;
const src = new GitHubSource({ owner: ref.owner, repo: ref.repo, ref: ref.sha, sha: ref.sha, token: settings().token });
const src = forge(this.spec.forge).source({ owner: ref.owner, repo: ref.repo, ref: ref.sha, sha: ref.sha, token: settings().token });
return src.readText(path);
}

Expand All @@ -342,7 +333,7 @@ class DiffApp {
this.proCache.set(
key,
(async () => {
const src = ref.src ?? new GitHubSource({ owner: ref.owner, repo: ref.repo, ref: ref.sha, sha: ref.sha, path: dir, token: settings().token });
const src = ref.src ?? forge(this.spec.forge).source({ owner: ref.owner, repo: ref.repo, ref: ref.sha, sha: ref.sha, path: dir, token: settings().token });
const pro = (await src.list()).find((p) => p.endsWith(".kicad_pro") && dirname(p) === dir) ?? null;
if (!pro) return { pro, vars: {} };
return { pro, vars: JSON.parse(await src.readText(pro)).text_variables ?? {} };
Expand Down Expand Up @@ -420,7 +411,7 @@ class DiffApp {
// A local copy opens its working files (the folder, with the project of this file), or the compared commit.
const viewerHash = this.cmp.root
? new URLSearchParams({ folder: this.cmp.root, project: fl.path.replace(/\.kicad_(sch|pcb|dru)$/i, ".kicad_pro"), ...(this.cmp.head.sha && { commit: this.cmp.head.sha }) })
: new URLSearchParams({ gh: `${this.cmp.head.owner}/${this.cmp.head.repo}@${this.cmp.head.sha}${dirname(fl.path) ? `:${dirname(fl.path)}` : ""}`, tab: res.kind === "sch" ? "sch" : "pcb", ...(res.kind !== "rules" && { file: fl.path }) });
: new URLSearchParams({ [forge(this.spec.forge).hashKey]: `${this.cmp.head.owner}/${this.cmp.head.repo}@${this.cmp.head.sha}${dirname(fl.path) ? `:${dirname(fl.path)}` : ""}`, tab: res.kind === "sch" ? "sch" : "pcb", ...(res.kind !== "rules" && { file: fl.path }) });
if (this.spec.pr) viewerHash.set("pr", `${this.spec.owner}/${this.spec.repo}#${this.spec.pr}`);
$("#open-viewer").href = `index.html#${viewerHash}`;
this.writeHash();
Expand Down
2 changes: 1 addition & 1 deletion src/app/history-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const PAGE = 50; // commits per "Show more"
/**
* Put the history menu in the project bar (replacing any earlier one), after the branch menu.
* @param {object} o
* @param {string} o.kind "github" | "local", so each side removes only its own menu
* @param {string} o.kind "forge" | "local", so each side removes only its own menu
* @param {string|null} o.viewing the commit shown (full SHA), or null for the latest
* @param {string} o.latest the first item, e.g. "Latest on main" or "Working files"
* @param {string} [o.latestNote] a note after it (local: the changed files)
Expand Down
Loading
Loading