feat: Auto-switch to new branch when no uncommitted changes

Modified BranchManager to automatically switch to newly created branch
if there are no uncommitted changes. If there are uncommitted changes,
the branch is created but the user stays on the current branch with a
helpful message to commit changes first.

Changes:
- Added useGitStatus hook to check for uncommitted changes
- Updated handleCreateBranch to auto-checkout new branch when safe
- Added conditional toast messages based on uncommitted changes state
This commit is contained in:
shakespeare.diy committed 2025-10-14 20:51:53 -05:00
1 parent e4c3fc8b05
commit 4ad315f964
1 file changed
+33 -5
+33 -5
View File
@@ -30,6 +30,7 @@ import {
ArrowUpRight,
} from 'lucide-react';
import { useGit } from '@/hooks/useGit';
import { useGitStatus } from '@/hooks/useGitStatus';
import { useToast } from '@/hooks/useToast';
import { cn } from '@/lib/utils';
@@ -60,6 +61,7 @@ export function BranchManager({ projectId, currentBranch, onBranchChange }: Bran
const { git } = useGit();
const { toast } = useToast();
const { data: gitStatus } = useGitStatus(projectId);
const projectPath = `/projects/${projectId}`;
const loadBranches = useCallback(async () => {
@@ -179,16 +181,42 @@ export function BranchManager({ projectId, currentBranch, onBranchChange }: Bran
});
// Create the new branch
const branchName = newBranchName.trim();
await git.branch({
dir: projectPath,
ref: newBranchName.trim(),
ref: branchName,
object: currentRef,
});
toast({
title: 'Branch created',
description: `Created branch "${newBranchName}"`,
});
// Check if we can automatically switch to the new branch
const hasUncommittedChanges = gitStatus?.hasUncommittedChanges ?? false;
if (!hasUncommittedChanges) {
// No uncommitted changes, automatically switch to the new branch
try {
await git.checkout({
dir: projectPath,
ref: branchName,
});
toast({
title: 'Branch created and switched',
description: `Created and switched to branch "${branchName}"`,
});
} catch (checkoutError) {
// Branch created but checkout failed
toast({
title: 'Branch created',
description: `Created branch "${branchName}" but failed to switch to it`,
});
}
} else {
// Has uncommitted changes, don't switch automatically
toast({
title: 'Branch created',
description: `Created branch "${branchName}". Commit your changes to switch to it.`,
});
}
setNewBranchName('');
setIsCreateDialogOpen(false);