mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Add worktree support and consolidate branch management UI (#78)
* Fix git credential saving and improve host matching - Fix token masking bug in GitCredentialDialog that corrupted tokens on edit - Add tokenEdited state to preserve original token when not modified - Default new credential host to https://github.com - Add normalizeHostname helper for consistent host comparison - Improve credential lookup logging to show configured vs requested hosts - Remove overly permissive fuzzy matching that could leak tokens * Make OpenCode reload non-fatal when saving git credentials Settings are now saved to database even if the OpenCode server reload fails. The reload error is returned separately so the UI can inform the user that credentials were saved but the server needs a manual restart. * Fix git fetch using credentials in silent mode Previously, silent mode (used for UI fetch operations) would skip credential lookup entirely and return empty, causing auth failures. Now credentials are always looked up via IPC regardless of silent mode. * Add worktree support and consolidate branch management UI * Remove duplicate SECURE_COOKIES property from auth config * Bump version to 0.7.8
This commit is contained in:
1 parent
fb9413782a
commit
204e79c713
12 files changed
+149
-489
No files matched your search
@@ -12,6 +12,7 @@ interface GitBranch {
|
||||
upstream?: string
|
||||
ahead?: number
|
||||
behind?: number
|
||||
isWorktree?: boolean
|
||||
}
|
||||
|
||||
export class GitBranchService {
|
||||
@@ -45,19 +46,22 @@ export class GitBranchService {
|
||||
if (!trimmed) continue
|
||||
|
||||
const isCurrent = trimmed.startsWith('*')
|
||||
const namePart = trimmed.replace(/^\*?\s*/, '')
|
||||
const isWorktree = trimmed.startsWith('+')
|
||||
const namePart = trimmed.replace(/^[*+]?\s*/, '')
|
||||
|
||||
const firstSpace = namePart.indexOf(' ')
|
||||
const firstBracket = namePart.indexOf('[')
|
||||
const cutIndex = firstSpace === -1 ? (firstBracket === -1 ? namePart.length : firstBracket) : (firstBracket === -1 ? firstSpace : Math.min(firstSpace, firstBracket))
|
||||
const branchName = namePart.slice(0, cutIndex)
|
||||
const branchName = namePart.slice(0, cutIndex).trim()
|
||||
|
||||
if (!branchName) continue
|
||||
if (!branchName || branchName === '+' || branchName === '->' || branchName.includes('->')) continue
|
||||
if (/^[0-9a-f]{6,40}$/.test(branchName)) continue
|
||||
|
||||
const branch: GitBranch = {
|
||||
name: branchName,
|
||||
type: branchName.startsWith('remotes/') ? 'remote' : 'local',
|
||||
current: isCurrent && (branchName === currentBranch || branchName === `remotes/${currentBranch}`)
|
||||
current: isCurrent && (branchName === currentBranch || branchName === `remotes/${currentBranch}`),
|
||||
isWorktree
|
||||
}
|
||||
|
||||
if (seenNames.has(branch.name)) continue
|
||||
|
||||
@@ -149,6 +149,7 @@ interface GitBranch {
|
||||
upstream?: string
|
||||
ahead?: number
|
||||
behind?: number
|
||||
isWorktree?: boolean
|
||||
}
|
||||
|
||||
export async function listBranches(id: number): Promise<{ branches: GitBranch[], status: { ahead: number, behind: number } }> {
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
import { GitBranch, Loader2 } from 'lucide-react'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
|
||||
|
||||
interface GitBranchSelectorProps {
|
||||
repoId: number | undefined
|
||||
branch: string | null
|
||||
branches: string[]
|
||||
onBranchChange: (branch: string) => void
|
||||
isLoading?: boolean
|
||||
error?: Error | null
|
||||
ahead?: number
|
||||
behind?: number
|
||||
}
|
||||
|
||||
export function GitBranchSelector({ branch, branches, onBranchChange, isLoading, error, ahead, behind }: GitBranchSelectorProps) {
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="flex items-center gap-2 px-3 py-2">
|
||||
<Loader2 className="w-4 h-4 animate-spin text-muted-foreground" />
|
||||
<span className="text-sm text-muted-foreground">Loading branches...</span>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<div className="flex items-center gap-2 px-3 py-2">
|
||||
<GitBranch className="w-4 h-4 text-muted-foreground" />
|
||||
<span className="text-sm text-red-500">Failed to load branches</span>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
if (!branch || branches.length === 0) {
|
||||
return (
|
||||
<div className="flex items-center gap-2 px-3 py-2">
|
||||
<GitBranch className="w-4 h-4 text-muted-foreground" />
|
||||
<span className="text-sm text-muted-foreground">No branches available</span>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
const getBranchLabel = (branchName: string, isCurrent: boolean) => {
|
||||
if (!isCurrent) return branchName
|
||||
const parts = [branchName]
|
||||
if (ahead && ahead > 0) parts.push(`${ahead} ahead`)
|
||||
if (behind && behind > 0) parts.push(`${behind} behind`)
|
||||
return parts.join(' ')
|
||||
}
|
||||
|
||||
return (
|
||||
<Select value={branch} onValueChange={onBranchChange}>
|
||||
<SelectTrigger className="w-full h-auto py-1.5">
|
||||
<div className="flex items-center gap-2 flex-1">
|
||||
<GitBranch className="w-4 h-4 text-muted-foreground" />
|
||||
<SelectValue placeholder="Select branch" />
|
||||
</div>
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
{branches.map((branchName) => (
|
||||
<SelectItem key={branchName} value={branchName}>
|
||||
<div className="flex items-center gap-2">
|
||||
<GitBranch className="w-3 h-3 text-muted-foreground" />
|
||||
<span className={cn(branchName === branch && 'font-medium')}>
|
||||
{getBranchLabel(branchName, branchName === branch)}
|
||||
</span>
|
||||
</div>
|
||||
</SelectItem>
|
||||
))}
|
||||
</SelectContent>
|
||||
</Select>
|
||||
)
|
||||
}
|
||||
@@ -1,138 +0,0 @@
|
||||
import { useState } from "react";
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { createRepo, createBranch } from "@/api/repos";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog";
|
||||
import { Loader2 } from "lucide-react";
|
||||
|
||||
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(false);
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const mutation = useMutation({
|
||||
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(false);
|
||||
onOpenChange(false);
|
||||
},
|
||||
});
|
||||
|
||||
const handleSubmit = (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (branch.trim()) {
|
||||
mutation.mutate();
|
||||
}
|
||||
};
|
||||
|
||||
const handleCheckboxChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
e.stopPropagation();
|
||||
setUseWorktree(e.target.checked);
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<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
|
||||
</DialogTitle>
|
||||
<p className="text-sm text-zinc-400 mt-1">
|
||||
Create a new branch for{" "}
|
||||
{repoUrl.split("/").pop()?.replace(".git", "")}
|
||||
</p>
|
||||
</DialogHeader>
|
||||
<form onSubmit={handleSubmit} className="space-y-4 mt-4">
|
||||
<div className="space-y-2">
|
||||
<label className="text-sm text-zinc-400">Branch *</label>
|
||||
<Input
|
||||
placeholder="feature-branch, main, etc."
|
||||
value={branch}
|
||||
onChange={(e) => setBranch(e.target.value)}
|
||||
disabled={mutation.isPending}
|
||||
className="bg-[#1a1a1a] border-[#2a2a2a] text-white placeholder:text-zinc-500"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<div className="flex items-center gap-2">
|
||||
<input
|
||||
type="checkbox"
|
||||
id="worktree-checkbox"
|
||||
checked={useWorktree}
|
||||
onChange={handleCheckboxChange}
|
||||
disabled={mutation.isPending}
|
||||
className="w-4 h-4 rounded border-gray-600 bg-gray-800 text-blue-600 focus:ring-blue-600 focus:ring-2 cursor-pointer accent-blue-600"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
/>
|
||||
<label
|
||||
htmlFor="worktree-checkbox"
|
||||
className="text-sm text-zinc-400 cursor-pointer select-none"
|
||||
onClick={(e) => {
|
||||
e.preventDefault();
|
||||
if (!mutation.isPending) {
|
||||
setUseWorktree(!useWorktree);
|
||||
}
|
||||
}}
|
||||
>
|
||||
Create as worktree
|
||||
</label>
|
||||
</div>
|
||||
<p className="text-xs text-zinc-500">
|
||||
{useWorktree
|
||||
? "Creates a separate workspace directory - allows working on multiple branches simultaneously"
|
||||
: "Creates a local branch in the current repository"}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<Button
|
||||
type="submit"
|
||||
disabled={!branch || mutation.isPending}
|
||||
className="w-full bg-blue-600 hover:bg-blue-700 text-white"
|
||||
>
|
||||
{mutation.isPending ? (
|
||||
<>
|
||||
<Loader2 className="w-4 h-4 mr-2 animate-spin" />
|
||||
{useWorktree ? "Creating worktree..." : "Creating branch..."}
|
||||
</>
|
||||
) : useWorktree ? (
|
||||
"Create Worktree"
|
||||
) : (
|
||||
"Create Branch"
|
||||
)}
|
||||
</Button>
|
||||
{mutation.isError && (
|
||||
<p className="text-sm text-red-400">{mutation.error.message}</p>
|
||||
)}
|
||||
</form>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -1,146 +0,0 @@
|
||||
import { useState, useCallback } from "react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuSeparator,
|
||||
DropdownMenuTrigger,
|
||||
} from "@/components/ui/dropdown-menu";
|
||||
import { GitBranch, Check, Plus, Loader2 } from "lucide-react";
|
||||
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { listBranches, switchBranch, GitAuthError } from "@/api/repos";
|
||||
import { AddBranchWorkspaceDialog } from "@/components/repo/AddBranchWorkspaceDialog";
|
||||
import { showToast } from "@/lib/toast";
|
||||
|
||||
interface BranchSwitcherProps {
|
||||
repoId: number;
|
||||
currentBranch: string;
|
||||
isWorktree?: boolean;
|
||||
repoUrl?: string | null;
|
||||
className?: string;
|
||||
iconOnly?: boolean;
|
||||
}
|
||||
|
||||
export function BranchSwitcher({ repoId, currentBranch, isWorktree, repoUrl, className, iconOnly }: BranchSwitcherProps) {
|
||||
const [addBranchOpen, setAddBranchOpen] = useState(false);
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const { data: branches, isLoading: branchesLoading, refetch: refetchBranches } = useQuery({
|
||||
queryKey: ["branches", repoId],
|
||||
queryFn: () => listBranches(repoId),
|
||||
enabled: false,
|
||||
staleTime: Infinity,
|
||||
});
|
||||
|
||||
const activeBranch = branches?.branches?.find(b => b.current)?.name ?? currentBranch;
|
||||
|
||||
const handleDropdownOpenChange = useCallback((open: boolean) => {
|
||||
if (open && repoId) {
|
||||
refetchBranches();
|
||||
}
|
||||
}, [repoId, refetchBranches]);
|
||||
|
||||
const switchBranchMutation = useMutation({
|
||||
mutationFn: (branch: string) => switchBranch(repoId, branch),
|
||||
onSuccess: async (updatedRepo) => {
|
||||
queryClient.setQueryData(["repo", repoId], updatedRepo);
|
||||
queryClient.invalidateQueries({ queryKey: ["repos"] });
|
||||
await refetchBranches();
|
||||
showToast.success(`Switched to branch: ${updatedRepo.currentBranch}`);
|
||||
},
|
||||
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');
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
const showLoading = switchBranchMutation.isPending || (branchesLoading && !branches);
|
||||
|
||||
return (
|
||||
<>
|
||||
<DropdownMenu onOpenChange={handleDropdownOpenChange}>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
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 || ""}`}
|
||||
>
|
||||
{showLoading ? (
|
||||
<Loader2 className="w-3 h-3 animate-spin" />
|
||||
) : (
|
||||
<GitBranch className="w-3 h-3" />
|
||||
)}
|
||||
{!iconOnly && <span className="truncate">{activeBranch}</span>}
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent sideOffset={0} align="end" className="bg-card border-border min-w-50 max-w-[95vw] sm:max-w-none">
|
||||
{isWorktree ? (
|
||||
<DropdownMenuItem disabled className="text-muted-foreground">
|
||||
<div className="flex items-center gap-2 w-full">
|
||||
<GitBranch className="w-3 h-3" />
|
||||
<span className="flex-1">Worktree: {activeBranch}</span>
|
||||
</div>
|
||||
</DropdownMenuItem>
|
||||
) : (
|
||||
<>
|
||||
{repoUrl && (
|
||||
<>
|
||||
<DropdownMenuItem
|
||||
onClick={() => setAddBranchOpen(true)}
|
||||
className="text-muted-foreground hover:text-foreground hover:bg-accent cursor-pointer"
|
||||
>
|
||||
<div className="flex items-center gap-2 w-full">
|
||||
<Plus className="w-3 h-3" />
|
||||
<span className="flex-1">Add Branch</span>
|
||||
</div>
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuSeparator />
|
||||
</>
|
||||
)}
|
||||
<div className="max-h-[60vh] overflow-y-auto w-full">
|
||||
{branchesLoading && !branches ? (
|
||||
<DropdownMenuItem disabled className="text-muted-foreground">
|
||||
Loading branches...
|
||||
</DropdownMenuItem>
|
||||
) : branches?.branches && branches.branches.length > 0 ? (
|
||||
branches.branches.filter(b => b.type === 'local').map((branch) => (
|
||||
<DropdownMenuItem
|
||||
key={branch.name}
|
||||
onClick={() => switchBranchMutation.mutate(branch.name)}
|
||||
disabled={switchBranchMutation.isPending}
|
||||
className="text-muted-foreground hover:text-foreground hover:bg-accent cursor-pointer"
|
||||
>
|
||||
<div className="flex items-center gap-2 w-full">
|
||||
<GitBranch className="w-3 h-3" />
|
||||
<span className="flex-1">{branch.name}</span>
|
||||
{branch.name === activeBranch && <Check className="w-3 h-3 text-green-500" />}
|
||||
</div>
|
||||
</DropdownMenuItem>
|
||||
))
|
||||
) : (
|
||||
<DropdownMenuItem disabled className="text-muted-foreground">
|
||||
No branches available
|
||||
</DropdownMenuItem>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
|
||||
{repoUrl && (
|
||||
<AddBranchWorkspaceDialog
|
||||
open={addBranchOpen}
|
||||
onOpenChange={setAddBranchOpen}
|
||||
repoUrl={repoUrl}
|
||||
repoId={repoId}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -1,14 +1,13 @@
|
||||
import { useState } from "react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
|
||||
import { Checkbox } from "@/components/ui/checkbox";
|
||||
import { Loader2, Trash2, Download, GitBranch, FolderOpen, AlertCircle } from "lucide-react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { downloadRepo } from "@/api/repos";
|
||||
import { showToast } from "@/lib/toast";
|
||||
import type { GitStatusResponse } from "@/types/git";
|
||||
|
||||
import { BranchSwitcher } from "./BranchSwitcher";
|
||||
import { SourceControlPanel } from "@/components/source-control/SourceControlPanel";
|
||||
|
||||
interface RepoCardProps {
|
||||
repo: {
|
||||
@@ -38,6 +37,7 @@ export function RepoCard({
|
||||
}: RepoCardProps) {
|
||||
const navigate = useNavigate();
|
||||
const [isDownloading, setIsDownloading] = useState(false);
|
||||
const [showSourceControl, setShowSourceControl] = useState(false);
|
||||
|
||||
const repoName = repo.repoUrl
|
||||
? repo.repoUrl.split("/").slice(-1)[0].replace(".git", "")
|
||||
@@ -53,7 +53,7 @@ export function RepoCard({
|
||||
const unstagedCount = gitStatus?.files.filter((f) => !f.staged).length || 0;
|
||||
|
||||
const handleCardClick = () => {
|
||||
if (isReady) {
|
||||
if (isReady && !showSourceControl) {
|
||||
navigate(`/repos/${repo.id}`);
|
||||
}
|
||||
};
|
||||
@@ -96,11 +96,7 @@ export function RepoCard({
|
||||
{isReady && (
|
||||
<div className={`w-2 h-2 rounded-full shrink-0 ${isDirty ? 'bg-orange-500' : 'bg-green-500'}`} />
|
||||
)}
|
||||
{repo.isWorktree && (
|
||||
<Badge variant="secondary" className="text-xs px-1.5 py-0 shrink-0">
|
||||
worktree
|
||||
</Badge>
|
||||
)}
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -113,7 +109,7 @@ export function RepoCard({
|
||||
</span>
|
||||
) : (
|
||||
<>
|
||||
<span className="flex items-center gap-1 shrink-0">
|
||||
<span className={`flex items-center gap-1 shrink-0 ${repo.isWorktree ? 'text-purple-400' : ''}`}>
|
||||
<GitBranch className="w-3.5 h-3.5 shrink-0" />
|
||||
<span className="truncate max-w-[80px]">{branchToDisplay || "main"}</span>
|
||||
</span>
|
||||
@@ -145,14 +141,16 @@ export function RepoCard({
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-1 shrink-0" onClick={(e) => e.stopPropagation()}>
|
||||
<BranchSwitcher
|
||||
repoId={repo.id}
|
||||
currentBranch={branchToDisplay || ""}
|
||||
isWorktree={repo.isWorktree}
|
||||
repoUrl={repo.repoUrl}
|
||||
iconOnly={true}
|
||||
className="h-8 w-8"
|
||||
/>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
onClick={(e) => handleActionClick(e, () => setShowSourceControl(true))}
|
||||
disabled={!isReady}
|
||||
className="h-8 w-8 p-0"
|
||||
title="Source Control"
|
||||
>
|
||||
<GitBranch className="w-4 h-4" />
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
size="sm"
|
||||
@@ -195,6 +193,16 @@ export function RepoCard({
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<SourceControlPanel
|
||||
repoId={repo.id}
|
||||
isOpen={showSourceControl}
|
||||
onClose={() => setShowSourceControl(false)}
|
||||
currentBranch={branchToDisplay || ""}
|
||||
repoUrl={repo.repoUrl}
|
||||
isRepoWorktree={repo.isWorktree}
|
||||
repoName={repoName}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
import { useState } from 'react'
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { listBranches, switchBranch, GitAuthError } from '@/api/repos'
|
||||
import { listBranches, switchBranch, createRepo, GitAuthError } from '@/api/repos'
|
||||
import { useGitStatus } from '@/api/git'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
import { Loader2, GitBranch, Check, Plus, AlertCircle, ArrowUp, ArrowDown, Globe } from 'lucide-react'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { showToast } from '@/lib/toast'
|
||||
@@ -13,12 +14,15 @@ import { GIT_UI_COLORS } from '@/lib/git-status-styles'
|
||||
interface BranchesTabProps {
|
||||
repoId: number
|
||||
currentBranch: string
|
||||
repoUrl?: string | null
|
||||
isRepoWorktree?: boolean
|
||||
}
|
||||
|
||||
export function BranchesTab({ repoId, currentBranch }: BranchesTabProps) {
|
||||
export function BranchesTab({ repoId, currentBranch, repoUrl, isRepoWorktree }: BranchesTabProps) {
|
||||
const queryClient = useQueryClient()
|
||||
const [newBranchName, setNewBranchName] = useState('')
|
||||
const [isCreating, setIsCreating] = useState(false)
|
||||
const [useWorktree, setUseWorktree] = useState(false)
|
||||
const git = useGit(repoId)
|
||||
|
||||
const { data: status } = useGitStatus(repoId)
|
||||
@@ -47,14 +51,33 @@ export function BranchesTab({ repoId, currentBranch }: BranchesTabProps) {
|
||||
},
|
||||
})
|
||||
|
||||
const worktreeMutation = useMutation({
|
||||
mutationFn: (branch: string) => createRepo(repoUrl || undefined, undefined, branch, undefined, true),
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ['repos'] })
|
||||
setNewBranchName('')
|
||||
setIsCreating(false)
|
||||
setUseWorktree(false)
|
||||
showToast.success('Branch workspace created')
|
||||
},
|
||||
onError: (error) => {
|
||||
showToast.error(error instanceof Error ? error.message : 'Failed to create branch workspace')
|
||||
},
|
||||
})
|
||||
|
||||
const handleCreateBranch = async () => {
|
||||
if (!newBranchName.trim()) return
|
||||
|
||||
try {
|
||||
await git.createBranch.mutateAsync(newBranchName.trim())
|
||||
setNewBranchName('')
|
||||
setIsCreating(false)
|
||||
refetch()
|
||||
if (useWorktree && repoUrl) {
|
||||
await worktreeMutation.mutateAsync(newBranchName.trim())
|
||||
} else {
|
||||
await git.createBranch.mutateAsync(newBranchName.trim())
|
||||
setNewBranchName('')
|
||||
setIsCreating(false)
|
||||
setUseWorktree(false)
|
||||
refetch()
|
||||
}
|
||||
} catch {
|
||||
// Error handled by mutation
|
||||
}
|
||||
@@ -103,44 +126,57 @@ export function BranchesTab({ repoId, currentBranch }: BranchesTabProps) {
|
||||
</div>
|
||||
|
||||
{isCreating ? (
|
||||
<div className="flex items-center gap-2">
|
||||
<Input
|
||||
placeholder="New branch name..."
|
||||
value={newBranchName}
|
||||
onChange={(e) => setNewBranchName(e.target.value)}
|
||||
className="h-8 text-sm"
|
||||
autoFocus
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter') handleCreateBranch()
|
||||
if (e.key === 'Escape') {
|
||||
<div className="space-y-2">
|
||||
<div className="flex items-center gap-2">
|
||||
<Input
|
||||
placeholder="New branch name..."
|
||||
value={newBranchName}
|
||||
onChange={(e) => setNewBranchName(e.target.value)}
|
||||
className="h-8 text-sm"
|
||||
autoFocus
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter') handleCreateBranch()
|
||||
if (e.key === 'Escape') {
|
||||
setIsCreating(false)
|
||||
setNewBranchName('')
|
||||
setUseWorktree(false)
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<Button
|
||||
size="sm"
|
||||
className="h-8"
|
||||
onClick={handleCreateBranch}
|
||||
disabled={!newBranchName.trim() || git.createBranch.isPending || worktreeMutation.isPending}
|
||||
>
|
||||
{(git.createBranch.isPending || worktreeMutation.isPending) ? (
|
||||
<Loader2 className="w-4 h-4 animate-spin" />
|
||||
) : (
|
||||
'Create'
|
||||
)}
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
className="h-8"
|
||||
onClick={() => {
|
||||
setIsCreating(false)
|
||||
setNewBranchName('')
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<Button
|
||||
size="sm"
|
||||
className="h-8"
|
||||
onClick={handleCreateBranch}
|
||||
disabled={!newBranchName.trim() || git.createBranch.isPending}
|
||||
>
|
||||
{git.createBranch.isPending ? (
|
||||
<Loader2 className="w-4 h-4 animate-spin" />
|
||||
) : (
|
||||
'Create'
|
||||
)}
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
className="h-8"
|
||||
onClick={() => {
|
||||
setIsCreating(false)
|
||||
setNewBranchName('')
|
||||
}}
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
setUseWorktree(false)
|
||||
}}
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
</div>
|
||||
{repoUrl && (
|
||||
<label className="flex items-center gap-2 text-sm text-muted-foreground cursor-pointer">
|
||||
<Checkbox
|
||||
checked={useWorktree}
|
||||
onCheckedChange={(checked) => setUseWorktree(checked === true)}
|
||||
/>
|
||||
<span>Create as separate workspace</span>
|
||||
</label>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<Button
|
||||
@@ -155,32 +191,37 @@ export function BranchesTab({ repoId, currentBranch }: BranchesTabProps) {
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="flex-1 overflow-y-auto">
|
||||
<div className="flex-1 overflow-y-auto relative">
|
||||
{switchBranchMutation.isPending && (
|
||||
<div className="absolute inset-0 bg-background/50 flex items-center justify-center z-10">
|
||||
<Loader2 className="w-5 h-5 animate-spin text-muted-foreground" />
|
||||
</div>
|
||||
)}
|
||||
{branches?.branches && branches.branches.length > 0 ? (
|
||||
<div className="py-1">
|
||||
{branches.branches.map((branch) => {
|
||||
const isCurrent = branch.name === activeBranch || branch.name === `remotes/${activeBranch}`
|
||||
const isRemote = branch.type === 'remote'
|
||||
const checkoutName = isRemote ? branch.name.replace(/^remotes\/[^/]+\//, '') : branch.name
|
||||
|
||||
const isCheckedOutElsewhere = branch.isWorktree && !isCurrent
|
||||
|
||||
const handleClick = () => {
|
||||
if (isCurrent) return
|
||||
|
||||
let branchToCheckout = branch.name
|
||||
if (isRemote) {
|
||||
branchToCheckout = branch.name.replace(/^remotes\/[^/]+\//, '')
|
||||
}
|
||||
switchBranchMutation.mutate(branchToCheckout)
|
||||
if (isCurrent || isCheckedOutElsewhere) return
|
||||
switchBranchMutation.mutate(checkoutName)
|
||||
}
|
||||
|
||||
return (
|
||||
<button
|
||||
key={branch.name}
|
||||
className={cn(
|
||||
'flex items-center gap-2 px-3 py-2 w-full text-left hover:bg-accent/50 transition-colors',
|
||||
isCurrent && 'bg-accent'
|
||||
'flex items-center gap-2 px-3 py-2 w-full text-left transition-colors',
|
||||
isCurrent && 'bg-accent',
|
||||
isCheckedOutElsewhere ? 'opacity-60 cursor-not-allowed' : 'hover:bg-accent/50'
|
||||
)}
|
||||
onClick={handleClick}
|
||||
disabled={isCurrent || switchBranchMutation.isPending}
|
||||
disabled={isCurrent || isCheckedOutElsewhere || switchBranchMutation.isPending}
|
||||
title={isCheckedOutElsewhere ? 'Branch is checked out in another worktree' : undefined}
|
||||
>
|
||||
{isRemote ? (
|
||||
<Globe className="w-4 h-4 text-blue-500" />
|
||||
@@ -188,13 +229,13 @@ export function BranchesTab({ repoId, currentBranch }: BranchesTabProps) {
|
||||
<GitBranch className={cn('w-4 h-4', isCurrent ? GIT_UI_COLORS.current : 'text-muted-foreground')} />
|
||||
)}
|
||||
<span className="flex-1 text-sm truncate">{branch.name}</span>
|
||||
{branch.type === 'local' && !branch.upstream && (
|
||||
{(isCheckedOutElsewhere || (isCurrent && isRepoWorktree)) && (
|
||||
<span className="text-[10px] px-1.5 py-0.5 rounded bg-purple-500/20 text-purple-400">worktree</span>
|
||||
)}
|
||||
{branch.type === 'local' && !branch.upstream && !branch.isWorktree && !(isCurrent && isRepoWorktree) && (
|
||||
<span className="text-[10px] px-1.5 py-0.5 rounded bg-muted text-muted-foreground">local</span>
|
||||
)}
|
||||
{isCurrent && <Check className={`w-4 h-4 ${GIT_UI_COLORS.current}`} />}
|
||||
{switchBranchMutation.isPending && switchBranchMutation.variables === branch.name && (
|
||||
<Loader2 className="w-4 h-4 animate-spin" />
|
||||
)}
|
||||
</button>
|
||||
)
|
||||
})}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { useState } from 'react'
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { useGitStatus, getApiErrorMessage } from '@/api/git'
|
||||
import { listBranches, switchBranch } from '@/api/repos'
|
||||
|
||||
import { useGit } from '@/hooks/useGit'
|
||||
import { ChangesTab } from './ChangesTab'
|
||||
import { CommitsTab } from './CommitsTab'
|
||||
@@ -9,7 +8,6 @@ import { BranchesTab } from './BranchesTab'
|
||||
import { FileDiffView } from '@/components/file-browser/FileDiffView'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'
|
||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@/components/ui/dropdown-menu'
|
||||
import { GIT_UI_COLORS } from '@/lib/git-status-styles'
|
||||
import {
|
||||
Loader2,
|
||||
@@ -22,8 +20,6 @@ import {
|
||||
RefreshCw,
|
||||
ArrowDownFromLine,
|
||||
X,
|
||||
ChevronDown,
|
||||
Check,
|
||||
} from 'lucide-react'
|
||||
import { cn } from '@/lib/utils'
|
||||
import { useMobile } from '@/hooks/useMobile'
|
||||
@@ -34,6 +30,9 @@ interface SourceControlPanelProps {
|
||||
isOpen: boolean
|
||||
onClose: () => void
|
||||
currentBranch: string
|
||||
repoUrl?: string | null
|
||||
isRepoWorktree?: boolean
|
||||
repoName?: string
|
||||
}
|
||||
|
||||
type Tab = 'changes' | 'commits' | 'branches'
|
||||
@@ -43,32 +42,15 @@ export function SourceControlPanel({
|
||||
isOpen,
|
||||
onClose,
|
||||
currentBranch,
|
||||
repoUrl,
|
||||
isRepoWorktree,
|
||||
repoName,
|
||||
}: SourceControlPanelProps) {
|
||||
const [activeTab, setActiveTab] = useState<Tab>('changes')
|
||||
const [selectedFile, setSelectedFile] = useState<{path: string, staged: boolean} | undefined>()
|
||||
const { data: status } = useGitStatus(repoId)
|
||||
const { data: branches } = useQuery({
|
||||
queryKey: ['branches', repoId],
|
||||
queryFn: () => listBranches(repoId),
|
||||
staleTime: 30000,
|
||||
})
|
||||
const git = useGit(repoId)
|
||||
const queryClient = useQueryClient()
|
||||
const isMobile = useMobile()
|
||||
|
||||
const switchBranchMutation = useMutation({
|
||||
mutationFn: (branch: string) => switchBranch(repoId, branch),
|
||||
onSuccess: (updatedRepo) => {
|
||||
queryClient.setQueryData(['repo', repoId], updatedRepo)
|
||||
queryClient.invalidateQueries({ queryKey: ['repos'] })
|
||||
queryClient.invalidateQueries({ queryKey: ['gitStatus', repoId] })
|
||||
queryClient.invalidateQueries({ queryKey: ['branches', repoId] })
|
||||
},
|
||||
onError: (error) => {
|
||||
showToast.error(getApiErrorMessage(error))
|
||||
},
|
||||
})
|
||||
|
||||
const handleGitAction = async (action: () => Promise<unknown>) => {
|
||||
try {
|
||||
await action()
|
||||
@@ -91,29 +73,10 @@ export function SourceControlPanel({
|
||||
<div className="flex flex-col h-full">
|
||||
<div className="flex items-center justify-between px-3 py-2 border-b border-border flex-shrink-0">
|
||||
<div className="flex items-center gap-2">
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<button className="flex items-center gap-2 hover:bg-accent rounded px-1 py-0.5 transition-colors">
|
||||
<GitBranch className="w-4 h-4 text-muted-foreground" />
|
||||
<span className="text-sm font-medium">{currentBranch}</span>
|
||||
<ChevronDown className="w-3 h-3 text-muted-foreground" />
|
||||
</button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="start" className="w-48">
|
||||
{branches?.branches?.filter(b => b.type === 'local')?.map((branch) => (
|
||||
<DropdownMenuItem
|
||||
key={branch.name}
|
||||
onClick={() => branch.name !== currentBranch && switchBranchMutation.mutate(branch.name)}
|
||||
disabled={branch.name === currentBranch || switchBranchMutation.isPending}
|
||||
className="flex items-center gap-2"
|
||||
>
|
||||
<GitBranch className="w-4 h-4" />
|
||||
<span className="flex-1 truncate">{branch.name}</span>
|
||||
{branch.name === currentBranch && <Check className={`w-4 h-4 ${GIT_UI_COLORS.current}`} />}
|
||||
</DropdownMenuItem>
|
||||
))}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
<div className="flex items-center gap-2">
|
||||
<GitBranch className="w-4 h-4 text-muted-foreground" />
|
||||
<span className="text-sm font-medium">{currentBranch}</span>
|
||||
</div>
|
||||
{status && (status.ahead > 0 || status.behind > 0) && (
|
||||
<div className="flex items-center gap-1 text-xs text-muted-foreground">
|
||||
{status.ahead > 0 && (
|
||||
@@ -218,7 +181,7 @@ export function SourceControlPanel({
|
||||
<CommitsTab repoId={repoId} />
|
||||
)}
|
||||
{activeTab === 'branches' && (
|
||||
<BranchesTab repoId={repoId} currentBranch={currentBranch} />
|
||||
<BranchesTab repoId={repoId} currentBranch={currentBranch} repoUrl={repoUrl} isRepoWorktree={isRepoWorktree} />
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -260,7 +223,7 @@ export function SourceControlPanel({
|
||||
)}>
|
||||
<DialogTitle className="flex items-center gap-2">
|
||||
<GitBranch className="w-5 h-5" />
|
||||
Source Control
|
||||
{isMobile && repoName ? repoName : 'Source Control'}
|
||||
</DialogTitle>
|
||||
{isMobile && (
|
||||
<Button
|
||||
|
||||
@@ -251,6 +251,7 @@ export function RepoDetail() {
|
||||
isOpen={sourceControlOpen}
|
||||
onClose={() => setSourceControlOpen(false)}
|
||||
currentBranch={currentBranch}
|
||||
repoName={repoName}
|
||||
/>
|
||||
|
||||
{repo && (
|
||||
|
||||
@@ -536,6 +536,7 @@ export function SessionDetail() {
|
||||
isOpen={sourceControlOpen}
|
||||
onClose={() => setSourceControlOpen(false)}
|
||||
currentBranch={repo.currentBranch || repo.branch || "main"}
|
||||
repoName={repo.repoUrl?.split("/").pop()?.replace(".git", "") || repo.localPath || "Repository"}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "opencode-manager",
|
||||
"version": "0.7.7",
|
||||
"version": "0.7.8",
|
||||
"private": false,
|
||||
"type": "module",
|
||||
"packageManager": "pnpm@10.28.1",
|
||||
|
||||
@@ -94,7 +94,6 @@ export const ENV = {
|
||||
ADMIN_EMAIL: process.env.ADMIN_EMAIL,
|
||||
ADMIN_PASSWORD: process.env.ADMIN_PASSWORD,
|
||||
ADMIN_PASSWORD_RESET: getEnvBoolean('ADMIN_PASSWORD_RESET', false),
|
||||
SECURE_COOKIES: getEnvBoolean('AUTH_SECURE_COOKIES', process.env.NODE_ENV === 'production'),
|
||||
GITHUB_CLIENT_ID: process.env.GITHUB_CLIENT_ID,
|
||||
GITHUB_CLIENT_SECRET: process.env.GITHUB_CLIENT_SECRET,
|
||||
GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID,
|
||||
|
||||
Reference in new issue
Block a user