Fix mobile scroll on session reload and git errors on empty repos

This commit is contained in:
Chris Scott committed 2025-11-28 22:33:51 -05:00
1 parent 7b2892b55e
commit f90683071f
3 files changed
+75 -44

No files matched your search

+15 -1
View File
@@ -2,6 +2,15 @@ import { executeCommand } from '../utils/process'
import { logger } from '../utils/logger'
import path from 'path'
async function hasCommits(repoPath: string): Promise<boolean> {
try {
await executeCommand(['git', '-C', repoPath, 'rev-parse', 'HEAD'])
return true
} catch {
return false
}
}
export type GitFileStatusType = 'modified' | 'added' | 'deleted' | 'renamed' | 'untracked' | 'copied'
export interface GitFileStatus {
@@ -170,7 +179,12 @@ export async function getFileDiff(repoPath: string, filePath: string): Promise<F
}
} else {
try {
diff = await executeCommand(['git', '-C', fullRepoPath, 'diff', 'HEAD', '--', filePath])
const repoHasCommits = await hasCommits(fullRepoPath)
if (repoHasCommits) {
diff = await executeCommand(['git', '-C', fullRepoPath, 'diff', 'HEAD', '--', filePath])
} else {
diff = `New file (no commits yet): ${filePath}`
}
} catch (error: any) {
logger.warn(`Failed to get diff for ${filePath}:`, error.message)
diff = null
+54 -43
View File
@@ -8,6 +8,33 @@ import { SettingsService } from './settings'
import { getReposPath } from '@opencode-webui/shared'
import path from 'path'
async function hasCommits(repoPath: string): Promise<boolean> {
try {
await executeCommand(['git', '-C', repoPath, 'rev-parse', 'HEAD'])
return true
} catch {
return false
}
}
async function safeGetCurrentBranch(repoPath: string): Promise<string | null> {
try {
const repoHasCommits = await hasCommits(repoPath)
if (!repoHasCommits) {
try {
const symbolicRef = await executeCommand(['git', '-C', repoPath, 'symbolic-ref', '--short', 'HEAD'])
return symbolicRef.trim()
} catch {
return null
}
}
const currentBranch = await executeCommand(['git', '-C', repoPath, 'rev-parse', '--abbrev-ref', 'HEAD'])
return currentBranch.trim()
} catch {
return null
}
}
export async function initLocalRepo(
database: Database,
localPath: string,
@@ -310,18 +337,8 @@ export async function cloneRepo(
export async function getCurrentBranch(repo: Repo): Promise<string | null> {
const repoPath = path.resolve(getReposPath(), repo.localPath)
try {
const currentBranch = await executeCommand(['git', '-C', repoPath, 'rev-parse', '--abbrev-ref', 'HEAD'])
return currentBranch.trim()
} catch {
try {
const symbolicRef = await executeCommand(['git', '-C', repoPath, 'symbolic-ref', '--short', 'HEAD'])
return symbolicRef.trim()
} catch {
return repo.branch || repo.defaultBranch || null
}
}
const branch = await safeGetCurrentBranch(repoPath)
return branch || repo.branch || repo.defaultBranch || null
}
export async function listBranches(repo: Repo): Promise<{ local: string[], remote: string[], current: string | null }> {
@@ -609,45 +626,39 @@ async function cleanupStaleWorktree(baseRepoPath: string, worktreePath: string):
}
async function isBranchCheckedOutInMainWorktree(baseRepoPath: string, branch: string): Promise<boolean> {
try {
const currentBranch = await executeCommand(['git', '-C', baseRepoPath, 'rev-parse', '--abbrev-ref', 'HEAD'])
return currentBranch.trim() === branch
} catch {
return false
}
const currentBranch = await safeGetCurrentBranch(baseRepoPath)
return currentBranch === branch
}
async function getAvailableBranchForWorktree(baseRepoPath: string, targetBranch: string): Promise<string> {
try {
const currentBranch = await executeCommand(['git', '-C', baseRepoPath, 'rev-parse', '--abbrev-ref', 'HEAD'])
const trimmedCurrent = currentBranch.trim()
if (trimmedCurrent === targetBranch) {
logger.info(`Branch '${targetBranch}' is currently checked out in main worktree`)
const defaultBranch = await executeCommand(['git', '-C', baseRepoPath, 'rev-parse', '--abbrev-ref', 'origin/HEAD']).then(ref => ref.trim()).catch(() => 'main')
const cleanDefaultBranch = defaultBranch.replace('origin/', '')
if (cleanDefaultBranch !== trimmedCurrent) {
logger.info(`Switching to '${cleanDefaultBranch}' to free up '${targetBranch}' for worktree`)
await executeCommand(['git', '-C', baseRepoPath, 'checkout', cleanDefaultBranch])
return targetBranch
} else {
logger.warn(`Cannot free up branch '${targetBranch}' - it's the default branch`)
return `${targetBranch}-worktree-${Date.now()}`
}
}
const currentBranch = await safeGetCurrentBranch(baseRepoPath)
if (!currentBranch) {
return targetBranch
} catch (error: any) {
logger.warn(`Failed to determine available branch: ${error.message}`)
return `${targetBranch}-worktree-${Date.now()}`
}
if (currentBranch === targetBranch) {
logger.info(`Branch '${targetBranch}' is currently checked out in main worktree`)
const defaultBranch = await executeCommand(['git', '-C', baseRepoPath, 'rev-parse', '--abbrev-ref', 'origin/HEAD']).then(ref => ref.trim()).catch(() => 'main')
const cleanDefaultBranch = defaultBranch.replace('origin/', '')
if (cleanDefaultBranch !== currentBranch) {
logger.info(`Switching to '${cleanDefaultBranch}' to free up '${targetBranch}' for worktree`)
await executeCommand(['git', '-C', baseRepoPath, 'checkout', cleanDefaultBranch])
return targetBranch
} else {
logger.warn(`Cannot free up branch '${targetBranch}' - it's the default branch`)
return `${targetBranch}-worktree-${Date.now()}`
}
}
return targetBranch
}
async function createWorktreeSafely(baseRepoPath: string, worktreePath: string, branch: string): Promise<void> {
const currentBranch = await executeCommand(['git', '-C', baseRepoPath, 'rev-parse', '--abbrev-ref', 'HEAD'])
if (currentBranch.trim() === branch) {
const currentBranch = await safeGetCurrentBranch(baseRepoPath)
if (currentBranch === branch) {
logger.info(`Branch '${branch}' is checked out in main repo, switching away...`)
const defaultBranch = await executeCommand(['git', '-C', baseRepoPath, 'rev-parse', '--abbrev-ref', 'origin/HEAD'])
.then(ref => ref.trim().replace('origin/', ''))
@@ -42,6 +42,12 @@ export const MessageThread = memo(function MessageThread({ opcodeUrl, sessionID,
const hasInitialScrolledRef = useRef(false)
const scrollRAFRef = useRef<number | null>(null)
const lastScrollTimeRef = useRef(0)
useEffect(() => {
hasInitialScrolledRef.current = false
userScrolledUpRef.current = false
lastMessageCountRef.current = 0
}, [sessionID])
const scrollToBottom = useCallback((force = false) => {
if (!containerRef?.current) return