import { spawnSync, spawn } from 'child_process' import { createWriteStream, existsSync, realpathSync } from 'fs' import * as fsp from 'fs/promises' import { Readable } from 'stream' import { pipeline } from 'stream/promises' import { join, dirname } from 'path' import { tmpdir } from 'os' import { getRepoRoot, getDirtyPaths, getHeadSha, getBranchName, getMirrorPatch, runGit, hasCommit, isAncestor, countCommitsAhead } from './local-repo.js' import { resolveOpenCodeProjectId } from '@opencode-manager/shared/project-id' import type { MirrorCheckoutState, MirrorCheckoutsResponse, WorktreeSetupResult } from '@opencode-manager/shared/schemas' import type { ManagerApi } from './manager-api.js' import { ManagerApiError, isManagerRouteMissing } from './manager-api.js' import { delay } from './delay.js' const HARDCODED_EXCLUDES = ['node_modules', 'dist', '.next', '.venv', '__pycache__', '.turbo', '.DS_Store', '._*'] const PART_RETRIES = 3 const PART_BACKOFF_MS = [500, 2000, 8000] const MIRROR_GZIP = true function getGitignoreExclusions(repoRoot: string): string[] { const res = spawnSync('git', ['ls-files', '--others', '--ignored', '--exclude-standard', '--directory'], { cwd: repoRoot, encoding: 'utf-8', }) if (res.status !== 0) return [] return (res.stdout ?? '').split('\n').filter((line) => line.length > 0) } async function carryOverIgnored(fromDir: string, toDir: string): Promise { if (!existsSync(fromDir)) return for (const rel of getGitignoreExclusions(fromDir)) { const clean = rel.replace(/\/+$/, '') if (!clean) continue const src = join(fromDir, clean) const dest = join(toDir, clean) if (!existsSync(src) || existsSync(dest)) continue await fsp.mkdir(dirname(dest), { recursive: true }) await fsp.rename(src, dest).catch(() => {}) } } export class MirrorAbort extends Error { constructor(message: string) { super(message) this.name = 'MirrorAbort' } } export interface RemoteRepoSummary { repoId: number name: string projectId: string | null branch: string | null } export interface MirrorPlan { repoRoot: string localProjectId: string matched: RemoteRepoSummary[] } export async function prepareMirror(cwd: string, remotes: RemoteRepoSummary[]): Promise { const repoRoot = getRepoRoot(cwd) if (!repoRoot) throw new MirrorAbort('not in a git repository') const localProjectId = await resolveOpenCodeProjectId(repoRoot) if (!localProjectId) throw new MirrorAbort('could not resolve an OpenCode project id for this repository') const matched = remotes.filter((r) => r.projectId && r.projectId === localProjectId) return { repoRoot, localProjectId, matched } } export function pickMatchedRepo(matched: RemoteRepoSummary[], localBranch: string | null): RemoteRepoSummary | null { const onBranch = matched.filter((r) => localBranch !== null && r.branch === localBranch) if (onBranch.length === 1) return onBranch[0]! return matched.length === 1 ? matched[0]! : null } export interface PushDivergence { serverHead: string | null serverBranch: string | null serverDirty: boolean diverged: boolean lostCommits: number } export async function checkPushDivergence( repoRoot: string, api: ManagerApi, repoId: number, directory?: string, ): Promise { const info = await api.mirrorHead(repoId, directory) return assessPushDivergence(repoRoot, info) } export function assessPushDivergence( repoRoot: string, state: { head: string | null; branch: string | null; dirty: boolean }, ): PushDivergence { const { head: serverHead, branch: serverBranch, dirty: serverDirty } = state const localHead = getHeadSha(repoRoot) if (!serverHead || serverHead === localHead) { return { serverHead, serverBranch, serverDirty, diverged: false, lostCommits: 0 } } if (!hasCommit(repoRoot, serverHead)) { return { serverHead, serverBranch, serverDirty, diverged: true, lostCommits: -1 } } if (localHead && isAncestor(repoRoot, serverHead, localHead)) { return { serverHead, serverBranch, serverDirty, diverged: false, lostCommits: 0 } } const lostCommits = localHead ? countCommitsAhead(repoRoot, localHead, serverHead) : -1 return { serverHead, serverBranch, serverDirty, diverged: true, lostCommits } } export interface MoveDestination { kind: 'in-place' | 'existing-worktree' | 'new-worktree' directory: string | null branch: string reasons: string[] } function checkoutUnusableReasons( repoRoot: string, label: string, state: MirrorCheckoutState, localBranch: string, ): string[] { if (state.branch !== localBranch) return [`${label} is on ${state.branch ?? 'a detached HEAD'}`] return describePushDivergence(assessPushDivergence(repoRoot, state), label) } function worktreeUnusableReasons( repoRoot: string, state: MirrorCheckoutState, ): string[] { return describePushDivergence(assessPushDivergence(repoRoot, state), `the worktree on ${state.branch ?? 'another branch'}`) } function localContainsCommit(repoRoot: string, sha: string): boolean { const localHead = getHeadSha(repoRoot) if (!localHead) return false return hasCommit(repoRoot, sha) && isAncestor(repoRoot, sha, localHead) } export function chooseMoveDestination( repoRoot: string, localBranch: string, checkouts: MirrorCheckoutsResponse, ): MoveDestination { const mainReasons = checkoutUnusableReasons(repoRoot, 'the server checkout', checkouts.main, localBranch) if (mainReasons.length === 0) { return { kind: 'in-place', directory: checkouts.main.directory, branch: localBranch, reasons: [] } } const worktreeReasons: string[] = [] for (const worktree of checkouts.worktrees) { const reasons = worktreeUnusableReasons(repoRoot, worktree) if (reasons.length === 0) { return { kind: 'existing-worktree', directory: worktree.directory, branch: worktree.branch ?? localBranch, reasons: [] } } worktreeReasons.push(...reasons) } const branchHeadMissing = checkouts.branchHead !== null && !localContainsCommit(repoRoot, checkouts.branchHead) const useSuffix = checkouts.branchCheckedOut || branchHeadMissing const reasons = [ ...mainReasons, ...worktreeReasons, ...(branchHeadMissing ? [`the server branch ${localBranch} has commits not in your local branch`] : []), ] return { kind: 'new-worktree', directory: null, branch: useSuffix ? checkouts.suffixedWorktreeBranch : localBranch, reasons, } } export function describePushDivergence(div: PushDivergence, subject = 'the server'): string[] { const reasons: string[] = [] if (div.diverged) { reasons.push(div.lostCommits >= 0 ? `${subject} is ${div.lostCommits} commit(s) ahead of your local branch` : `${subject} has commit(s) not present in your local branch`) } if (div.serverDirty) reasons.push(`${subject} has uncommitted changes`) return reasons } export interface PullDivergence { diverged: boolean lostCommits: number serverHead: string | null } export async function checkPullDivergence( repoRoot: string, api: ManagerApi, repoId: number, directory?: string, ): Promise { const localHead = getHeadSha(repoRoot) if (!localHead) return { diverged: false, lostCommits: 0, serverHead: null } const { contained } = await api.mirrorContains(repoId, localHead, directory) if (contained) return { diverged: false, lostCommits: 0, serverHead: null } let serverHead: string | null = null let lostCommits = -1 try { serverHead = (await api.mirrorHead(repoId, directory)).head if (serverHead) lostCommits = countCommitsAhead(repoRoot, serverHead, localHead) } catch { // best-effort: count is informational only } return { diverged: true, lostCommits, serverHead } } export interface BranchCheckoutSelection { directory?: string targetBranch?: string } export async function resolveBranchCheckout( api: ManagerApi, repoId: number, repoRoot: string, ): Promise { const localBranch = getBranchName(repoRoot) if (!localBranch) return {} let checkouts: MirrorCheckoutsResponse try { checkouts = await api.mirrorCheckouts(repoId, localBranch) } catch (error) { if (isManagerRouteMissing(error) || (error instanceof ManagerApiError && error.status === 410)) return {} throw error } if (checkouts.main.branch === localBranch) return {} const worktree = checkouts.worktrees[0] if (!worktree) return {} return { directory: worktree.directory, targetBranch: worktree.branch !== null && worktree.branch !== localBranch ? worktree.branch : undefined, } } export interface MirrorProgress { bytesSent: number } export interface MirrorUpOpts { api: ManagerApi force: boolean create?: { name: string; originUrl: string | null; branch: string | null } onProgress?: (p: MirrorProgress) => void } function isRetryablePartError(err: unknown): boolean { if (err instanceof ManagerApiError) { return err.status >= 500 || err.status === 408 || err.status === 429 } return true } async function uploadPartWithRetry( api: ManagerApi, repoId: number, uploadId: string, index: number, chunk: Buffer, ): Promise { let lastError: unknown for (let attempt = 0; attempt < PART_RETRIES; attempt++) { try { await api.mirrorUploadPart(repoId, uploadId, index, chunk) return } catch (err) { lastError = err if (!isRetryablePartError(err)) break if (attempt < PART_RETRIES - 1) { await delay(PART_BACKOFF_MS[attempt]!) } } } throw lastError instanceof Error ? lastError : new Error(`part ${index} failed: ${String(lastError)}`) } interface PartFlusher { push(buf: Buffer): Promise finish(): Promise } function createPartFlusher( api: ManagerApi, repoId: number, uploadId: string, chunkSize: number, ): PartFlusher { let acc: Buffer[] = [] let accLen = 0 let index = 0 const flush = async (): Promise => { if (accLen === 0) return const buf = Buffer.concat(acc, accLen) acc = [] accLen = 0 await uploadPartWithRetry(api, repoId, uploadId, index, buf) index += 1 } return { async push(buf: Buffer): Promise { let offset = 0 while (offset < buf.length) { const room = chunkSize - accLen const take = Math.min(room, buf.length - offset) acc.push(buf.subarray(offset, offset + take)) accLen += take offset += take if (accLen >= chunkSize) { await flush() } } }, async finish(): Promise { await flush() return index }, } } export async function mirrorUp( plan: MirrorPlan, opts: MirrorUpOpts, ): Promise<{ repoId: number; branch: string | null; head: string | null; created: boolean }> { const repoId = opts.create ? 0 : plan.matched[0]!.repoId const begin = await opts.api.mirrorBegin(repoId, { force: opts.force, create: opts.create }) let bytesSent = 0 const tarArgs = ['-c', '-C', plan.repoRoot] if (MIRROR_GZIP) tarArgs.unshift('-z') for (const dir of HARDCODED_EXCLUDES) tarArgs.push('--exclude', dir) const ignoredPaths = getGitignoreExclusions(plan.repoRoot) let excludeFile: string | null = null if (ignoredPaths.length > 0) { excludeFile = join(tmpdir(), `ocm-exclude-${Date.now()}-${Math.random().toString(36).slice(2)}.txt`) await fsp.writeFile(excludeFile, ignoredPaths.join('\n')) tarArgs.push('--exclude-from', excludeFile) } tarArgs.push('.') const child = spawn('tar', tarArgs, { stdio: ['pipe', 'pipe', 'pipe'], env: { ...process.env, COPYFILE_DISABLE: '1' }, }) const stderrChunks: Buffer[] = [] child.stderr.on('data', (chunk: Buffer) => stderrChunks.push(chunk)) const tarExit = new Promise((resolve, reject) => { child.on('close', (code) => { if (code === 0) resolve() else { const stderr = Buffer.concat(stderrChunks).toString('utf-8').trim() reject(new Error(`tar exited with code ${code}${stderr ? `: ${stderr}` : ''}`)) } }) child.on('error', reject) }) const flusher = createPartFlusher(opts.api, begin.repoId, begin.uploadId, begin.chunkSize) try { for await (const chunk of child.stdout as AsyncIterable) { await flusher.push(chunk) bytesSent += chunk.length opts.onProgress?.({ bytesSent }) } await tarExit const totalParts = await flusher.finish() opts.onProgress?.({ bytesSent }) const result = await opts.api.mirrorCommit(begin.repoId, begin.uploadId, totalParts, MIRROR_GZIP) return result } catch (err) { if (!child.killed) child.kill('SIGKILL') await opts.api.mirrorAbort(begin.repoId, begin.uploadId) throw err } finally { if (excludeFile) { await fsp.rm(excludeFile, { force: true }).catch(() => {}) } } } export async function mirrorDown( repoId: number, repoRoot: string, api: ManagerApi, opts: { force: boolean; onProgress?: (bytesReceived: number) => void } = { force: false }, ): Promise { if (!opts.force && getDirtyPaths(repoRoot).size > 0) { throw new MirrorAbort('working tree has uncommitted changes; rerun with --force') } const staging = `${repoRoot}.ocm-recv-${Date.now()}` await fsp.mkdir(staging, { recursive: true }) try { const tarball = await api.mirrorDown(repoId, MIRROR_GZIP) const tarArgs = ['-x', '-f', '-', '-C', staging] if (MIRROR_GZIP) tarArgs.unshift('-z') const child = spawn('tar', tarArgs, { stdio: ['pipe', 'pipe', 'pipe'] }) const stderrChunks: Buffer[] = [] child.stderr.on('data', (chunk: Buffer) => stderrChunks.push(chunk)) const tarDone = new Promise((resolve, reject) => { child.on('close', (code) => { if (code === 0) resolve() else { const stderr = Buffer.concat(stderrChunks).toString('utf-8').trim() reject(new Error(`tar exited with code ${code}${stderr ? `: ${stderr}` : ''}`)) } }) child.on('error', reject) }) const stdinWritable = Readable.fromWeb(tarball as unknown as Parameters[0]) let received = 0 stdinWritable.on('data', (chunk: Buffer) => { received += chunk.length opts.onProgress?.(received) }) stdinWritable.pipe(child.stdin) await tarDone const backupDir = `${repoRoot}.ocm-backup-${Date.now()}` await fsp.mkdir(backupDir, { recursive: true }) if (existsSync(repoRoot)) { const entries = await fsp.readdir(repoRoot) for (const entry of entries) { await fsp.rename(join(repoRoot, entry), join(backupDir, entry)) } } try { const stagingEntries = await fsp.readdir(staging) for (const entry of stagingEntries) { await fsp.rename(join(staging, entry), join(repoRoot, entry)) } await carryOverIgnored(backupDir, repoRoot) await fsp.rm(backupDir, { recursive: true, force: true }).catch(() => {}) await fsp.rm(staging, { recursive: true, force: true }).catch(() => {}) } catch (swapError) { const backupEntries = await fsp.readdir(backupDir).catch(() => []) for (const entry of backupEntries) { await fsp.rename(join(backupDir, entry), join(repoRoot, entry)).catch(() => {}) } await fsp.rm(backupDir, { recursive: true, force: true }).catch(() => {}) throw swapError } } catch (error) { await fsp.rm(staging, { recursive: true, force: true }).catch(() => {}) throw error } } export async function mirrorUpPatch( plan: MirrorPlan, opts: Pick & { directory?: string }, ): Promise<{ repoId: number; fullPath: string; branch: string | null; head: string | null; created: false; applied: true }> { const repoId = plan.matched[0]!.repoId const patch = getMirrorPatch(plan.repoRoot) return opts.api.mirrorPatch(repoId, { baseHead: getHeadSha(plan.repoRoot), patch, force: opts.force, directory: opts.directory }) } function applyPatch(repoRoot: string, patch: string): void { if (!patch) return const child = spawnSync('git', ['apply', '--binary', '--whitespace=nowarn', '-'], { cwd: repoRoot, input: patch, encoding: 'utf-8', }) if (child.status !== 0) { const stderr = (child.stderr ?? '').trim() throw new Error(`git apply failed${stderr ? `: ${stderr}` : ''}`) } } async function createLocalBundle(repoRoot: string): Promise { const bundlePath = join(tmpdir(), `ocm-bundle-${Date.now()}-${Math.random().toString(36).slice(2)}.bundle`) await new Promise((resolve, reject) => { const child = spawn('git', ['bundle', 'create', bundlePath, '--all'], { cwd: repoRoot }) const stderrChunks: Buffer[] = [] child.stderr.on('data', (chunk: Buffer) => stderrChunks.push(chunk)) child.on('error', reject) child.on('close', (code) => { if (code === 0) return resolve() const stderr = Buffer.concat(stderrChunks).toString('utf-8').trim() reject(new Error(`git bundle create failed${stderr ? `: ${stderr}` : ''}`)) }) }) return bundlePath } function listWorktreeBranches(repoRoot: string): Map { const out = runGit(repoRoot, ['worktree', 'list', '--porcelain']) const ownership = new Map() let currentPath: string | null = null for (const line of out.split('\n')) { if (line.startsWith('worktree ')) { currentPath = line.slice('worktree '.length).trim() } else if (line.startsWith('branch ')) { if (!currentPath) continue const branch = line.slice('branch '.length).trim().replace(/^refs\/heads\//, '') const paths = ownership.get(branch) ?? [] paths.push(currentPath) ownership.set(branch, paths) } } return ownership } function listLocalBranches(repoRoot: string): Set { const out = runGit(repoRoot, ['for-each-ref', '--format=%(refname:short)', 'refs/heads']) return new Set(out.split('\n').map((l) => l.trim()).filter(Boolean)) } function canonicalPath(path: string): string { try { return realpathSync(path) } catch { return path } } function branchOwnedElsewhere(ownership: Map, repoRoot: string): Set { const canonicalRoot = canonicalPath(repoRoot) const locked = new Set() for (const [branch, paths] of ownership) { if (paths.some((path) => canonicalPath(path) !== canonicalRoot)) locked.add(branch) } return locked } function deleteSyncRefs(repoRoot: string): void { try { const syncRefsOut = runGit(repoRoot, ['for-each-ref', '--format=%(refname)', 'refs/remotes/ocm-sync']) const deletes = syncRefsOut.split('\n').map((l) => l.trim()).filter(Boolean).map((ref) => `delete ${ref}\n`) if (deletes.length > 0) { runGit(repoRoot, ['update-ref', '--stdin'], deletes.join('')) } } catch { return } } function importLocalBundle( repoRoot: string, bundlePath: string, branch: string | null, force: boolean, sourceBranch?: string, ): void { runGit(repoRoot, ['fetch', bundlePath, '+refs/heads/*:refs/remotes/ocm-sync/*', '+refs/tags/*:refs/tags/*']) try { const refs = runGit(repoRoot, ['for-each-ref', '--format=%(refname:strip=3) %(objectname)', 'refs/remotes/ocm-sync']) const incoming = new Map() for (const line of refs.split('\n')) { const trimmed = line.trim() if (!trimmed) continue const firstSpace = trimmed.indexOf(' ') if (firstSpace === -1) continue const name = trimmed.slice(0, firstSpace) if (name === 'HEAD') continue incoming.set(name, trimmed.slice(firstSpace + 1)) } const lockedElsewhere = branchOwnedElsewhere(listWorktreeBranches(repoRoot), repoRoot) const currentBranch = getBranchName(repoRoot) const source = sourceBranch ?? branch if (branch && source) { const incomingSha = incoming.get(source) if (!incomingSha) throw new MirrorAbort(`no incoming branch '${source}' in the server bundle`) if (lockedElsewhere.has(branch)) { throw new MirrorAbort(`branch '${branch}' is checked out in another worktree; release it there before pulling`) } if (currentBranch !== branch) { const checkoutArgs = force ? ['-f'] : [] if (listLocalBranches(repoRoot).has(branch)) { runGit(repoRoot, ['checkout', ...checkoutArgs, branch]) } else { runGit(repoRoot, ['checkout', ...checkoutArgs, '-b', branch, incomingSha]) } } } const skipRefs = new Set() if (branch) skipRefs.add(branch) if (source) skipRefs.add(source) const updates: string[] = [] for (const [name, sha] of incoming) { if (skipRefs.has(name)) continue if (lockedElsewhere.has(name)) continue if (branch === null && name === currentBranch) continue updates.push(`update refs/heads/${name} ${sha}\n`) } if (updates.length > 0) { runGit(repoRoot, ['update-ref', '--stdin'], updates.join('')) } if (branch && source) { const targetSha = incoming.get(source)! runGit(repoRoot, ['reset', '--hard', targetSha]) runGit(repoRoot, ['clean', '-fd']) } } finally { deleteSyncRefs(repoRoot) } } async function writeBundleStream(repoId: number, api: ManagerApi, directory?: string): Promise { const bundlePath = join(tmpdir(), `ocm-bundle-down-${Date.now()}-${Math.random().toString(36).slice(2)}.bundle`) const stream = await api.mirrorDownloadBundle(repoId, directory) await pipeline( Readable.fromWeb(stream as unknown as Parameters[0]), createWriteStream(bundlePath), ) return bundlePath } export type MirrorUpFastPhase = | { kind: 'bundling' } | { kind: 'uploading'; bytesSent: number; totalBytes: number } | { kind: 'processing' } | { kind: 'patching' } type MirrorUpFastOpts = Pick & { requireCurrentBranch?: boolean directory?: string targetBranch?: string createWorktree?: { targetBranch: string } onPhase?: (phase: MirrorUpFastPhase) => void } export interface MirrorUpFastResult { repoId: number fullPath: string branch: string | null head: string | null created: boolean worktreeSetup?: WorktreeSetupResult } export async function mirrorUpFast( plan: MirrorPlan, opts: MirrorUpFastOpts, ): Promise { const repoId = plan.matched[0]!.repoId const onPhase = opts.onPhase onPhase?.({ kind: 'bundling' }) const bundlePath = await createLocalBundle(plan.repoRoot) try { const { size } = await fsp.stat(bundlePath) const progressFor = (totalBytes: number) => onPhase ? (bytesSent: number) => { onPhase({ kind: 'uploading', bytesSent, totalBytes }) if (bytesSent >= totalBytes) onPhase({ kind: 'processing' }) } : undefined if (opts.createWorktree) { const branch = getBranchName(plan.repoRoot) if (!branch) throw new MirrorAbort('cannot create a worktree from a detached HEAD') const patch = getMirrorPatch(plan.repoRoot) const totalBytes = size + Buffer.byteLength(patch) onPhase?.({ kind: 'uploading', bytesSent: 0, totalBytes }) const created = await opts.api.mirrorCreateWorktree(repoId, bundlePath, { branch, targetBranch: opts.createWorktree.targetBranch, patch, onProgress: progressFor(totalBytes), }) return { repoId: created.repoId, fullPath: created.fullPath, branch: created.branch, head: created.head, created: true, worktreeSetup: created.worktreeSetup, } } onPhase?.({ kind: 'uploading', bytesSent: 0, totalBytes: size }) const onProgress = progressFor(size) await opts.api.mirrorUploadBundle(repoId, bundlePath, { branch: getBranchName(plan.repoRoot), force: opts.force, requireCurrentBranch: opts.requireCurrentBranch, directory: opts.directory, targetBranch: opts.targetBranch, onProgress, }) onPhase?.({ kind: 'patching' }) const patchResult = await mirrorUpPatch(plan, opts) return { repoId: patchResult.repoId, fullPath: patchResult.fullPath, branch: patchResult.branch, head: patchResult.head, created: false } } finally { await fsp.rm(bundlePath, { force: true }).catch(() => {}) } } export async function mirrorDownFast( repoId: number, repoRoot: string, api: ManagerApi, opts: { force: boolean; directory?: string; sourceBranch?: string; targetBranch?: string } = { force: false }, ): Promise { if (!opts.force && getDirtyPaths(repoRoot).size > 0) { throw new MirrorAbort('working tree has uncommitted changes; rerun with --force') } const snapshot = await api.mirrorPatchSnapshot(repoId, opts.directory) const bundlePath = await writeBundleStream(repoId, api, opts.directory) try { const branch = opts.targetBranch ?? snapshot.branch const sourceBranch = opts.sourceBranch ?? branch ?? undefined importLocalBundle(repoRoot, bundlePath, branch, opts.force, sourceBranch) applyPatch(repoRoot, snapshot.patch) } finally { await fsp.rm(bundlePath, { force: true }).catch(() => {}) } }