Default diffs to working changes (#3974)

Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
Jake LeventhalandJulius Marminge authored and GitHub committed 2026-07-18 13:17:30 +02:00
1 parent 765e1b5fc0
commit 38e0bb07bb
4 files changed
+47 -9

No files matched your search

+12 -3
View File
@@ -2252,12 +2252,13 @@ function ChatViewContent(props: ChatViewProps) {
worktreePath: activeThread?.worktreePath ?? null,
})
: null;
const gitStatusCwd = activeThread?.worktreePath ?? gitCwd;
const gitStatusQuery = useEnvironmentQuery(
gitCwd === null
gitStatusCwd === null
? null
: vcsEnvironment.status({
environmentId,
input: { cwd: gitCwd },
input: { cwd: gitStatusCwd },
}),
);
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
@@ -2291,6 +2292,9 @@ function ChatViewContent(props: ChatViewProps) {
terminalUiLaunchContext?.threadId === activeThreadId ? terminalUiLaunchContext : null;
// Default true while loading to avoid toolbar flicker.
const isGitRepo = gitStatusQuery.data?.isRepo ?? true;
const initialDiffPanelGitScope =
gitStatusQuery.data?.hasWorkingTreeChanges === true ? "unstaged" : "branch";
const diffPanelGitStatusResolutionKey = gitStatusQuery.data ? "resolved" : "pending";
const terminalShortcutLabelOptions = useMemo(
() => ({
context: {
@@ -5136,7 +5140,12 @@ function ChatViewContent(props: ChatViewProps) {
/>
) : activeRightPanelSurface?.kind === "diff" ? (
<Suspense fallback={null}>
<DiffPanel mode="embedded" composerDraftTarget={composerDraftTarget} />
<DiffPanel
key={`${activeThreadKey}:${diffPanelGitStatusResolutionKey}`}
mode="embedded"
composerDraftTarget={composerDraftTarget}
initialGitScope={initialDiffPanelGitScope}
/>
</Suspense>
) : activeRightPanelSurface?.kind === "plan" ? (
<PlanSidebar
+14 -4
View File
@@ -178,13 +178,19 @@ const DIFF_PANEL_UNSAFE_CSS = `
interface DiffPanelProps {
mode?: DiffPanelMode;
composerDraftTarget: ScopedThreadRef | DraftId;
initialGitScope: "branch" | "unstaged";
}
export { DiffWorkerPoolProvider } from "./DiffWorkerPoolProvider";
export default function DiffPanel({ mode = "inline", composerDraftTarget }: DiffPanelProps) {
export default function DiffPanel({
mode = "inline",
composerDraftTarget,
initialGitScope: initialGitScopeProp,
}: DiffPanelProps) {
const { resolvedTheme } = useTheme();
const settings = useClientSettings();
const [initialGitScope] = useState(initialGitScopeProp);
const [diffRenderMode, setDiffRenderMode] = useState<DiffRenderMode>("stacked");
const [wordWrap, setWordWrap] = useState(settings.wordWrap);
const [diffIgnoreWhitespace, setDiffIgnoreWhitespace] = useState(settings.diffIgnoreWhitespace);
@@ -199,9 +205,6 @@ export default function DiffPanel({ mode = "inline", composerDraftTarget }: Diff
strict: false,
select: (params) => resolveThreadRouteRef(params),
});
const diffSelection = useDiffPanelStore((state) =>
selectThreadDiffPanelSelection(state.byThreadKey, routeThreadRef),
);
const activeThreadId = routeThreadRef?.threadId ?? null;
const activeThread = useThread(routeThreadRef);
const activeProjectId = activeThread?.projectId ?? null;
@@ -229,6 +232,13 @@ export default function DiffPanel({ mode = "inline", composerDraftTarget }: Diff
})
: null,
);
const diffSelection = useDiffPanelStore((state) =>
selectThreadDiffPanelSelection(
state.byThreadKey,
routeThreadRef,
initialGitScope === "unstaged",
),
);
const isGitRepo = gitStatusQuery.data?.isRepo ?? true;
const { turnDiffSummaries, inferredCheckpointTurnCountByTurnId } =
useTurnDiffSummaries(activeThread);
+15 -1
View File
@@ -9,12 +9,26 @@ const THREAD_REF = scopeThreadRef(EnvironmentId.make("environment-1"), ThreadId.
describe("diffPanelStore", () => {
beforeEach(() => useDiffPanelStore.setState({ byThreadKey: {}, branchBaseRefByThreadKey: {} }));
it("defaults each thread to branch changes with automatic base selection", () => {
it("defaults each thread to branch changes when the working tree is clean", () => {
expect(
selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF),
).toEqual({ kind: "branch", baseRef: null });
});
it("defaults each thread to working changes when the working tree is dirty", () => {
expect(
selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF, true),
).toEqual({ kind: "unstaged" });
});
it("preserves an explicit scope selection when the working tree state changes", () => {
useDiffPanelStore.getState().selectGitScope(THREAD_REF, "branch");
expect(
selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF, true),
).toEqual({ kind: "branch", baseRef: null });
});
it("clears incompatible selection fields when changing scopes", () => {
const store = useDiffPanelStore.getState();
store.selectTurn(THREAD_REF, TurnId.make("turn-1"), "src/app.ts");
+6 -1
View File
@@ -11,6 +11,7 @@ export type DiffPanelSelection =
| { kind: "turn"; turnId: TurnId; filePath: string | null; revealRequestId: number };
const DEFAULT_SELECTION: DiffPanelSelection = { kind: "branch", baseRef: null };
const DEFAULT_WORKING_TREE_SELECTION: DiffPanelSelection = { kind: "unstaged" };
interface DiffPanelStoreState {
byThreadKey: Record<string, DiffPanelSelection>;
@@ -133,7 +134,11 @@ export const useDiffPanelStore = create<DiffPanelStoreState>()(
export function selectThreadDiffPanelSelection(
byThreadKey: Record<string, DiffPanelSelection>,
ref: ScopedThreadRef | null | undefined,
hasWorkingTreeChanges = false,
): DiffPanelSelection {
if (!ref) return DEFAULT_SELECTION;
return byThreadKey[scopedThreadKey(ref)] ?? DEFAULT_SELECTION;
return (
byThreadKey[scopedThreadKey(ref)] ??
(hasWorkingTreeChanges ? DEFAULT_WORKING_TREE_SELECTION : DEFAULT_SELECTION)
);
}