diff --git a/src/cli.ts b/src/cli.ts index 12ab2fd..dd2fad4 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -315,7 +315,7 @@ program program .command("diff") .argument("", "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 ─────────────────────────────────────────────────────── diff --git a/src/git.ts b/src/git.ts index 5196985..69a869d 100644 --- a/src/git.ts +++ b/src/git.ts @@ -112,3 +112,10 @@ export async function commitMerge(cwd: string): Promise { export async function abortMerge(cwd: string): Promise { await $`git merge --abort`.cwd(cwd).nothrow().quiet() } + +/** Detect the main branch name (main or master). */ +export async function mainBranch(cwd?: string): Promise { + const dir = cwd ?? "." + const result = await $`git rev-parse --verify --quiet refs/heads/main`.cwd(dir).nothrow().quiet() + return result.exitCode === 0 ? "main" : "master" +}