@bakers-build/addon-workspace-git 0.6.1-next.1 → 0.6.1-next.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,24 @@
1
+ import type { ConductorLogger } from '@bakers-build/core';
2
+ /**
3
+ * Set `branch.autoSetupMerge=false` in the checkout's repository unless it
4
+ * already is. `git worktree add -b <branch> <path> origin/<base>` then writes
5
+ * no upstream into the shared `.git/config`, so it no longer fails while a
6
+ * sandbox holds a placeholder `config.lock`. Throws when the write fails.
7
+ */
8
+ export declare function setNoAutoSetupMerge(root: string): Promise<'set' | 'unchanged'>;
9
+ /**
10
+ * Run env that makes a plain `git push` go to the same-name remote branch
11
+ * (`push.default=current`): run branches have no upstream, and a per-run entry
12
+ * in the shared `.git/config` fails while a sandbox holds `config.lock`. It
13
+ * appends after the inherited `GIT_CONFIG_COUNT` and copies those entries, so
14
+ * it stays valid where the run does not inherit the conductor's env.
15
+ */
16
+ export declare function pushCurrentEnv(inherited: NodeJS.ProcessEnv): Record<string, string>;
17
+ /**
18
+ * The Git setup of a workspace backend before it creates or reuses a run
19
+ * worktree: `branch.autoSetupMerge=false` in `root`, then the run env from
20
+ * {@link pushCurrentEnv}. Best-effort: a failed write (a held `config.lock`)
21
+ * is logged with its repair command, since `worktree add` from
22
+ * `origin/<base>` then fails with a raw Git error.
23
+ */
24
+ export declare function runBranchGit(root: string, logger?: Pick<ConductorLogger, 'warn'>): Promise<Record<string, string>>;
@@ -0,0 +1,53 @@
1
+ import { execFile } from 'node:child_process';
2
+ import { promisify } from 'node:util';
3
+ const run = promisify(execFile);
4
+ /**
5
+ * Set `branch.autoSetupMerge=false` in the checkout's repository unless it
6
+ * already is. `git worktree add -b <branch> <path> origin/<base>` then writes
7
+ * no upstream into the shared `.git/config`, so it no longer fails while a
8
+ * sandbox holds a placeholder `config.lock`. Throws when the write fails.
9
+ */
10
+ export async function setNoAutoSetupMerge(root) {
11
+ const current = await run('git', [
12
+ '-C',
13
+ root,
14
+ 'config',
15
+ '--get',
16
+ 'branch.autoSetupMerge',
17
+ ]).then(({ stdout }) => stdout.trim(), () => '');
18
+ if (current === 'false')
19
+ return 'unchanged';
20
+ await run('git', ['-C', root, 'config', 'branch.autoSetupMerge', 'false']);
21
+ return 'set';
22
+ }
23
+ /**
24
+ * Run env that makes a plain `git push` go to the same-name remote branch
25
+ * (`push.default=current`): run branches have no upstream, and a per-run entry
26
+ * in the shared `.git/config` fails while a sandbox holds `config.lock`. It
27
+ * appends after the inherited `GIT_CONFIG_COUNT` and copies those entries, so
28
+ * it stays valid where the run does not inherit the conductor's env.
29
+ */
30
+ export function pushCurrentEnv(inherited) {
31
+ const count = inherited.GIT_CONFIG_COUNT ?? '';
32
+ const n = /^\d+$/.test(count) ? Number(count) : 0;
33
+ const env = { GIT_CONFIG_COUNT: String(n + 1) };
34
+ for (let i = 0; i < n; i++) {
35
+ for (const name of [`GIT_CONFIG_KEY_${i}`, `GIT_CONFIG_VALUE_${i}`]) {
36
+ env[name] = inherited[name] ?? '';
37
+ }
38
+ }
39
+ env[`GIT_CONFIG_KEY_${n}`] = 'push.default';
40
+ env[`GIT_CONFIG_VALUE_${n}`] = 'current';
41
+ return env;
42
+ }
43
+ /**
44
+ * The Git setup of a workspace backend before it creates or reuses a run
45
+ * worktree: `branch.autoSetupMerge=false` in `root`, then the run env from
46
+ * {@link pushCurrentEnv}. Best-effort: a failed write (a held `config.lock`)
47
+ * is logged with its repair command, since `worktree add` from
48
+ * `origin/<base>` then fails with a raw Git error.
49
+ */
50
+ export async function runBranchGit(root, logger) {
51
+ await setNoAutoSetupMerge(root).catch((err) => logger?.warn({ root, err: String(err) }, `could not set branch.autoSetupMerge=false; new worktrees fail while a sandbox holds config.lock. Repair: git -C ${root} config branch.autoSetupMerge false`));
52
+ return pushCurrentEnv(process.env);
53
+ }
@@ -1,4 +1,6 @@
1
1
  import type { BbWorkspace, EnsuredWorkspace, WorkspacePlugin } from '@bakers-build/conductor/contract';
