GeezerVison is showing github.com — Make bb's git reads ignore user git display settings (#7785) · get-bb/bb@b9cc4c6
File treeExpand file treeCollapse file tree Open diff view settings packages/environment-provider-host plugins/environment-git-worktree/host
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/environment-provider-host/src/workspace-git.ts +57-6Lines changed: 57 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -120,16 +120,67 @@ export function trimOutput(value: string): string { | 120 | 120 | return value.trim().replace(/\n+$/u, ""); | 121 | 121 | } | 122 | 122 |
| | 123 | +type GitConfigEntry = readonly [key: string, value: string]; | | 124 | + | | 125 | +const MACHINE_READ_GIT_CONFIG: readonly GitConfigEntry[] = [ | | 126 | + ["color.ui", "never"], | | 127 | + ["color.diff", "never"], | | 128 | + ["color.branch", "never"], | | 129 | + ["color.status", "never"], | | 130 | + ["diff.noprefix", "false"], | | 131 | + ["diff.mnemonicPrefix", "false"], | | 132 | + ["diff.srcPrefix", "a/"], | | 133 | + ["diff.dstPrefix", "b/"], | | 134 | + ["diff.suppressBlankEmpty", "false"], | | 135 | + ["diff.relative", "false"], | | 136 | + ["diff.submodule", "short"], | | 137 | + ["log.showSignature", "false"], | | 138 | +]; | | 139 | + | | 140 | +const GIT_CONFIG_ENV_KEY = /^GIT_CONFIG_(?:COUNT|KEY_\d+|VALUE_\d+)$/u; | | 141 | + | | 142 | +function readGitConfigEnv(env: NodeJS.ProcessEnv): GitConfigEntry[] { | | 143 | + const count = Number(env.GIT_CONFIG_COUNT ?? "0"); | | 144 | + if (!Number.isSafeInteger(count) || count < 0) return []; | | 145 | + const entries: GitConfigEntry[] = []; | | 146 | + for (let index = 0; index < count; index += 1) { | | 147 | + const key = env[`GIT_CONFIG_KEY_${index}`]; | | 148 | + if (key === undefined) continue; | | 149 | + entries.push([key, env[`GIT_CONFIG_VALUE_${index}`] ?? ""]); | | 150 | + } | | 151 | + return entries; | | 152 | +} | | 153 | + | | 154 | +function withoutGitConfigEnv(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv { | | 155 | + return Object.fromEntries( | | 156 | + Object.entries(env).filter(([key]) => !GIT_CONFIG_ENV_KEY.test(key)), | | 157 | + ); | | 158 | +} | | 159 | + | 123 | 160 | export function resolveGitProcessEnv( | 124 | 161 | args: ResolveGitProcessEnvArgs, | 125 | 162 | ): NodeJS.ProcessEnv { | 126 | | - return { | 127 | | - ...sanitizeInheritedChildProcessEnv({ | 128 | | - env: process.env, | 129 | | - ...(args.shellPath !== undefined ? { shellPath: args.shellPath } : {}), | 130 | | - }), | 131 | | - ...args.env, | | 163 | + const inherited = sanitizeInheritedChildProcessEnv({ | | 164 | + env: process.env, | | 165 | + ...(args.shellPath !== undefined ? { shellPath: args.shellPath } : {}), | | 166 | + }); | | 167 | + const requested = args.env ?? {}; | | 168 | + const config = [ | | 169 | + ...readGitConfigEnv(inherited), | | 170 | + ...MACHINE_READ_GIT_CONFIG, | | 171 | + ...readGitConfigEnv(requested), | | 172 | + ]; | | 173 | + const env: NodeJS.ProcessEnv = { | | 174 | + ...withoutGitConfigEnv(inherited), | | 175 | + LANGUAGE: "C", | | 176 | + ...withoutGitConfigEnv(requested), | | 177 | + GIT_CONFIG_COUNT: String(config.length), | 132 | 178 | }; | | 179 | + config.forEach(([key, value], index) => { | | 180 | + env[`GIT_CONFIG_KEY_${index}`] = key; | | 181 | + env[`GIT_CONFIG_VALUE_${index}`] = value; | | 182 | + }); | | 183 | + return env; | 133 | 184 | } | 134 | 185 |
| 135 | 186 | function createGitCommandTimedOutError( | |
Collapse file Copy file name to clipboardExpand all lines: packages/environment-provider-host/test/workspace-git.test.ts +190Lines changed: 190 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { execFile } from "node:child_process"; | 1 | 2 | import fs from "node:fs/promises"; | 2 | 3 | import os from "node:os"; | 3 | 4 | import path from "node:path"; | | 5 | +import { promisify } from "node:util"; | 4 | 6 | import { | 5 | 7 | afterAll, | 6 | 8 | afterEach, | @@ -15,12 +17,14 @@ import { | 15 | 17 | detectGitRepoKind, | 16 | 18 | ensureGitRepo, | 17 | 19 | hasUncommittedChanges, | | 20 | + isMissingHeadRevisionError, | 18 | 21 | parsePorcelainV2StatusZ, | 19 | 22 | readDefaultBranchRefs, | 20 | 23 | runGit, | 21 | 24 | } from "../src/workspace-git.js"; | 22 | 25 | import { fetchRemoteBranches } from "../src/branch-options.js"; | 23 | 26 |
| | 27 | +const execFileAsync = promisify(execFile); | 24 | 28 | const tempDirs: string[] = []; | 25 | 29 |
| 26 | 30 | async function initReadGitBlobRepo() { | @@ -157,6 +161,192 @@ describe("runGit", () => { | 157 | 161 | }); | 158 | 162 | }); | 159 | 163 |
| | 164 | +describe("user Git settings on machine reads", () => { | | 165 | + let templatePath: string; | | 166 | + | | 167 | + beforeAll(async () => { | | 168 | + templatePath = await fs.mkdtemp(path.join(os.tmpdir(), "bb-git-display-")); | | 169 | + await runGit(["init", "-b", "main"], { cwd: templatePath }); | | 170 | + await writeTestIdentity(templatePath); | | 171 | + await fs.mkdir(path.join(templatePath, "app")); | | 172 | + await fs.mkdir(path.join(templatePath, "sub")); | | 173 | + await fs.writeFile(path.join(templatePath, "blank.txt"), "a\n\nb\n"); | | 174 | + await fs.writeFile(path.join(templatePath, "app", "inner.txt"), "one\n"); | | 175 | + await runGit( | | 176 | + ["update-index", "--add", "--cacheinfo", `160000,${"1".repeat(40)},sub`], | | 177 | + { cwd: templatePath }, | | 178 | + ); | | 179 | + await runGit(["add", "."], { cwd: templatePath }); | | 180 | + await runGit(["commit", "-m", "base"], { cwd: templatePath }); | | 181 | + await fs.writeFile(path.join(templatePath, "blank.txt"), "a\n\nB\n"); | | 182 | + await fs.writeFile(path.join(templatePath, "app", "inner.txt"), "two\n"); | | 183 | + await runGit( | | 184 | + ["update-index", "--cacheinfo", `160000,${"2".repeat(40)},sub`], | | 185 | + { cwd: templatePath }, | | 186 | + ); | | 187 | + await runGit(["commit", "-am", "change"], { cwd: templatePath }); | | 188 | + await fs.writeFile(path.join(templatePath, "blank.txt"), "a\n\nC\n"); | | 189 | + }); | | 190 | + | | 191 | + afterAll(() => fs.rm(templatePath, { recursive: true, force: true })); | | 192 | + | | 193 | + async function repoWithConfig(config: Record<string, string>) { | | 194 | + const parent = await fs.mkdtemp(path.join(os.tmpdir(), "bb-git-display-")); | | 195 | + tempDirs.push(parent); | | 196 | + const repoPath = path.join(parent, "repo"); | | 197 | + await fs.cp(templatePath, repoPath, { recursive: true }); | | 198 | + for (const [key, value] of Object.entries(config)) { | | 199 | + await runGit(["config", key, value], { cwd: repoPath }); | | 200 | + } | | 201 | + return repoPath; | | 202 | + } | | 203 | + | | 204 | + async function readPatches(repoPath: string) { | | 205 | + const cwd = path.join(repoPath, "app"); | | 206 | + const diff = await runGit(["diff", "HEAD~1", "HEAD"], { cwd }); | | 207 | + const log = await runGit(["log", "-p", "-1", "--format=%s"], { cwd }); | | 208 | + const show = await runGit(["show", "--format=%s", "HEAD"], { cwd }); | | 209 | + const worktree = await runGit(["diff", "HEAD", "--", ":/"], { cwd }); | | 210 | + return { | | 211 | + diff: diff.stdout, | | 212 | + log: log.stdout, | | 213 | + show: show.stdout, | | 214 | + worktree: worktree.stdout, | | 215 | + }; | | 216 | + } | | 217 | + | | 218 | + it.for<[string, Record<string, string>]>([ | | 219 | + ["color.ui=always", { "color.ui": "always" }], | | 220 | + ["color.diff=always", { "color.ui": "never", "color.diff": "always" }], | | 221 | + ["diff.noprefix", { "diff.noprefix": "true" }], | | 222 | + ["diff.mnemonicPrefix", { "diff.mnemonicPrefix": "true" }], | | 223 | + [ | | 224 | + "diff.srcPrefix/dstPrefix", | | 225 | + { "diff.srcPrefix": "x/", "diff.dstPrefix": "y/" }, | | 226 | + ], | | 227 | + ["diff.suppressBlankEmpty", { "diff.suppressBlankEmpty": "true" }], | | 228 | + ["diff.relative", { "diff.relative": "true" }], | | 229 | + ["diff.submodule=log", { "diff.submodule": "log" }], | | 230 | + ["diff.submodule=diff", { "diff.submodule": "diff" }], | | 231 | + ])("keeps the standard patch format under %s", async ([, config]) => { | | 232 | + const expected = await readPatches(await repoWithConfig({})); | | 233 | + expect(expected.diff).toContain("diff --git a/blank.txt b/blank.txt"); | | 234 | + expect(expected.diff).toContain(" a\n \n-b\n+B\n"); | | 235 | + expect(expected.diff).toContain("+Subproject commit 2222"); | | 236 | + expect(expected.diff).toContain("diff --git a/app/inner.txt"); | | 237 | + expect(expected.worktree).toContain("--- a/blank.txt\n+++ b/blank.txt"); | | 238 | + | | 239 | + await expect(readPatches(await repoWithConfig(config))).resolves.toEqual( | | 240 | + expected, | | 241 | + ); | | 242 | + }); | | 243 | + | | 244 | + it.skipIf(process.platform === "win32")( | | 245 | + "reads Git messages in English when the user's language is translated", | | 246 | + async () => { | | 247 | + const binPath = await fs.mkdtemp(path.join(os.tmpdir(), "bb-git-lang-")); | | 248 | + tempDirs.push(binPath); | | 249 | + await fs.writeFile( | | 250 | + path.join(binPath, "git"), | | 251 | + [ | | 252 | + "#!/bin/sh", | | 253 | + 'if [ "$LANGUAGE" = C ]; then', | | 254 | + " echo \"fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.\" >&2", | | 255 | + "else", | | 256 | + " echo \"Schwerwiegend: mehrdeutiges Argument 'HEAD': unbekannter Commit\" >&2", | | 257 | + "fi", | | 258 | + 'printf "%s|%s" "$LANG" "$LC_ALL"', | | 259 | + "exit 128", | | 260 | + "", | | 261 | + ].join("\n"), | | 262 | + { mode: 0o755 }, | | 263 | + ); | | 264 | + vi.stubEnv("LANG", "de_DE.UTF-8"); | | 265 | + vi.stubEnv("LANGUAGE", "de"); | | 266 | + vi.stubEnv("LC_ALL", "de_DE.UTF-8"); | | 267 | + | | 268 | + const result = await runGit(["rev-parse", "HEAD"], { | | 269 | + cwd: binPath, | | 270 | + shellPath: binPath, | | 271 | + allowFailure: true, | | 272 | + }); | | 273 | + | | 274 | + expect(isMissingHeadRevisionError(result.stderr)).toBe(true); | | 275 | + expect(result.stdout).toBe("de_DE.UTF-8|de_DE.UTF-8"); | | 276 | + }, | | 277 | + ); | | 278 | + | | 279 | + it("keeps inherited and caller Git config alongside the overrides", async () => { | | 280 | + const repoPath = await repoWithConfig({}); | | 281 | + vi.stubEnv("GIT_CONFIG_COUNT", "1"); | | 282 | + vi.stubEnv("GIT_CONFIG_KEY_0", "bb.inherited"); | | 283 | + vi.stubEnv("GIT_CONFIG_VALUE_0", "machine"); | | 284 | + | | 285 | + const readConfig = async (key: string) => | | 286 | + ( | | 287 | + await runGit(["config", "--get", key], { | | 288 | + cwd: repoPath, | | 289 | + env: { | | 290 | + GIT_CONFIG_COUNT: "1", | | 291 | + GIT_CONFIG_KEY_0: "core.splitIndex", | | 292 | + GIT_CONFIG_VALUE_0: "false", | | 293 | + }, | | 294 | + }) | | 295 | + ).stdout.trim(); | | 296 | + | | 297 | + await expect( | | 298 | + Promise.all( | | 299 | + ["bb.inherited", "color.ui", "core.splitIndex"].map(readConfig), | | 300 | + ), | | 301 | + ).resolves.toEqual(["machine", "never", "false"]); | | 302 | + }); | | 303 | + | | 304 | + it.skipIf(process.platform === "win32")( | | 305 | + "leaves signature checks out of log and show output under log.showSignature", | | 306 | + async () => { | | 307 | + const repoPath = await repoWithConfig({}); | | 308 | + const keyPath = path.join(path.dirname(repoPath), "signing-key"); | | 309 | + await execFileAsync("ssh-keygen", [ | | 310 | + "-q", | | 311 | + "-t", | | 312 | + "ed25519", | | 313 | + "-N", | | 314 | + "", | | 315 | + "-f", | | 316 | + keyPath, | | 317 | + ]); | | 318 | + const allowedSigners = path.join(path.dirname(repoPath), "signers"); | | 319 | + await fs.writeFile( | | 320 | + allowedSigners, | | 321 | + | | 322 | + ); | | 323 | + for (const [key, value] of [ | | 324 | + ["gpg.format", "ssh"], | | 325 | + ["user.signingKey", `${keyPath}.pub`], | | 326 | + ["gpg.ssh.allowedSignersFile", allowedSigners], | | 327 | + ["log.showSignature", "true"], | | 328 | + ]) { | | 329 | + await runGit(["config", key, value], { cwd: repoPath }); | | 330 | + } | | 331 | + await runGit(["commit", "-S", "--allow-empty", "-m", "signed"], { | | 332 | + cwd: repoPath, | | 333 | + }); | | 334 | + const sha = ( | | 335 | + await runGit(["rev-parse", "HEAD"], { cwd: repoPath }) | | 336 | + ).stdout.trim(); | | 337 | + | | 338 | + const log = await runGit(["log", "-1", "--format=%H"], { | | 339 | + cwd: repoPath, | | 340 | + }); | | 341 | + const show = await runGit(["show", "--format=%H", "--no-patch", "HEAD"], { | | 342 | + cwd: repoPath, | | 343 | + }); | | 344 | + | | 345 | + expect([log.stdout, show.stdout]).toEqual([`${sha}\n`, `${sha}\n`]); | | 346 | + }, | | 347 | + ); | | 348 | +}); | | 349 | + | 160 | 350 | describe("detectGitRepoKind", () => { | 161 | 351 | it("tells a bare repository root apart from its worktrees and plain directories", async () => { | 162 | 352 | const { root, worktreePath } = await initBareWorktreeLayout(); | |
Collapse file Copy file name to clipboardExpand all lines: plugins/environment-git-worktree/host/worktree.ts +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -501,7 +501,23 @@ async function readCompletedBranch( | 501 | 501 | } | 502 | 502 | } | 503 | 503 |
| | 504 | +async function refreshWorktreeIndex( | | 505 | + worktreePath: string, | | 506 | + signal: AbortSignal | undefined, | | 507 | +): Promise<void> { | | 508 | + try { | | 509 | + await runGit(["update-index", "-q", "--refresh", "--force-write-index"], { | | 510 | + cwd: worktreePath, | | 511 | + allowFailure: true, | | 512 | + ...signalOptions(signal), | | 513 | + }); | | 514 | + } catch (error) { | | 515 | + if (signal?.aborted) throw createProvisionCancelledError(error); | | 516 | + } | | 517 | +} | | 518 | + | 504 | 519 | async function finishWorktreeSetup(args: CreateWorktreeArgs): Promise<void> { | | 520 | + await refreshWorktreeIndex(args.targetPath, args.signal); | 505 | 521 | emitCwd({ | 506 | 522 | onProgress: args.onProgress, | 507 | 523 | keySuffix: "target", | |
|