Skip to content

Commit b069e64

Browse files
donjor-agent[bot]donjor
authored andcommitted
fix(fork-meta): install pins gh's default repository to the fork (#1862)
## Problem The fork keeps an `upstream` remote, so a fresh clone's `gh` refuses to pick a repository or targets `pingdotgg/t3code`. Unqualified `gh pr create` is one App installation away from posting upstream. ## Change - **Hook:** root `postinstall` runs `scripts/fork-gh-default.ts` - **Mark:** writes `remote.<fork>.gh-resolved base`, as `gh repo set-default` does - **No `gh` needed:** plain `git config`; any failure is silent - **Stale marks:** cleared on other remotes, since `gh` prefers `upstream` - **Scope:** only a remote pointing at `RSI-Software/t3code-hyprws`; worktrees share it `postinstall` is a new line, so `prepare` (upstream-owned) stays untouched. `vp i` delegates to `pnpm install`, so both install paths run it. ## Scope and approval Closes #96 ## Close conditions - [ ] After a fresh clone and `pnpm install`, `gh repo view --json nameWithOwner -q .nameWithOwner` prints `RSI-Software/t3code-hyprws`. - [ ] `pnpm install` still succeeds on a machine without `gh`. ## Verification Fresh clone of this branch, `upstream` added, then `vp i`: https://github.com/user-attachments/assets/8cf285db-60f3-4c95-b87c-55a3609d6f6d Fresh clone, `gh` hidden from `PATH`, then `pnpm install`: ```text $ command -v gh # no gh on PATH exit 1 $ pnpm install exit 0 . postinstall$ node scripts/fork-gh-default.ts . postinstall: [fork-gh-default] gh now targets remote 'origin' $ git config --get remote.origin.gh-resolved base $ gh repo view --json nameWithOwner -q .nameWithOwner # gh restored RSI-Software/t3code-hyprws ``` - [x] `scripts/fork-gh-default.test.ts`: moves a stale `upstream` mark; leaves non-fork clones alone - [x] `vp run fork:ci` → exit 0 - [ ] `vp run check:changed` → exit 1: 3 tests unrelated to this diff - 2 web browser tests pass alone; `hyprws` CI is red on one at `5fafe8e` - `GitManager` CLAUDE.md test fails locally only; `hyprws` CI passes it Claude Opus 5.5 in Claude Code, hosted by T3 Code. ## Fork trailers Squashes: - f2dbf34 fix(fork-meta): install pins gh's default repository to the fork (#1862) (#1862) - 482de01 fixup! fix(fork-meta): install pins gh's default repository to the fork (#1862) (#1862) Fork-Domain: fork-meta Fork-Tier: bugfix Fork-Upstreamable: no Co-authored-by: donjor <[email protected]>
1 parent dbbd60d commit b069e64

4 files changed

Lines changed: 117 additions & 0 deletions

File tree

‎docs/fork/internals/fork-development.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Nothing opens a browser; agents place windows through `donjor hypr`.
6060
| `origin` | `RSI-Software/t3code-hyprws` | Fork branches, published |
6161

6262
Local `main` mirrors `upstream/main`, pushed fast-forward only.
63+
With `upstream` present, `gh` refuses or targets `pingdotgg/t3code`; install pins its default to the fork (`scripts/fork-gh-default.ts`).
6364
`hyprws` is the single trunk, every domain in one rebased stack.
6465
Domains are not branches: a commit declares one with `Fork-Domain`.
6566

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"fork:sync": "node --import ./scripts/lib/await-setup.ts scripts/fork-sync.ts",
6262
"fork:regenerate-route-tree": "node scripts/lib/regenerate-route-tree.ts",
6363
"fork:upstream-refs": "node scripts/fork-upstream-refs.ts",
64+
"postinstall": "node scripts/fork-gh-default.ts",
6465
"clean": "rm -rf node_modules apps/*/node_modules packages/*/node_modules apps/*/dist apps/*/dist-electron packages/*/dist .vite-plus apps/*/.vite-plus packages/*/.vite-plus",
6566
"sync:repos": "node scripts/sync-reference-repos.ts"
6667
},

