@oakgreencc/graft 0.2.15 → 0.2.17

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oakgreencc/graft",
3
- "version": "0.2.15",
3
+ "version": "0.2.17",
4
4
  "description": "Let an agent work a GitHub project without the local git CLI's write path: Verified commits created by a GitHub App, the repo's git hooks run at the right moments, CODEOWNERS as the manifest of what needs a human, and a scoped maintainer mode for when one says yes.",
5
5
  "license": "AGPL-3.0-only",
6
6
  "type": "module",
package/src/git-hooks.mjs CHANGED
@@ -112,7 +112,9 @@ export function hookRunner({ git, root, config, skip = false, report = () => {},
112
112
  stdio: [input === undefined ? 'ignore' : 'pipe', 2, 2],
113
113
  })
114
114
  ran.push(name)
115
- if (res.error)
115
+ // A hook that exits without reading its stdin (pre-push's ref lines) makes the write fail with EPIPE. That is
116
+ // not a failure to run: as git does, judge the hook by its exit status.
117
+ if (res.error && !(epipe(res.error) && res.status !== null))
116
118
  throw new PublishError(`refusing to publish: the \`${name}\` hook could not run: ${res.error.message}`)
117
119
  if (res.status !== 0)
118
120
  throw new PublishError(
@@ -230,6 +232,9 @@ export function previewCommit({ git, tree, parents, message }) {
230
232
  }).trim()
231
233
  }
232
234
 
235
+ /** @param {Error} err */
236
+ const epipe = (err) => /** @type {NodeJS.ErrnoException} */ (err).code === 'EPIPE'
237
+
233
238
  function defaultSpawn(file, args, opts) {
234
239
  return spawnSync(file, args, { ...opts, encoding: 'utf8' })
235
240
  }
@@ -27,8 +27,16 @@
27
27
  * own commits is left exactly as it is.
28
28
  *
29
29
  * Every failure is reported and none is fatal: the hook always exits 0.
30
+ *
31
+ * ---------------------------------------------------------------------------
32
+ * THE MAIN CHECKOUT TOO. The repository's primary worktree is where a human
33
+ * reads, runs and starts things, and what the next worktree hydrates from. Its
34
+ * base branch is fast-forwarded to the same fetched commit under the same
35
+ * rules: checked out there and clean → `merge --ff-only`; on another branch →
36
+ * the ref alone, by a fast-forward-only `fetch .`; anything else is reported.
30
37
  */
31
- import { existsSync } from 'node:fs'
38
+ import { execFileSync } from 'node:child_process'
39
+ import { existsSync, realpathSync } from 'node:fs'
32
40
  import { resolveContext } from '../context.mjs'
33
41
 
34
42
  /**
@@ -41,6 +49,47 @@ export function worktreePath(event) {
41
49
  return m ? m[1] : (event?.cwd ?? process.cwd())
42
50
  }
43
51
 
52
+ /**
53
+ * Fast-forward the primary worktree's `base` to `target`. Null when there is
54
+ * nothing to say: it is the same checkout as `wt`, or already there.
55
+ *
56
+ * @param {string} wt @param {string} base @param {string} target
57
+ */
58
+ export function syncMain(wt, base, target) {
59
+ const g = (/** @type {string} */ dir, /** @type {string[]} */ args) =>
60
+ execFileSync('git', ['-C', dir, ...args], { encoding: 'utf8', stdio: 'pipe' })
61
+ const first = g(wt, ['worktree', 'list', '--porcelain']).split('\n')[0]
62
+ const main = first.startsWith('worktree ') ? first.slice('worktree '.length) : null
63
+ if (!main || !existsSync(main) || realpathSync(main) === realpathSync(wt)) return null
64
+ const tip = (() => {
65
+ try {
66
+ return g(main, ['rev-parse', '-q', '--verify', `refs/heads/${base}`]).trim()
67
+ } catch {
68
+ return null
69
+ }
70
+ })()
71
+ if (!tip || tip === target) return null
72
+ const head = (() => {
73
+ try {
74
+ return g(main, ['symbolic-ref', '--short', '-q', 'HEAD']).trim()
75
+ } catch {
76
+ return ''
77
+ }
78
+ })()
79
+ const moved = `Main checkout ${base} fast-forwarded ${tip.slice(0, 7)} → ${target.slice(0, 7)}.`
80
+ try {
81
+ if (head === base) {
82
+ if (g(main, ['status', '--porcelain', '--untracked-files=no']).trim()) {
83
+ return `Main checkout (${main}) has uncommitted changes, so its ${base} was NOT fast-forwarded to ${target.slice(0, 7)}.`
84
+ }
85
+ g(main, ['merge', '--ff-only', '--quiet', target])
86
+ } else g(main, ['fetch', '--quiet', '.', `${target}:refs/heads/${base}`])
87
+ return moved
88
+ } catch {
89
+ return `Main checkout ${base} (${tip.slice(0, 7)}) could not be fast-forwarded to ${target.slice(0, 7)}: it has diverged. Left untouched.`
90
+ }
91
+ }
92
+
44
93
  /**
45
94
  * The whole hook, as a function of the event. Returns the message, if any.
46
95
  * The base is Context's offline rule (`baseBranchOffline`): a hook must not
@@ -69,13 +118,22 @@ export function sync(event, { env = process.env } = {}) {
69
118
  const before = git(['rev-parse', 'HEAD']).trim()
70
119
  git(['fetch', 'origin', base])
71
120
  const target = git(['rev-parse', 'FETCH_HEAD']).trim()
72
- if (before === target) return null
121
+ let main = null
122
+ try {
123
+ main = syncMain(wt, base, target)
124
+ } catch (err) {
125
+ main = `Could not sync the main checkout: ${/** @type {Error} */ (err)?.message ?? err}.`
126
+ }
127
+ const both = (/** @type {string | null} */ own) => [own, main].filter(Boolean).join(' ') || null
128
+ if (before === target) return both(null)
73
129
  try {
74
130
  git(['merge', '--ff-only', 'FETCH_HEAD'])
75
131
  } catch {
76
- return `Worktree at ${before.slice(0, 7)} could not be fast-forwarded to origin/${base} (${target.slice(0, 7)}) — it has diverged. Left untouched; reconcile it before publishing.`
132
+ return both(
133
+ `Worktree at ${before.slice(0, 7)} could not be fast-forwarded to origin/${base} (${target.slice(0, 7)}) — it has diverged. Left untouched; reconcile it before publishing.`,
134
+ )
77
135
  }
78
- return `Worktree fast-forwarded ${before.slice(0, 7)} → ${target.slice(0, 7)} (origin/${base}).`
136
+ return both(`Worktree fast-forwarded ${before.slice(0, 7)} → ${target.slice(0, 7)} (origin/${base}).`)
79
137
  } catch (err) {
80
138
  return `Could not sync the worktree to origin/${base ?? '<base>'}: ${/** @type {Error} */ (err)?.message ?? err}. Run \`graft advance ${base ?? '<base>'}\` before publishing.`
81
139
  }