cc-haha/scripts/pr/changed-files.ts
程序员阿江(Relakkes) fa47adc33a fix: prevent upgrade crashes from stale persistence
Desktop users can carry provider indexes, managed settings, localStorage state, and native update state from builds that no longer match current readers. This adds startup migrations and recovery paths before server and React state are consumed, plus a persistence upgrade gate so future storage protocol changes ship with old-format fixtures.

Constraint: Existing installs may contain malformed or legacy JSON/localStorage that must not block startup.
Constraint: Local verify should evaluate the current worktree diff rather than unrelated detached-worktree history.
Rejected: Treat invalid persisted state as fatal | reproduces white-screen and startup failure behavior for existing users.
Rejected: Bypass PR policy locally | hides real gate behavior and does not fix detached-worktree false positives.
Confidence: high
Scope-risk: moderate
Directive: Any local JSON, localStorage, or app config shape change must add a migration fixture and keep `bun run check:persistence-upgrade` green.
Tested: bun run check:persistence-upgrade; bun run check:policy; bun run check:desktop; bun run check:server; bun run check:native; bun run verify (9 passed, 1 coverage baseline failure)
Not-tested: Live provider baseline; existing user configs beyond covered fixtures
2026-05-06 23:20:21 +08:00

71 lines
1.9 KiB
TypeScript

async function output(cmd: string[]) {
const proc = Bun.spawn(cmd, {
stdout: 'pipe',
stderr: 'pipe',
})
const stdout = await new Response(proc.stdout).text()
const stderr = await new Response(proc.stderr).text()
const code = await proc.exited
if (code !== 0) {
throw new Error(stderr || stdout || `Command failed: ${cmd.join(' ')}`)
}
return stdout.trim()
}
async function outputOrEmpty(cmd: string[]) {
try {
return await output(cmd)
} catch {
return ''
}
}
function splitFiles(output: string) {
return output.split(/\r?\n/).filter(Boolean)
}
function unique(files: string[]) {
return [...new Set(files.filter(Boolean))]
}
export async function localChangedFiles() {
const staged = await outputOrEmpty(['git', 'diff', '--name-only', '--cached'])
const unstaged = await outputOrEmpty(['git', 'diff', '--name-only'])
const untracked = await outputOrEmpty(['git', 'ls-files', '--others', '--exclude-standard'])
return unique([
...splitFiles(staged),
...splitFiles(unstaged),
...splitFiles(untracked),
])
}
export async function changedFilesForLocalPrCheck(explicitFiles: string[] = []) {
if (explicitFiles.length > 0) {
return unique(explicitFiles)
}
const localFiles = await localChangedFiles()
const explicitBase = process.env.PR_BASE_REF?.trim()
const branch = await outputOrEmpty(['git', 'branch', '--show-current'])
if (!explicitBase && !branch && localFiles.length > 0) {
return localFiles
}
const base = explicitBase || 'origin/main'
try {
const diff = await output(['git', 'diff', '--name-only', `${base}...HEAD`])
return unique([...splitFiles(diff), ...localFiles])
} catch {
try {
const diff = await output(['git', 'diff', '--name-only', 'main...HEAD'])
return unique([...splitFiles(diff), ...localFiles])
} catch {
return localFiles
}
}
}