diff: show uncommitted changes or full branch diff vs main when working tree is clean

This commit is contained in:
Chris Wanstrath 2026-02-19 10:14:11 -08:00
parent 586e72610a
commit 2a90c59b3b
2 changed files with 29 additions and 4 deletions

View File

@ -315,7 +315,7 @@ program
program
.command("diff")
.argument("<branch>", "branch name")
.description("Show unstaged changes in a session's worktree")
.description("Show uncommitted changes, or full branch diff vs main")
.action(async (branch: string) => {
const root = await git.repoRoot()
const session = await state.getSession(root, branch)
@ -324,11 +324,29 @@ program
process.exit(1)
}
const result = await $`git -C ${session.worktree} diff`.nothrow()
if (result.exitCode !== 0) {
console.error("git diff failed")
// 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)
}
if (status.text().trim().length > 0) {
// Show uncommitted changes (both staged and unstaged)
const result = await $`git -C ${session.worktree} diff HEAD`.nothrow()
if (result.exitCode !== 0) {
// HEAD may not exist yet (no commits); fall back to showing all tracked + untracked
await $`git -C ${session.worktree} diff`.nothrow()
}
} else {
// No uncommitted changes — show full branch diff vs main
const main = await git.mainBranch(root)
const result = await $`git -C ${session.worktree} diff ${main}...${branch}`.nothrow()
if (result.exitCode !== 0) {
console.error("git diff failed")
process.exit(1)
}
}
})
// ── sandlot vm ───────────────────────────────────────────────────────

View File

@ -112,3 +112,10 @@ export async function commitMerge(cwd: string): Promise<void> {
export async function abortMerge(cwd: string): Promise<void> {
await $`git merge --abort`.cwd(cwd).nothrow().quiet()
}
/** Detect the main branch name (main or master). */
export async function mainBranch(cwd?: string): Promise<string> {
const dir = cwd ?? "."
const result = await $`git rev-parse --verify --quiet refs/heads/main`.cwd(dir).nothrow().quiet()
return result.exitCode === 0 ? "main" : "master"
}