mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
Add configurable global git identity settings with new Git tab (#43)
This commit is contained in:
1 parent
1382916b97
commit
f1a7d36433
7 files changed
+378
-171
No files matched your search
@@ -12,10 +12,10 @@ import {
|
||||
import { logger } from '../utils/logger'
|
||||
import { opencodeServerManager } from '../services/opencode-single-server'
|
||||
import { DEFAULT_AGENTS_MD } from '../index'
|
||||
import { exec } from 'child_process'
|
||||
import { execFile } from 'child_process'
|
||||
import { promisify } from 'util'
|
||||
|
||||
const execAsync = promisify(exec)
|
||||
const execFileAsync = promisify(execFile)
|
||||
|
||||
const UpdateSettingsSchema = z.object({
|
||||
preferences: UserPreferencesSchema.partial(),
|
||||
@@ -93,6 +93,24 @@ export function createSettingsRoutes(db: Database) {
|
||||
}
|
||||
}
|
||||
|
||||
if (validated.preferences.gitIdentity !== undefined) {
|
||||
const identity = validated.preferences.gitIdentity
|
||||
if (identity.name) {
|
||||
await execFileAsync('git', ['config', '--global', 'user.name', identity.name])
|
||||
logger.info(`Set global git user.name: ${identity.name}`)
|
||||
} else {
|
||||
await execFileAsync('git', ['config', '--global', '--unset', 'user.name']).catch(() => {})
|
||||
logger.info('Unset global git user.name')
|
||||
}
|
||||
if (identity.email) {
|
||||
await execFileAsync('git', ['config', '--global', 'user.email', identity.email])
|
||||
logger.info(`Set global git user.email: ${identity.email}`)
|
||||
} else {
|
||||
await execFileAsync('git', ['config', '--global', '--unset', 'user.email']).catch(() => {})
|
||||
logger.info('Unset global git user.email')
|
||||
}
|
||||
}
|
||||
|
||||
return c.json({ ...settings, serverRestarted })
|
||||
} catch (error) {
|
||||
logger.error('Failed to update settings:', error)
|
||||
|
||||
@@ -108,6 +108,35 @@ function getGitEnv(database: Database): Record<string, string> {
|
||||
}
|
||||
}
|
||||
|
||||
interface GitIdentity {
|
||||
name: string
|
||||
email: string
|
||||
}
|
||||
|
||||
function getGitIdentity(database: Database): GitIdentity {
|
||||
const defaultIdentity: GitIdentity = {
|
||||
name: 'OpenCode User',
|
||||
email: 'opencode@localhost'
|
||||
}
|
||||
|
||||
try {
|
||||
const settingsService = new SettingsService(database)
|
||||
const settings = settingsService.getSettings('default')
|
||||
const identity = settings.preferences.gitIdentity
|
||||
|
||||
if (identity?.name && identity?.email) {
|
||||
return identity
|
||||
}
|
||||
|
||||
return {
|
||||
name: identity?.name || defaultIdentity.name,
|
||||
email: identity?.email || defaultIdentity.email
|
||||
}
|
||||
} catch {
|
||||
return defaultIdentity
|
||||
}
|
||||
}
|
||||
|
||||
export async function initLocalRepo(
|
||||
database: Database,
|
||||
localPath: string,
|
||||
@@ -151,6 +180,25 @@ export async function initLocalRepo(
|
||||
|
||||
await executeCommand(['git', 'init'], { cwd: fullPath })
|
||||
|
||||
const hasGlobalEmail = await executeCommand(['git', 'config', '--global', 'user.email'], { silent: true })
|
||||
.then(email => email.trim().length > 0)
|
||||
.catch(() => false)
|
||||
const hasGlobalName = await executeCommand(['git', 'config', '--global', 'user.name'], { silent: true })
|
||||
.then(name => name.trim().length > 0)
|
||||
.catch(() => false)
|
||||
|
||||
if (!hasGlobalEmail || !hasGlobalName) {
|
||||
const identity = getGitIdentity(database)
|
||||
if (!hasGlobalEmail) {
|
||||
await executeCommand(['git', 'config', '--global', 'user.email', identity.email])
|
||||
logger.info(`Set global git user.email: ${identity.email}`)
|
||||
}
|
||||
if (!hasGlobalName) {
|
||||
await executeCommand(['git', 'config', '--global', 'user.name', identity.name])
|
||||
logger.info(`Set global git user.name: ${identity.name}`)
|
||||
}
|
||||
}
|
||||
|
||||
await executeCommand(['git', '-C', fullPath, 'commit', '--allow-empty', '-m', 'Initial commit'])
|
||||
|
||||
if (branch && branch !== 'main') {
|
||||
|
||||
Reference in new issue
Block a user