Add branch creation endpoint and improve git auth fallback handling

This commit is contained in:
Chris Scott committed 2025-12-14 19:11:27 -05:00
1 parent 2c38f7f169
commit 99ed632355
5 files changed
+212 -49

No files matched your search

@@ -1,6 +1,6 @@
import { useState } from "react";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { createRepo } from "@/api/repos";
import { createRepo, createBranch } from "@/api/repos";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import {
@@ -15,24 +15,32 @@ interface AddBranchWorkspaceDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
repoUrl: string;
repoId: number;
}
export function AddBranchWorkspaceDialog({
open,
onOpenChange,
repoUrl,
repoId,
}: AddBranchWorkspaceDialogProps) {
const [branch, setBranch] = useState("");
const [useWorktree, setUseWorktree] = useState(true);
const [useWorktree, setUseWorktree] = useState(false);
const queryClient = useQueryClient();
const mutation = useMutation({
mutationFn: () =>
createRepo(repoUrl, undefined, branch || undefined, undefined, useWorktree),
mutationFn: () => {
if (useWorktree) {
return createRepo(repoUrl, undefined, branch || undefined, undefined, true)
}
return createBranch(repoId, branch)
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["repos"] });
queryClient.invalidateQueries({ queryKey: ["branches", repoId] });
queryClient.invalidateQueries({ queryKey: ["repo", repoId] });
setBranch("");
setUseWorktree(true);
setUseWorktree(false);
onOpenChange(false);
},
});
@@ -54,10 +62,10 @@ export function AddBranchWorkspaceDialog({
<DialogContent className="max-w-[90%] sm:max-w-[500px] bg-[#141414] border-[#2a2a2a]">
<DialogHeader>
<DialogTitle className="text-xl bg-gradient-to-r from-white to-gray-300 bg-clip-text text-transparent">
Add Branch Workspace
Add Branch
</DialogTitle>
<p className="text-sm text-zinc-400 mt-1">
Create a new workspace for{" "}
Create a new branch for{" "}
{repoUrl.split("/").pop()?.replace(".git", "")}
</p>
</DialogHeader>
@@ -99,8 +107,8 @@ export function AddBranchWorkspaceDialog({
</div>
<p className="text-xs text-zinc-500">
{useWorktree
? "Creates a git worktree - shares git history with base repo (recommended, faster)"
: "Creates a full clone - independent copy of the repository (slower, more disk space)"}
? "Creates a separate workspace directory - allows working on multiple branches simultaneously"
: "Creates a local branch in the current repository"}
</p>
</div>
@@ -112,12 +120,12 @@ export function AddBranchWorkspaceDialog({
{mutation.isPending ? (
<>
<Loader2 className="w-4 h-4 mr-2 animate-spin" />
{useWorktree ? "Creating worktree..." : "Cloning..."}
{useWorktree ? "Creating worktree..." : "Creating branch..."}
</>
) : useWorktree ? (
"Create Worktree"
) : (
"Clone Branch"
"Create Branch"
)}
</Button>
{mutation.isError && (
@@ -9,10 +9,11 @@ import {
} from "@/components/ui/dropdown-menu";
import { GitBranch, Check, Plus, GitCommit, Loader2 } from "lucide-react";
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { listBranches, switchBranch } from "@/api/repos";
import { listBranches, switchBranch, GitAuthError } from "@/api/repos";
import { useGitStatus } from "@/api/git";
import { AddBranchWorkspaceDialog } from "@/components/repo/AddBranchWorkspaceDialog";
import { GitChangesSheet } from "@/components/file-browser/GitChangesSheet";
import { showToast } from "@/lib/toast";
interface BranchSwitcherProps {
repoId: number;
@@ -50,12 +51,22 @@ export function BranchSwitcher({ repoId, currentBranch, isWorktree, repoUrl, rep
queryClient.invalidateQueries({ queryKey: ["branches", repoId] });
queryClient.invalidateQueries({ queryKey: ["repos"] });
},
onError: (error) => {
if (error instanceof GitAuthError) {
showToast.error('Authentication failed. Please update your Git token in Settings or run "gh auth login".');
} else {
showToast.error(error.message || 'Failed to switch branch');
}
},
});
if (isWorktree) {
return null;
}
const isCurrentBranchInList = branches?.all?.includes(currentBranch) ?? false;
const showLoading = switchBranchMutation.isPending || (branchesLoading && !isCurrentBranchInList);
return (
<>
<DropdownMenu onOpenChange={handleDropdownOpenChange}>
@@ -66,7 +77,7 @@ export function BranchSwitcher({ repoId, currentBranch, isWorktree, repoUrl, rep
disabled={switchBranchMutation.isPending}
className={`h-6 px-1 sm:px-2 text-[10px] sm:text-xs text-blue-600 dark:text-blue-400 hover:text-blue-700 dark:hover:text-blue-300 hover:bg-accent gap-1 border border-blue-500/20 ${iconOnly ? 'w-6' : ''} ${className || ""}`}
>
{switchBranchMutation.isPending ? (
{showLoading ? (
<Loader2 className="w-3 h-3 animate-spin" />
) : (
<GitBranch className="w-3 h-3" />
@@ -79,8 +90,8 @@ export function BranchSwitcher({ repoId, currentBranch, isWorktree, repoUrl, rep
<DropdownMenuItem disabled className="text-muted-foreground">
Loading branches...
</DropdownMenuItem>
) : branches?.local && branches.local.length > 0 ? (
branches.local.map((branch: string) => (
) : branches?.all && branches.all.length > 0 ? (
branches.all.map((branch: string) => (
<DropdownMenuItem
key={branch}
onClick={() => switchBranchMutation.mutate(branch)}
@@ -136,6 +147,7 @@ export function BranchSwitcher({ repoId, currentBranch, isWorktree, repoUrl, rep
open={addBranchOpen}
onOpenChange={setAddBranchOpen}
repoUrl={repoUrl}
repoId={repoId}
/>
)}