mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
fix(ocm): make /ocm-move worktree moves safe and round-trippable
- Answer the old /mirror/target routes with a 410 upgrade message and serve the lookup at GET /mirror/checkouts; detect older Managers via the token probe (MANAGER_FEATURE_MISSING). - Create the move worktree and import the bundle in one call; worktree setup runs after the import and the worktree is removed on failure. - Never rewind a server branch: suffix the new worktree branch when the server branch has commits the local branch lacks, and skip both the source and target branch when updating refs. - Reuse clean <branch>-ocm[-N] worktrees and legacy worktree repo rows instead of creating new ones on every move. - Let ocm push/pull target the worktree on the local branch via a directory parameter on the mirror read routes; no tarball fallback for worktrees. - Share one sibling loader (listRepoSiblings), validate branch names, compare canonical paths in isDirectoryInUse, and cut redundant git work in the checkouts lookup and the CLI destination choice.
This commit is contained in:
1 parent
bd7c3e3fef
commit
67f70e9635
21 files changed
+1777
-517
No files matched your search
@@ -5,16 +5,16 @@ import { copyFileSync, createReadStream, createWriteStream, existsSync } from 'f
|
||||
import { mkdtempSync, writeFileSync } from 'fs'
|
||||
import { Readable } from 'stream'
|
||||
import { pipeline } from 'stream/promises'
|
||||
import { join } from 'path'
|
||||
import { join, resolve } from 'path'
|
||||
import * as fsp from 'fs/promises'
|
||||
import { getReposPath } from '@opencode-manager/shared/config/env'
|
||||
import {
|
||||
MirrorTargetBranchRequestSchema,
|
||||
type MirrorCheckoutState,
|
||||
type MirrorMoveTargetResponse,
|
||||
type MirrorCheckoutsResponse,
|
||||
type MirrorWorktreeCreateResponse,
|
||||
} from '@opencode-manager/shared/schemas'
|
||||
import { isWorktreeSibling, sanitizeRepoDirectoryName } from '@opencode-manager/shared/utils'
|
||||
import { isWorktreeSibling, sanitizeRepoDirectoryName, type RepoSibling } from '@opencode-manager/shared/utils'
|
||||
import type { Repo } from '@opencode-manager/shared/types'
|
||||
import { getRepoById, updateLastPulled, updateRepoBranch, deleteRepo } from '../../db/queries'
|
||||
import {
|
||||
@@ -22,7 +22,7 @@ import {
|
||||
createRepoRow,
|
||||
isDirectoryInUse,
|
||||
isRepoInUse,
|
||||
getSiblingRepos,
|
||||
listRepoSiblings,
|
||||
listGitWorktrees,
|
||||
resolveRepoWorkingDirectory,
|
||||
} from '../../services/repo'
|
||||
@@ -31,7 +31,7 @@ import type { GitAuthService } from '../../services/git-auth'
|
||||
import type { OpenCodeClient } from '../../services/opencode/client'
|
||||
import { logger } from '../../utils/logger'
|
||||
import { getErrorMessage } from '../../utils/error-utils'
|
||||
import { mkdirSyncSafe } from '../../utils/fs-safe'
|
||||
import { canonicalPathSync, mkdirSyncSafe } from '../../utils/fs-safe'
|
||||
import { safeGitOut, gitOut } from './repo-sync-helpers'
|
||||
import {
|
||||
MIRROR_CHUNK_SIZE,
|
||||
@@ -73,6 +73,7 @@ interface PatchBody {
|
||||
}
|
||||
|
||||
const LEGACY_UPGRADE_MESSAGE = 'this ocm CLI is too old for this server; upgrade to ocm-cli >= 0.1.2 (the mirror upload protocol changed to chunked uploads)'
|
||||
const LEGACY_MOVE_UPGRADE_MESSAGE = 'this ocm CLI is too old for this server; upgrade to ocm-cli >= 0.4.0 (/ocm-move now moves sessions into OpenCode worktrees)'
|
||||
|
||||
function gitRaw(repoPath: string, args: string[], env: NodeJS.ProcessEnv = process.env, input?: string): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -142,26 +143,39 @@ async function listLocalBranchNames(fullPath: string): Promise<Set<string>> {
|
||||
return new Set(out.split('\n').map((l) => l.trim()).filter(Boolean))
|
||||
}
|
||||
|
||||
async function isValidBranchName(repoPath: string, name: string): Promise<boolean> {
|
||||
if (name.startsWith('-')) return false
|
||||
return (await safeGitOut(repoPath, ['check-ref-format', '--branch', name])) !== null
|
||||
}
|
||||
|
||||
async function checkoutState(directory: string): Promise<MirrorCheckoutState> {
|
||||
const branch = await safeGitOut(directory, ['symbolic-ref', '--quiet', '--short', 'HEAD'])
|
||||
const head = await safeGitOut(directory, ['rev-parse', 'HEAD'])
|
||||
const status = await safeGitOut(directory, ['status', '--porcelain', '--untracked-files=all'])
|
||||
const [branch, head, status] = await Promise.all([
|
||||
currentBranchName(directory),
|
||||
safeGitOut(directory, ['rev-parse', 'HEAD']),
|
||||
safeGitOut(directory, ['status', '--porcelain', '--untracked-files=all']),
|
||||
])
|
||||
return {
|
||||
directory,
|
||||
branch: branch?.trim() || null,
|
||||
branch,
|
||||
head: head?.trim() || null,
|
||||
dirty: (status?.trim().length ?? 0) > 0,
|
||||
}
|
||||
}
|
||||
|
||||
async function listMirrorWorktreeSiblings(
|
||||
function isMirrorCheckoutSibling(repo: Repo): (sibling: RepoSibling) => boolean {
|
||||
return (sibling) => {
|
||||
if (isWorktreeSibling(sibling)) return sibling.worktreeSource !== 'schedule'
|
||||
return sibling.isWorktree === true && sibling.id > 0 && sibling.id !== repo.id
|
||||
}
|
||||
}
|
||||
|
||||
async function listMirrorCheckoutSiblings(
|
||||
db: Database,
|
||||
repo: Repo,
|
||||
gitAuthService: GitAuthService,
|
||||
openCodeClient: OpenCodeClient,
|
||||
): Promise<Array<{ fullPath: string }>> {
|
||||
const siblings = await getSiblingRepos(db, repo.id, gitAuthService.getGitEnvironment(), openCodeClient, { includeBranch: false })
|
||||
return siblings.filter((sibling) => isWorktreeSibling(sibling) && sibling.worktreeSource !== 'schedule')
|
||||
): Promise<RepoSibling[]> {
|
||||
return listRepoSiblings(db, repo.id, gitAuthService.getGitEnvironment(), openCodeClient, isMirrorCheckoutSibling(repo))
|
||||
}
|
||||
|
||||
async function resolveMirrorDirectory(
|
||||
@@ -172,19 +186,31 @@ async function resolveMirrorDirectory(
|
||||
openCodeClient: OpenCodeClient,
|
||||
): Promise<string | null> {
|
||||
return resolveRepoWorkingDirectory(repo, directory, () =>
|
||||
listMirrorWorktreeSiblings(db, repo, gitAuthService, openCodeClient),
|
||||
listMirrorCheckoutSiblings(db, repo, gitAuthService, openCodeClient),
|
||||
)
|
||||
}
|
||||
|
||||
function nextWorktreeBranch(branch: string, localBranches: Set<string>, checkedOut: Set<string>): string {
|
||||
if (!checkedOut.has(branch)) return branch
|
||||
let candidate = `${branch}-ocm`
|
||||
let suffix = 2
|
||||
while (localBranches.has(candidate) || checkedOut.has(candidate)) {
|
||||
candidate = `${branch}-ocm-${suffix}`
|
||||
function mirrorWorktreeBranchRank(worktreeBranch: string | null, branch: string): number | null {
|
||||
if (worktreeBranch === branch) return 0
|
||||
const prefix = `${branch}-ocm`
|
||||
if (worktreeBranch === prefix) return 1
|
||||
if (!worktreeBranch || !worktreeBranch.startsWith(`${prefix}-`)) return null
|
||||
const suffix = Number(worktreeBranch.slice(prefix.length + 1))
|
||||
return Number.isInteger(suffix) && suffix >= 2 ? suffix : null
|
||||
}
|
||||
|
||||
function nextSuffixedWorktreeBranch(branch: string, localBranches: Set<string>, checkedOut: Set<string>): string {
|
||||
let suffix = 1
|
||||
while (true) {
|
||||
const candidate = suffix === 1 ? `${branch}-ocm` : `${branch}-ocm-${suffix}`
|
||||
if (!localBranches.has(candidate) && !checkedOut.has(candidate)) return candidate
|
||||
suffix += 1
|
||||
}
|
||||
return candidate
|
||||
}
|
||||
|
||||
async function branchHeadSha(fullPath: string, branch: string): Promise<string | null> {
|
||||
const sha = await safeGitOut(fullPath, ['rev-parse', '--verify', '--quiet', `refs/heads/${branch}`])
|
||||
return sha?.trim() || null
|
||||
}
|
||||
|
||||
async function importBundle(fullPath: string, bundlePath: string, branch: string | null, requireCurrentBranch: boolean, force: boolean, targetBranch: string | null = branch): Promise<void> {
|
||||
@@ -227,10 +253,12 @@ async function importBundle(fullPath: string, bundlePath: string, branch: string
|
||||
}
|
||||
}
|
||||
|
||||
const skipRef = targetBranch ?? branch
|
||||
const skipRefs = new Set<string>()
|
||||
if (branch !== null) skipRefs.add(branch)
|
||||
if (targetBranch !== null) skipRefs.add(targetBranch)
|
||||
const updates: string[] = []
|
||||
for (const [name, sha] of incoming) {
|
||||
if (skipRef !== null && name === skipRef) continue
|
||||
if (skipRefs.has(name)) continue
|
||||
if (locked.has(name)) continue
|
||||
updates.push(`update refs/heads/${name} ${sha}\n`)
|
||||
}
|
||||
@@ -260,6 +288,21 @@ async function createBundle(fullPath: string): Promise<string> {
|
||||
return bundlePath
|
||||
}
|
||||
|
||||
async function stageBundleFromBody(body: ReadableStream, dirPrefix: string): Promise<{ bundleDir: string; bundlePath: string }> {
|
||||
const stagingRoot = getStagingRoot()
|
||||
mkdirSyncSafe(stagingRoot)
|
||||
const bundleDir = mkdtempSync(join(stagingRoot, dirPrefix))
|
||||
try {
|
||||
const bundlePath = join(bundleDir, 'repo.bundle')
|
||||
const readable = Readable.fromWeb(body as unknown as Parameters<typeof Readable.fromWeb>[0])
|
||||
await pipeline(readable, createWriteStream(bundlePath))
|
||||
return { bundleDir, bundlePath }
|
||||
} catch (error) {
|
||||
await fsp.rm(bundleDir, { recursive: true, force: true }).catch(() => {})
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export function createInternalRepoMirrorRoutes(
|
||||
db: Database,
|
||||
openCodeClient: OpenCodeClient,
|
||||
@@ -442,9 +485,12 @@ export function createInternalRepoMirrorRoutes(
|
||||
const repo = getRepoById(db, repoId)
|
||||
if (!repo) return c.json({ error: 'repo not found' }, 404)
|
||||
|
||||
const directory = await resolveMirrorDirectory(db, repo, c.req.query('directory'), gitAuthService, openCodeClient)
|
||||
if (!directory) return c.json({ error: 'Directory is not part of this repository' }, 400)
|
||||
|
||||
let bundlePath: string | undefined
|
||||
try {
|
||||
bundlePath = await createBundle(repo.fullPath)
|
||||
bundlePath = await createBundle(directory)
|
||||
const stream = createReadStream(bundlePath)
|
||||
stream.on('close', () => {
|
||||
if (bundlePath) fsp.rm(join(bundlePath, '..'), { recursive: true, force: true }).catch(() => {})
|
||||
@@ -477,42 +523,46 @@ export function createInternalRepoMirrorRoutes(
|
||||
const rawBody = c.req.raw.body
|
||||
if (!rawBody) return c.json({ error: 'no body provided' }, 400)
|
||||
|
||||
const stagingRoot = getStagingRoot()
|
||||
mkdirSyncSafe(stagingRoot)
|
||||
const bundleDir = mkdtempSync(join(stagingRoot, 'bundle-upload-'))
|
||||
const bundlePath = join(bundleDir, 'repo.bundle')
|
||||
const branch = c.req.header('x-ocm-branch')?.trim() || null
|
||||
const targetBranch = c.req.header('x-ocm-target-branch')?.trim() || branch
|
||||
const requireCurrentBranch = c.req.header('x-ocm-require-current-branch')?.trim() === '1'
|
||||
|
||||
let bundleDir: string | undefined
|
||||
try {
|
||||
const body = Readable.fromWeb(rawBody as unknown as Parameters<typeof Readable.fromWeb>[0])
|
||||
await pipeline(body, createWriteStream(bundlePath))
|
||||
await importBundle(directory, bundlePath, branch, requireCurrentBranch, force, targetBranch)
|
||||
const staged = await stageBundleFromBody(rawBody, 'bundle-upload-')
|
||||
bundleDir = staged.bundleDir
|
||||
await importBundle(directory, staged.bundlePath, branch, requireCurrentBranch, force, targetBranch)
|
||||
|
||||
const branchName = await safeGitOut(directory, ['rev-parse', '--abbrev-ref', 'HEAD'])
|
||||
const head = await safeGitOut(directory, ['rev-parse', 'HEAD'])
|
||||
const state = await checkoutState(directory)
|
||||
if (directory === repo.fullPath) {
|
||||
if (branchName) updateRepoBranch(db, repoId, branchName.trim())
|
||||
if (state.branch) updateRepoBranch(db, repoId, state.branch)
|
||||
updateLastPulled(db, repoId)
|
||||
}
|
||||
|
||||
return c.json({
|
||||
repoId,
|
||||
fullPath: directory,
|
||||
branch: branchName?.trim() || null,
|
||||
head: head?.trim() || null,
|
||||
branch: state.branch,
|
||||
head: state.head,
|
||||
created: false,
|
||||
})
|
||||
} catch (error) {
|
||||
logger.error('mirror bundle upload failed:', error)
|
||||
return c.json({ error: getErrorMessage(error) }, 409)
|
||||
} finally {
|
||||
await fsp.rm(bundleDir, { recursive: true, force: true }).catch(() => {})
|
||||
if (bundleDir) await fsp.rm(bundleDir, { recursive: true, force: true }).catch(() => {})
|
||||
}
|
||||
})
|
||||
|
||||
app.get('/:repoId/mirror/target', async (c) => {
|
||||
app.get('/:repoId/mirror/target', (c) => {
|
||||
return c.json({ error: 'cli_too_old', message: LEGACY_MOVE_UPGRADE_MESSAGE }, 410)
|
||||
})
|
||||
|
||||
app.post('/:repoId/mirror/target', (c) => {
|
||||
return c.json({ error: 'cli_too_old', message: LEGACY_MOVE_UPGRADE_MESSAGE }, 410)
|
||||
})
|
||||
|
||||
app.get('/:repoId/mirror/checkouts', async (c) => {
|
||||
const repoId = Number(c.req.param('repoId'))
|
||||
if (!Number.isFinite(repoId)) return c.json({ error: 'invalid repoId' }, 400)
|
||||
const branchParsed = MirrorTargetBranchRequestSchema.safeParse({ branch: c.req.query('branch') })
|
||||
@@ -520,32 +570,48 @@ export function createInternalRepoMirrorRoutes(
|
||||
const { branch } = branchParsed.data
|
||||
const repo = getRepoById(db, repoId)
|
||||
if (!repo) return c.json({ error: 'repo not found' }, 404)
|
||||
if (!(await isValidBranchName(repo.fullPath, branch))) return c.json({ error: 'invalid branch name' }, 400)
|
||||
|
||||
try {
|
||||
const main = await checkoutState(repo.fullPath)
|
||||
const siblings = await listMirrorWorktreeSiblings(db, repo, gitAuthService, openCodeClient)
|
||||
let branchWorktree: MirrorCheckoutState | null = null
|
||||
for (const sibling of siblings) {
|
||||
const state = await checkoutState(sibling.fullPath)
|
||||
if (state.branch === branch) {
|
||||
branchWorktree = state
|
||||
break
|
||||
}
|
||||
const env = gitAuthService.getGitEnvironment()
|
||||
const [main, siblings, gitWorktrees, localBranches, branchHead] = await Promise.all([
|
||||
checkoutState(repo.fullPath),
|
||||
listMirrorCheckoutSiblings(db, repo, gitAuthService, openCodeClient),
|
||||
listGitWorktrees(repo.fullPath, env),
|
||||
listLocalBranchNames(repo.fullPath),
|
||||
branchHeadSha(repo.fullPath, branch),
|
||||
])
|
||||
|
||||
const branchByCanonicalPath = new Map<string, string | null>()
|
||||
const checkedOut = new Set<string>()
|
||||
for (const worktree of gitWorktrees) {
|
||||
branchByCanonicalPath.set(canonicalPathSync(resolve(worktree.path)), worktree.branch)
|
||||
if (worktree.branch) checkedOut.add(worktree.branch)
|
||||
}
|
||||
|
||||
const gitWorktrees = await listGitWorktrees(repo.fullPath, gitAuthService.getGitEnvironment())
|
||||
const checkedOut = new Set(
|
||||
gitWorktrees.map((worktree) => worktree.branch).filter((name): name is string => name !== null),
|
||||
)
|
||||
const localBranches = await listLocalBranchNames(repo.fullPath)
|
||||
const response: MirrorMoveTargetResponse = {
|
||||
const candidates = siblings
|
||||
.map((sibling) => ({
|
||||
sibling,
|
||||
rank: mirrorWorktreeBranchRank(
|
||||
branchByCanonicalPath.get(canonicalPathSync(resolve(sibling.fullPath))) ?? null,
|
||||
branch,
|
||||
),
|
||||
}))
|
||||
.filter((candidate): candidate is { sibling: RepoSibling; rank: number } => candidate.rank !== null)
|
||||
.sort((left, right) => left.rank - right.rank)
|
||||
|
||||
const worktrees = await Promise.all(candidates.map((candidate) => checkoutState(candidate.sibling.fullPath)))
|
||||
|
||||
const response: MirrorCheckoutsResponse = {
|
||||
main,
|
||||
branchWorktree,
|
||||
newWorktreeBranch: nextWorktreeBranch(branch, localBranches, checkedOut),
|
||||
worktrees,
|
||||
branchHead,
|
||||
branchCheckedOut: checkedOut.has(branch),
|
||||
suffixedWorktreeBranch: nextSuffixedWorktreeBranch(branch, localBranches, checkedOut),
|
||||
}
|
||||
return c.json(response)
|
||||
} catch (error) {
|
||||
logger.error('mirror target resolution failed:', error)
|
||||
logger.error('mirror checkouts resolution failed:', error)
|
||||
return c.json({ error: getErrorMessage(error) }, 500)
|
||||
}
|
||||
})
|
||||
@@ -553,30 +619,49 @@ export function createInternalRepoMirrorRoutes(
|
||||
app.post('/:repoId/mirror/worktree', async (c) => {
|
||||
const repoId = Number(c.req.param('repoId'))
|
||||
if (!Number.isFinite(repoId)) return c.json({ error: 'invalid repoId' }, 400)
|
||||
let json: unknown
|
||||
try {
|
||||
json = await c.req.json()
|
||||
} catch {
|
||||
return c.json({ error: 'invalid json body' }, 400)
|
||||
}
|
||||
const branchParsed = MirrorTargetBranchRequestSchema.safeParse(json)
|
||||
if (!branchParsed.success) return c.json({ error: 'branch required' }, 400)
|
||||
const { branch } = branchParsed.data
|
||||
|
||||
const branch = c.req.header('x-ocm-branch')?.trim()
|
||||
const targetBranch = c.req.header('x-ocm-target-branch')?.trim()
|
||||
if (!branch || !targetBranch) return c.json({ error: 'branch and target branch required' }, 400)
|
||||
|
||||
const repo = getRepoById(db, repoId)
|
||||
if (!repo) return c.json({ error: 'repo not found' }, 404)
|
||||
|
||||
const gitWorktrees = await listGitWorktrees(repo.fullPath, gitAuthService.getGitEnvironment())
|
||||
if (gitWorktrees.some((worktree) => worktree.branch === branch)) {
|
||||
return c.json({ error: 'branch_in_use', message: `branch '${branch}' is checked out in a worktree of this repository` }, 409)
|
||||
if (!(await isValidBranchName(repo.fullPath, branch)) || !(await isValidBranchName(repo.fullPath, targetBranch))) {
|
||||
return c.json({ error: 'invalid branch name' }, 400)
|
||||
}
|
||||
|
||||
const gitWorktrees = await listGitWorktrees(repo.fullPath, gitAuthService.getGitEnvironment())
|
||||
if (gitWorktrees.some((worktree) => worktree.branch === targetBranch)) {
|
||||
return c.json({ error: 'branch_in_use', message: `branch '${targetBranch}' is checked out in a worktree of this repository` }, 409)
|
||||
}
|
||||
|
||||
const rawBody = c.req.raw.body
|
||||
if (!rawBody) return c.json({ error: 'no body provided' }, 400)
|
||||
|
||||
let bundleDir: string | undefined
|
||||
try {
|
||||
const worktree = await repoWorkspaces.create(repo, { name: sanitizeRepoDirectoryName(branch, 'move') })
|
||||
const response: MirrorWorktreeCreateResponse = { directory: worktree.directory, branch }
|
||||
const staged = await stageBundleFromBody(rawBody, 'bundle-upload-')
|
||||
bundleDir = staged.bundleDir
|
||||
const worktree = await repoWorkspaces.create(repo, {
|
||||
name: sanitizeRepoDirectoryName(targetBranch, 'move'),
|
||||
initialize: (directory) => importBundle(directory, staged.bundlePath, branch, false, true, targetBranch),
|
||||
})
|
||||
const state = await checkoutState(worktree.directory)
|
||||
const response: MirrorWorktreeCreateResponse = {
|
||||
repoId: repo.id,
|
||||
fullPath: worktree.directory,
|
||||
branch: state.branch,
|
||||
head: state.head,
|
||||
created: true,
|
||||
worktreeSetup: worktree.worktreeSetup,
|
||||
}
|
||||
return c.json(response)
|
||||
} catch (error) {
|
||||
logger.error('mirror worktree create failed:', error)
|
||||
return c.json({ error: getErrorMessage(error) }, 409)
|
||||
} finally {
|
||||
if (bundleDir) await fsp.rm(bundleDir, { recursive: true, force: true }).catch(() => {})
|
||||
}
|
||||
})
|
||||
|
||||
@@ -587,7 +672,10 @@ export function createInternalRepoMirrorRoutes(
|
||||
const repo = getRepoById(db, repoId)
|
||||
if (!repo) return c.json({ error: 'repo not found' }, 404)
|
||||
|
||||
const state = await checkoutState(repo.fullPath)
|
||||
const directory = await resolveMirrorDirectory(db, repo, c.req.query('directory'), gitAuthService, openCodeClient)
|
||||
if (!directory) return c.json({ error: 'Directory is not part of this repository' }, 400)
|
||||
|
||||
const state = await checkoutState(directory)
|
||||
return c.json({
|
||||
repoId: repo.id,
|
||||
branch: state.branch,
|
||||
@@ -605,7 +693,10 @@ export function createInternalRepoMirrorRoutes(
|
||||
const repo = getRepoById(db, repoId)
|
||||
if (!repo) return c.json({ error: 'repo not found' }, 404)
|
||||
|
||||
const ancestry = await safeGitOut(repo.fullPath, ['merge-base', '--is-ancestor', sha, 'HEAD'])
|
||||
const directory = await resolveMirrorDirectory(db, repo, c.req.query('directory'), gitAuthService, openCodeClient)
|
||||
if (!directory) return c.json({ error: 'Directory is not part of this repository' }, 400)
|
||||
|
||||
const ancestry = await safeGitOut(directory, ['merge-base', '--is-ancestor', sha, 'HEAD'])
|
||||
return c.json({ repoId: repo.id, contained: ancestry !== null })
|
||||
})
|
||||
|
||||
@@ -616,14 +707,16 @@ export function createInternalRepoMirrorRoutes(
|
||||
const repo = getRepoById(db, repoId)
|
||||
if (!repo) return c.json({ error: 'repo not found' }, 404)
|
||||
|
||||
const directory = await resolveMirrorDirectory(db, repo, c.req.query('directory'), gitAuthService, openCodeClient)
|
||||
if (!directory) return c.json({ error: 'Directory is not part of this repository' }, 400)
|
||||
|
||||
try {
|
||||
const branchName = await safeGitOut(repo.fullPath, ['rev-parse', '--abbrev-ref', 'HEAD'])
|
||||
const head = await safeGitOut(repo.fullPath, ['rev-parse', 'HEAD'])
|
||||
const patch = await createMirrorPatch(repo.fullPath)
|
||||
const state = await checkoutState(directory)
|
||||
const patch = await createMirrorPatch(directory)
|
||||
return c.json({
|
||||
repoId: repo.id,
|
||||
branch: branchName?.trim() || null,
|
||||
head: head?.trim() || null,
|
||||
branch: state.branch,
|
||||
head: state.head,
|
||||
patch,
|
||||
})
|
||||
} catch (error) {
|
||||
@@ -659,28 +752,24 @@ export function createInternalRepoMirrorRoutes(
|
||||
}
|
||||
|
||||
try {
|
||||
const currentHead = await safeGitOut(directory, ['rev-parse', 'HEAD'])
|
||||
const currentHeadTrimmed = currentHead?.trim() || null
|
||||
const state = await checkoutState(directory)
|
||||
const baseHead = body.baseHead?.trim() || null
|
||||
if (baseHead && currentHeadTrimmed && baseHead !== currentHeadTrimmed) {
|
||||
if (baseHead && state.head && baseHead !== state.head) {
|
||||
return c.json({ error: 'head_mismatch', message: 'Manager repo HEAD differs from patch base' }, 409)
|
||||
}
|
||||
|
||||
await applyMirrorPatch(directory, body.patch)
|
||||
|
||||
const branchName = await safeGitOut(directory, ['rev-parse', '--abbrev-ref', 'HEAD'])
|
||||
const head = await safeGitOut(directory, ['rev-parse', 'HEAD'])
|
||||
|
||||
if (directory === repo.fullPath) {
|
||||
if (branchName) updateRepoBranch(db, repoId, branchName.trim())
|
||||
if (state.branch) updateRepoBranch(db, repoId, state.branch)
|
||||
updateLastPulled(db, repoId)
|
||||
}
|
||||
|
||||
return c.json({
|
||||
repoId,
|
||||
fullPath: directory,
|
||||
branch: branchName?.trim() || null,
|
||||
head: head?.trim() || null,
|
||||
branch: state.branch,
|
||||
head: state.head,
|
||||
created: false,
|
||||
applied: true,
|
||||
})
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { Context } from 'hono'
|
||||
import type { Database } from 'bun:sqlite'
|
||||
import type { Repo } from '@opencode-manager/shared/types'
|
||||
import { getSiblingRepos, resolveRepoOrAssistant, resolveRepoWorkingDirectory } from '../services/repo'
|
||||
import { listRepoSiblings, resolveRepoOrAssistant, resolveRepoWorkingDirectory } from '../services/repo'
|
||||
import { getRepoById } from '../db/queries'
|
||||
import type { GitAuthService } from '../services/git-auth'
|
||||
import type { OpenCodeClient } from '../services/opencode/client'
|
||||
@@ -36,12 +36,11 @@ export async function resolveRepoRequestDirectory(
|
||||
}
|
||||
|
||||
const resolved = await resolveRepoWorkingDirectory(repo, directory, () =>
|
||||
getSiblingRepos(
|
||||
listRepoSiblings(
|
||||
deps.database,
|
||||
repo.id,
|
||||
deps.gitAuthService.getGitEnvironment(),
|
||||
deps.openCodeClient,
|
||||
{ includeBranch: false },
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
@@ -7,10 +7,13 @@ import type { OpenCodeClient } from './opencode/client'
|
||||
import type { ProjectConfigService } from './project-config'
|
||||
import type { TerminalService } from './terminal'
|
||||
import type { ScheduleService } from './schedules'
|
||||
import { findSiblingByDirectory, getSiblingRepos, removeWorktree, RepoWorkspaceError, resolveRepoProjectId } from './repo'
|
||||
import { findSiblingByDirectory, listRepoSiblings, removeWorktree, RepoWorkspaceError, resolveRepoProjectId } from './repo'
|
||||
import { getErrorMessage } from '../utils/error-utils'
|
||||
|
||||
/** Single owner of repo worktree lifecycle side effects: worktree setup on create, and terminal cleanup plus owner-specific removal on remove. */
|
||||
/**
|
||||
* Single owner of repo worktree lifecycle side effects: worktree setup on create (after an
|
||||
* optional caller-provided initializer), and terminal cleanup plus owner-specific removal on remove.
|
||||
*/
|
||||
export class RepoWorkspaceService {
|
||||
constructor(
|
||||
private readonly database: Database,
|
||||
@@ -21,13 +24,30 @@ export class RepoWorkspaceService {
|
||||
private readonly scheduleWorktrees: Pick<ScheduleService, 'removeWorktrees'>,
|
||||
) {}
|
||||
|
||||
async create(repo: Repo, options: { name?: string; ref?: string } = {}) {
|
||||
async create(
|
||||
repo: Repo,
|
||||
options: { name?: string; ref?: string; initialize?: (directory: string) => Promise<void> } = {},
|
||||
) {
|
||||
const projectID = await resolveRepoProjectId(this.openCodeClient, repo.fullPath)
|
||||
const worktree = await this.openCodeClient.api.worktree.create({
|
||||
projectID,
|
||||
...(options.name ? { name: options.name } : {}),
|
||||
...(options.ref ? { branch: options.ref } : {}),
|
||||
})
|
||||
|
||||
if (options.initialize) {
|
||||
try {
|
||||
await options.initialize(worktree.directory)
|
||||
} catch (error: unknown) {
|
||||
try {
|
||||
await this.openCodeClient.api.worktree.remove({ projectID, directory: worktree.directory, force: true })
|
||||
} catch (removeError: unknown) {
|
||||
logger.warn(`Failed to remove worktree ${worktree.directory} after initialization failed:`, removeError)
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
const worktreeSetup = await this.projectConfigService.runWorktreeSetupForRepo(repo, worktree.directory, this.terminalService)
|
||||
return { ...worktree, worktreeSetup }
|
||||
}
|
||||
@@ -77,14 +97,13 @@ export class RepoWorkspaceService {
|
||||
}
|
||||
|
||||
private async listWorktreeSiblings(repoId: number) {
|
||||
const siblings = await getSiblingRepos(
|
||||
return listRepoSiblings(
|
||||
this.database,
|
||||
repoId,
|
||||
this.gitAuthService.getGitEnvironment(),
|
||||
this.openCodeClient,
|
||||
{ includeBranch: false },
|
||||
isWorktreeSibling,
|
||||
)
|
||||
return siblings.filter(isWorktreeSibling)
|
||||
}
|
||||
|
||||
private async removeTerminals(directory: string): Promise<void> {
|
||||
|
||||
@@ -1127,7 +1127,10 @@ export function createRepoRow(
|
||||
}
|
||||
|
||||
export function isDirectoryInUse(directory: string): boolean {
|
||||
return sseAggregator.getActiveDirectories().includes(directory)
|
||||
const target = canonicalPathSync(path.resolve(directory))
|
||||
return sseAggregator.getActiveDirectories().some(
|
||||
(activeDirectory) => canonicalPathSync(path.resolve(activeDirectory)) === target,
|
||||
)
|
||||
}
|
||||
|
||||
export function isRepoInUse(db: Database, repoId: number): boolean {
|
||||
@@ -1289,6 +1292,21 @@ export async function getSiblingRepos(
|
||||
return [...repoSiblings, ...worktreeSiblings]
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists a repository's siblings in its OpenCode project without resolving their branches,
|
||||
* optionally narrowed by a caller-supplied filter (for example worktree-only siblings).
|
||||
*/
|
||||
export async function listRepoSiblings(
|
||||
database: Database,
|
||||
repoId: number,
|
||||
gitEnv: Record<string, string>,
|
||||
openCodeClient: OpenCodeClient,
|
||||
filter?: (sibling: RepoSibling) => boolean,
|
||||
): Promise<RepoSibling[]> {
|
||||
const siblings = await getSiblingRepos(database, repoId, gitEnv, openCodeClient, { includeBranch: false })
|
||||
return filter ? siblings.filter(filter) : siblings
|
||||
}
|
||||
|
||||
async function listOpenCodeWorktrees(openCodeClient: OpenCodeClient | undefined, directory: string) {
|
||||
if (!openCodeClient) return []
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user