‎scripts/fork-gh-default.test.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// @effect-diagnostics nodeBuiltinImport:off - These fixtures drive the install script against real Git config.
2+
3+
import * as NodeChildProcess from "node:child_process";
4+
import * as NodeFS from "node:fs";
5+
import * as NodeOS from "node:os";
6+
import * as NodePath from "node:path";
7+
8+
import { afterEach, assert, describe, it } from "@effect/vitest";
9+
10+
import { pinGhDefault } from "./fork-gh-default.ts";
11+
12+
const containers: string[] = [];
13+
14+
afterEach(() => {
15+
for (const container of containers.splice(0)) {
16+
NodeFS.rmSync(container, { recursive: true, force: true });
17+
}
18+
});
19+
20+
function repository(remotes: Readonly<Record<string, string>>): {
21+
readonly cwd: string;
22+
readonly config: (key: string) => string | undefined;
23+
} {
24+
const cwd = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-gh-default-"));
25+
containers.push(cwd);
26+
const git = (args: readonly string[]) =>
27+
NodeChildProcess.spawnSync("git", [...args], { cwd, encoding: "utf8" });
28+
git(["init", "-q"]);
29+
for (const [name, url] of Object.entries(remotes)) git(["remote", "add", name, url]);
30+
return {
31+
cwd,
32+
config: (key) => {
33+
const result = git(["config", "--get", key]);
34+
return result.status === 0 ? result.stdout.trim() : undefined;
35+
},
36+
};
37+
}
38+
39+
describe("pinGhDefault", () => {
40+
it("moves gh's default from upstream to the fork remote, once", () => {
41+
const repo = repository({
42+
origin: "[email protected]:RSI-Software/t3code-hyprws.git",
43+
upstream: "[email protected]:pingdotgg/t3code.git",
44+
});
45+
NodeChildProcess.execFileSync("git", ["config", "remote.upstream.gh-resolved", "base"], {
46+
cwd: repo.cwd,
47+
});
48+
49+
assert.strictEqual(pinGhDefault(repo.cwd), "origin");
50+
assert.strictEqual(repo.config("remote.origin.gh-resolved"), "base");
51+
assert.strictEqual(repo.config("remote.upstream.gh-resolved"), undefined);
52+
assert.strictEqual(pinGhDefault(repo.cwd), undefined);
53+
});
54+
55+
it("leaves a clone without a fork remote, or no repository, untouched", () => {
56+
const repo = repository({ origin: "https://github.com/pingdotgg/t3code.git" });
57+
assert.strictEqual(pinGhDefault(repo.cwd), undefined);
58+
assert.strictEqual(repo.config("remote.origin.gh-resolved"), undefined);
59+
60+
const plain = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-gh-default-"));
61+
containers.push(plain);
62+
assert.strictEqual(pinGhDefault(plain), undefined);
63+
});
64+
});

‎scripts/fork-gh-default.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// @effect-diagnostics nodeBuiltinImport:off - an install lifecycle script; Node built-ins only, so it never fails an install.
2+
/**
3+
* Pins `gh` in this clone to the fork on every install: with an `upstream` remote too, `gh` refuses
4+
* or picks `pingdotgg/t3code` until `gh repo set-default` runs. Writing the same
5+
* `remote.<name>.gh-resolved` mark directly leaves machines without `gh` unchanged. Linked
6+
* worktrees share the clone's config.
7+
*/
8+
import * as NodeChildProcess from "node:child_process";
9+
10+
const FORK_REPOSITORY = /[:/]RSI-Software\/t3code-hyprws(?:\.git)?\/?$/iu;
11+
12+
function git(cwd: string, args: readonly string[]): string | undefined {
13+
const result = NodeChildProcess.spawnSync("git", [...args], {
14+
cwd,
15+
encoding: "utf8",
16+
stdio: ["ignore", "pipe", "ignore"],
17+
});
18+
return result.status === 0 ? result.stdout : undefined;
19+
}
20+
21+
function remoteValues(cwd: string, key: string): ReadonlyArray<readonly [string, string]> {
22+
const pattern = new RegExp(`^remote\\.(.+)\\.${key}\\s+(\\S+)$`, "u");
23+
return (git(cwd, ["config", "--get-regexp", `^remote\\..*\\.${key}$`]) ?? "")
24+
.split(/\r?\n/u)
25+
.flatMap((line) => {
26+
const match = pattern.exec(line.trim());
27+
return match ? [[match[1]!, match[2]!] as const] : [];
28+
});
29+
}
30+
31+
/** Marks the fork remote as `gh`'s default, clearing marks on other remotes; returns it when changed. */
32+
export function pinGhDefault(cwd: string): string | undefined {
33+
const fork = remoteValues(cwd, "url").find(([, url]) => FORK_REPOSITORY.test(url))?.[0];
34+
if (fork === undefined) return undefined;
35+
const marks = remoteValues(cwd, "gh-resolved");
36+
if (marks.length === 1 && marks[0]![0] === fork && marks[0]![1] === "base") return undefined;
37+
// gh reads the first marked remote in upstream, github, origin order, so a stale mark elsewhere wins.
38+
for (const [remote] of marks) {
39+
if (remote !== fork) git(cwd, ["config", "--unset-all", `remote.${remote}.gh-resolved`]);
40+
}
41+
return git(cwd, ["config", `remote.${fork}.gh-resolved`, "base"]) === undefined
42+
? undefined
43+
: fork;
44+
}
45+
46+
if (import.meta.main) {
47+
const remote = pinGhDefault(process.cwd());
48+
if (remote !== undefined) {
49+
process.stdout.write(`[fork-gh-default] gh now targets remote '${remote}'\n`);
50+
}
51+
}

0 commit comments

Comments
 (0)