diff --git a/backend/src/routes/settings.ts b/backend/src/routes/settings.ts index f8a3293d..5814821b 100644 --- a/backend/src/routes/settings.ts +++ b/backend/src/routes/settings.ts @@ -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) diff --git a/backend/src/services/repo.ts b/backend/src/services/repo.ts index 2122d386..9935a1cc 100644 --- a/backend/src/services/repo.ts +++ b/backend/src/services/repo.ts @@ -108,6 +108,35 @@ function getGitEnv(database: Database): Record { } } +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') { diff --git a/frontend/src/api/types/settings.ts b/frontend/src/api/types/settings.ts index 6e845bcc..9ea1d4af 100644 --- a/frontend/src/api/types/settings.ts +++ b/frontend/src/api/types/settings.ts @@ -28,6 +28,11 @@ export interface GitCredential { username?: string } +export interface GitIdentity { + name: string + email: string +} + export interface UserPreferences { theme: 'dark' | 'light' | 'system' mode: 'plan' | 'build' @@ -41,6 +46,7 @@ export interface UserPreferences { customCommands: CustomCommand[] customAgents: CustomAgent[] gitCredentials?: GitCredential[] + gitIdentity?: GitIdentity tts?: TTSConfig } diff --git a/frontend/src/components/settings/GeneralSettings.tsx b/frontend/src/components/settings/GeneralSettings.tsx index d24c64ff..f52985bf 100644 --- a/frontend/src/components/settings/GeneralSettings.tsx +++ b/frontend/src/components/settings/GeneralSettings.tsx @@ -1,68 +1,12 @@ -import { useState, useEffect } from 'react' import { useSettings } from '@/hooks/useSettings' -import { Loader2, Plus, Trash2, Save } from 'lucide-react' +import { Loader2 } from 'lucide-react' import { Label } from '@/components/ui/label' import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' import { Switch } from '@/components/ui/switch' -import { Input } from '@/components/ui/input' -import { Button } from '@/components/ui/button' import { TTSSettings } from './TTSSettings' -import { showToast } from '@/lib/toast' -import type { GitCredential } from '@/api/types/settings' export function GeneralSettings() { - const { preferences, isLoading, updateSettings, updateSettingsAsync, isUpdating } = useSettings() - const [gitCredentials, setGitCredentials] = useState([]) - const [isSaving, setIsSaving] = useState(false) - const [hasChanges, setHasChanges] = useState(false) - - useEffect(() => { - if (preferences) { - setGitCredentials(preferences.gitCredentials || []) - setHasChanges(false) - } - }, [preferences]) - - const checkForChanges = (newCredentials: GitCredential[]) => { - const currentCreds = JSON.stringify(preferences?.gitCredentials || []) - const newCreds = JSON.stringify(newCredentials) - setHasChanges(currentCreds !== newCreds) - } - - const addCredential = () => { - const newCredentials = [...gitCredentials, { name: '', host: '', token: '', username: '' }] - setGitCredentials(newCredentials) - checkForChanges(newCredentials) - } - - const updateCredential = (index: number, field: keyof GitCredential, value: string) => { - const newCredentials = [...gitCredentials] - newCredentials[index] = { ...newCredentials[index], [field]: value } - setGitCredentials(newCredentials) - checkForChanges(newCredentials) - } - - const removeCredential = (index: number) => { - const newCredentials = gitCredentials.filter((_, i) => i !== index) - setGitCredentials(newCredentials) - checkForChanges(newCredentials) - } - - const saveCredentials = async () => { - const validCredentials = gitCredentials.filter(cred => cred.name && cred.host && cred.token) - - setIsSaving(true) - try { - showToast.loading('Saving credentials and restarting server...', { id: 'git-credentials' }) - await updateSettingsAsync({ gitCredentials: validCredentials }) - setHasChanges(false) - showToast.success('Git credentials updated', { id: 'git-credentials' }) - } catch { - showToast.error('Failed to update git credentials', { id: 'git-credentials' }) - } finally { - setIsSaving(false) - } - } + const { preferences, isLoading, updateSettings, isUpdating } = useSettings() if (isLoading) { return ( @@ -172,114 +116,6 @@ export function GeneralSettings() { /> -
-
-
-

Git Credentials

-

- Add credentials for cloning private repositories from any Git host -

-
-
- {hasChanges && ( - - )} - -
-
- - {gitCredentials.length === 0 ? ( -
-

- No git credentials configured. Click "Add" to add credentials for GitHub, GitLab, Gitea, or other Git hosts. -

-
- ) : ( -
- {gitCredentials.map((cred, index) => ( -
-
- updateCredential(index, 'name', e.target.value)} - disabled={isSaving} - className="bg-background border-border text-foreground placeholder:text-muted-foreground font-medium" - /> - -
- -
-
- - updateCredential(index, 'host', e.target.value)} - disabled={isSaving} - className="bg-background border-border text-foreground placeholder:text-muted-foreground" - /> -
-
- - updateCredential(index, 'username', e.target.value)} - disabled={isSaving} - className="bg-background border-border text-foreground placeholder:text-muted-foreground" - /> -
-
- -
- - updateCredential(index, 'token', e.target.value)} - disabled={isSaving} - className="bg-background border-border text-foreground placeholder:text-muted-foreground" - /> -
-
- ))} -
- )} - -

