astrocms 0.3.0 → 0.4.1
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/README.md +27 -3
- package/backend/config.ts +40 -1
- package/backend/github.ts +95 -0
- package/backend/parsers/components.ts +2 -2
- package/backend/routes/claude.ts +30 -1
- package/backend/routes/git.ts +518 -28
- package/dist/_cms/{clike-D_6L3tnO.js → clike-xvONpD0Z.js} +1 -1
- package/dist/_cms/{dist-DyweOumh.js → dist-1TbecjrO.js} +1 -1
- package/dist/_cms/{dist-DJjiXz-_.js → dist-3IZXNfzQ.js} +1 -1
- package/dist/_cms/{dist-7J3SMScN.js → dist-B77EDfNH.js} +1 -1
- package/dist/_cms/dist-BZ2bx3dk.js +13 -0
- package/dist/_cms/dist-BbYR4JZL.js +1 -0
- package/dist/_cms/{dist-D4bsfpFv.js → dist-BkwcwlN_.js} +1 -1
- package/dist/_cms/dist-CB4XdWP2.js +1 -0
- package/dist/_cms/{dist-CNW8yujW.js → dist-CF_IWM-w.js} +1 -1
- package/dist/_cms/{dist-8nvsczSZ.js → dist-CenmsTzP.js} +1 -1
- package/dist/_cms/{dist-G1pI55d8.js → dist-CfQ9P9kz.js} +1 -1
- package/dist/_cms/{dist-D3O5UDsl.js → dist-DgIcoGq0.js} +1 -1
- package/dist/_cms/{dist-BzcZNbxs.js → dist-Dz3T_6mF.js} +1 -1
- package/dist/_cms/{dist-3q3EeGvf.js → dist-MTs40hNv.js} +1 -1
- package/dist/_cms/{dist-zip7q785.js → dist-R7eMD-f2.js} +1 -1
- package/dist/_cms/{dist-Bi_DBs0h.js → dist-SeNI8896.js} +1 -1
- package/dist/_cms/{dist-CKTtGY0K.js → dist-Wey5_cFO.js} +1 -1
- package/dist/_cms/{dist-BCyZQxMf.js → dist-gJ6KMSdz.js} +1 -1
- package/dist/_cms/dist-xIMq_6r-.js +1 -0
- package/dist/_cms/{dylan-BCDoL_-p.js → dylan-S7ucR4pR.js} +1 -1
- package/dist/_cms/forth-D6RzAl64.js +1 -0
- package/dist/_cms/index-B2SPzLJc.js +456 -0
- package/dist/_cms/index-BwI3wX9l.css +1 -0
- package/dist/_cms/javascript-Bu8mWwtz.js +1 -0
- package/dist/_cms/{lua-CiA1ziua.js → lua-DPCEkfD_.js} +1 -1
- package/dist/_cms/{nginx-BMaDbqW3.js → nginx-8H7HVH4w.js} +1 -1
- package/dist/_cms/{ntriples-B9h1VB_g.js → ntriples-CzMBulkp.js} +1 -1
- package/dist/_cms/{powershell-BcIto6Sf.js → powershell-D8WpQPaX.js} +1 -1
- package/dist/_cms/pug-Dwd_FmgJ.js +1 -0
- package/dist/_cms/{sieve-BAxa3IjF.js → sieve-tLWDfEkZ.js} +1 -1
- package/dist/_cms/solr-B78wSQJQ.js +1 -0
- package/dist/_cms/{tiki-AK6FwT8q.js → tiki-DTszW0OQ.js} +1 -1
- package/dist/_cms/{verilog-7LoJAqYT.js → verilog-CjI9Y6RI.js} +1 -1
- package/dist/index.html +3 -3
- package/package.json +34 -35
- package/dist/_cms/dist-BcBf0brb.js +0 -1
- package/dist/_cms/dist-CL9duWcU.js +0 -1
- package/dist/_cms/dist-Cvt9tHuf.js +0 -1
- package/dist/_cms/dist-IOAcKAnp.js +0 -13
- package/dist/_cms/forth-Cezjo90N.js +0 -1
- package/dist/_cms/index-CTA91OkF.js +0 -466
- package/dist/_cms/index-Dp1gAhoa.css +0 -1
- package/dist/_cms/javascript-DoL9Wb-Q.js +0 -1
- package/dist/_cms/pug-Gsm0M45u.js +0 -1
- package/dist/_cms/solr-BBopId9w.js +0 -1
- package/patches/hast-util-to-estree+3.1.3.patch +0 -14
package/backend/routes/git.ts
CHANGED
|
@@ -4,6 +4,13 @@ import { promisify } from 'util'
|
|
|
4
4
|
import { unlink } from 'fs/promises'
|
|
5
5
|
import { join } from 'path'
|
|
6
6
|
import { ROOT_DIR } from '../root.js'
|
|
7
|
+
import { loadConfig, type GitFlowConfig } from '../config.js'
|
|
8
|
+
import {
|
|
9
|
+
createPullRequest,
|
|
10
|
+
findOpenPr,
|
|
11
|
+
parseRepoFromUrl,
|
|
12
|
+
type GitHubPr,
|
|
13
|
+
} from '../github.js'
|
|
7
14
|
|
|
8
15
|
const exec = promisify(execFile)
|
|
9
16
|
|
|
@@ -39,6 +46,31 @@ function getBranch() {
|
|
|
39
46
|
return process.env.GIT_BRANCH || 'main'
|
|
40
47
|
}
|
|
41
48
|
|
|
49
|
+
async function getGitSettings(): Promise<GitFlowConfig> {
|
|
50
|
+
const config = await loadConfig()
|
|
51
|
+
// The git block is optional in the type (optional section in
|
|
52
|
+
// astrocms.json); loadConfig fills it with defaults, so this fallback is
|
|
53
|
+
// a safety net only.
|
|
54
|
+
return config.git ?? { prBasedEdits: false, baseBranch: 'main' }
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* The branch HEAD is currently on. Empty string on a detached HEAD.
|
|
59
|
+
*/
|
|
60
|
+
async function getCurrentBranch(): Promise<string> {
|
|
61
|
+
const out = await git('branch', '--show-current')
|
|
62
|
+
return out.trim()
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Subject of the latest commit (HEAD); empty when there are no commits. */
|
|
66
|
+
async function getLastCommitSubject(): Promise<string> {
|
|
67
|
+
try {
|
|
68
|
+
return (await git('log', '-1', '--pretty=%s')).trim()
|
|
69
|
+
} catch {
|
|
70
|
+
return ''
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
42
74
|
async function ensureAuthedRemote() {
|
|
43
75
|
const pat = process.env.GIT_PAT
|
|
44
76
|
const repoUrl = process.env.GIT_REPO_URL
|
|
@@ -56,10 +88,9 @@ async function isWorktreeClean(): Promise<boolean> {
|
|
|
56
88
|
return status.trim().length === 0
|
|
57
89
|
}
|
|
58
90
|
|
|
59
|
-
async function
|
|
60
|
-
const branch = getBranch()
|
|
91
|
+
async function countCommits(range: string): Promise<number> {
|
|
61
92
|
try {
|
|
62
|
-
const out = await git('rev-list', '--count',
|
|
93
|
+
const out = await git('rev-list', '--count', range)
|
|
63
94
|
const n = parseInt(out.trim(), 10)
|
|
64
95
|
return Number.isFinite(n) ? n : 0
|
|
65
96
|
} catch {
|
|
@@ -67,6 +98,33 @@ async function countBehind(): Promise<number> {
|
|
|
67
98
|
}
|
|
68
99
|
}
|
|
69
100
|
|
|
101
|
+
async function countBehind(branch: string): Promise<number> {
|
|
102
|
+
return countCommits(`HEAD..origin/${branch}`)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** True when the remote-tracking ref for `branch` exists locally. */
|
|
106
|
+
async function hasRemoteRef(branch: string): Promise<boolean> {
|
|
107
|
+
try {
|
|
108
|
+
await git('show-ref', '--verify', '--quiet', `refs/remotes/origin/${branch}`)
|
|
109
|
+
return true
|
|
110
|
+
} catch {
|
|
111
|
+
return false
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Fetch one branch from origin. Returns false when the branch does not exist
|
|
117
|
+
* on the remote yet (e.g. a working branch that was never pushed).
|
|
118
|
+
*/
|
|
119
|
+
async function fetchBranch(branch: string): Promise<boolean> {
|
|
120
|
+
try {
|
|
121
|
+
await git('fetch', 'origin', branch)
|
|
122
|
+
return true
|
|
123
|
+
} catch {
|
|
124
|
+
return false
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
70
128
|
// --- Remote poll state ---
|
|
71
129
|
|
|
72
130
|
interface RemoteState {
|
|
@@ -75,6 +133,8 @@ interface RemoteState {
|
|
|
75
133
|
lastCheckedAt: number | null
|
|
76
134
|
lastPulledAt: number | null
|
|
77
135
|
error?: string
|
|
136
|
+
openPr: GitHubPr | null
|
|
137
|
+
openPrError?: string
|
|
78
138
|
}
|
|
79
139
|
|
|
80
140
|
const remoteState: RemoteState = {
|
|
@@ -82,30 +142,93 @@ const remoteState: RemoteState = {
|
|
|
82
142
|
behind: 0,
|
|
83
143
|
lastCheckedAt: null,
|
|
84
144
|
lastPulledAt: null,
|
|
145
|
+
openPr: null,
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Sync the working branch with its remote ref when it is behind and the
|
|
150
|
+
* worktree is clean (ff-only). Shared by both modes.
|
|
151
|
+
*/
|
|
152
|
+
async function syncWorkingBranch(branch: string): Promise<void> {
|
|
153
|
+
const behind = await countBehind(branch)
|
|
154
|
+
if (behind > 0) {
|
|
155
|
+
const clean = await isWorktreeClean()
|
|
156
|
+
if (clean) {
|
|
157
|
+
await git('pull', '--ff-only', 'origin', branch)
|
|
158
|
+
remoteState.behind = 0
|
|
159
|
+
remoteState.updateAvailable = false
|
|
160
|
+
remoteState.lastPulledAt = Date.now()
|
|
161
|
+
} else {
|
|
162
|
+
remoteState.behind = behind
|
|
163
|
+
remoteState.updateAvailable = true
|
|
164
|
+
}
|
|
165
|
+
} else {
|
|
166
|
+
remoteState.behind = 0
|
|
167
|
+
remoteState.updateAvailable = false
|
|
168
|
+
}
|
|
85
169
|
}
|
|
86
170
|
|
|
87
171
|
async function checkRemote(): Promise<void> {
|
|
88
172
|
try {
|
|
173
|
+
const settings = await getGitSettings()
|
|
89
174
|
await ensureAuthedRemote()
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
175
|
+
|
|
176
|
+
if (settings.prBasedEdits) {
|
|
177
|
+
const current = await getCurrentBranch()
|
|
178
|
+
|
|
179
|
+
// The base branch must exist on the remote; a failure here is a real
|
|
180
|
+
// error and lands in remoteState.error. /status derives ahead/behind
|
|
181
|
+
// from the local origin/<base> ref, so this fetch only keeps that ref
|
|
182
|
+
// fresh.
|
|
183
|
+
await git('fetch', 'origin', settings.baseBranch)
|
|
184
|
+
|
|
185
|
+
if (current && current !== settings.baseBranch) {
|
|
186
|
+
// The working branch may not exist on the remote yet.
|
|
187
|
+
if (await fetchBranch(current)) {
|
|
188
|
+
await syncWorkingBranch(current)
|
|
189
|
+
} else {
|
|
190
|
+
remoteState.behind = 0
|
|
191
|
+
remoteState.updateAvailable = false
|
|
192
|
+
}
|
|
193
|
+
} else {
|
|
194
|
+
// On the base branch: no working-branch ref to sync.
|
|
98
195
|
remoteState.behind = 0
|
|
99
196
|
remoteState.updateAvailable = false
|
|
100
|
-
remoteState.lastPulledAt = Date.now()
|
|
101
|
-
} else {
|
|
102
|
-
remoteState.behind = behind
|
|
103
|
-
remoteState.updateAvailable = true
|
|
104
197
|
}
|
|
198
|
+
|
|
199
|
+
// Best-effort open-PR lookup for the working branch. Skipped when the
|
|
200
|
+
// origin is not a GitHub URL or no PAT is configured; failures land in
|
|
201
|
+
// openPrError without affecting the git state above. The result is
|
|
202
|
+
// collected locally and published in one step at the end: clearing
|
|
203
|
+
// remoteState.openPr up front would blank the PR link that /status
|
|
204
|
+
// serves while this lookup is still in flight, right after a push
|
|
205
|
+
// cached it.
|
|
206
|
+
let openPr: GitHubPr | null = null
|
|
207
|
+
let openPrError: string | undefined
|
|
208
|
+
if (current && current !== settings.baseBranch) {
|
|
209
|
+
try {
|
|
210
|
+
// Read the stored origin URL straight from config: `git remote
|
|
211
|
+
// get-url` would apply url.*.insteadOf rewrites, which can turn a
|
|
212
|
+
// GitHub URL into a transport/mirror URL that no longer names the repo.
|
|
213
|
+
const originUrl = await git('config', '--get', 'remote.origin.url')
|
|
214
|
+
const parsed = parseRepoFromUrl(originUrl)
|
|
215
|
+
if (parsed && process.env.GIT_PAT) {
|
|
216
|
+
openPr = await findOpenPr(parsed.owner, parsed.repo, current)
|
|
217
|
+
}
|
|
218
|
+
} catch (err) {
|
|
219
|
+
openPrError = String((err as any)?.message ?? err)
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
remoteState.openPr = openPr
|
|
223
|
+
remoteState.openPrError = openPrError
|
|
105
224
|
} else {
|
|
106
|
-
|
|
107
|
-
|
|
225
|
+
const branch = getBranch()
|
|
226
|
+
await git('fetch', 'origin', branch)
|
|
227
|
+
await syncWorkingBranch(branch)
|
|
108
228
|
}
|
|
229
|
+
|
|
230
|
+
remoteState.lastCheckedAt = Date.now()
|
|
231
|
+
remoteState.error = undefined
|
|
109
232
|
} catch (err) {
|
|
110
233
|
remoteState.error = String((err as any)?.message ?? err)
|
|
111
234
|
remoteState.lastCheckedAt = Date.now()
|
|
@@ -113,7 +236,17 @@ async function checkRemote(): Promise<void> {
|
|
|
113
236
|
}
|
|
114
237
|
|
|
115
238
|
const REMOTE_CHECK_THROTTLE_MS = 60_000
|
|
116
|
-
let checkInFlight =
|
|
239
|
+
let checkInFlight: Promise<void> | null = null
|
|
240
|
+
|
|
241
|
+
/** Start a remote check if none is running; returns the shared promise. */
|
|
242
|
+
function runRemoteCheck(): Promise<void> {
|
|
243
|
+
if (!checkInFlight) {
|
|
244
|
+
checkInFlight = checkRemote().finally(() => {
|
|
245
|
+
checkInFlight = null
|
|
246
|
+
})
|
|
247
|
+
}
|
|
248
|
+
return checkInFlight
|
|
249
|
+
}
|
|
117
250
|
|
|
118
251
|
/**
|
|
119
252
|
* Fire-and-forget remote check, called from the hot path (`/status`,
|
|
@@ -121,13 +254,18 @@ let checkInFlight = false
|
|
|
121
254
|
* a burst of UI requests doesn't flood `git fetch`.
|
|
122
255
|
*/
|
|
123
256
|
function checkRemoteIfStale(): void {
|
|
124
|
-
if (checkInFlight) return
|
|
125
257
|
const last = remoteState.lastCheckedAt ?? 0
|
|
126
258
|
if (Date.now() - last < REMOTE_CHECK_THROTTLE_MS) return
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
259
|
+
void runRemoteCheck()
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Await a fresh remote check (the UI's refresh button). Reuses an in-flight
|
|
264
|
+
* check; otherwise starts one even inside the throttle window, so external
|
|
265
|
+
* changes (a merged PR, a push to main) show up in the very next response.
|
|
266
|
+
*/
|
|
267
|
+
async function awaitRemoteCheck(): Promise<void> {
|
|
268
|
+
await runRemoteCheck()
|
|
131
269
|
}
|
|
132
270
|
|
|
133
271
|
export const gitRoutes = new Hono()
|
|
@@ -174,12 +312,42 @@ function parsePorcelainZ(raw: string): PorcelainEntry[] {
|
|
|
174
312
|
return out
|
|
175
313
|
}
|
|
176
314
|
|
|
315
|
+
/**
|
|
316
|
+
* Git + PR state for the review UI, present in `/status` when PR-based edits
|
|
317
|
+
* are enabled. `openPr` is filled by the GitHub client (see
|
|
318
|
+
* docs/pr-based-edits.md, phase 3); until then it is always null.
|
|
319
|
+
*/
|
|
320
|
+
interface BranchInfo {
|
|
321
|
+
prMode: boolean
|
|
322
|
+
currentBranch: string
|
|
323
|
+
baseBranch: string
|
|
324
|
+
onBaseBranch: boolean
|
|
325
|
+
aheadOfBase: number
|
|
326
|
+
behindBase: number
|
|
327
|
+
/**
|
|
328
|
+
* Commits not yet on the remote working branch. Drives the push
|
|
329
|
+
* affordance: aheadOfBase stays > 0 until the PR merges, so it cannot
|
|
330
|
+
* tell us whether there is anything left to push. 0 on the base branch.
|
|
331
|
+
*/
|
|
332
|
+
unpushed: number
|
|
333
|
+
/** Subject of HEAD; the review UI offers it as the default PR title. */
|
|
334
|
+
lastCommitSubject: string
|
|
335
|
+
openPr: GitHubPr | null
|
|
336
|
+
openPrError?: string
|
|
337
|
+
}
|
|
338
|
+
|
|
177
339
|
gitRoutes.get('/status', async (c) => {
|
|
178
340
|
// Piggyback a throttled remote check on the most-called endpoint. Non
|
|
179
341
|
// blocking: the request returns immediately with whatever we know now;
|
|
180
342
|
// the follow-up fetch updates `lastPulledAt` and the UI detects the
|
|
181
|
-
// change on its next status fetch.
|
|
182
|
-
|
|
343
|
+
// change on its next status fetch. `?force=1` (the UI's refresh button)
|
|
344
|
+
// waits for a fresh check instead, so e.g. a PR merged on GitHub is
|
|
345
|
+
// reflected immediately rather than after the next throttle window.
|
|
346
|
+
if (c.req.query('force') === '1') {
|
|
347
|
+
await awaitRemoteCheck()
|
|
348
|
+
} else {
|
|
349
|
+
checkRemoteIfStale()
|
|
350
|
+
}
|
|
183
351
|
try {
|
|
184
352
|
const raw = await git('status', '--porcelain', '-z', '-uall')
|
|
185
353
|
const files = parsePorcelainZ(raw)
|
|
@@ -199,7 +367,45 @@ gitRoutes.get('/status', async (c) => {
|
|
|
199
367
|
// Sort by path so the row order stays stable when a file toggles
|
|
200
368
|
// between staged and unstaged (git's native output groups by status).
|
|
201
369
|
.sort((a, b) => a.path.localeCompare(b.path))
|
|
202
|
-
|
|
370
|
+
|
|
371
|
+
const settings = await getGitSettings()
|
|
372
|
+
let branch: BranchInfo | undefined
|
|
373
|
+
if (settings.prBasedEdits) {
|
|
374
|
+
const current = await getCurrentBranch()
|
|
375
|
+
// Ahead/behind are derived from local refs at response time instead of
|
|
376
|
+
// being cached by the background check: HEAD moves on every commit, so
|
|
377
|
+
// a cached count would lag one status call behind (the check that
|
|
378
|
+
// refreshes it only finishes after this response is sent). The
|
|
379
|
+
// throttled checkRemote() keeps origin/<base> fresh; when the ref does
|
|
380
|
+
// not exist yet (fresh clone, first fetch in flight) both counts are 0.
|
|
381
|
+
const aheadOfBase = await countCommits(`origin/${settings.baseBranch}..HEAD`)
|
|
382
|
+
const behindBase = await countCommits(`HEAD..origin/${settings.baseBranch}`)
|
|
383
|
+
|
|
384
|
+
// A never-pushed branch has no remote ref: everything is unpushed.
|
|
385
|
+
let unpushed = 0
|
|
386
|
+
if (current && current !== settings.baseBranch) {
|
|
387
|
+
unpushed = (await hasRemoteRef(current))
|
|
388
|
+
? await countCommits(`origin/${current}..HEAD`)
|
|
389
|
+
: aheadOfBase
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
branch = {
|
|
393
|
+
prMode: true,
|
|
394
|
+
currentBranch: current,
|
|
395
|
+
baseBranch: settings.baseBranch,
|
|
396
|
+
onBaseBranch: current === settings.baseBranch,
|
|
397
|
+
aheadOfBase,
|
|
398
|
+
behindBase,
|
|
399
|
+
unpushed,
|
|
400
|
+
lastCommitSubject: await getLastCommitSubject(),
|
|
401
|
+
openPr: remoteState.openPr,
|
|
402
|
+
...(remoteState.openPrError
|
|
403
|
+
? { openPrError: remoteState.openPrError }
|
|
404
|
+
: {}),
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
return c.json({ files, remote: remoteSnapshot(), ...(branch ? { branch } : {}) })
|
|
203
409
|
} catch (err) {
|
|
204
410
|
return c.json({ error: String(err) }, 500)
|
|
205
411
|
}
|
|
@@ -321,15 +527,40 @@ gitRoutes.post('/commit', async (c) => {
|
|
|
321
527
|
return c.json({ error: 'Missing commit message' }, 400)
|
|
322
528
|
}
|
|
323
529
|
|
|
530
|
+
const settings = await getGitSettings()
|
|
531
|
+
let currentBranch: string | undefined
|
|
532
|
+
if (settings.prBasedEdits) {
|
|
533
|
+
currentBranch = await getCurrentBranch()
|
|
534
|
+
if (!currentBranch) {
|
|
535
|
+
return c.json(
|
|
536
|
+
{ error: 'Detached HEAD: check out a branch before committing.' },
|
|
537
|
+
400
|
|
538
|
+
)
|
|
539
|
+
}
|
|
540
|
+
if (currentBranch === settings.baseBranch) {
|
|
541
|
+
return c.json(
|
|
542
|
+
{
|
|
543
|
+
error: `PR-based edits are enabled, so the CMS never commits on '${settings.baseBranch}'. Create a working branch first.`,
|
|
544
|
+
},
|
|
545
|
+
400
|
|
546
|
+
)
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
324
550
|
try {
|
|
325
551
|
const commitOutput = await git('commit', '-m', body.message)
|
|
326
552
|
|
|
327
553
|
let pushOutput = ''
|
|
328
554
|
if (body.push) {
|
|
329
555
|
await ensureAuthedRemote()
|
|
330
|
-
|
|
556
|
+
const branch = settings.prBasedEdits ? currentBranch! : getBranch()
|
|
557
|
+
pushOutput = await git('push', 'origin', branch)
|
|
331
558
|
}
|
|
332
559
|
|
|
560
|
+
// A commit moves HEAD, so cached ahead/behind counts are stale; force a
|
|
561
|
+
// fresh remote check on the next status poll (drives the push affordance).
|
|
562
|
+
remoteState.lastCheckedAt = null
|
|
563
|
+
|
|
333
564
|
return c.json({ ok: true, commit: commitOutput, push: pushOutput })
|
|
334
565
|
} catch (err) {
|
|
335
566
|
return c.json({ error: String(err) }, 500)
|
|
@@ -350,7 +581,22 @@ gitRoutes.post('/pull', async (c) => {
|
|
|
350
581
|
400
|
|
351
582
|
)
|
|
352
583
|
}
|
|
353
|
-
const
|
|
584
|
+
const settings = await getGitSettings()
|
|
585
|
+
let branch = getBranch()
|
|
586
|
+
if (settings.prBasedEdits) {
|
|
587
|
+
const current = await getCurrentBranch()
|
|
588
|
+
if (!current || current === settings.baseBranch) {
|
|
589
|
+
return c.json(
|
|
590
|
+
{
|
|
591
|
+
ok: false,
|
|
592
|
+
error: `PR-based edits are enabled; pull a working branch, not '${settings.baseBranch}'.`,
|
|
593
|
+
},
|
|
594
|
+
400
|
|
595
|
+
)
|
|
596
|
+
}
|
|
597
|
+
branch = current
|
|
598
|
+
}
|
|
599
|
+
const out = await git('pull', '--ff-only', 'origin', branch)
|
|
354
600
|
remoteState.behind = 0
|
|
355
601
|
remoteState.updateAvailable = false
|
|
356
602
|
remoteState.lastPulledAt = Date.now()
|
|
@@ -360,6 +606,250 @@ gitRoutes.post('/pull', async (c) => {
|
|
|
360
606
|
}
|
|
361
607
|
})
|
|
362
608
|
|
|
609
|
+
// Create a working branch from the latest base (PR-based edits only).
|
|
610
|
+
gitRoutes.post('/branch', async (c) => {
|
|
611
|
+
const body = await c.req.json<{ name?: string }>()
|
|
612
|
+
const name = (body.name ?? '').trim()
|
|
613
|
+
if (!name) {
|
|
614
|
+
return c.json({ error: 'Missing branch name' }, 400)
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
const settings = await getGitSettings()
|
|
618
|
+
if (!settings.prBasedEdits) {
|
|
619
|
+
return c.json(
|
|
620
|
+
{ error: 'PR-based edits are disabled; enable git.prBasedEdits in astrocms.json.' },
|
|
621
|
+
400
|
|
622
|
+
)
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
try {
|
|
626
|
+
// The name is passed as a positional argument to git below, so reject
|
|
627
|
+
// option-like input up front; check-ref-format rejects the rest.
|
|
628
|
+
if (name.startsWith('-')) {
|
|
629
|
+
return c.json({ error: `Invalid branch name '${name}'` }, 400)
|
|
630
|
+
}
|
|
631
|
+
try {
|
|
632
|
+
await git('check-ref-format', '--branch', name)
|
|
633
|
+
} catch {
|
|
634
|
+
return c.json({ error: `Invalid branch name '${name}'` }, 400)
|
|
635
|
+
}
|
|
636
|
+
if (name === settings.baseBranch) {
|
|
637
|
+
return c.json(
|
|
638
|
+
{ error: `Cannot create a branch with the base branch name '${settings.baseBranch}'.` },
|
|
639
|
+
400
|
|
640
|
+
)
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
const clean = await isWorktreeClean()
|
|
644
|
+
if (!clean) {
|
|
645
|
+
return c.json(
|
|
646
|
+
{ ok: false, error: 'Working tree is not clean. Commit or discard changes first.' },
|
|
647
|
+
400
|
|
648
|
+
)
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
// Refuse to clobber an existing local branch.
|
|
652
|
+
try {
|
|
653
|
+
await git('show-ref', '--verify', '--quiet', `refs/heads/${name}`)
|
|
654
|
+
return c.json({ error: `Branch '${name}' already exists.` }, 409)
|
|
655
|
+
} catch {
|
|
656
|
+
// No such local branch: proceed.
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
await ensureAuthedRemote()
|
|
660
|
+
// Start from the freshly fetched base so the branch never lags behind.
|
|
661
|
+
await git('fetch', 'origin', settings.baseBranch)
|
|
662
|
+
await git('switch', '-c', name, `origin/${settings.baseBranch}`)
|
|
663
|
+
|
|
664
|
+
// The branch switch invalidates the cached ahead/behind counts; force a
|
|
665
|
+
// fresh remote check on the next status poll.
|
|
666
|
+
remoteState.lastCheckedAt = null
|
|
667
|
+
|
|
668
|
+
return c.json({ ok: true, branch: name })
|
|
669
|
+
} catch (err) {
|
|
670
|
+
return c.json({ error: String((err as any)?.message ?? err) }, 500)
|
|
671
|
+
}
|
|
672
|
+
})
|
|
673
|
+
|
|
674
|
+
// Merge the latest base into the current working branch (PR-based edits only).
|
|
675
|
+
gitRoutes.post('/branch/update', async (c) => {
|
|
676
|
+
const settings = await getGitSettings()
|
|
677
|
+
if (!settings.prBasedEdits) {
|
|
678
|
+
return c.json(
|
|
679
|
+
{ error: 'PR-based edits are disabled; enable git.prBasedEdits in astrocms.json.' },
|
|
680
|
+
400
|
|
681
|
+
)
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
try {
|
|
685
|
+
const current = await getCurrentBranch()
|
|
686
|
+
if (!current) {
|
|
687
|
+
return c.json(
|
|
688
|
+
{ error: 'Detached HEAD: check out a working branch first.' },
|
|
689
|
+
400
|
|
690
|
+
)
|
|
691
|
+
}
|
|
692
|
+
if (current === settings.baseBranch) {
|
|
693
|
+
return c.json(
|
|
694
|
+
{ error: `You are on the base branch '${settings.baseBranch}'; update a working branch instead.` },
|
|
695
|
+
400
|
|
696
|
+
)
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
const clean = await isWorktreeClean()
|
|
700
|
+
if (!clean) {
|
|
701
|
+
return c.json(
|
|
702
|
+
{ ok: false, error: 'Working tree is not clean. Commit or discard changes first.' },
|
|
703
|
+
400
|
|
704
|
+
)
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
await ensureAuthedRemote()
|
|
708
|
+
await git('fetch', 'origin', settings.baseBranch)
|
|
709
|
+
|
|
710
|
+
// Merge (not rebase): it never rewrites pushed history, so an open PR
|
|
711
|
+
// doesn't need a force-push. On conflict the merge is left in progress:
|
|
712
|
+
// the review UI shows the conflicted files, and resolving (or discarding)
|
|
713
|
+
// them plus a commit concludes the merge.
|
|
714
|
+
try {
|
|
715
|
+
const out = await git('merge', `origin/${settings.baseBranch}`)
|
|
716
|
+
return c.json({ ok: true, updated: !/already up to date/i.test(out) })
|
|
717
|
+
} catch (err: any) {
|
|
718
|
+
const message = String(err?.message ?? err)
|
|
719
|
+
return c.json(
|
|
720
|
+
{ ok: false, error: `Merge conflict while updating from '${settings.baseBranch}': ${message}` },
|
|
721
|
+
409
|
|
722
|
+
)
|
|
723
|
+
}
|
|
724
|
+
} catch (err) {
|
|
725
|
+
return c.json({ error: String((err as any)?.message ?? err) }, 500)
|
|
726
|
+
}
|
|
727
|
+
})
|
|
728
|
+
|
|
729
|
+
/**
|
|
730
|
+
* PR body: the list of files changed on the branch (three-dot diff against
|
|
731
|
+
* the base, so only this branch's commits count) plus a fixed footer.
|
|
732
|
+
* `nameStatusOutput` is the output of
|
|
733
|
+
* `git diff --name-status origin/<base>...HEAD`; renames (R*) carry the new
|
|
734
|
+
* path as their last field.
|
|
735
|
+
*/
|
|
736
|
+
function buildPrBody(nameStatusOutput: string): string {
|
|
737
|
+
const lines = nameStatusOutput.split('\n').filter((l) => l.trim())
|
|
738
|
+
const fileList = lines.length
|
|
739
|
+
? lines
|
|
740
|
+
.map((line) => {
|
|
741
|
+
const [status, ...paths] = line.split('\t')
|
|
742
|
+
return `- ${status} ${paths[paths.length - 1]}`
|
|
743
|
+
})
|
|
744
|
+
.join('\n')
|
|
745
|
+
: '- (no file changes)'
|
|
746
|
+
return `## Changed files\n\n${fileList}\n\n---\nCreated with AstroCMS`
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
// Push the working branch and open its pull request (or reuse an existing
|
|
750
|
+
// one). PR-based edits only; non-PR mode keeps using /commit?push=true.
|
|
751
|
+
gitRoutes.post('/push', async (c) => {
|
|
752
|
+
const body = await c.req.json<{ title?: string }>()
|
|
753
|
+
|
|
754
|
+
const settings = await getGitSettings()
|
|
755
|
+
if (!settings.prBasedEdits) {
|
|
756
|
+
return c.json(
|
|
757
|
+
{ error: 'PR-based edits are disabled; enable git.prBasedEdits in astrocms.json.' },
|
|
758
|
+
400
|
|
759
|
+
)
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
const current = await getCurrentBranch()
|
|
763
|
+
if (!current) {
|
|
764
|
+
return c.json(
|
|
765
|
+
{ error: 'Detached HEAD: check out a working branch first.' },
|
|
766
|
+
400
|
|
767
|
+
)
|
|
768
|
+
}
|
|
769
|
+
if (current === settings.baseBranch) {
|
|
770
|
+
return c.json(
|
|
771
|
+
{ error: `PR-based edits are enabled, so the CMS never pushes '${settings.baseBranch}'. Push a working branch instead.` },
|
|
772
|
+
400
|
|
773
|
+
)
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
try {
|
|
777
|
+
await ensureAuthedRemote()
|
|
778
|
+
|
|
779
|
+
// Resolve the GitHub repository from origin's URL before pushing, so a
|
|
780
|
+
// missing title or unparseable origin doesn't leave a stray push behind.
|
|
781
|
+
// Read the stored URL from config (not `git remote get-url`, which applies
|
|
782
|
+
// url.*.insteadOf rewrites that could hide the GitHub origin).
|
|
783
|
+
const originUrl = await git('config', '--get', 'remote.origin.url')
|
|
784
|
+
const parsed = parseRepoFromUrl(originUrl)
|
|
785
|
+
if (!parsed) {
|
|
786
|
+
return c.json(
|
|
787
|
+
{ error: 'PR-based edits require a GitHub origin (https://github.com/<owner>/<repo>).' },
|
|
788
|
+
400
|
|
789
|
+
)
|
|
790
|
+
}
|
|
791
|
+
if (!process.env.GIT_PAT) {
|
|
792
|
+
return c.json(
|
|
793
|
+
{ error: 'GIT_PAT is required to create pull requests.' },
|
|
794
|
+
400
|
|
795
|
+
)
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
const open = await findOpenPr(parsed.owner, parsed.repo, current)
|
|
799
|
+
if (!open) {
|
|
800
|
+
const title = (body.title ?? '').trim()
|
|
801
|
+
if (!title) {
|
|
802
|
+
return c.json({ error: 'Missing PR title' }, 400)
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
await git('push', '-u', 'origin', current)
|
|
807
|
+
|
|
808
|
+
if (open) {
|
|
809
|
+
// An open PR already exists: the push is enough. Cache it so the
|
|
810
|
+
// status refetch right after this call already shows the PR link.
|
|
811
|
+
remoteState.openPr = open
|
|
812
|
+
remoteState.openPrError = undefined
|
|
813
|
+
remoteState.lastCheckedAt = null
|
|
814
|
+
return c.json({ ok: true, pushed: true, pr: open })
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
await git('fetch', 'origin', settings.baseBranch)
|
|
818
|
+
const changed = await git(
|
|
819
|
+
'diff',
|
|
820
|
+
'--name-status',
|
|
821
|
+
`origin/${settings.baseBranch}...HEAD`
|
|
822
|
+
)
|
|
823
|
+
|
|
824
|
+
try {
|
|
825
|
+
const pr = await createPullRequest(parsed.owner, parsed.repo, {
|
|
826
|
+
title: (body.title ?? '').trim(),
|
|
827
|
+
body: buildPrBody(changed),
|
|
828
|
+
head: current,
|
|
829
|
+
base: settings.baseBranch,
|
|
830
|
+
})
|
|
831
|
+
// Cache the new PR so the status refetch right after this call already
|
|
832
|
+
// shows the link instead of waiting for the next background lookup.
|
|
833
|
+
remoteState.openPr = pr
|
|
834
|
+
remoteState.openPrError = undefined
|
|
835
|
+
remoteState.lastCheckedAt = null
|
|
836
|
+
return c.json({ ok: true, pushed: true, created: true, pr })
|
|
837
|
+
} catch (err) {
|
|
838
|
+
// The push already succeeded; report both facts so the UI can show
|
|
839
|
+
// that the branch is up while PR creation failed.
|
|
840
|
+
return c.json(
|
|
841
|
+
{ ok: false, pushed: true, error: String((err as any)?.message ?? err) },
|
|
842
|
+
502
|
|
843
|
+
)
|
|
844
|
+
}
|
|
845
|
+
} catch (err) {
|
|
846
|
+
return c.json(
|
|
847
|
+
{ ok: false, pushed: false, error: String((err as any)?.message ?? err) },
|
|
848
|
+
500
|
|
849
|
+
)
|
|
850
|
+
}
|
|
851
|
+
})
|
|
852
|
+
|
|
363
853
|
// Discard a specific hunk (single @@ block) from the working tree.
|
|
364
854
|
// The hunk comes from `git diff HEAD`, so it can span both staged and
|
|
365
855
|
// unstaged changes. We unstage the file first so the worktree is the
|