refactor: Separate git watcher from terminal service
- Create dedicated git-watcher.ts with its own WebSocket server (port 4105) - Remove git watcher code from terminal.ts (no more PTY dependency) - Add /ws/git endpoint for Traefik routing - GitPage now connects to dedicated git WebSocket instead of terminal
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
import { spawn, type IPty } from '@skitee3000/bun-pty'
|
||||
import { watch, type FSWatcher } from 'fs'
|
||||
import { join } from 'path'
|
||||
import { PORT_TERMINAL, WORKING_DIR, SHELL, SHELL_ARGS, DEFAULT_SESSION_ID, MAX_BUFFER_LINES } from '../config'
|
||||
|
||||
interface TerminalSession {
|
||||
@@ -266,93 +264,3 @@ export function broadcastClaudeStatus(status: ClaudeStatus, tool?: string) {
|
||||
|
||||
console.log(`[Terminal] Claude status broadcast: ${status}${tool ? ` (${tool})` : ''} → ${clientCount} clients`)
|
||||
}
|
||||
|
||||
// Git watcher
|
||||
let gitWatcher: FSWatcher | null = null
|
||||
let gitDebounceTimer: Timer | null = null
|
||||
const GIT_DEBOUNCE_MS = 300
|
||||
|
||||
function broadcastGitChange() {
|
||||
const message = JSON.stringify({
|
||||
type: 'git-change',
|
||||
timestamp: Date.now()
|
||||
})
|
||||
|
||||
let clientCount = 0
|
||||
for (const [, session] of sessions) {
|
||||
for (const ws of session.clients) {
|
||||
try {
|
||||
ws.send(message)
|
||||
clientCount++
|
||||
} catch {
|
||||
// Client disconnected
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (clientCount > 0) {
|
||||
console.log(`[Git] Change detected → ${clientCount} clients notified`)
|
||||
}
|
||||
}
|
||||
|
||||
// Files to ignore (noisy or irrelevant)
|
||||
const GIT_IGNORE_PATTERNS = [
|
||||
'FETCH_HEAD',
|
||||
'gc.log',
|
||||
'.lock',
|
||||
'COMMIT_EDITMSG',
|
||||
'ORIG_HEAD',
|
||||
'gitk.cache',
|
||||
'sourcetreeconfig',
|
||||
'.DS_Store',
|
||||
'fsmonitor',
|
||||
'packed-refs'
|
||||
]
|
||||
|
||||
export function startGitWatcher() {
|
||||
const gitDir = join(WORKING_DIR, '.git')
|
||||
|
||||
try {
|
||||
// Watch .git directory recursively
|
||||
gitWatcher = watch(gitDir, { recursive: true }, (eventType, filename) => {
|
||||
if (!filename) return
|
||||
|
||||
// Ignore noisy files
|
||||
const shouldIgnore = GIT_IGNORE_PATTERNS.some(pattern =>
|
||||
filename.includes(pattern)
|
||||
)
|
||||
if (shouldIgnore) return
|
||||
|
||||
// Only react to meaningful changes
|
||||
const isRelevant =
|
||||
filename.includes('refs/') || // branch/tag changes
|
||||
filename === 'HEAD' || // checkout
|
||||
filename === 'index' || // staging area
|
||||
filename.includes('objects/') // new commits/blobs
|
||||
|
||||
if (!isRelevant) return
|
||||
|
||||
// Debounce to avoid flooding
|
||||
if (gitDebounceTimer) {
|
||||
clearTimeout(gitDebounceTimer)
|
||||
}
|
||||
|
||||
gitDebounceTimer = setTimeout(() => {
|
||||
console.log(`[Git] Change: ${filename}`)
|
||||
broadcastGitChange()
|
||||
}, GIT_DEBOUNCE_MS)
|
||||
})
|
||||
|
||||
console.log(`[Git] Watching ${gitDir} for changes`)
|
||||
} catch (e: any) {
|
||||
console.error(`[Git] Failed to watch .git directory: ${e.message}`)
|
||||
}
|
||||
}
|
||||
|
||||
export function stopGitWatcher() {
|
||||
if (gitWatcher) {
|
||||
gitWatcher.close()
|
||||
gitWatcher = null
|
||||
console.log('[Git] Watcher stopped')
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user