Improve git status details and streamline quick action flows

- add working-tree file/line stats to git status contracts, server responses, and tests
- refactor web git queries/mutations into shared React Query helpers
- update git quick-action logic/UI to prefer commit/push/PR flows and clearer disabled states
- hide branch toolbar for non-repos and tighten related chat/toolbar behavior
This commit is contained in:
Julius Marminge authored and Julius Marminge committed 2026-02-14 23:35:48 -08:00
1 parent 5a30594009
commit 85eb379dd7
12 files changed
+871 -353

No files matched your search

+85 -2
View File
@@ -94,6 +94,51 @@ function parseBranchAb(value: string): { ahead: number; behind: number } {
};
}
function parseNumstatEntries(
stdout: string,
): Array<{ path: string; insertions: number; deletions: number }> {
const entries: Array<{ path: string; insertions: number; deletions: number }> = [];
for (const line of stdout.split(/\r?\n/g)) {
if (line.trim().length === 0) continue;
const [addedRaw, deletedRaw, ...pathParts] = line.split("\t");
const rawPath = pathParts.length > 1 ? (pathParts.at(-1) ?? "").trim() : pathParts.join("\t").trim();
if (rawPath.length === 0) continue;
const added = Number.parseInt(addedRaw ?? "0", 10);
const deleted = Number.parseInt(deletedRaw ?? "0", 10);
const renameArrowIndex = rawPath.indexOf(" => ");
const normalizedPath =
renameArrowIndex >= 0 ? rawPath.slice(renameArrowIndex + " => ".length).trim() : rawPath;
entries.push({
path: normalizedPath.length > 0 ? normalizedPath : rawPath,
insertions: Number.isFinite(added) ? added : 0,
deletions: Number.isFinite(deleted) ? deleted : 0,
});
}
return entries;
}
function parsePorcelainPath(line: string): string | null {
if (line.startsWith("? ") || line.startsWith("! ")) {
const simple = line.slice(2).trim();
return simple.length > 0 ? simple : null;
}
if (!(line.startsWith("1 ") || line.startsWith("2 ") || line.startsWith("u "))) {
return null;
}
const tabIndex = line.indexOf("\t");
if (tabIndex >= 0) {
const fromTab = line.slice(tabIndex + 1);
const [path] = fromTab.split("\t");
return path?.trim().length ? path.trim() : null;
}
const parts = line.trim().split(/\s+/g);
const path = parts.at(-1) ?? "";
return path.length > 0 ? path : null;
}
function commandLabel(args: readonly string[]): string {
return `git ${args.join(" ")}`;
}
@@ -176,6 +221,7 @@ export class GitCoreService {
return gitStatusResultSchema.parse({
branch: details.branch,
hasWorkingTreeChanges: details.hasWorkingTreeChanges,
workingTree: details.workingTree,
hasUpstream: details.hasUpstream,
aheadCount: details.aheadCount,
behindCount: details.behindCount,
@@ -184,15 +230,20 @@ export class GitCoreService {
}
async statusDetails(cwd: string): Promise<GitStatusDetails> {
const stdout = await this.gitStdout(cwd, ["status", "--porcelain=2", "--branch"]);
const [statusStdout, unstagedNumstatStdout, stagedNumstatStdout] = await Promise.all([
this.gitStdout(cwd, ["status", "--porcelain=2", "--branch"]),
this.gitStdout(cwd, ["diff", "--numstat"]),
this.gitStdout(cwd, ["diff", "--cached", "--numstat"]),
]);
let branch: string | null = null;
let upstreamRef: string | null = null;
let aheadCount = 0;
let behindCount = 0;
let hasWorkingTreeChanges = false;
const changedFilesWithoutNumstat = new Set<string>();
for (const line of stdout.split(/\r?\n/g)) {
for (const line of statusStdout.split(/\r?\n/g)) {
if (line.startsWith("# branch.head ")) {
const value = line.slice("# branch.head ".length).trim();
branch = value.startsWith("(") ? null : value;
@@ -212,13 +263,45 @@ export class GitCoreService {
}
if (line.trim().length > 0 && !line.startsWith("#")) {
hasWorkingTreeChanges = true;
const pathValue = parsePorcelainPath(line);
if (pathValue) changedFilesWithoutNumstat.add(pathValue);
}
}
const stagedEntries = parseNumstatEntries(stagedNumstatStdout);
const unstagedEntries = parseNumstatEntries(unstagedNumstatStdout);
const fileStatMap = new Map<string, { insertions: number; deletions: number }>();
for (const entry of [...stagedEntries, ...unstagedEntries]) {
const existing = fileStatMap.get(entry.path) ?? { insertions: 0, deletions: 0 };
existing.insertions += entry.insertions;
existing.deletions += entry.deletions;
fileStatMap.set(entry.path, existing);
}
let insertions = 0;
let deletions = 0;
const files = Array.from(fileStatMap.entries())
.map(([path, stat]) => {
insertions += stat.insertions;
deletions += stat.deletions;
return { path, insertions: stat.insertions, deletions: stat.deletions };
})
.toSorted((a, b) => a.path.localeCompare(b.path));
for (const filePath of changedFilesWithoutNumstat) {
if (fileStatMap.has(filePath)) continue;
files.push({ path: filePath, insertions: 0, deletions: 0 });
}
files.sort((a, b) => a.path.localeCompare(b.path));
return {
branch,
upstreamRef,
hasWorkingTreeChanges,
workingTree: {
files,
insertions,
deletions,
},
hasUpstream: upstreamRef !== null,
aheadCount,
behindCount,
+1
View File
@@ -218,6 +218,7 @@ export class GitManager {
return {
branch: details.branch,
hasWorkingTreeChanges: details.hasWorkingTreeChanges,
workingTree: details.workingTree,
hasUpstream: details.hasUpstream,
aheadCount: details.aheadCount,
behindCount: details.behindCount,
+10
View File
@@ -557,6 +557,11 @@ describe("WebSocket Server", () => {
status: vi.fn().mockResolvedValue({
branch: "feature/test",
hasWorkingTreeChanges: true,
workingTree: {
files: [{ path: "src/index.ts", insertions: 7, deletions: 2 }],
insertions: 7,
deletions: 2,
},
hasUpstream: false,
aheadCount: 0,
behindCount: 0,
@@ -581,6 +586,11 @@ describe("WebSocket Server", () => {
expect(response.result).toEqual({
branch: "feature/test",
hasWorkingTreeChanges: true,
workingTree: {
files: [{ path: "src/index.ts", insertions: 7, deletions: 2 }],
insertions: 7,
deletions: 2,
},
hasUpstream: false,
aheadCount: 0,
behindCount: 0,
+118 -152
View File
@@ -3,6 +3,11 @@ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useEffect, useState } from "react";
import { useNativeApi } from "../hooks/useNativeApi";
import {
gitBranchesQueryOptions,
gitCheckoutMutationOptions,
gitCreateBranchAndCheckoutMutationOptions,
} from "../lib/gitReactQuery";
import { useStore } from "../store";
import { Button } from "./ui/button";
import {
@@ -37,28 +42,19 @@ export default function BranchToolbar({ envMode, onEnvModeChange, envLocked }: B
const activeThreadId = activeThread?.id;
const activeThreadBranch = activeThread?.branch ?? null;
const activeWorktreePath = activeThread?.worktreePath ?? null;
const branchCwd = activeWorktreePath ?? activeProject?.cwd;
const branchCwd = activeWorktreePath ?? activeProject?.cwd ?? null;
// ── Queries ───────────────────────────────────────────────────────────
const branchesQuery = useQuery({
queryKey: ["git", "branches", branchCwd],
queryFn: () => api!.git.listBranches({ cwd: branchCwd! }),
enabled: !!api && !!branchCwd,
});
const branchesQuery = useQuery(gitBranchesQueryOptions(api, branchCwd));
const branches = branchesQuery.data?.branches ?? [];
const branchNames = branches.map((branch) => branch.name);
const branchByName = new Map(branches.map((branch) => [branch.name, branch]));
// Default to true while loading — showing "Initialize git" during a fetch is wrong,
// and worktrees are inherently git repos.
const isRepo = branchesQuery.data?.isRepo ?? !branchesQuery.isLoading;
// ── Mutations ─────────────────────────────────────────────────────────
const checkoutMutation = useMutation({
mutationFn: (branch: string) => api!.git.checkout({ cwd: branchCwd!, branch }),
onSuccess: () => queryClient.invalidateQueries({ queryKey: ["git", "branches", branchCwd] }),
...gitCheckoutMutationOptions({ api, cwd: branchCwd, queryClient }),
onError: (error) => {
setThreadError(error instanceof Error ? error.message : "Failed to checkout branch.");
setIsBranchMenuOpen(true);
@@ -66,25 +62,11 @@ export default function BranchToolbar({ envMode, onEnvModeChange, envLocked }: B
});
const createBranchMutation = useMutation({
mutationFn: (branch: string) =>
api!.git
.createBranch({ cwd: branchCwd!, branch })
.then(() => api!.git.checkout({ cwd: branchCwd!, branch })),
onSuccess: () => queryClient.invalidateQueries({ queryKey: ["git", "branches", branchCwd] }),
...gitCreateBranchAndCheckoutMutationOptions({ api, cwd: branchCwd, queryClient }),
onError: (error) =>
setThreadError(error instanceof Error ? error.message : "Failed to create branch."),
});
const initMutation = useMutation({
mutationFn: () => api!.git.init({ cwd: branchCwd! }),
onSuccess: () => {
setThreadError(null);
queryClient.invalidateQueries({ queryKey: ["git", "branches", branchCwd] });
},
onError: (error) =>
setThreadError(error instanceof Error ? error.message : "Failed to initialize git repo."),
});
// ── Effects ───────────────────────────────────────────────────────────
// Keep thread branch synced to git current branch for local threads.
@@ -194,134 +176,118 @@ export default function BranchToolbar({ envMode, onEnvModeChange, envLocked }: B
)}
</div>
{!isRepo ? (
<button
type="button"
className="inline-flex items-center gap-1.5 rounded-lg border border-border px-2 py-1 text-[12px] text-muted-foreground/70 transition-colors duration-150 hover:bg-accent hover:text-foreground/80 disabled:cursor-not-allowed disabled:opacity-50"
disabled={!branchCwd || initMutation.isPending}
onClick={() => initMutation.mutate()}
<Combobox
items={branchNames}
autoHighlight
onOpenChange={(open) => setIsBranchMenuOpen(open)}
open={isBranchMenuOpen}
value={activeThread.branch}
>
<ComboboxTrigger
render={<Button variant="ghost" size="xs" />}
className="text-muted-foreground/70 hover:text-foreground/80"
disabled={branchesQuery.isLoading}
>
{initMutation.isPending ? "Initializing\u2026" : "Initialize git"}
</button>
) : (
<Combobox
items={branchNames}
autoHighlight
onOpenChange={(open) => setIsBranchMenuOpen(open)}
open={isBranchMenuOpen}
value={activeThread.branch}
>
<ComboboxTrigger
render={<Button variant="ghost" size="xs" />}
className="text-muted-foreground/70 hover:text-foreground/80"
disabled={branchesQuery.isLoading}
>
<span className="max-w-[240px] truncate">
{activeThread.branch
? envMode === "worktree" && !activeWorktreePath
? `From ${activeThread.branch}`
: activeThread.branch
: "Select branch"}
</span>
<ChevronDownIcon />
</ComboboxTrigger>
<ComboboxPopup align="end" side="top" className="w-64">
<div className="border-b">
<ComboboxInput
className="rounded-b-none before:rounded-b-none [&_input]:font-sans"
placeholder="Search branches..."
showClear
showTrigger={false}
size="sm"
/>
</div>
<ComboboxEmpty>No branches found.</ComboboxEmpty>
<span className="max-w-[240px] truncate">
{activeThread.branch
? envMode === "worktree" && !activeWorktreePath
? `From ${activeThread.branch}`
: activeThread.branch
: "Select branch"}
</span>
<ChevronDownIcon />
</ComboboxTrigger>
<ComboboxPopup align="end" side="top" className="w-64">
<div className="border-b">
<ComboboxInput
className="rounded-b-none before:rounded-b-none [&_input]:font-sans"
placeholder="Search branches..."
showClear
showTrigger={false}
size="sm"
/>
</div>
<ComboboxEmpty>No branches found.</ComboboxEmpty>
<ComboboxList className="max-h-56">
{(branchName) => {
const branch = branchByName.get(branchName);
if (!branch) return null;
<ComboboxList className="max-h-56">
{(branchName) => {
const branch = branchByName.get(branchName);
if (!branch) return null;
const hasSecondaryWorktree =
branch.worktreePath && branch.worktreePath !== activeProject.cwd;
return (
<ComboboxItem
hideIndicator
key={branchName}
value={branchName}
className={branchName === activeThread.branch ? "bg-accent text-foreground" : undefined}
onClick={() => selectBranch(branch)}
>
<div className="flex w-full items-center justify-between gap-2">
<span className="truncate">{branchName}</span>
{(branch.current || branch.isDefault || hasSecondaryWorktree) && (
<span className="shrink-0 text-[10px] text-muted-foreground/45">
{branch.current
? "current"
: hasSecondaryWorktree
? "worktree"
: "default"}
</span>
)}
</div>
</ComboboxItem>
);
}}
</ComboboxList>
{envMode === "local" && (
<>
{isCreatingBranch ? (
<form
onSubmit={(event) => {
event.preventDefault();
createBranch();
}}
>
<InputGroup className="rounded-t-none before:rounded-t-none">
<InputGroupInput
className="[&_input]:font-sans"
size="sm"
placeholder="branch-name"
type="text"
value={newBranchName}
onChange={(event) => setNewBranchName(event.target.value)}
onKeyDown={(event) => {
if (event.key === "Escape") {
event.stopPropagation();
setIsCreatingBranch(false);
setNewBranchName("");
}
}}
autoFocus
/>
<InputGroupAddon align="inline-end">
<Button
size="xs"
variant="secondary"
type="submit"
disabled={!newBranchName.trim() || createBranchMutation.isPending}
>
Create
</Button>
</InputGroupAddon>
</InputGroup>
</form>
) : (
<Button
size="sm"
className="w-full justify-start h-9 rounded-t-none rounded-b-lg before:rounded-t-none"
variant="outline"
onClick={() => setIsCreatingBranch(true)}
>
<PlusIcon />
New branch
</Button>
)}
</>
)}
</ComboboxPopup>
</Combobox>
)}
const hasSecondaryWorktree = branch.worktreePath && branch.worktreePath !== activeProject.cwd;
return (
<ComboboxItem
hideIndicator
key={branchName}
value={branchName}
className={branchName === activeThread.branch ? "bg-accent text-foreground" : undefined}
onClick={() => selectBranch(branch)}
>
<div className="flex w-full items-center justify-between gap-2">
<span className="truncate">{branchName}</span>
{(branch.current || branch.isDefault || hasSecondaryWorktree) && (
<span className="shrink-0 text-[10px] text-muted-foreground/45">
{branch.current ? "current" : hasSecondaryWorktree ? "worktree" : "default"}
</span>
)}
</div>
</ComboboxItem>
);
}}
</ComboboxList>
{envMode === "local" && (
<>
{isCreatingBranch ? (
<form
onSubmit={(event) => {
event.preventDefault();
createBranch();
}}
>
<InputGroup className="rounded-t-none before:rounded-t-none">
<InputGroupInput
className="[&_input]:font-sans"
size="sm"
placeholder="branch-name"
type="text"
value={newBranchName}
onChange={(event) => setNewBranchName(event.target.value)}
onKeyDown={(event) => {
if (event.key === "Escape") {
event.stopPropagation();
setIsCreatingBranch(false);
setNewBranchName("");
}
}}
autoFocus
/>
<InputGroupAddon align="inline-end">
<Button
size="xs"
variant="secondary"
type="submit"
disabled={!newBranchName.trim() || createBranchMutation.isPending}
>
Create
</Button>
</InputGroupAddon>
</InputGroup>
</form>
) : (
<Button
size="sm"
className="w-full justify-start h-9 rounded-t-none rounded-b-lg before:rounded-t-none"
variant="outline"
onClick={() => setIsCreatingBranch(true)}
>
<PlusIcon />
New branch
</Button>
)}
</>
)}
</ComboboxPopup>
</Combobox>
</div>
);
}
+8 -3
View File
@@ -19,6 +19,8 @@ import {
useRef,
useState,
} from "react";
import { useQuery } from "@tanstack/react-query";
import { gitBranchesQueryOptions } from "~/lib/gitReactQuery";
import { EDITORS, type EditorId } from "@t3tools/contracts";
import { isElectron } from "../env";
@@ -244,6 +246,9 @@ export default function ChatView() {
sandboxMode: "workspace-write",
} as const);
const gitCwd = activeThread?.worktreePath ?? activeProject?.cwd ?? null;
const branchesQuery = useQuery(gitBranchesQueryOptions(api, gitCwd));
// Default true while loading to avoid toolbar flicker.
const isGitRepo = branchesQuery.data?.isRepo ?? true;
const envLocked = Boolean(
activeThread &&
@@ -1123,7 +1128,7 @@ export default function ChatView() {
</div>
{/* Input bar */}
<div className="px-5 pb-1 pt-2">
<div className={cn("px-5 pt-2", isGitRepo ? "pb-1" : "pb-4")}>
<form onSubmit={onSend} className="mx-auto max-w-3xl">
<div
className={`group rounded-[20px] border bg-card transition-colors duration-200 focus-within:border-ring ${
@@ -1299,7 +1304,7 @@ export default function ChatView() {
</form>
</div>
<BranchToolbar envMode={envMode} onEnvModeChange={setEnvMode} envLocked={envLocked} />
{isGitRepo && <BranchToolbar envMode={envMode} onEnvModeChange={setEnvMode} envLocked={envLocked} />}
{activeThread.terminalOpen && api && activeProject && (
<ThreadTerminalDrawer
@@ -1436,8 +1441,8 @@ function OpenInPicker() {
const api = useNativeApi();
const { state } = useStore();
const activeProject = state.projects.find((p) => p.id === state.activeThreadId);
const activeThread = state.threads.find((t) => t.id === state.activeThreadId);
const activeProject = state.projects.find((p) => p.id === activeThread?.projectId);
const openInEditor = useCallback(
(editorId: EditorId | null) => {
@@ -10,6 +10,11 @@ function status(overrides: Partial<GitStatusResult> = {}): GitStatusResult {
return {
branch: "feature/test",
hasWorkingTreeChanges: false,
workingTree: {
files: [],
insertions: 0,
deletions: 0,
},
hasUpstream: true,
aheadCount: 0,
behindCount: 0,
@@ -81,7 +86,7 @@ describe("when: actions are busy", () => {
const quick = resolveQuickAction(status(), true);
assert.deepInclude(quick, {
kind: "show_hint",
label: "Running...",
label: "Commit",
disabled: true,
hint: "Git action in progress.",
});
@@ -123,7 +128,7 @@ describe("when: git status is unavailable", () => {
const quick = resolveQuickAction(null, false);
assert.deepInclude(quick, {
kind: "show_hint",
label: "Git actions",
label: "Commit",
disabled: true,
hint: "Git status is unavailable.",
});
@@ -314,9 +319,34 @@ describe("when: branch is behind upstream", () => {
});
describe("when: working tree has local changes", () => {
it("resolveQuickAction returns commit", () => {
it("resolveQuickAction returns commit, push, and create PR", () => {
const quick = resolveQuickAction(status({ hasWorkingTreeChanges: true }), false);
assert.deepInclude(quick, { kind: "run_action", action: "commit", label: "Commit" });
assert.deepInclude(quick, {
kind: "run_action",
action: "commit_push_pr",
label: "Commit, push & create PR",
});
});
it("resolveQuickAction returns commit and push when open PR exists", () => {
const quick = resolveQuickAction(
status({
hasWorkingTreeChanges: true,
openPr: {
number: 16,
title: "Existing PR",
url: "https://example.com/pr/16",
baseBranch: "main",
headBranch: "feature/test",
},
}),
false,
);
assert.deepInclude(quick, {
kind: "run_action",
action: "commit_push",
label: "Commit & push",
});
});
it("buildMenuItems enables commit and disables push and PR", () => {
@@ -350,10 +380,36 @@ describe("when: working tree has local changes", () => {
});
});
describe("when: on default branch without open PR", () => {
it("resolveQuickAction returns commit and push when local changes exist", () => {
const quick = resolveQuickAction(status({ branch: "main", hasWorkingTreeChanges: true }), false, true);
assert.deepInclude(quick, {
kind: "run_action",
action: "commit_push",
label: "Commit & push",
disabled: false,
});
});
it("resolveQuickAction returns push when branch is ahead", () => {
const quick = resolveQuickAction(status({ branch: "main", aheadCount: 2, openPr: null }), false, true);
assert.deepInclude(quick, {
kind: "run_action",
action: "commit_push",
label: "Push",
disabled: false,
});
});
});
describe("when: working tree has local changes and branch is behind upstream", () => {
it("resolveQuickAction still prefers commit", () => {
it("resolveQuickAction still prefers commit, push, and create PR", () => {
const quick = resolveQuickAction(status({ hasWorkingTreeChanges: true, behindCount: 1 }), false);
assert.deepInclude(quick, { kind: "run_action", action: "commit", label: "Commit" });
assert.deepInclude(quick, {
kind: "run_action",
action: "commit_push_pr",
label: "Commit, push & create PR",
});
});
it("buildMenuItems enables commit and keeps push and PR disabled", () => {
@@ -393,7 +449,7 @@ describe("when: HEAD is detached and there are no local changes", () => {
status({ branch: null, hasWorkingTreeChanges: false, hasUpstream: false }),
false,
);
assert.deepInclude(quick, { kind: "show_hint", label: "Detached HEAD", disabled: false });
assert.deepInclude(quick, { kind: "show_hint", label: "Commit", disabled: true });
});
it("buildMenuItems keeps commit, push, and PR disabled", () => {
@@ -428,17 +484,16 @@ describe("when: HEAD is detached and there are no local changes", () => {
});
describe("when: branch has no upstream configured", () => {
it("resolveQuickAction runs push and create PR when clean and no open PR", () => {
it("resolveQuickAction returns disabled commit state when clean and no commits are ahead", () => {
const quick = resolveQuickAction(status({ hasUpstream: false, openPr: null, aheadCount: 0 }), false);
assert.deepInclude(quick, {
kind: "run_action",
action: "commit_push_pr",
label: "Push & create PR",
disabled: false,
kind: "show_hint",
label: "Commit",
disabled: true,
});
});
it("resolveQuickAction opens PR when clean, no upstream, and no local commits ahead", () => {
it("resolveQuickAction remains disabled when clean, no upstream, and no local commits ahead", () => {
const quick = resolveQuickAction(
status({
hasUpstream: false,
@@ -454,9 +509,9 @@ describe("when: branch has no upstream configured", () => {
false,
);
assert.deepInclude(quick, {
kind: "open_pr",
label: "Open PR",
disabled: false,
kind: "show_hint",
label: "Commit",
disabled: true,
});
});
@@ -483,7 +538,7 @@ describe("when: branch has no upstream configured", () => {
});
});
it("buildMenuItems enables push and disables create PR", () => {
it("buildMenuItems disables push and create PR when no commits are ahead", () => {
const items = buildMenuItems(status({ hasUpstream: false, openPr: null, aheadCount: 0 }), false);
assert.deepEqual(items, [
{
@@ -497,7 +552,7 @@ describe("when: branch has no upstream configured", () => {
{
id: "push",
label: "Push",
disabled: false,
disabled: true,
icon: "push",
kind: "open_dialog",
dialogAction: "push",
@@ -61,7 +61,7 @@ export function buildMenuItems(
hasBranch &&
!hasChanges &&
!isBehind &&
(gitStatus.aheadCount > 0 || !gitStatus.hasUpstream);
gitStatus.aheadCount > 0;
const canCreatePr =
!isBusy &&
hasBranch &&
@@ -111,14 +111,15 @@ export function buildMenuItems(
export function resolveQuickAction(
gitStatus: GitStatusResult | null,
isBusy: boolean,
isDefaultBranch = false,
): GitQuickAction {
if (isBusy) {
return { label: "Running...", disabled: true, kind: "show_hint", hint: "Git action in progress." };
return { label: "Commit", disabled: true, kind: "show_hint", hint: "Git action in progress." };
}
if (!gitStatus) {
return {
label: "Git actions",
label: "Commit",
disabled: true,
kind: "show_hint",
hint: "Git status is unavailable.",
@@ -128,14 +129,31 @@ export function resolveQuickAction(
const hasBranch = gitStatus.branch !== null;
const hasChanges = gitStatus.hasWorkingTreeChanges;
const hasOpenPr = gitStatus.openPr !== null;
const isAhead = gitStatus.aheadCount > 0;
const isBehind = gitStatus.behindCount > 0;
const canPush = hasBranch && !hasChanges && !isBehind;
if (!hasChanges && hasOpenPr && gitStatus.aheadCount === 0 && !isBehind) {
return { label: "Open PR", disabled: false, kind: "open_pr" };
if (!hasBranch) {
return {
label: "Commit",
disabled: true,
kind: "show_hint",
hint: "Create and checkout a branch before pushing or opening a PR.",
};
}
if (!hasChanges && isBehind) {
if (hasChanges) {
if (hasOpenPr || isDefaultBranch) {
return { label: "Commit & push", disabled: false, kind: "run_action", action: "commit_push" };
}
return {
label: "Commit, push & create PR",
disabled: false,
kind: "run_action",
action: "commit_push_pr",
};
}
if (isBehind) {
return {
label: "Pull",
disabled: false,
@@ -143,8 +161,8 @@ export function resolveQuickAction(
};
}
if (!hasChanges && canPush && (gitStatus.aheadCount > 0 || !gitStatus.hasUpstream)) {
if (hasOpenPr) {
if (isAhead) {
if (hasOpenPr || isDefaultBranch) {
return { label: "Push", disabled: false, kind: "run_action", action: "commit_push" };
}
return {
@@ -155,36 +173,16 @@ export function resolveQuickAction(
};
}
if (hasChanges) {
return { label: "Commit", disabled: false, kind: "run_action", action: "commit" };
}
if (hasOpenPr) {
if (hasOpenPr && gitStatus.hasUpstream) {
return { label: "Open PR", disabled: false, kind: "open_pr" };
}
if (hasBranch && gitStatus.hasUpstream && !isBehind) {
if (gitStatus.aheadCount > 0) {
return { label: "Create PR", disabled: false, kind: "run_action", action: "commit_push_pr" };
}
return {
label: "Commit",
disabled: true,
kind: "show_hint",
hint: "Branch is up to date. Nothing to commit, push, pull, or open as a PR.",
};
}
if (!hasBranch) {
return {
label: "Detached HEAD",
disabled: false,
kind: "show_hint",
hint: "Create and checkout a branch before pushing or opening a PR.",
};
}
return { label: "Commit", disabled: true, kind: "show_hint", hint: "No git action needed." };
return {
label: "Commit",
disabled: true,
kind: "show_hint",
hint: "Branch is up to date. Nothing to commit, push, pull, or open as a PR.",
};
}
export function requiresDefaultBranchConfirmation(
+386 -138
View File
@@ -1,8 +1,9 @@
import {
type GitStatusResult,
type GitStackedAction,
type NativeApi,
} from "@t3tools/contracts";
import { queryOptions, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useCallback, useMemo, useState } from "react";
import { ChevronDownIcon, CloudUploadIcon, GitCommitIcon, InfoIcon } from "lucide-react";
import { GitHubIcon } from "./Icons";
@@ -16,6 +17,7 @@ import {
requiresDefaultBranchConfirmation,
resolveQuickAction,
} from "./GitActionsControl.logic";
import { Badge } from "~/components/ui/badge";
import { Button } from "~/components/ui/button";
import {
Dialog,
@@ -28,14 +30,97 @@ import {
} from "~/components/ui/dialog";
import { Group, GroupSeparator } from "~/components/ui/group";
import { Menu, MenuItem, MenuPopup, MenuTrigger } from "~/components/ui/menu";
import { Popover, PopoverPopup, PopoverTrigger } from "~/components/ui/popover";
import { Textarea } from "~/components/ui/textarea";
import { toastManager } from "~/components/ui/toast";
import {
gitBranchesQueryOptions,
gitInitMutationOptions,
gitPullMutationOptions,
gitRunStackedActionMutationOptions,
gitStatusQueryOptions,
invalidateGitQueries,
} from "~/lib/gitReactQuery";
import { preferredTerminalEditor, resolvePathLinkTarget } from "~/terminal-links";
interface GitActionsControlProps {
api: NativeApi | undefined;
gitCwd: string | null;
}
function getMenuActionDisabledReason(
item: GitActionMenuItem,
gitStatus: GitStatusResult | null,
isBusy: boolean,
): string | null {
if (!item.disabled) return null;
if (isBusy) return "Git action in progress.";
if (!gitStatus) return "Git status is unavailable.";
const hasBranch = gitStatus.branch !== null;
const hasChanges = gitStatus.hasWorkingTreeChanges;
const hasOpenPr = gitStatus.openPr !== null;
const isAhead = gitStatus.aheadCount > 0;
const isBehind = gitStatus.behindCount > 0;
if (item.id === "commit") {
if (!hasChanges) {
return "Worktree is clean. Make changes before committing.";
}
return "Commit is currently unavailable.";
}
if (item.id === "push") {
if (!hasBranch) {
return "Detached HEAD: checkout a branch before pushing.";
}
if (hasChanges) {
return "Commit or stash local changes before pushing.";
}
if (isBehind) {
return "Branch is behind upstream. Pull/rebase before pushing.";
}
if (!isAhead) {
return "No local commits to push.";
}
return "Push is currently unavailable.";
}
if (hasOpenPr) {
return "Open PR is currently unavailable.";
}
if (!hasBranch) {
return "Detached HEAD: checkout a branch before creating a PR.";
}
if (hasChanges) {
return "Commit local changes before creating a PR.";
}
if (!isAhead) {
return "No local commits to include in a PR.";
}
if (!gitStatus.hasUpstream) {
return "Set an upstream branch before creating a PR.";
}
if (isBehind) {
return "Branch is behind upstream. Pull/rebase before creating a PR.";
}
return "Create PR is currently unavailable.";
}
function formatUpstreamStatus(input: {
hasUpstream: boolean;
aheadCount: number;
behindCount: number;
}): string {
if (!input.hasUpstream) return "No upstream configured";
if (input.aheadCount === 0 && input.behindCount === 0) return "Up to date";
if (input.aheadCount > 0 && input.behindCount > 0) {
return `Diverged (+${input.aheadCount} / -${input.behindCount})`;
}
if (input.aheadCount > 0) return `Ahead by ${input.aheadCount}`;
return `Behind by ${input.behindCount}`;
}
function GitActionItemIcon({ icon }: { icon: GitActionIconName }) {
if (icon === "commit") return <GitCommitIcon />;
if (icon === "push") return <CloudUploadIcon />;
@@ -54,21 +139,20 @@ function GitQuickActionIcon({ quickAction }: { quickAction: GitQuickAction }) {
return <InfoIcon className="size-4" />;
}
function getGitStatusQueryKey(gitCwd: string | null): readonly ["git", "status", string | null] {
return ["git", "status", gitCwd];
}
function gitStatusQueryOptions(api: NativeApi | undefined, gitCwd: string | null) {
return queryOptions({
queryKey: getGitStatusQueryKey(gitCwd),
queryFn: async () => {
if (!api || !gitCwd) {
throw new Error("Git status is unavailable.");
}
return api.git.status({ cwd: gitCwd });
},
enabled: !!api && !!gitCwd,
});
function buildGitActionProgressStages(input: {
action: GitStackedAction;
hasCustomCommitMessage: boolean;
}): string[] {
const commitStages = input.hasCustomCommitMessage
? ["Committing..."]
: ["Generating commit message...", "Committing..."];
if (input.action === "commit") {
return commitStages;
}
if (input.action === "commit_push") {
return [...commitStages, "Pushing..."];
}
return [...commitStages, "Pushing...", "Creating PR..."];
}
export default function GitActionsControl({ api, gitCwd }: GitActionsControlProps) {
@@ -80,41 +164,16 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
gitStatusQueryOptions(api, gitCwd),
);
const { data: branchList = null } = useQuery({
queryKey: ["git", "branches", gitCwd],
queryFn: async () => {
if (!api || !gitCwd) throw new Error("Git branches are unavailable.");
return api.git.listBranches({ cwd: gitCwd });
},
enabled: !!api && !!gitCwd,
});
const { data: branchList = null } = useQuery(gitBranchesQueryOptions(api, gitCwd));
// Default to true while loading so we don't flash init controls.
const isRepo = branchList?.isRepo ?? true;
const runImmediateGitActionMutation = useMutation({
mutationFn: async ({
action,
commitMessage,
}: {
action: GitStackedAction;
commitMessage?: string;
}) => {
if (!api || !gitCwd) {
throw new Error("Git action is unavailable.");
}
return api.git.runStackedAction({ cwd: gitCwd, action, ...(commitMessage ? { commitMessage } : {}) });
},
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: ["git"] });
},
});
const pullMutation = useMutation({
mutationFn: async () => {
if (!api || !gitCwd) throw new Error("Git pull is unavailable.");
return api.git.pull({ cwd: gitCwd });
},
onSuccess: async () => {
await queryClient.invalidateQueries({ queryKey: ["git"] });
},
});
const initMutation = useMutation(gitInitMutationOptions({ api, cwd: gitCwd, queryClient }));
const runImmediateGitActionMutation = useMutation(
gitRunStackedActionMutationOptions({ api, cwd: gitCwd, queryClient }),
);
const pullMutation = useMutation(gitPullMutationOptions({ api, cwd: gitCwd, queryClient }));
const isGitActionRunning = runImmediateGitActionMutation.isPending || pullMutation.isPending;
const isDefaultBranch = useMemo(() => {
@@ -129,13 +188,20 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
[gitStatus, isGitActionRunning],
);
const quickAction = useMemo(
() => resolveQuickAction(gitStatus, isGitActionRunning),
[gitStatus, isGitActionRunning],
() => resolveQuickAction(gitStatus, isGitActionRunning, isDefaultBranch),
[gitStatus, isDefaultBranch, isGitActionRunning],
);
const quickActionDisabledReason = quickAction.disabled
? (quickAction.hint ?? "This action is currently unavailable.")
: null;
const [isQuickActionPopoverOpen, setIsQuickActionPopoverOpen] = useState(false);
const [openDisabledMenuItemId, setOpenDisabledMenuItemId] = useState<GitActionMenuItem["id"] | null>(
null,
);
const refreshGitStatus = useCallback(async () => {
if (!api || !gitCwd) return;
await queryClient.invalidateQueries({ queryKey: ["git"] });
await invalidateGitQueries(queryClient);
}, [api, gitCwd, queryClient]);
const maybeConfirmPushToDefaultBranch = useCallback(
@@ -184,31 +250,60 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
const confirmed = await maybeConfirmPushToDefaultBranch(action);
if (!confirmed) return;
const progressStages = buildGitActionProgressStages({
action,
hasCustomCommitMessage: !!commitMessage?.trim(),
});
const progressToastId = toastManager.add({
type: "loading",
title: progressStages[0] ?? "Running git action...",
timeout: 0,
});
let stageIndex = 0;
const stageInterval = setInterval(() => {
stageIndex = Math.min(stageIndex + 1, progressStages.length - 1);
toastManager.update(progressToastId, {
title: progressStages[stageIndex] ?? "Running git action...",
type: "loading",
timeout: 0,
});
}, 1100);
const stopProgressUpdates = () => {
clearInterval(stageInterval);
};
const promise = runImmediateGitActionMutation.mutateAsync({
action,
...(commitMessage ? { commitMessage } : {}),
});
toastManager.promise(promise, {
loading: {
title:
action === "commit"
? "Committing..."
: action === "commit_push"
? "Pushing..."
: "Creating PR...",
},
success: (result) => {
const prUrl = result.pr.url ?? gitStatus?.openPr?.url;
const shouldOfferCreatePrCta =
action === "commit_push" &&
!prUrl &&
result.push.status === "pushed";
return {
title: "Done",
description: describeGitResult(result),
...(prUrl
try {
const result = await promise;
stopProgressUpdates();
const prUrl = result.pr.url ?? gitStatus?.openPr?.url;
const shouldOfferPushCta = action === "commit" && result.commit.status === "created";
const shouldOfferOpenPrCta =
(action === "commit_push" || action === "commit_push_pr") && !!prUrl;
const shouldOfferCreatePrCta =
action === "commit_push" && !prUrl && result.push.status === "pushed";
toastManager.update(progressToastId, {
type: "success",
title: "Done",
description: describeGitResult(result),
timeout: 10_000,
...(shouldOfferPushCta
? {
actionProps: {
children: "Push",
onClick: () => setActiveDialogAction("push"),
},
}
: shouldOfferOpenPrCta
? {
timeout: 10_000,
actionProps: {
children: "Open PR",
onClick: () => void api?.shell.openExternal(prUrl),
@@ -216,21 +311,21 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
}
: shouldOfferCreatePrCta
? {
timeout: 10_000,
actionProps: {
children: "Create PR",
onClick: () => setActiveDialogAction("create_pr"),
},
}
: {}),
};
},
error: (err) => ({
});
} catch (err) {
stopProgressUpdates();
toastManager.update(progressToastId, {
type: "error",
title: "Action failed",
description: err instanceof Error ? err.message : "An error occurred.",
}),
});
void promise.catch(() => undefined);
});
}
},
[
api,
@@ -309,6 +404,27 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
});
}, [activeDialogAction, dialogCommitMessage, runGitActionWithToast]);
const openChangedFileInEditor = useCallback(
(filePath: string) => {
if (!api || !gitCwd) {
toastManager.add({
type: "error",
title: "Editor opening is unavailable.",
});
return;
}
const target = resolvePathLinkTarget(filePath, gitCwd);
void api.shell.openInEditor(target, preferredTerminalEditor()).catch((error) => {
toastManager.add({
type: "error",
title: "Unable to open file",
description: error instanceof Error ? error.message : "An error occurred.",
});
});
},
[api, gitCwd],
);
const dialogTitle =
activeDialogAction === "commit"
? "Commit changes"
@@ -321,65 +437,144 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
: activeDialogAction === "push"
? "Push this branch now. If this is the default branch, you'll be asked to confirm."
: "Create a pull request using generated title/body content.";
const showCompactStatusCard =
!!gitStatus && gitStatus.hasWorkingTreeChanges && !gitStatus.hasUpstream;
if (!gitCwd) return null;
return (
<>
<Group aria-label="Git actions">
{!isRepo ? (
<Button
variant="outline"
size="xs"
disabled={isGitActionRunning || quickAction.disabled}
onClick={runQuickAction}
disabled={initMutation.isPending}
onClick={() => initMutation.mutate()}
>
<GitQuickActionIcon quickAction={quickAction} />
{quickAction.label}
{initMutation.isPending ? "Initializing..." : "Initialize Git"}
</Button>
<GroupSeparator />
<Menu
onOpenChange={(open) => {
if (open) void refreshGitStatus();
}}
>
<MenuTrigger
render={<Button aria-label="Git action options" size="icon-xs" variant="outline" />}
disabled={isGitActionRunning}
>
<ChevronDownIcon aria-hidden="true" className="size-4" />
</MenuTrigger>
<MenuPopup align="end" sideOffset={4}>
{gitActionMenuItems.map((item) => (
<MenuItem
key={`${item.id}-${item.label}`}
disabled={item.disabled}
onClick={() => {
openDialogForMenuItem(item);
}}
) : (
<Group aria-label="Git actions">
{quickActionDisabledReason ? (
<Popover open={isQuickActionPopoverOpen} onOpenChange={setIsQuickActionPopoverOpen}>
<PopoverTrigger
render={
<span
className="inline-flex cursor-not-allowed"
onBlur={() => setIsQuickActionPopoverOpen(false)}
onFocus={() => setIsQuickActionPopoverOpen(true)}
onMouseEnter={() => setIsQuickActionPopoverOpen(true)}
onMouseLeave={() => setIsQuickActionPopoverOpen(false)}
tabIndex={0}
/>
}
>
<GitActionItemIcon icon={item.icon} />
{item.label}
</MenuItem>
))}
{gitStatus?.branch === null && (
<p className="px-2 py-1.5 text-xs text-warning">
Detached HEAD: create and checkout a branch to enable push and PR actions.
</p>
)}
{gitStatus &&
gitStatus.branch !== null &&
!gitStatus.hasWorkingTreeChanges &&
gitStatus.behindCount > 0 && (
<Button
variant="outline"
size="xs"
disabled={isGitActionRunning || quickAction.disabled}
className="cursor-not-allowed disabled:cursor-not-allowed"
onClick={runQuickAction}
>
<GitQuickActionIcon quickAction={quickAction} />
{quickAction.label}
</Button>
</PopoverTrigger>
<PopoverPopup tooltipStyle side="bottom" align="start">
{quickActionDisabledReason}
</PopoverPopup>
</Popover>
) : (
<Button
variant="outline"
size="xs"
disabled={isGitActionRunning || quickAction.disabled}
onClick={runQuickAction}
>
<GitQuickActionIcon quickAction={quickAction} />
{quickAction.label}
</Button>
)}
<GroupSeparator />
<Menu
onOpenChange={(open) => {
if (open) void refreshGitStatus();
if (!open) setOpenDisabledMenuItemId(null);
}}
>
<MenuTrigger
render={<Button aria-label="Git action options" size="icon-xs" variant="outline" />}
disabled={isGitActionRunning}
>
<ChevronDownIcon aria-hidden="true" className="size-4" />
</MenuTrigger>
<MenuPopup align="end" sideOffset={4}>
{gitActionMenuItems.map((item) => {
const disabledReason = getMenuActionDisabledReason(item, gitStatus, isGitActionRunning);
if (item.disabled && disabledReason) {
return (
<Popover
key={`${item.id}-${item.label}`}
open={openDisabledMenuItemId === item.id}
onOpenChange={(open) => setOpenDisabledMenuItemId(open ? item.id : null)}
>
<PopoverTrigger
render={
<span
className="inline-flex w-full cursor-not-allowed"
onBlur={() => setOpenDisabledMenuItemId(null)}
onFocus={() => setOpenDisabledMenuItemId(item.id)}
onMouseEnter={() => setOpenDisabledMenuItemId(item.id)}
onMouseLeave={() => setOpenDisabledMenuItemId(null)}
tabIndex={0}
/>
}
>
<MenuItem disabled>
<GitActionItemIcon icon={item.icon} />
{item.label}
</MenuItem>
</PopoverTrigger>
<PopoverPopup tooltipStyle side="left" align="center">
{disabledReason}
</PopoverPopup>
</Popover>
);
}
return (
<MenuItem
key={`${item.id}-${item.label}`}
disabled={item.disabled}
onClick={() => {
openDialogForMenuItem(item);
}}
>
<GitActionItemIcon icon={item.icon} />
{item.label}
</MenuItem>
);
})}
{gitStatus?.branch === null && (
<p className="px-2 py-1.5 text-xs text-warning">
Behind upstream. Pull/rebase first.
Detached HEAD: create and checkout a branch to enable push and PR actions.
</p>
)}
{gitStatusError && (
<p className="px-2 py-1.5 text-xs text-destructive">{gitStatusError.message}</p>
)}
</MenuPopup>
</Menu>
</Group>
{gitStatus &&
gitStatus.branch !== null &&
!gitStatus.hasWorkingTreeChanges &&
gitStatus.behindCount > 0 && (
<p className="px-2 py-1.5 text-xs text-warning">
Behind upstream. Pull/rebase first.
</p>
)}
{gitStatusError && (
<p className="px-2 py-1.5 text-xs text-destructive">{gitStatusError.message}</p>
)}
</MenuPopup>
</Menu>
</Group>
)}
<Dialog
open={activeDialogAction !== null}
@@ -396,19 +591,72 @@ export default function GitActionsControl({ api, gitCwd }: GitActionsControlProp
<DialogDescription>{dialogDescription}</DialogDescription>
</DialogHeader>
<DialogPanel className="space-y-4">
<div className="rounded-lg border border-input bg-muted/40 p-3 text-xs">
<p>
<span className="font-medium">Branch:</span>{" "}
{gitStatus?.branch ?? "(detached HEAD)"}
</p>
<p>
<span className="font-medium">Working tree:</span>{" "}
{gitStatus?.hasWorkingTreeChanges ? "Has changes" : "Clean"}
</p>
<p>
<span className="font-medium">Ahead/behind:</span>{" "}
{gitStatus ? `${gitStatus.aheadCount}/${gitStatus.behindCount}` : "unknown"}
</p>
<div className="space-y-3 rounded-lg border border-input bg-muted/40 p-3 text-xs">
<div className="grid grid-cols-[auto_1fr] items-center gap-x-2 gap-y-1">
<span className="text-muted-foreground">Branch</span>
<span className="font-medium">{gitStatus?.branch ?? "(detached HEAD)"}</span>
{!showCompactStatusCard && (
<>
<span className="text-muted-foreground">Upstream</span>
<span className="font-medium">
{gitStatus
? formatUpstreamStatus({
hasUpstream: gitStatus.hasUpstream,
aheadCount: gitStatus.aheadCount,
behindCount: gitStatus.behindCount,
})
: "unknown"}
</span>
<span className="text-muted-foreground">Working tree</span>
<span className="font-medium">
{!gitStatus || !gitStatus.hasWorkingTreeChanges
? "Clean"
: `${gitStatus.workingTree.files.length} file(s)`}
</span>
<span className="text-muted-foreground">Diff</span>
<span className="font-mono">
{!gitStatus || !gitStatus.hasWorkingTreeChanges ? (
"none"
) : (
<>
<span className="text-success">+{gitStatus.workingTree.insertions}</span>
<span className="text-muted-foreground"> / </span>
<span className="text-destructive">-{gitStatus.workingTree.deletions}</span>
</>
)}
</span>
</>
)}
</div>
<div className="space-y-1">
<p className="text-muted-foreground">Files</p>
{!gitStatus || gitStatus.workingTree.files.length === 0 ? (
<p className="font-medium">none</p>
) : (
<div className="space-y-1">
{gitStatus.workingTree.files.slice(0, 6).map((file) => (
<button
type="button"
key={file.path}
className="flex w-full items-center justify-between gap-3 rounded-md border border-input bg-background px-2 py-1 font-mono text-left transition-colors hover:bg-accent/50"
onClick={() => openChangedFileInEditor(file.path)}
>
<span className="truncate">{file.path}</span>
<span className="shrink-0">
<span className="text-success">+{file.insertions}</span>
<span className="text-muted-foreground"> / </span>
<span className="text-destructive">-{file.deletions}</span>
</span>
</button>
))}
{gitStatus.workingTree.files.length > 6 && (
<Badge variant="outline" size="sm">
+{gitStatus.workingTree.files.length - 6} more
</Badge>
)}
</div>
)}
</div>
</div>
{activeDialogAction === "commit" && (
<div className="space-y-1">
+4 -4
View File
@@ -37,7 +37,7 @@ interface ToastProviderProps extends Toast.Provider.Props {
function ToastProvider({
children,
position = "bottom-right",
position = "top-right",
...props
}: ToastProviderProps) {
return (
@@ -48,7 +48,7 @@ function ToastProvider({
);
}
function Toasts({ position = "bottom-right" }: { position: ToastPosition }) {
function Toasts({ position = "top-right" }: { position: ToastPosition }) {
const { toasts } = Toast.useToastManager();
const isTop = position.startsWith("top");
@@ -56,9 +56,9 @@ function Toasts({ position = "bottom-right" }: { position: ToastPosition }) {
<Toast.Portal data-slot="toast-portal">
<Toast.Viewport
className={cn(
"fixed z-50 mx-auto flex w-[calc(100%-var(--toast-inset)*2)] max-w-90 [--toast-inset:--spacing(4)] sm:[--toast-inset:--spacing(8)]",
"fixed z-50 mx-auto flex w-[calc(100%-var(--toast-inset)*2)] max-w-90 [--toast-header-offset:52px] [--toast-inset:--spacing(4)] sm:[--toast-inset:--spacing(8)]",
// Vertical positioning
"data-[position*=top]:top-(--toast-inset)",
"data-[position*=top]:top-[calc(var(--toast-inset)+var(--toast-header-offset))]",
"data-[position*=bottom]:bottom-(--toast-inset)",
// Horizontal positioning
"data-[position*=left]:left-(--toast-inset)",
+131
View File
@@ -0,0 +1,131 @@
import type { GitStackedAction, NativeApi } from "@t3tools/contracts";
import { mutationOptions, queryOptions, type QueryClient } from "@tanstack/react-query";
export const gitQueryKeys = {
all: ["git"] as const,
status: (cwd: string | null) => ["git", "status", cwd] as const,
branches: (cwd: string | null) => ["git", "branches", cwd] as const,
};
export function invalidateGitQueries(queryClient: QueryClient) {
return queryClient.invalidateQueries({ queryKey: gitQueryKeys.all });
}
export function gitStatusQueryOptions(api: NativeApi | undefined, cwd: string | null) {
return queryOptions({
queryKey: gitQueryKeys.status(cwd),
queryFn: async () => {
if (!api || !cwd) {
throw new Error("Git status is unavailable.");
}
return api.git.status({ cwd });
},
enabled: !!api && !!cwd,
});
}
export function gitBranchesQueryOptions(api: NativeApi | undefined, cwd: string | null) {
return queryOptions({
queryKey: gitQueryKeys.branches(cwd),
queryFn: async () => {
if (!api || !cwd) {
throw new Error("Git branches are unavailable.");
}
return api.git.listBranches({ cwd });
},
enabled: !!api && !!cwd,
});
}
export function gitInitMutationOptions(input: {
api: NativeApi | undefined;
cwd: string | null;
queryClient: QueryClient;
}) {
return mutationOptions({
mutationFn: async () => {
if (!input.api || !input.cwd) throw new Error("Git init is unavailable.");
return input.api.git.init({ cwd: input.cwd });
},
onSuccess: async () => {
await invalidateGitQueries(input.queryClient);
},
});
}
export function gitCheckoutMutationOptions(input: {
api: NativeApi | undefined;
cwd: string | null;
queryClient: QueryClient;
}) {
return mutationOptions({
mutationFn: async (branch: string) => {
if (!input.api || !input.cwd) throw new Error("Git checkout is unavailable.");
return input.api.git.checkout({ cwd: input.cwd, branch });
},
onSuccess: async () => {
await invalidateGitQueries(input.queryClient);
},
});
}
export function gitCreateBranchAndCheckoutMutationOptions(input: {
api: NativeApi | undefined;
cwd: string | null;
queryClient: QueryClient;
}) {
return mutationOptions({
mutationFn: async (branch: string) => {
if (!input.api || !input.cwd) throw new Error("Git branch creation is unavailable.");
await input.api.git.createBranch({ cwd: input.cwd, branch });
return input.api.git.checkout({ cwd: input.cwd, branch });
},
onSuccess: async () => {
await invalidateGitQueries(input.queryClient);
},
});
}
export function gitRunStackedActionMutationOptions(input: {
api: NativeApi | undefined;
cwd: string | null;
queryClient: QueryClient;
}) {
return mutationOptions({
mutationFn: async ({
action,
commitMessage,
}: {
action: GitStackedAction;
commitMessage?: string;
}) => {
if (!input.api || !input.cwd) {
throw new Error("Git action is unavailable.");
}
return input.api.git.runStackedAction({
cwd: input.cwd,
action,
...(commitMessage ? { commitMessage } : {}),
});
},
onSuccess: async () => {
await invalidateGitQueries(input.queryClient);
},
});
}
export function gitPullMutationOptions(input: {
api: NativeApi | undefined;
cwd: string | null;
queryClient: QueryClient;
}) {
return mutationOptions({
mutationFn: async () => {
if (!input.api || !input.cwd) throw new Error("Git pull is unavailable.");
return input.api.git.pull({ cwd: input.cwd });
},
onSuccess: async () => {
await invalidateGitQueries(input.queryClient);
},
});
}
+10
View File
@@ -25,6 +25,14 @@ describe("git contracts", () => {
const parsed = gitStatusResultSchema.parse({
branch: "feature/git-actions",
hasWorkingTreeChanges: true,
workingTree: {
files: [
{ path: "src/git.ts", insertions: 8, deletions: 2 },
{ path: "src/git.test.ts", insertions: 4, deletions: 1 },
],
insertions: 12,
deletions: 3,
},
hasUpstream: false,
aheadCount: 0,
behindCount: 0,
@@ -32,6 +40,8 @@ describe("git contracts", () => {
});
expect(parsed.branch).toBe("feature/git-actions");
expect(parsed.hasWorkingTreeChanges).toBe(true);
expect(parsed.workingTree.files[0]?.insertions).toBe(8);
expect(parsed.workingTree.files).toHaveLength(2);
});
it("accepts supported stacked actions", () => {
+11
View File
@@ -75,6 +75,17 @@ export const gitPullInputSchema = z.object({
export const gitStatusResultSchema = z.object({
branch: z.string().min(1).nullable(),
hasWorkingTreeChanges: z.boolean(),
workingTree: z.object({
files: z.array(
z.object({
path: z.string().min(1),
insertions: z.number().int().nonnegative(),
deletions: z.number().int().nonnegative(),
}),
),
insertions: z.number().int().nonnegative(),
deletions: z.number().int().nonnegative(),
}),
hasUpstream: z.boolean(),
aheadCount: z.number().int().nonnegative(),
behindCount: z.number().int().nonnegative(),