- Username defaults: github.com uses "x-access-token", gitlab.com uses "oauth2". For other hosts, specify your username if required. -

-
- {isUpdating && (
diff --git a/frontend/src/components/settings/GitSettings.tsx b/frontend/src/components/settings/GitSettings.tsx new file mode 100644 index 00000000..228ab6a7 --- /dev/null +++ b/frontend/src/components/settings/GitSettings.tsx @@ -0,0 +1,278 @@ +import { useState, useEffect } from 'react' +import { useSettings } from '@/hooks/useSettings' +import { Loader2, Plus, Trash2, Save } from 'lucide-react' +import { Label } from '@/components/ui/label' +import { Input } from '@/components/ui/input' +import { Button } from '@/components/ui/button' +import { showToast } from '@/lib/toast' +import type { GitCredential, GitIdentity } from '@/api/types/settings' + +export function GitSettings() { + const { preferences, isLoading, updateSettingsAsync, isUpdating } = useSettings() + const [gitCredentials, setGitCredentials] = useState([]) + const [gitIdentity, setGitIdentity] = useState({ name: '', email: '' }) + const [isSaving, setIsSaving] = useState(false) + const [hasCredentialChanges, setHasCredentialChanges] = useState(false) + const [hasIdentityChanges, setHasIdentityChanges] = useState(false) + + useEffect(() => { + if (preferences) { + setGitCredentials(preferences.gitCredentials || []) + setGitIdentity(preferences.gitIdentity || { name: '', email: '' }) + setHasCredentialChanges(false) + setHasIdentityChanges(false) + } + }, [preferences]) + + const checkForCredentialChanges = (newCredentials: GitCredential[]) => { + const currentCreds = JSON.stringify(preferences?.gitCredentials || []) + const newCreds = JSON.stringify(newCredentials) + setHasCredentialChanges(currentCreds !== newCreds) + } + + const checkForIdentityChanges = (newIdentity: GitIdentity) => { + const currentIdentity = preferences?.gitIdentity || { name: '', email: '' } + setHasIdentityChanges( + currentIdentity.name !== newIdentity.name || + currentIdentity.email !== newIdentity.email + ) + } + + const addCredential = () => { + const newCredentials = [...gitCredentials, { name: '', host: '', token: '', username: '' }] + setGitCredentials(newCredentials) + checkForCredentialChanges(newCredentials) + } + + const updateCredential = (index: number, field: keyof GitCredential, value: string) => { + const newCredentials = [...gitCredentials] + newCredentials[index] = { ...newCredentials[index], [field]: value } + setGitCredentials(newCredentials) + checkForCredentialChanges(newCredentials) + } + + const removeCredential = (index: number) => { + const newCredentials = gitCredentials.filter((_, i) => i !== index) + setGitCredentials(newCredentials) + checkForCredentialChanges(newCredentials) + } + + const updateIdentity = (field: keyof GitIdentity, value: string) => { + const newIdentity = { ...gitIdentity, [field]: value } + setGitIdentity(newIdentity) + checkForIdentityChanges(newIdentity) + } + + const saveCredentials = async () => { + const validCredentials = gitCredentials.filter(cred => cred.name && cred.host && cred.token) + + setIsSaving(true) + try { + showToast.loading('Saving credentials and restarting server...', { id: 'git-credentials' }) + await updateSettingsAsync({ gitCredentials: validCredentials }) + setHasCredentialChanges(false) + showToast.success('Git credentials updated', { id: 'git-credentials' }) + } catch { + showToast.error('Failed to update git credentials', { id: 'git-credentials' }) + } finally { + setIsSaving(false) + } + } + + const saveIdentity = async () => { + setIsSaving(true) + try { + showToast.loading('Saving git identity...', { id: 'git-identity' }) + await updateSettingsAsync({ gitIdentity }) + setHasIdentityChanges(false) + showToast.success('Git identity updated', { id: 'git-identity' }) + } catch { + showToast.error('Failed to update git identity', { id: 'git-identity' }) + } finally { + setIsSaving(false) + } + } + + if (isLoading) { + return ( +
+ +
+ ) + } + + return ( +
+
+

Git Identity

+ +
+

+ Configure the default author identity used for git commits in local repositories. + Leave empty to use system defaults. +

+ +
+
+ + updateIdentity('name', e.target.value)} + disabled={isSaving} + className="bg-background border-border text-foreground placeholder:text-muted-foreground" + /> +
+
+ + updateIdentity('email', e.target.value)} + disabled={isSaving} + className="bg-background border-border text-foreground placeholder:text-muted-foreground" + /> +
+
+ + {hasIdentityChanges && ( +
+ +
+ )} + +

+ If not configured, defaults to "OpenCode User" and "opencode@localhost" for new local repositories. +

+
+
+ +
+
+
+

Git Credentials

+

+ Add credentials for cloning private repositories from any Git host +

+
+
+ {hasCredentialChanges && ( + + )} + +
+
+ + {gitCredentials.length === 0 ? ( +
+

+ No git credentials configured. Click "Add" to add credentials for GitHub, GitLab, Gitea, or other Git hosts. +

+
+ ) : ( +
+ {gitCredentials.map((cred, index) => ( +
+
+ updateCredential(index, 'name', e.target.value)} + disabled={isSaving} + className="bg-background border-border text-foreground placeholder:text-muted-foreground font-medium" + /> + +
+ +
+
+ + updateCredential(index, 'host', e.target.value)} + disabled={isSaving} + className="bg-background border-border text-foreground placeholder:text-muted-foreground" + /> +
+
+ + updateCredential(index, 'username', e.target.value)} + disabled={isSaving} + className="bg-background border-border text-foreground placeholder:text-muted-foreground" + /> +
+
+ +
+ + updateCredential(index, 'token', e.target.value)} + disabled={isSaving} + className="bg-background border-border text-foreground placeholder:text-muted-foreground" + /> +
+
+ ))} +
+ )} + +

