Public: anyone can read this; the crew can change it.

A workspace's dev checkout is synced with the forge's own git, never the operator's environment #17318

opened this

Merged 31h 48m ago, live in production

fix/dev-launch-server-git → main 0 commits

  • Merged. fix/dev-launch-server-git was merged into main as 9a612d3.

Review

Sign in to review this change.
2 files changed +62 −4
src/apps/dev_launch.ts +4 −4
@@ -15,7 +15,7 @@ import type { App } from "../app.ts";
1515 import type { AppRecord } from "../core/apps.ts";
1616 import type { Repo } from "../core/repos.ts";
1717 import { repoDir } from "../git/repos.ts";
18 import { runLocal } from "../git/local.ts";
18 import { runGit } from "../git/env.ts";
1919 import { resolveOid } from "../git/merge.ts";
2020 import { isSafeRef } from "../git/guard.ts";
2121 import { err, ok, type Result } from "../kernel/result.ts";
@@ -51,12 +51,12 @@ export async function syncCheckout(bare: string, dir: string, branch: string): P
5151 const cloned = await Bun.file(join(dir, ".git", "HEAD")).exists();
5252 if (!cloned) {
5353 await mkdir(join(dir, ".."), { recursive: true });
54 const made = await runLocal(["clone", "--quiet", "--no-hardlinks", "--no-checkout", bare, dir], { cwd: join(dir, "..") });
54 const made = await runGit(["clone", "--quiet", "--no-hardlinks", "--no-checkout", bare, dir], { cwd: join(dir, "..") });
5555 if (!made.ok) return err("could not check the branch out");
5656 }
57 const got = await runLocal(["fetch", "--quiet", bare, `+refs/heads/${branch}:refs/remotes/dev/head`], { cwd: dir });
57 const got = await runGit(["fetch", "--quiet", bare, `+refs/heads/${branch}:refs/remotes/dev/head`], { cwd: dir });
5858 if (!got.ok) return err("could not read the branch");
59 const set = await runLocal(["reset", "--quiet", "--hard", tip], { cwd: dir });
59 const set = await runGit(["reset", "--quiet", "--hard", tip], { cwd: dir });
6060 return set.ok ? ok(tip) : err("could not check the branch out");
6161 }
6262
test/dev-launch-git-env.test.ts added +58
@@ -0,0 +1,58 @@
1 /**
2 * A workspace's dev checkout is synced with the forge's own git (`syncCheckout`, apps/dev_launch.ts):
3 * the scrubbed environment of git/env.ts, never the operator's (#14780). A forge process that read
4 * the operator's ~/.gitconfig would carry their settings, credential helpers and identity into a
5 * person's checkout.
6 */
7 import { afterAll, beforeAll, expect, test } from "bun:test";
8 import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
9 import { tmpdir } from "node:os";
10 import { join } from "node:path";
11 import { syncCheckout } from "../src/apps/dev_launch.ts";
12
13 let dir = "";
14 const priorHome = process.env["HOME"]; // env-restored: afterAll
15 const priorGlobal = process.env["GIT_CONFIG_GLOBAL"]; // env-restored: afterAll
16
17 const git = (cwd: string, ...args: string[]): void => {
18 const done = Bun.spawnSync(["git", "-c", "user.name=t", "-c", "user.email=t@t", "-c", "commit.gpgsign=false", ...args], {
19 cwd,
20 env: { PATH: process.env["PATH"] ?? "", HOME: "/dev/null", GIT_CONFIG_GLOBAL: "/dev/null" },
21 stdout: "ignore",
22 stderr: "pipe",
23 });
24 if (done.exitCode !== 0) throw new Error(`git ${args.join(" ")}: ${done.stderr.toString()}`);
25 };
26
27 beforeAll(() => {
28 dir = mkdtempSync(join(tmpdir(), "dev-launch-git-env-"));
29 const work = join(dir, "work");
30 mkdirSync(work);
31 git(work, "init", "-q", "-b", "main");
32 writeFileSync(join(work, "README.md"), "# app\n");
33 git(work, "add", "-A");
34 git(work, "commit", "-qm", "first");
35 git(dir, "clone", "-q", "--bare", work, join(dir, "app.git"));
36 // An operator's own settings, where an inheriting git reads them: a clone names its remote by them.
37 const home = join(dir, "home");
38 mkdirSync(home);
39 writeFileSync(join(home, ".gitconfig"), "[clone]\n\tdefaultRemoteName = operators-own\n");
40 process.env["HOME"] = home; // env-restored: afterAll
41 delete process.env["GIT_CONFIG_GLOBAL"]; // env-restored: afterAll
42 });
43 afterAll(() => {
44 if (priorHome === undefined) delete process.env["HOME"];
45 else process.env["HOME"] = priorHome;
46 if (priorGlobal === undefined) delete process.env["GIT_CONFIG_GLOBAL"];
47 else process.env["GIT_CONFIG_GLOBAL"] = priorGlobal;
48 rmSync(dir, { recursive: true, force: true });
49 });
50
51 test("a dev checkout is cloned and synced without the operator's git settings", async () => {
52 const checkout = join(dir, "cells", "main");
53 const synced = await syncCheckout(join(dir, "app.git"), checkout, "main");
54 expect(synced.ok).toBe(true);
55 const config = readFileSync(join(checkout, ".git", "config"), "utf8");
56 expect(config).not.toContain("operators-own");
57 expect(config).toContain('[remote "origin"]');
58 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs