mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
feat(ocm-move): plan worktree mirror targets and stream move progress (#345)
* feat(ocm-move): plan worktree mirror targets and stream move progress - Plan/ensure mirror targets per branch via new internal endpoints, creating a worktree repo when the target branch is not checked out in the source repo - Skip branches checked out in other worktrees during bundle import and reset/clean before checkout on both server and CLI - Rewrite workspace paths in message.updated events when replaying moved sessions - Stream live move progress (bundle, upload, import, patch, replay) in the TUI - Prefer branch-matched repos when moving sessions and share push-divergence descriptions * chore(ocm-cli): bump version to 0.2.7 * fix(ocm): harden worktree mirror synchronization * fix(ocm): protect active branch during branchless pull
This commit is contained in:
1 parent
4ed3460759
commit
68d586f183
23 files changed
+2026
-158
No files matched your search
@@ -7,7 +7,7 @@ import type { Database } from 'bun:sqlite'
|
||||
import type { Repo, CreateRepoInput } from '../types/repo'
|
||||
import { logger } from '../utils/logger'
|
||||
import { getReposPath, getScheduleWorktreesPath } from '@opencode-manager/shared/config/env'
|
||||
import { normalizeRepoDirectoryName, sanitizeRepoDirectoryName, sanitizeBranchForDirectory, normalizeRepoUrlForCompare, isSSHUrl, normalizeSSHUrl, SCP_STYLE_URL_PATTERN } from '@opencode-manager/shared/utils'
|
||||
import { normalizeRepoDirectoryName, sanitizeRepoDirectoryName, sanitizeBranchForDirectory, getRepoBaseDirectoryName, normalizeRepoUrlForCompare, isSSHUrl, normalizeSSHUrl, SCP_STYLE_URL_PATTERN } from '@opencode-manager/shared/utils'
|
||||
import type { GitAuthService } from './git-auth'
|
||||
import { isGitHubHttpsUrl } from '../utils/git-auth'
|
||||
import path from 'path'
|
||||
@@ -1056,6 +1056,61 @@ export async function createWorktreeSafely(baseRepoPath: string, worktreePath: s
|
||||
}
|
||||
}
|
||||
|
||||
export type MirrorTargetPlan =
|
||||
| { kind: 'in-place'; repo: Repo; currentBranch: string | null }
|
||||
| { kind: 'existing'; repo: Repo; currentBranch: string | null }
|
||||
| { kind: 'new'; localPath: string; fullPath: string; currentBranch: string | null }
|
||||
|
||||
export async function planMirrorTarget(database: Database, repo: Repo, branch: string): Promise<MirrorTargetPlan> {
|
||||
const currentBranch = await safeGetCurrentBranch(repo.fullPath, {})
|
||||
if (currentBranch === branch) return { kind: 'in-place', repo, currentBranch }
|
||||
|
||||
const localPath = `${getRepoBaseDirectoryName(repo)}-${sanitizeBranchForDirectory(branch)}`
|
||||
const fullPath = path.join(getReposPath(), localPath)
|
||||
const existing = getRepoByLocalPath(database, localPath)
|
||||
|
||||
if (existing) {
|
||||
if (existing.branch !== branch) {
|
||||
throw new Error(`Mirror target '${localPath}' is occupied by repo ${existing.id} registered for branch '${existing.branch ?? 'none'}' instead of '${branch}'`)
|
||||
}
|
||||
|
||||
if (!existsSync(existing.fullPath)) {
|
||||
throw new Error(`Repo ${existing.id} for branch '${branch}' is missing its worktree directory at '${existing.fullPath}'`)
|
||||
}
|
||||
|
||||
const checkedOutBranch = await safeGetCurrentBranch(existing.fullPath, {})
|
||||
if (checkedOutBranch !== branch) {
|
||||
throw new Error(`Repo ${existing.id} for branch '${branch}' has branch '${checkedOutBranch ?? 'none'}' checked out at '${existing.fullPath}'`)
|
||||
}
|
||||
|
||||
return { kind: 'existing', repo: existing, currentBranch }
|
||||
}
|
||||
|
||||
return { kind: 'new', localPath, fullPath, currentBranch }
|
||||
}
|
||||
|
||||
export async function ensureMirrorTarget(database: Database, repo: Repo, branch: string): Promise<{ repo: Repo; created: boolean }> {
|
||||
const plan = await planMirrorTarget(database, repo, branch)
|
||||
if (plan.kind !== 'new') return { repo: plan.repo, created: false }
|
||||
|
||||
await createWorktreeSafely(repo.fullPath, plan.fullPath, branch, {})
|
||||
|
||||
try {
|
||||
const worktreeRepo = createRepo(database, repo.repoUrl
|
||||
? { repoUrl: repo.repoUrl, localPath: plan.localPath, branch, defaultBranch: branch, cloneStatus: 'ready', clonedAt: Date.now(), isWorktree: true }
|
||||
: { isLocal: true, localPath: plan.localPath, branch, defaultBranch: branch, cloneStatus: 'ready', clonedAt: Date.now(), isWorktree: true })
|
||||
|
||||
if (worktreeRepo.localPath !== plan.localPath) {
|
||||
throw new Error(`branch ${branch} is already registered as repo ${worktreeRepo.id} at ${worktreeRepo.fullPath}`)
|
||||
}
|
||||
|
||||
return { repo: worktreeRepo, created: true }
|
||||
} catch (error: unknown) {
|
||||
await removeWorktree(repo.fullPath, plan.fullPath)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export function ensureMirrorTargetPath(name: string): { fullPath: string; localPath: string } {
|
||||
const slugified = name
|
||||
.toLowerCase()
|
||||
|
||||
Reference in new issue
Block a user