diff: show uncommitted changes or full branch diff vs main when working tree is clean
This commit is contained in:
parent
586e72610a
commit
2a90c59b3b
22
src/cli.ts
22
src/cli.ts
|
|
@ -315,7 +315,7 @@ program
|
||||||
program
|
program
|
||||||
.command("diff")
|
.command("diff")
|
||||||
.argument("<branch>", "branch name")
|
.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) => {
|
.action(async (branch: string) => {
|
||||||
const root = await git.repoRoot()
|
const root = await git.repoRoot()
|
||||||
const session = await state.getSession(root, branch)
|
const session = await state.getSession(root, branch)
|
||||||
|
|
@ -324,11 +324,29 @@ program
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await $`git -C ${session.worktree} diff`.nothrow()
|
// 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) {
|
if (result.exitCode !== 0) {
|
||||||
console.error("git diff failed")
|
console.error("git diff failed")
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// ── sandlot vm ───────────────────────────────────────────────────────
|
// ── sandlot vm ───────────────────────────────────────────────────────
|
||||||
|
|
|
||||||
|
|
@ -112,3 +112,10 @@ export async function commitMerge(cwd: string): Promise<void> {
|
||||||
export async function abortMerge(cwd: string): Promise<void> {
|
export async function abortMerge(cwd: string): Promise<void> {
|
||||||
await $`git merge --abort`.cwd(cwd).nothrow().quiet()
|
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"
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user