2
+ import { type ConductorLogger } from '@bakers-build/core';
3
+ export { pushCurrentEnv, runBranchGit, setNoAutoSetupMerge, } from './git-config.js';
2
4
  export { loadInstructions } from './instructions.js';
3
5
  /** Default branch template: `<prefix><key>-<title-slug>` (readable). */
4
6
  export declare const DEFAULT_BRANCH_TEMPLATE = "{branchPrefix}{key}-{titleSlug}";
@@ -15,6 +17,13 @@ export interface GitWorkspaceOptions {
15
17
  * {identifier}, {titleSlug}. Default: `{branchPrefix}{key}-{titleSlug}`.
16
18
  */
17
19
  branchTemplate?: string;
20
+ /**
21
+ * Default `false`: before creating a worktree, set `branch.autoSetupMerge=false`
22
+ * in `root`, and give runs `push.default=current`. `true` leaves Git alone.
23
+ */
24
+ autoSetupMerge?: boolean;
25
+ /** Receives the warning when `branch.autoSetupMerge` cannot be written. */
26
+ logger?: ConductorLogger;
18
27
  runGit?: GitRunner;
19
28
  }
20
29
  export declare class GitWorkspace implements BbWorkspace {
@@ -23,6 +32,8 @@ export declare class GitWorkspace implements BbWorkspace {
23
32
  private readonly worktreesRoot;
24
33
  private readonly branchPrefix;
25
34
  private readonly branchTemplate;
35
+ private readonly autoSetupMerge;
36
+ private readonly logger;
26
37
  constructor(options: GitWorkspaceOptions);
27
38
  /** Render the branch name for a ticket from the configured template. */
28
39
  branchFor(identifier: string, title?: string): string;
@@ -31,8 +42,5 @@ export declare class GitWorkspace implements BbWorkspace {
31
42
  ensure(identifier: string, title?: string, _baseRef?: string): Promise<EnsuredWorkspace>;
32
43
  remove(identifier: string): Promise<void>;
33
44
  }
34
- export declare function gitWorkspace(opts?: {
35
- branchPrefix?: string;
36
- branchTemplate?: string;
37
- }): WorkspacePlugin;
45
+ export declare function gitWorkspace(opts?: Pick<GitWorkspaceOptions, 'branchPrefix' | 'branchTemplate' | 'autoSetupMerge'>): WorkspacePlugin;
38
46
  export default gitWorkspace;
package/dist/src/index.js CHANGED
@@ -5,8 +5,10 @@
5
5
  // package for it: the one addon→addon edge the layered acyclic rule permits.
6
6
  import { existsSync } from 'node:fs';
7
7
  import { join, resolve } from 'node:path';
8
- import { deriveCheckoutRoot, exec, slugify } from '@bakers-build/core';
8
+ import { deriveCheckoutRoot, exec, slugify, } from '@bakers-build/core';
9
+ import { runBranchGit } from './git-config.js';
9
10
  import { loadInstructions } from './instructions.js';
11
+ export { pushCurrentEnv, runBranchGit, setNoAutoSetupMerge, } from './git-config.js';
10
12
  export { loadInstructions } from './instructions.js';
11
13
  /** Default branch template: `<prefix><key>-<title-slug>` (readable). */
12
14
  export const DEFAULT_BRANCH_TEMPLATE = '{branchPrefix}{key}-{titleSlug}';
@@ -28,6 +30,8 @@ export class GitWorkspace {
28
30
  worktreesRoot;
29
31
  branchPrefix;
30
32
  branchTemplate;
33
+ autoSetupMerge;
34
+ logger;
31
35
  constructor(options) {
32
36
  this.runGit = options.runGit ?? defaultGitRunner;
33
37
  this.root = resolve(options.root);
@@ -36,6 +40,8 @@ export class GitWorkspace {
36
40
  : `${this.root}-worktrees`;
37
41
  this.branchPrefix = options.branchPrefix ?? 'bakers/';
38
42
  this.branchTemplate = options.branchTemplate ?? DEFAULT_BRANCH_TEMPLATE;
43
+ this.autoSetupMerge = options.autoSetupMerge ?? false;
44
+ this.logger = options.logger;
39
45
  }
40
46
  /** Render the branch name for a ticket from the configured template. */
41
47
  branchFor(identifier, title) {
@@ -60,14 +66,28 @@ export class GitWorkspace {
60
66
  throw new Error(`invalid workspace identifier: ${identifier}`);
61
67
  }
62
68
  const path = this.pathFor(key);
69
+ const env = this.autoSetupMerge
70
+ ? undefined
71
+ : await runBranchGit(this.root, this.logger);
72
+ const extra = env ? { env } : {};
63
73
  if (existsSync(path)) {
64
- return { path, instructions: loadInstructions(path), created: false };
74
+ return {
75
+ path,
76
+ instructions: loadInstructions(path),
77
+ created: false,
78
+ ...extra,
79
+ };
65
80
  }
66
81
  const branch = this.branchFor(identifier, title);
67
82
  await this.runGit(['-C', this.root, 'worktree', 'add', '--force', '-B', branch, path], this.root);
68
83
  // The `after_create` hook is run by the executor (BB-84), not here — the
69
84
  // workspace only reports that it created a fresh worktree.
70
- return { path, instructions: loadInstructions(path), created: true };
85
+ return {
86
+ path,
87
+ instructions: loadInstructions(path),
88
+ created: true,
89
+ ...extra,
90
+ };
71
91
  }
72
92
  async remove(identifier) {
73
93
  const key = this.keyFor(identifier);
@@ -80,7 +100,7 @@ export function gitWorkspace(opts = {}) {
80
100
  kind: 'workspace',
81
101
  id: 'git',
82
102
  requiredModules: [],
83
- async createWorkspace(config) {
103
+ async createWorkspace(config, deps) {
84
104
  // BB-450 D2: the checkout root, through the one shared helper, never a
85
105
  // bare `dirname(config_path)` re-derived here.
86
106
  const root = await deriveCheckoutRoot(config.config_path);
@@ -88,6 +108,8 @@ export function gitWorkspace(opts = {}) {
88
108
  root,
89
109
  ...(opts.branchPrefix ? { branchPrefix: opts.branchPrefix } : {}),
90
110
  ...(opts.branchTemplate ? { branchTemplate: opts.branchTemplate } : {}),
111
+ ...(opts.autoSetupMerge ? { autoSetupMerge: true } : {}),
112
+ ...(deps?.logger ? { logger: deps.logger } : {}),
91
113
  });
92
114
  },
93
115
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bakers-build/addon-workspace-git",
3
- "version": "0.6.1-next.1",
3
+ "version": "0.6.1-next.3",
4
4
  "description": "bakers.build conductor workspace addon: plain git-worktree workspace + the WORKFLOW.md instruction loader.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -20,11 +20,11 @@
20
20
  "directory": "bakers-cli/addons/workspace-git"
21
21
  },
22
22
  "peerDependencies": {
23
- "@bakers-build/conductor": "^0.6.1-next.1",
24
- "@bakers-build/core": "^0.6.1-next.1"
23
+ "@bakers-build/conductor": "^0.6.1-next.3",
24
+ "@bakers-build/core": "^0.6.1-next.3"
25
25
  },
26
26
  "bakersBuild": {
27
- "commit": "a92b86267d70b1d4bc469e62ef3c6964807c7dd0",
27
+ "commit": "1531a0bb0d34ccf6b54a051fcb8b016902c31f0f",
28
28
  "channel": "next"
29
29
  }
30
30
  }