This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import { readFile, rm } from 'node:fs/promises'
|
||||
import { existsSync } from 'node:fs'
|
||||
import { join } from 'node:path'
|
||||
import { simpleGit, type SimpleGit } from 'simple-git'
|
||||
import { spawn } from 'node:child_process'
|
||||
|
||||
const delay = (ms: number): Promise<void> => new Promise((r) => setTimeout(r, ms))
|
||||
|
||||
export type GitStatusLetter = 'A' | 'M' | 'D' | 'R' | 'U'
|
||||
|
||||
@@ -17,8 +20,44 @@ export interface GitLoad {
|
||||
changes: GitChange[]
|
||||
}
|
||||
|
||||
function git(root: string): SimpleGit {
|
||||
return simpleGit({ baseDir: root, maxConcurrentProcesses: 4 })
|
||||
interface GitResult {
|
||||
code: number
|
||||
stdout: string
|
||||
stderr: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a git subcommand directly via child_process.
|
||||
*
|
||||
* We deliberately spawn `git` ourselves instead of going through simple-git:
|
||||
* inside Electron's main process simple-git's spawn (which wires up a stdin
|
||||
* pipe) trips `spawn EBADF` on macOS. Forcing stdin to 'ignore' — the child
|
||||
* never reads input — sidesteps the bad-descriptor crash entirely.
|
||||
*/
|
||||
function runGit(root: string, args: string[]): Promise<GitResult> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn('git', args, {
|
||||
cwd: root,
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
env: process.env,
|
||||
windowsHide: true,
|
||||
})
|
||||
let stdout = ''
|
||||
let stderr = ''
|
||||
child.stdout.setEncoding('utf8')
|
||||
child.stderr.setEncoding('utf8')
|
||||
child.stdout.on('data', (d: string) => { stdout += d })
|
||||
child.stderr.on('data', (d: string) => { stderr += d })
|
||||
child.on('error', reject)
|
||||
child.on('close', (code) => resolve({ code: code ?? -1, stdout, stderr }))
|
||||
})
|
||||
}
|
||||
|
||||
/** Run git, rejecting on a non-zero exit. */
|
||||
async function git(root: string, args: string[]): Promise<string> {
|
||||
const { code, stdout, stderr } = await runGit(root, args)
|
||||
if (code !== 0) throw new Error(`git ${args.join(' ')} failed (${code}): ${stderr.trim()}`)
|
||||
return stdout
|
||||
}
|
||||
|
||||
/** Map a porcelain code pair to our display letter + staged flag. */
|
||||
@@ -36,9 +75,45 @@ export function classify(index: string, working: string): { letter: GitStatusLet
|
||||
return { letter, staged }
|
||||
}
|
||||
|
||||
async function headText(g: SimpleGit, path: string): Promise<string> {
|
||||
/** Parse a `## ...` porcelain branch header into a display branch name. */
|
||||
function parseBranch(header: string): string {
|
||||
// e.g. "main...origin/main [ahead 1]", "main", "No commits yet on main",
|
||||
// "HEAD (no branch)".
|
||||
const noCommits = header.match(/^No commits yet on (.+)$/)
|
||||
if (noCommits) return noCommits[1].trim()
|
||||
if (header.startsWith('HEAD ')) return 'HEAD'
|
||||
const upstream = header.indexOf('...')
|
||||
const head = upstream >= 0 ? header.slice(0, upstream) : header
|
||||
return head.split(' ')[0].trim() || 'HEAD'
|
||||
}
|
||||
|
||||
interface StatusEntry { index: string; working: string; path: string }
|
||||
|
||||
interface ParsedStatus { branch: string; files: StatusEntry[] }
|
||||
|
||||
/** Parse `git status --porcelain -b -z` output. NUL-separated, never quoted. */
|
||||
export function parseStatus(raw: string): ParsedStatus {
|
||||
const parts = raw.split('\0')
|
||||
let branch = 'HEAD'
|
||||
const files: StatusEntry[] = []
|
||||
for (let i = 0; i < parts.length; i++) {
|
||||
const p = parts[i]
|
||||
if (!p) continue
|
||||
if (p.startsWith('## ')) { branch = parseBranch(p.slice(3)); continue }
|
||||
const index = p[0]
|
||||
const working = p[1]
|
||||
const path = p.slice(3) // skip "XY "
|
||||
// For renames/copies the original path follows as its own NUL field; the
|
||||
// destination (this entry's path) is what we display, so just skip it.
|
||||
if (index === 'R' || index === 'C' || working === 'R' || working === 'C') i++
|
||||
files.push({ index, working, path })
|
||||
}
|
||||
return { branch, files }
|
||||
}
|
||||
|
||||
async function headText(root: string, path: string): Promise<string> {
|
||||
try {
|
||||
return await g.show([`HEAD:${path}`])
|
||||
return await git(root, ['show', `HEAD:${path}`])
|
||||
} catch {
|
||||
return ''
|
||||
}
|
||||
@@ -57,29 +132,75 @@ async function diskText(root: string, path: string): Promise<string> {
|
||||
|
||||
export async function isRepo(root: string): Promise<boolean> {
|
||||
try {
|
||||
return await git(root).checkIsRepo()
|
||||
const { code, stdout } = await runGit(root, ['rev-parse', '--is-inside-work-tree'])
|
||||
if (code === 0 && stdout.trim() === 'true') return true
|
||||
} catch {
|
||||
return false
|
||||
/* git missing / transient — fall back to disk. */
|
||||
}
|
||||
// An external branch switch briefly rewrites .git; trust its presence on
|
||||
// disk rather than blanking the whole panel on a momentary probe failure.
|
||||
return existsSync(join(root, '.git'))
|
||||
}
|
||||
|
||||
export async function load(root: string): Promise<GitLoad | null> {
|
||||
const g = git(root)
|
||||
// Coalesce overlapping loads per root: a branch switch or rapid edits fire
|
||||
// several watcher pings, each triggering a reload. We keep at most one read in
|
||||
// flight plus one trailing read (which captures whatever changed during the
|
||||
// first), so the panel always settles on fresh state without a spawn pile-up.
|
||||
const loadInFlight = new Map<string, Promise<GitLoad | null>>()
|
||||
const loadPending = new Map<string, Promise<GitLoad | null>>()
|
||||
|
||||
export function load(root: string): Promise<GitLoad | null> {
|
||||
const running = loadInFlight.get(root)
|
||||
if (running) {
|
||||
let pending = loadPending.get(root)
|
||||
if (!pending) {
|
||||
pending = running.catch(() => {}).then(() => {
|
||||
loadPending.delete(root)
|
||||
return startLoad(root)
|
||||
})
|
||||
loadPending.set(root, pending)
|
||||
}
|
||||
return pending
|
||||
}
|
||||
return startLoad(root)
|
||||
}
|
||||
|
||||
function startLoad(root: string): Promise<GitLoad | null> {
|
||||
const p = doLoad(root).finally(() => {
|
||||
if (loadInFlight.get(root) === p) loadInFlight.delete(root)
|
||||
})
|
||||
loadInFlight.set(root, p)
|
||||
return p
|
||||
}
|
||||
|
||||
async function doLoad(root: string): Promise<GitLoad | null> {
|
||||
if (!(await isRepo(root))) return null
|
||||
|
||||
const status = await g.status()
|
||||
const branch = status.current || 'HEAD'
|
||||
// A branch switch / checkout from an external tool (Sublime Merge, the CLI)
|
||||
// rewrites .git and briefly holds .git/index.lock; a status that lands in
|
||||
// that window fails. Retry so we settle on the real new state instead of
|
||||
// rejecting (which would leave the git column stale or blank).
|
||||
let raw: string | null = null
|
||||
for (let attempt = 0; attempt < 6; attempt++) {
|
||||
try {
|
||||
raw = await git(root, ['status', '--porcelain', '-b', '--untracked-files=all', '-z'])
|
||||
break
|
||||
} catch (err) {
|
||||
if (attempt === 5) throw err
|
||||
await delay(120)
|
||||
}
|
||||
}
|
||||
if (raw == null) return null
|
||||
|
||||
const { branch, files } = parseStatus(raw)
|
||||
const changes: GitChange[] = []
|
||||
for (const f of status.files) {
|
||||
// simple-git uses path "from -> to" for renames; take the destination.
|
||||
const path = f.path.includes(' -> ') ? f.path.split(' -> ').pop()! : f.path
|
||||
const { letter, staged } = classify(f.index, f.working_dir)
|
||||
for (const f of files) {
|
||||
const { letter, staged } = classify(f.index, f.working)
|
||||
const isNew = f.index === '?' || f.index === 'A'
|
||||
const isDeleted = letter === 'D'
|
||||
const original = isNew ? '' : await headText(g, path)
|
||||
const updated = isDeleted ? '' : await diskText(root, path)
|
||||
changes.push({ path, status: letter, staged, original, updated })
|
||||
const original = isNew ? '' : await headText(root, f.path)
|
||||
const updated = isDeleted ? '' : await diskText(root, f.path)
|
||||
changes.push({ path: f.path, status: letter, staged, original, updated })
|
||||
}
|
||||
|
||||
return { branch, changes }
|
||||
@@ -87,20 +208,19 @@ export async function load(root: string): Promise<GitLoad | null> {
|
||||
|
||||
export async function stage(root: string, paths: string[]): Promise<void> {
|
||||
// `git add` stages modifications, additions AND deletions of the given paths.
|
||||
await git(root).add(paths)
|
||||
await git(root, ['add', '--', ...paths])
|
||||
}
|
||||
|
||||
export async function unstage(root: string, paths: string[]): Promise<void> {
|
||||
try {
|
||||
await git(root).reset(['--', ...paths])
|
||||
} catch {
|
||||
const { code } = await runGit(root, ['reset', '--', ...paths])
|
||||
if (code !== 0) {
|
||||
// empty repo (no HEAD yet): fall back to removing from the index.
|
||||
await git(root).raw(['rm', '--cached', '-r', '--', ...paths])
|
||||
await git(root, ['rm', '--cached', '-r', '--', ...paths])
|
||||
}
|
||||
}
|
||||
|
||||
export async function commit(root: string, message: string): Promise<void> {
|
||||
await git(root).commit(message)
|
||||
await git(root, ['commit', '-m', message])
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -109,13 +229,12 @@ export async function commit(root: string, message: string): Promise<void> {
|
||||
* - not in HEAD → a new file (staged or untracked): unstage + delete from disk
|
||||
*/
|
||||
export async function discard(root: string, paths: string[]): Promise<void> {
|
||||
const g = git(root)
|
||||
for (const p of paths) {
|
||||
const inHead = await g.raw(['cat-file', '-e', `HEAD:${p}`]).then(() => true).catch(() => false)
|
||||
if (inHead) {
|
||||
await g.checkout(['HEAD', '--', p])
|
||||
const { code } = await runGit(root, ['cat-file', '-e', `HEAD:${p}`])
|
||||
if (code === 0) {
|
||||
await git(root, ['checkout', 'HEAD', '--', p])
|
||||
} else {
|
||||
try { await g.raw(['reset', '-q', 'HEAD', '--', p]) } catch { /* no HEAD / not staged */ }
|
||||
await runGit(root, ['reset', '-q', 'HEAD', '--', p]) // no HEAD / not staged — ignore result
|
||||
await rm(join(root, p), { force: true })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user