@bakers-build/addon-forge 0.10.1-next.8 → 0.11.0
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.
- package/dist/src/git.d.ts +26 -0
- package/dist/src/git.js +84 -3
- package/dist/src/tool-definitions.js +5 -1
- package/dist/src/tools.js +74 -3
- package/options.schema.json +1 -1
- package/package.json +5 -5
package/dist/src/git.d.ts
CHANGED
|
@@ -26,3 +26,29 @@ export declare function fetchBranches(worktree: string, auth: GitAuth, branches:
|
|
|
26
26
|
* Returns the pushed SHA and records it as refs/remotes/origin/<branch>.
|
|
27
27
|
*/
|
|
28
28
|
export declare function pushBranch(worktree: string, auth: GitAuth, branch: string, lease: PushLease): Promise<string>;
|
|
29
|
+
export interface BranchUpdate {
|
|
30
|
+
/** The branch tip before the update. */
|
|
31
|
+
previous: string;
|
|
32
|
+
/** The fetched target tip that was merged. */
|
|
33
|
+
target: string;
|
|
34
|
+
/** The branch tip after the update; `previous` for a no-op. */
|
|
35
|
+
resulting: string;
|
|
36
|
+
noop: boolean;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Merge the clone's `origin/<target>` into `refs/heads/<branch>` without a
|
|
40
|
+
* checkout: merge-tree and commit-tree in the scratch, so no partial merge,
|
|
41
|
+
* hook, driver or repository config can run, then compare-and-swap the branch.
|
|
42
|
+
* Refuses a branch moved past `expected`, uncommitted work in `workspace`
|
|
43
|
+
* against `clean`, a remote branch the local one lacks, and conflicts (naming their paths). Pushes nothing.
|
|
44
|
+
*/
|
|
45
|
+
export declare function updateBranch(worktree: string, auth: GitAuth, opts: {
|
|
46
|
+
branch: string;
|
|
47
|
+
target: string;
|
|
48
|
+
/** The tip the caller last saw; a branch moved past it is refused. */
|
|
49
|
+
expected?: string;
|
|
50
|
+
workspace?: {
|
|
51
|
+
path: string;
|
|
52
|
+
clean: string;
|
|
53
|
+
};
|
|
54
|
+
}): Promise<BranchUpdate>;
|
package/dist/src/git.js
CHANGED
|
@@ -5,9 +5,10 @@
|
|
|
5
5
|
// header, never argv, URL or file. Against the clone itself only `rev-parse`,
|
|
6
6
|
// `for-each-ref` and `update-ref` run, with hooks and fsmonitor disabled.
|
|
7
7
|
import { execFile } from 'node:child_process';
|
|
8
|
-
import { mkdtemp, readFile, rm } from 'node:fs/promises';
|
|
8
|
+
import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
|
|
9
9
|
import { tmpdir } from 'node:os';
|
|
10
10
|
import { join } from 'node:path';
|
|
11
|
+
import { UI_REFRESH_FILE } from '@bakers-build/core';
|
|
11
12
|
const HARDEN = ['-c', 'core.hooksPath=/dev/null', '-c', 'core.fsmonitor=false'];
|
|
12
13
|
function baseEnv(home) {
|
|
13
14
|
return {
|
|
@@ -23,7 +24,7 @@ function run(args, env, input) {
|
|
|
23
24
|
return new Promise((resolve, reject) => {
|
|
24
25
|
const child = execFile('git', args, { env, maxBuffer: 16 * 1024 * 1024 }, (err, stdout, stderr) => {
|
|
25
26
|
if (err)
|
|
26
|
-
reject(new Error(`git failed: ${stderr.trim() || err.message}`));
|
|
27
|
+
reject(Object.assign(new Error(`git failed: ${stderr.trim() || err.message}`), { code: err.code, stdout }));
|
|
27
28
|
else
|
|
28
29
|
resolve(stdout.trim());
|
|
29
30
|
});
|
|
@@ -86,7 +87,8 @@ async function withScratch(worktree, auth, body) {
|
|
|
86
87
|
return await body({
|
|
87
88
|
common,
|
|
88
89
|
env,
|
|
89
|
-
|
|
90
|
+
home,
|
|
91
|
+
remote: (args, input, extra) => run(args, { ...remoteEnv, ...extra }, input),
|
|
90
92
|
});
|
|
91
93
|
}
|
|
92
94
|
finally {
|
|
@@ -175,3 +177,82 @@ export async function pushBranch(worktree, auth, branch, lease) {
|
|
|
175
177
|
return sha;
|
|
176
178
|
});
|
|
177
179
|
}
|
|
180
|
+
/**
|
|
181
|
+
* Merge the clone's `origin/<target>` into `refs/heads/<branch>` without a
|
|
182
|
+
* checkout: merge-tree and commit-tree in the scratch, so no partial merge,
|
|
183
|
+
* hook, driver or repository config can run, then compare-and-swap the branch.
|
|
184
|
+
* Refuses a branch moved past `expected`, uncommitted work in `workspace`
|
|
185
|
+
* against `clean`, a remote branch the local one lacks, and conflicts (naming their paths). Pushes nothing.
|
|
186
|
+
*/
|
|
187
|
+
export async function updateBranch(worktree, auth, opts) {
|
|
188
|
+
const { branch, target, expected, workspace } = opts;
|
|
189
|
+
return withScratch(worktree, auth, async ({ common, env, home, remote }) => {
|
|
190
|
+
const previous = await readRef(common, env, `refs/heads/${branch}`);
|
|
191
|
+
if (!previous)
|
|
192
|
+
throw new Error(`no local branch '${branch}' to update`);
|
|
193
|
+
if (expected && previous !== expected)
|
|
194
|
+
throw new Error(`'${branch}' moved to ${previous} since the run last synced at ${expected}; nothing changed`);
|
|
195
|
+
const merged = await readRef(common, env, `refs/remotes/origin/${target}`);
|
|
196
|
+
if (!merged)
|
|
197
|
+
throw new Error(`target '${target}' was not fetched; nothing changed`);
|
|
198
|
+
const isAncestor = (a, b) => remote(['merge-base', '--is-ancestor', a, b]).then(() => true, () => false);
|
|
199
|
+
if (workspace) {
|
|
200
|
+
// A throwaway index and only the scratch's clean config: status runs no filter or fsmonitor.
|
|
201
|
+
await mkdir(join(home, 'scratch.git', 'info'), { recursive: true });
|
|
202
|
+
await writeFile(join(home, 'scratch.git', 'info', 'exclude'), `/.bakers/${UI_REFRESH_FILE}\n`);
|
|
203
|
+
const tree = {
|
|
204
|
+
GIT_WORK_TREE: workspace.path,
|
|
205
|
+
GIT_INDEX_FILE: join(home, 'index'),
|
|
206
|
+
};
|
|
207
|
+
await remote(['update-ref', '--no-deref', 'HEAD', workspace.clean]);
|
|
208
|
+
await remote(['read-tree', 'HEAD'], undefined, tree);
|
|
209
|
+
const dirty = await remote(['status', '--porcelain', '--untracked-files=all', '--no-renames'], undefined, tree);
|
|
210
|
+
if (dirty)
|
|
211
|
+
throw new Error(`uncommitted work in the run (${dirty
|
|
212
|
+
.split('\n')
|
|
213
|
+
.map((l) => l.replace(/^[ MADRCU?!]{1,2} /, ''))
|
|
214
|
+
.join(', ')}); commit or remove it, then call forge_update_branch again; nothing changed`);
|
|
215
|
+
}
|
|
216
|
+
const pushed = await readRef(common, env, `refs/remotes/origin/${branch}`);
|
|
217
|
+
if (pushed && !(await isAncestor(pushed, previous)))
|
|
218
|
+
throw new Error(`origin/${branch} has commits '${branch}' lacks (pushed elsewhere); integrate them first; nothing changed`);
|
|
219
|
+
if (await isAncestor(merged, previous))
|
|
220
|
+
return { previous, target: merged, resulting: previous, noop: true };
|
|
221
|
+
let tree;
|
|
222
|
+
try {
|
|
223
|
+
tree = await remote([
|
|
224
|
+
'merge-tree',
|
|
225
|
+
'--write-tree',
|
|
226
|
+
'--name-only',
|
|
227
|
+
'--no-messages',
|
|
228
|
+
previous,
|
|
229
|
+
merged,
|
|
230
|
+
]);
|
|
231
|
+
}
|
|
232
|
+
catch (err) {
|
|
233
|
+
const { code, stdout } = err;
|
|
234
|
+
if (code !== 1)
|
|
235
|
+
throw err;
|
|
236
|
+
const paths = [...new Set(String(stdout).split('\n').slice(1))].filter(Boolean);
|
|
237
|
+
throw new Error(`merging '${target}' into '${branch}' conflicts in ${paths.join(', ')}; nothing changed. The run cannot resolve this: stop and report it to the developers. A developer with write access merges origin/${target} into '${branch}' outside the run, resolves those paths and pushes; the next run starts there`);
|
|
238
|
+
}
|
|
239
|
+
const resulting = await remote([
|
|
240
|
+
'commit-tree',
|
|
241
|
+
tree,
|
|
242
|
+
'-p',
|
|
243
|
+
previous,
|
|
244
|
+
'-p',
|
|
245
|
+
merged,
|
|
246
|
+
'-m',
|
|
247
|
+
`Merge ${target} into ${branch}`,
|
|
248
|
+
], undefined, {
|
|
249
|
+
GIT_AUTHOR_NAME: 'bakers.build',
|
|
250
|
+
GIT_AUTHOR_EMAIL: 'noreply@bakers.build',
|
|
251
|
+
GIT_COMMITTER_NAME: 'bakers.build',
|
|
252
|
+
GIT_COMMITTER_EMAIL: 'noreply@bakers.build',
|
|
253
|
+
});
|
|
254
|
+
// Compare-and-swap: a branch that moved meanwhile is refused, never overwritten.
|
|
255
|
+
await inClone(common, env, 'update-ref', `refs/heads/${branch}`, resulting, previous);
|
|
256
|
+
return { previous, target: merged, resulting, noop: false };
|
|
257
|
+
});
|
|
258
|
+
}
|
|
@@ -7,7 +7,7 @@ const branch = {
|
|
|
7
7
|
};
|
|
8
8
|
export const FORGE_TOOL_DEFINITIONS = {
|
|
9
9
|
forge_fetch: {
|
|
10
|
-
description: "Fetch the ticket's base_branch, its own branch and the named parent branches into refs/remotes/origin/*; read only.
|
|
10
|
+
description: "Fetch the ticket's base_branch, its own branch and the named parent branches into refs/remotes/origin/*; read only. To integrate the target into the ticket branch call forge_update_branch.",
|
|
11
11
|
input: {
|
|
12
12
|
type: 'object',
|
|
13
13
|
properties: {
|
|
@@ -18,6 +18,10 @@ export const FORGE_TOOL_DEFINITIONS = {
|
|
|
18
18
|
},
|
|
19
19
|
},
|
|
20
20
|
},
|
|
21
|
+
forge_update_branch: {
|
|
22
|
+
description: "Merge the ticket's current target (fetched fresh) into the ticket branch of an isolated run; pushes nothing, changes no PR/MR. Commit first. Returns previous, target and resulting SHAs and sync: none (no-op), in_place (run the returned git merge --ff-only before further work) or restart (protected paths changed: stop, cancel the run with bakers_run_cancel; the next run starts at resulting). Refuses uncommitted work, conflicts and concurrent branch movement without changing anything. A conflict names its paths: stop and report it to the developers, who merge outside the run.",
|
|
23
|
+
input: { type: 'object', properties: {} },
|
|
24
|
+
},
|
|
21
25
|
forge_push: {
|
|
22
26
|
description: "Push an owned branch's local head. Fast-forward only, unless force_with_lease: then the remote must still be at expected_sha (default: refs/remotes/origin/<branch>). Bare force does not exist.",
|
|
23
27
|
input: {
|
package/dist/src/tools.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// The forge tools. Every call takes the ticket, branch_name and base_branch
|
|
2
2
|
// from the calling run; the token stays with the backend and is redacted
|
|
3
3
|
// from every result and error.
|
|
4
|
-
import { homeBbDir, loadRunClone, refreshRunTarget, saveRunClone, transferIsolatedCommit, } from '@bakers-build/core';
|
|
4
|
+
import { changedProtectedPaths, homeBbDir, loadRunClone, refreshRunTarget, runCloneTip, saveRunClone, transferIsolatedCommit, } from '@bakers-build/core';
|
|
5
5
|
import { FORGE_NEEDS_BACKEND, loadForgeBackend, mergeRule } from './config.js';
|
|
6
|
-
import { fetchBranches, pushBranch } from './git.js';
|
|
6
|
+
import { fetchBranches, pushBranch, updateBranch } from './git.js';
|
|
7
7
|
import { derivedBase, isConventionalTitle, isOwned, ownershipRefusal, redact, TITLE_REFUSAL, } from './rules.js';
|
|
8
8
|
export const FORGE_NEEDS_RUN = 'forge tools act only for an agent run: the call carries no ticket branch_name and base_branch (an operator delivers by hand)';
|
|
9
9
|
function runOf(req) {
|
|
@@ -42,6 +42,22 @@ function ownedBranch(req, run) {
|
|
|
42
42
|
throw new Error(ownershipRefusal(branch, run.branchName));
|
|
43
43
|
return branch;
|
|
44
44
|
}
|
|
45
|
+
const restartRefusal = (sha) => `forge: the branch was updated to ${sha} with protected changes this run cannot see; stop and cancel this run with bakers_run_cancel on its ticket, the next run starts there`;
|
|
46
|
+
/**
|
|
47
|
+
* Bring the run's commits into the parent. A run behind its branch (an update
|
|
48
|
+
* it has not synced) or awaiting a restart is refused: it would act on stale files.
|
|
49
|
+
*/
|
|
50
|
+
function transferRun(clone) {
|
|
51
|
+
if (clone.restartPending)
|
|
52
|
+
throw new Error(restartRefusal(clone.restartPending));
|
|
53
|
+
const tip = runCloneTip(clone);
|
|
54
|
+
const expected = transferIsolatedCommit(clone, tip);
|
|
55
|
+
if (expected !== tip)
|
|
56
|
+
throw new Error(`forge: the run is behind its branch at ${expected}; run git merge --ff-only ${expected} and validate first`);
|
|
57
|
+
const saved = { ...clone, expected };
|
|
58
|
+
saveRunClone(saved);
|
|
59
|
+
return saved;
|
|
60
|
+
}
|
|
45
61
|
export const LOG_DEFAULT_BYTES = 16_384;
|
|
46
62
|
export const LOG_MAX_BYTES = 262_144;
|
|
47
63
|
const json = (value) => ({
|
|
@@ -98,7 +114,7 @@ export function forgeTools(deps) {
|
|
|
98
114
|
const clone = loadRunClone(req.worktreePath);
|
|
99
115
|
// Only checked commits leave the run: transfer first, push from the parent.
|
|
100
116
|
if (clone && branch === clone.branch)
|
|
101
|
-
|
|
117
|
+
transferRun(clone);
|
|
102
118
|
const sha = await pushBranch(clone?.parentGitDir ?? req.worktreePath, await b.gitAuth(), branch, {
|
|
103
119
|
forceWithLease: req.args.force_with_lease === true,
|
|
104
120
|
...(typeof req.args.expected_sha === 'string'
|
|
@@ -109,6 +125,61 @@ export function forgeTools(deps) {
|
|
|
109
125
|
refreshRunTarget(clone, [branch]);
|
|
110
126
|
return json({ branch, sha });
|
|
111
127
|
},
|
|
128
|
+
forge_update_branch: async (req) => {
|
|
129
|
+
const run = runOf(req);
|
|
130
|
+
const loaded = loadRunClone(req.worktreePath);
|
|
131
|
+
if (!loaded)
|
|
132
|
+
throw new Error('forge_update_branch serves isolated run clones; in this checkout run forge_fetch, then merge origin/<target> locally');
|
|
133
|
+
if (loaded.restartPending)
|
|
134
|
+
throw new Error(restartRefusal(loaded.restartPending));
|
|
135
|
+
const target = await currentBase(req, run.branchName, run);
|
|
136
|
+
const auth = await (await backend()).gitAuth();
|
|
137
|
+
const tip = runCloneTip(loaded);
|
|
138
|
+
// Run commits first, so the merge keeps them; a run behind its branch keeps its tip.
|
|
139
|
+
const clone = {
|
|
140
|
+
...loaded,
|
|
141
|
+
expected: transferIsolatedCommit(loaded, tip),
|
|
142
|
+
};
|
|
143
|
+
saveRunClone(clone);
|
|
144
|
+
await fetchBranches(clone.parentGitDir, auth, [target, run.branchName]);
|
|
145
|
+
refreshRunTarget(clone, [target]);
|
|
146
|
+
const u = await updateBranch(clone.parentGitDir, auth, {
|
|
147
|
+
branch: run.branchName,
|
|
148
|
+
target,
|
|
149
|
+
expected: clone.expected,
|
|
150
|
+
workspace: { path: clone.workspace, clean: tip },
|
|
151
|
+
});
|
|
152
|
+
const protectedPaths = u.resulting === tip
|
|
153
|
+
? []
|
|
154
|
+
: changedProtectedPaths(clone, tip, u.resulting);
|
|
155
|
+
const sync = u.resulting === tip
|
|
156
|
+
? 'none'
|
|
157
|
+
: protectedPaths.length > 0
|
|
158
|
+
? 'restart'
|
|
159
|
+
: 'in_place';
|
|
160
|
+
if (sync === 'in_place')
|
|
161
|
+
refreshRunTarget(clone, [target], [u.resulting]);
|
|
162
|
+
saveRunClone({
|
|
163
|
+
...clone,
|
|
164
|
+
expected: u.resulting,
|
|
165
|
+
...(sync === 'restart' ? { restartPending: u.resulting } : {}),
|
|
166
|
+
});
|
|
167
|
+
const next = {
|
|
168
|
+
none: `nothing to do: '${run.branchName}' already contains ${u.target}`,
|
|
169
|
+
in_place: `run git merge --ff-only ${u.resulting} before any further work, then validate and forge_push`,
|
|
170
|
+
restart: `protected paths changed (${protectedPaths.join(', ')}): do no further work or publication; cancel this run with bakers_run_cancel on its ticket, the next run starts at ${u.resulting}`,
|
|
171
|
+
}[sync];
|
|
172
|
+
return json({
|
|
173
|
+
branch: run.branchName,
|
|
174
|
+
target_branch: target,
|
|
175
|
+
previous: u.previous,
|
|
176
|
+
target: u.target,
|
|
177
|
+
resulting: u.resulting,
|
|
178
|
+
noop: u.noop,
|
|
179
|
+
sync,
|
|
180
|
+
next,
|
|
181
|
+
});
|
|
182
|
+
},
|
|
112
183
|
forge_pr_upsert: async (req) => {
|
|
113
184
|
const title = String(req.args.title);
|
|
114
185
|
// Refused before any request reaches the forge.
|
package/options.schema.json
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"description": "The backend package."
|
|
23
23
|
},
|
|
24
24
|
"with": {
|
|
25
|
-
"description": "The backend package's own settings, checked by that package's options.schema.json."
|
|
25
|
+
"description": "The backend package's own settings, checked by that package's options.schema.json: [GitHub](/addons/forge-github#options) or [GitLab](/addons/forge-gitlab#options)."
|
|
26
26
|
}
|
|
27
27
|
},
|
|
28
28
|
"description": "The forge backend: the package and its own settings."
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bakers-build/addon-forge",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "bakers.build forge tools: a sandboxed run fetches, pushes its own branches, opens its PR/MR, reads checks and merges through the conductor, which holds the token. Backends implement the ForgeBackend contract (./backend).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,11 +22,11 @@
|
|
|
22
22
|
"directory": "bakers-cli/addons/forge"
|
|
23
23
|
},
|
|
24
24
|
"peerDependencies": {
|
|
25
|
-
"@bakers-build/conductor": "^0.
|
|
26
|
-
"@bakers-build/core": "^0.
|
|
25
|
+
"@bakers-build/conductor": "^0.11.0",
|
|
26
|
+
"@bakers-build/core": "^0.11.0"
|
|
27
27
|
},
|
|
28
28
|
"bakersBuild": {
|
|
29
|
-
"commit": "
|
|
30
|
-
"channel": "
|
|
29
|
+
"commit": "4e1d1a59439a6a0019f3b5b213d9cc9f09d14381",
|
|
30
|
+
"channel": "latest"
|
|
31
31
|
}
|
|
32
32
|
}
|