sandlot/src/commands/diff.ts

38 lines
1.2 KiB
TypeScript

import { $ } from "bun"
import * as git from "../git.ts"
import { requireSession } from "./helpers.ts"
export async function action(branch: string) {
const { session } = await requireSession(branch)
// Check for uncommitted changes (staged + unstaged)
const status = await $`git -C ${session.worktree} status --porcelain`.nothrow().quiet()
if (status.exitCode !== 0) {
console.error("✖ git status failed")
process.exit(1)
}
let args: string[]
if (status.text().trim().length > 0) {
// Show uncommitted changes (both staged and unstaged)
const hasHead = await $`git -C ${session.worktree} rev-parse --verify HEAD`.nothrow().quiet()
args = hasHead.exitCode === 0 ? ["diff", "HEAD"] : ["diff"]
} else {
// No uncommitted changes — show full branch diff vs main
const main = await git.mainBranch(session.worktree)
args = ["diff", `${main}...${branch}`]
}
// Run git diff with inherited stdio so external diff tools (e.g. difftastic)
// see a real TTY and git can use its own pager
const proc = Bun.spawn(["git", "-C", session.worktree, ...args], {
stdin: "inherit",
stdout: "inherit",
stderr: "inherit",
})
const exitCode = await proc.exited
if (exitCode !== 0) {
process.exit(exitCode)
}
}