+ Username defaults: github.com uses "x-access-token", gitlab.com uses "oauth2". For other hosts, specify your username if required. +

+ + {isUpdating && ( +
+ + Saving... +
+ )} +
+
+ ) +} diff --git a/frontend/src/components/settings/SettingsDialog.tsx b/frontend/src/components/settings/SettingsDialog.tsx index 1bac0481..4fca1c25 100644 --- a/frontend/src/components/settings/SettingsDialog.tsx +++ b/frontend/src/components/settings/SettingsDialog.tsx @@ -1,11 +1,12 @@ import { useState, useRef, useEffect, useCallback } from 'react' import { GeneralSettings } from '@/components/settings/GeneralSettings' +import { GitSettings } from '@/components/settings/GitSettings' import { KeyboardShortcuts } from '@/components/settings/KeyboardShortcuts' import { OpenCodeConfigManager } from '@/components/settings/OpenCodeConfigManager' import { ProviderSettings } from '@/components/settings/ProviderSettings' import { Dialog, DialogContent } from '@/components/ui/dialog' import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs' -import { Settings2, Keyboard, Code, ChevronLeft, X, Key } from 'lucide-react' +import { Settings2, Keyboard, Code, ChevronLeft, X, Key, GitBranch } from 'lucide-react' import { Button } from '@/components/ui/button' import { useSwipeBack } from '@/hooks/useMobile' @@ -14,7 +15,7 @@ interface SettingsDialogProps { onOpenChange: (open: boolean) => void } -type SettingsView = 'menu' | 'general' | 'shortcuts' | 'opencode' | 'providers' +type SettingsView = 'menu' | 'general' | 'git' | 'shortcuts' | 'opencode' | 'providers' export function SettingsDialog({ open, onOpenChange }: SettingsDialogProps) { const [mobileView, setMobileView] = useState('menu') @@ -39,6 +40,7 @@ export function SettingsDialog({ open, onOpenChange }: SettingsDialogProps) { const menuItems = [ { id: 'general', icon: Settings2, label: 'General Settings', description: 'App preferences and behavior' }, + { id: 'git', icon: GitBranch, label: 'Git', description: 'Git identity and credentials for repositories' }, { id: 'shortcuts', icon: Keyboard, label: 'Keyboard Shortcuts', description: 'Customize keyboard shortcuts' }, { id: 'opencode', icon: Code, label: 'OpenCode Config', description: 'Manage OpenCode configurations, commands, and agents' }, { id: 'providers', icon: Key, label: 'Providers', description: 'Manage AI provider API keys' }, @@ -65,10 +67,13 @@ export function SettingsDialog({ open, onOpenChange }: SettingsDialogProps) {
- + General + + Git + Shortcuts @@ -84,6 +89,7 @@ export function SettingsDialog({ open, onOpenChange }: SettingsDialogProps) {
+ @@ -143,6 +149,7 @@ export function SettingsDialog({ open, onOpenChange }: SettingsDialogProps) { )} {mobileView === 'general' && } + {mobileView === 'git' && } {mobileView === 'shortcuts' && } {mobileView === 'opencode' && } {mobileView === 'providers' && } diff --git a/shared/src/schemas/settings.ts b/shared/src/schemas/settings.ts index 57f70bcc..7baa2858 100644 --- a/shared/src/schemas/settings.ts +++ b/shared/src/schemas/settings.ts @@ -69,6 +69,18 @@ export const GitCredentialSchema = z.object({ export type GitCredential = z.infer; +export const GitIdentitySchema = z.object({ + name: z.string(), + email: z.string(), +}); + +export type GitIdentity = z.infer; + +export const DEFAULT_GIT_IDENTITY: GitIdentity = { + name: '', + email: '', +}; + export const UserPreferencesSchema = z.object({ theme: z.enum(["dark", "light", "system"]), mode: z.enum(["plan", "build"]), @@ -82,6 +94,7 @@ export const UserPreferencesSchema = z.object({ customCommands: z.array(CustomCommandSchema), customAgents: z.array(CustomAgentSchema), gitCredentials: z.array(GitCredentialSchema).optional(), + gitIdentity: GitIdentitySchema.optional(), tts: TTSConfigSchema.optional(), lastKnownGoodConfig: z.string().optional(), }); @@ -111,6 +124,7 @@ export const DEFAULT_USER_PREFERENCES = { customCommands: [], customAgents: [], gitCredentials: [] as GitCredential[], + gitIdentity: DEFAULT_GIT_IDENTITY, tts: DEFAULT_TTS_CONFIG, };