Add git changes viewer with diff display

- Add backend git-operations service for git status and file diffs
- Add git/status and git/diff API routes to repos
- Add GitChangesPanel component to list changed files with status
- Add FileDiffView component with unified diff rendering
- Add GitChangesSheet as full-screen dialog wrapper
- Integrate View Changes option into BranchSwitcher dropdown
- Support both mobile (stacked) and desktop (split) layouts
This commit is contained in:
Chris Scott committed 2025-11-27 10:10:49 -05:00
1 parent 12b7ab54b2
commit ed82b99236
8 files changed
+815 -3

No files matched your search

+47 -2
View File
@@ -2,12 +2,14 @@ import { Hono } from 'hono'
import type { Database } from 'bun:sqlite'
import * as db from '../db/queries'
import * as repoService from '../services/repo'
import * as gitOperations from '../services/git-operations'
import { SettingsService } from '../services/settings'
import { writeFileContent } from '../services/file-operations'
import { opencodeServerManager } from '../services/opencode-single-server'
import { logger } from '../utils/logger'
import { withTransactionAsync } from '../db/transactions'
import { getOpenCodeConfigFilePath } from '@opencode-webui/shared'
import { getOpenCodeConfigFilePath, getReposPath } from '@opencode-webui/shared'
import path from 'path'
export function createRepoRoutes(database: Database) {
const app = new Hono()
@@ -146,7 +148,6 @@ export function createRepoRoutes(database: Database) {
logger.info(`Switched config for repo ${id} to '${configName}'`)
logger.info(`Updated OpenCode config: ${openCodeConfigPath}`)
// Restart OpenCode server to pick up new workspace config
logger.info('Restarting OpenCode server due to workspace config change')
await opencodeServerManager.stop()
await opencodeServerManager.start()
@@ -204,6 +205,50 @@ export function createRepoRoutes(database: Database) {
return c.json({ error: error.message }, 500)
}
})
app.get('/:id/git/status', async (c) => {
try {
const id = parseInt(c.req.param('id'))
const repo = db.getRepoById(database, id)
if (!repo) {
return c.json({ error: 'Repo not found' }, 404)
}
const repoPath = path.resolve(getReposPath(), repo.localPath)
const status = await gitOperations.getGitStatus(repoPath)
return c.json(status)
} catch (error: any) {
logger.error('Failed to get git status:', error)
return c.json({ error: error.message }, 500)
}
})
app.get('/:id/git/diff', async (c) => {
try {
const id = parseInt(c.req.param('id'))
const filePath = c.req.query('path')
if (!filePath) {
return c.json({ error: 'path query parameter is required' }, 400)
}
const repo = db.getRepoById(database, id)
if (!repo) {
return c.json({ error: 'Repo not found' }, 404)
}
const repoPath = path.resolve(getReposPath(), repo.localPath)
const diff = await gitOperations.getFileDiff(repoPath, filePath)
return c.json(diff)
} catch (error: any) {
logger.error('Failed to get file diff:', error)
return c.json({ error: error.message }, 500)
}
})
return app
}
+207
View File
@@ -0,0 +1,207 @@
import { executeCommand } from '../utils/process'
import { logger } from '../utils/logger'
import path from 'path'
export type GitFileStatusType = 'modified' | 'added' | 'deleted' | 'renamed' | 'untracked' | 'copied'
export interface GitFileStatus {
path: string
status: GitFileStatusType
staged: boolean
oldPath?: string
}
export interface GitStatusResponse {
branch: string
ahead: number
behind: number
files: GitFileStatus[]
hasChanges: boolean
}
export interface FileDiffResponse {
path: string
status: GitFileStatusType
diff: string | null
additions: number
deletions: number
isBinary: boolean
}
function parseStatusCode(code: string): GitFileStatusType {
switch (code) {
case 'M': return 'modified'
case 'A': return 'added'
case 'D': return 'deleted'
case 'R': return 'renamed'
case 'C': return 'copied'
case '?': return 'untracked'
default: return 'modified'
}
}
function parsePorcelainV2(output: string): { branch: string; ahead: number; behind: number; files: GitFileStatus[] } {
const lines = output.split('\n').filter(line => line.trim())
let branch = 'HEAD'
let ahead = 0
let behind = 0
const files: GitFileStatus[] = []
for (const line of lines) {
if (line.startsWith('# branch.head ')) {
branch = line.replace('# branch.head ', '')
} else if (line.startsWith('# branch.ab ')) {
const match = line.match(/# branch\.ab \+(\d+) -(\d+)/)
if (match && match[1] && match[2]) {
ahead = parseInt(match[1], 10)
behind = parseInt(match[2], 10)
}
} else if (line.startsWith('1 ') || line.startsWith('2 ')) {
const parts = line.split(' ')
const xy = parts[1]
if (!xy || xy.length < 2) continue
const stagedStatus = xy[0] as string
const unstagedStatus = xy[1] as string
if (line.startsWith('2 ')) {
const pathParts = parts.slice(8).join(' ').split('\t')
const filePath = pathParts[1] || pathParts[0] || ''
const oldPath = pathParts[0] || ''
if (stagedStatus !== '.') {
files.push({
path: filePath,
status: parseStatusCode(stagedStatus),
staged: true,
oldPath: stagedStatus === 'R' || stagedStatus === 'C' ? oldPath : undefined
})
}
if (unstagedStatus !== '.') {
files.push({
path: filePath,
status: parseStatusCode(unstagedStatus),
staged: false
})
}
} else {
const filePath = parts.slice(8).join(' ') || ''
if (stagedStatus !== '.') {
files.push({
path: filePath,
status: parseStatusCode(stagedStatus),
staged: true
})
}
if (unstagedStatus !== '.') {
files.push({
path: filePath,
status: parseStatusCode(unstagedStatus),
staged: false
})
}
}
} else if (line.startsWith('? ')) {
const filePath = line.substring(2)
files.push({
path: filePath,
status: 'untracked',
staged: false
})
}
}
return { branch, ahead, behind, files }
}
export async function getGitStatus(repoPath: string): Promise<GitStatusResponse> {
try {
const fullPath = path.resolve(repoPath)
const output = await executeCommand(['git', '-C', fullPath, 'status', '--porcelain=v2', '--branch'])
const { branch, ahead, behind, files } = parsePorcelainV2(output)
return {
branch,
ahead,
behind,
files,
hasChanges: files.length > 0
}
} catch (error: any) {
logger.error(`Failed to get git status for ${repoPath}:`, error)
throw new Error(`Failed to get git status: ${error.message}`)
}
}
export async function getFileDiff(repoPath: string, filePath: string): Promise<FileDiffResponse> {
try {
const fullRepoPath = path.resolve(repoPath)
const status = await getGitStatus(repoPath)
const fileStatus = status.files.find(f => f.path === filePath)
if (!fileStatus) {
return {
path: filePath,
status: 'modified',
diff: null,
additions: 0,
deletions: 0,
isBinary: false
}
}
let diff: string | null = null
let additions = 0
let deletions = 0
let isBinary = false
if (fileStatus.status === 'untracked') {
try {
const content = await executeCommand(['git', '-C', fullRepoPath, 'diff', '--no-index', '--', '/dev/null', filePath])
diff = content
} catch (error: any) {
if (error.message?.includes('exit code 1') || error.message?.includes('Command failed with code 1')) {
const output = error.message || ''
const diffMatch = output.match(/diff --git[\s\S]*/)
diff = diffMatch ? diffMatch[0] : `New file: ${filePath}`
} else {
diff = `New file: ${filePath}`
}
}
} else {
try {
diff = await executeCommand(['git', '-C', fullRepoPath, 'diff', 'HEAD', '--', filePath])
} catch (error: any) {
logger.warn(`Failed to get diff for ${filePath}:`, error.message)
diff = null
}
}
if (diff) {
if (diff.includes('Binary files') || diff.includes('GIT binary patch')) {
isBinary = true
} else {
const lines = diff.split('\n')
for (const line of lines) {
if (line.startsWith('+') && !line.startsWith('+++')) {
additions++
} else if (line.startsWith('-') && !line.startsWith('---')) {
deletions++
}
}
}
}
return {
path: filePath,
status: fileStatus.status,
diff,
additions,
deletions,
isBinary
}
} catch (error: any) {
logger.error(`Failed to get file diff for ${filePath}:`, error)
throw new Error(`Failed to get file diff: ${error.message}`)
}
}