@oakgreencc/graft 0.2.15 → 0.2.16
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 +1 -1
- package/src/hooks/sync-worktree.mjs +62 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oakgreencc/graft",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.16",
|
|
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",
|
|
@@ -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 {
|
|
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
|
-
|
|
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
|
|
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
|
}
|