Add local repository support and permission request handling

This commit is contained in:
Chris Scott committed 2025-11-28 10:26:03 -05:00
1 parent 9a2ebc9652
commit 83834e7ed7
22 files changed
+685 -120

No files matched your search

+46 -1
View File
@@ -4,6 +4,41 @@ import { logger } from '../utils/logger'
export function runMigrations(db: Database): void {
try {
const tableInfo = db.prepare("PRAGMA table_info(repos)").all() as any[]
const repoUrlColumn = tableInfo.find((col: any) => col.name === 'repo_url')
if (repoUrlColumn && repoUrlColumn.notnull === 1) {
logger.info('Migrating repos table to allow nullable repo_url for local repos')
db.run('BEGIN TRANSACTION')
try {
db.run(`
CREATE TABLE repos_new (
id INTEGER PRIMARY KEY AUTOINCREMENT,
repo_url TEXT,
local_path TEXT NOT NULL,
branch TEXT,
default_branch TEXT,
clone_status TEXT NOT NULL,
cloned_at INTEGER NOT NULL,
last_pulled INTEGER,
opencode_config_name TEXT,
is_worktree BOOLEAN DEFAULT FALSE,
is_local BOOLEAN DEFAULT FALSE
)
`)
db.run(`
INSERT INTO repos_new (id, repo_url, local_path, branch, default_branch, clone_status, cloned_at, last_pulled, opencode_config_name, is_worktree, is_local)
SELECT id, repo_url, local_path, branch, default_branch, clone_status, cloned_at, last_pulled, opencode_config_name, is_worktree, is_local FROM repos
`)
db.run('DROP TABLE repos')
db.run('ALTER TABLE repos_new RENAME TO repos')
db.run('COMMIT')
logger.info('Successfully migrated repos table to allow nullable repo_url')
} catch (migrationError) {
db.run('ROLLBACK')
throw migrationError
}
}
const hasBranchColumn = tableInfo.some(col => col.name === 'branch')
if (!hasBranchColumn) {
@@ -21,13 +56,23 @@ export function runMigrations(db: Database): void {
logger.debug('Index already exists or could not be created', error)
}
try {
db.run(`
CREATE UNIQUE INDEX IF NOT EXISTS idx_local_path
ON repos(local_path)
`)
} catch (error) {
logger.debug('Local path index already exists or could not be created', error)
}
const requiredColumns = [
{ name: 'default_branch', sql: 'ALTER TABLE repos ADD COLUMN default_branch TEXT' },
{ name: 'clone_status', sql: 'ALTER TABLE repos ADD COLUMN clone_status TEXT NOT NULL DEFAULT "cloning"' },
{ name: 'cloned_at', sql: 'ALTER TABLE repos ADD COLUMN cloned_at INTEGER NOT NULL DEFAULT 0' },
{ name: 'last_pulled', sql: 'ALTER TABLE repos ADD COLUMN last_pulled INTEGER' },
{ name: 'opencode_config_name', sql: 'ALTER TABLE repos ADD COLUMN opencode_config_name TEXT' },
{ name: 'is_worktree', sql: 'ALTER TABLE repos ADD COLUMN is_worktree BOOLEAN DEFAULT FALSE' }
{ name: 'is_worktree', sql: 'ALTER TABLE repos ADD COLUMN is_worktree BOOLEAN DEFAULT FALSE' },
{ name: 'is_local', sql: 'ALTER TABLE repos ADD COLUMN is_local BOOLEAN DEFAULT FALSE' }
]
for (const column of requiredColumns) {
+54 -22
View File
@@ -5,7 +5,7 @@ import path from 'path'
export interface RepoRow {
id: number
repo_url: string
repo_url?: string
local_path: string
branch?: string
default_branch: string
@@ -14,6 +14,7 @@ export interface RepoRow {
last_pulled?: number
opencode_config_name?: string
is_worktree?: number
is_local?: number
}
function rowToRepo(row: RepoRow): Repo {
@@ -29,35 +30,59 @@ function rowToRepo(row: RepoRow): Repo {
lastPulled: row.last_pulled,
openCodeConfigName: row.opencode_config_name,
isWorktree: row.is_worktree ? Boolean(row.is_worktree) : undefined,
isLocal: row.is_local ? Boolean(row.is_local) : undefined,
}
}
export function createRepo(db: Database, repo: CreateRepoInput): Repo {
const stmt = db.prepare(`
INSERT OR IGNORE INTO repos (repo_url, local_path, branch, default_branch, clone_status, cloned_at, is_worktree)
VALUES (?, ?, ?, ?, ?, ?, ?)
`)
const normalizedPath = repo.localPath.replace(/\/+$/, '')
const result = stmt.run(
repo.repoUrl,
repo.localPath,
repo.branch || null,
repo.defaultBranch,
repo.cloneStatus,
repo.clonedAt,
repo.isWorktree ? 1 : 0
)
const existing = repo.isLocal
? getRepoByLocalPath(db, normalizedPath)
: getRepoByUrlAndBranch(db, repo.repoUrl!, repo.branch)
if (result.changes === 0) {
const existing = getRepoByUrlAndBranch(db, repo.repoUrl, repo.branch)
if (existing) {
return existing
}
throw new Error('Failed to create repo and no existing repo found')
if (existing) {
return existing
}
const newRepo = getRepoById(db, Number(result.lastInsertRowid))!
return newRepo
const stmt = db.prepare(`
INSERT INTO repos (repo_url, local_path, branch, default_branch, clone_status, cloned_at, is_worktree, is_local)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
`)
try {
const result = stmt.run(
repo.repoUrl || null,
normalizedPath,
repo.branch || null,
repo.defaultBranch,
repo.cloneStatus,
repo.clonedAt,
repo.isWorktree ? 1 : 0,
repo.isLocal ? 1 : 0
)
const newRepo = getRepoById(db, Number(result.lastInsertRowid))
if (!newRepo) {
throw new Error(`Failed to retrieve newly created repo with id ${result.lastInsertRowid}`)
}
return newRepo
} catch (error: any) {
if (error.message?.includes('UNIQUE constraint failed') || error.code === 'SQLITE_CONSTRAINT_UNIQUE') {
const conflictRepo = repo.isLocal
? getRepoByLocalPath(db, normalizedPath)
: getRepoByUrlAndBranch(db, repo.repoUrl!, repo.branch)
if (conflictRepo) {
return conflictRepo
}
const identifier = repo.isLocal ? `path '${normalizedPath}'` : `url '${repo.repoUrl}' branch '${repo.branch || 'default'}'`
throw new Error(`Repository with ${identifier} already exists but could not be retrieved. This may indicate database corruption.`)
}
throw new Error(`Failed to create repository: ${error.message}`)
}
}
export function getRepoById(db: Database, id: number): Repo | null {
@@ -87,6 +112,13 @@ export function getRepoByUrlAndBranch(db: Database, repoUrl: string, branch?: st
return row ? rowToRepo(row) : null
}
export function getRepoByLocalPath(db: Database, localPath: string): Repo | null {
const stmt = db.prepare('SELECT * FROM repos WHERE local_path = ?')
const row = stmt.get(localPath) as RepoRow | undefined
return row ? rowToRepo(row) : null
}
export function listRepos(db: Database): Repo[] {
const stmt = db.prepare('SELECT * FROM repos ORDER BY cloned_at DESC')
const rows = stmt.all() as RepoRow[]
+2 -3
View File
@@ -11,7 +11,7 @@ export function initializeDatabase(dbPath: string = './data/opencode.db'): Datab
db.run(`
CREATE TABLE IF NOT EXISTS repos (
id INTEGER PRIMARY KEY AUTOINCREMENT,
repo_url TEXT NOT NULL,
repo_url TEXT,
local_path TEXT NOT NULL,
branch TEXT,
default_branch TEXT,
@@ -20,8 +20,7 @@ export function initializeDatabase(dbPath: string = './data/opencode.db'): Datab
last_pulled INTEGER,
opencode_config_name TEXT,
is_worktree BOOLEAN DEFAULT FALSE,
UNIQUE(repo_url, branch)
is_local BOOLEAN DEFAULT FALSE
);
CREATE INDEX IF NOT EXISTS idx_repo_clone_status ON repos(clone_status);