feat(repo): add graphical folder picker for adding local repositories (#324)

* feat(repo): add graphical folder picker for adding local repositories

Previously, adding a local repository or scanning a folder in the
"Add Repository" dialog required typing an absolute filesystem path by
hand. This adds a navigable directory browser so users can select a
folder graphically instead.

Backend
- New shared Zod schemas (DirectoryEntry, BrowseDirectoryResponse,
  BrowseDirectoryRequest) exported via shared/types and shared/schemas.
- New env var REPO_BROWSE_ROOT plus getBrowseRootPath() helper. It is
  intentionally NOT defaulted to WORKSPACE_PATH: folder browsing is
  opt-in and only works when an administrator explicitly configures a
  root directory.
- New filesystem service (browseDirectory) that lists only directories
  (hidden entries filtered), flags Git repositories, and confines all
  access to the configured root:
    - 501 when REPO_BROWSE_ROOT is not configured
    - 403 on path traversal outside the root
    - 404 for missing directories, 400 when the target is a file
- New authenticated route GET /api/filesystem/browse, registered under
  the protected API group.

Frontend
- New api client browseDirectory() (uses fetchWrapper, so auth cookies
  are sent).
- New DirectoryPickerDialog: navigate into subfolders, go up a level,
  Git repos marked with an icon, and a "Select This Folder" action.
  Retries are disabled so a configuration error is not polled
  repeatedly.
- When browsing is disabled (501), the dialog shows a clear, actionable
  message telling the user to set REPO_BROWSE_ROOT and to enter the
  path manually in the meantime, instead of a generic error.
- AddRepoDialog gains a "Browse" button next to both the Local and
  Folder path inputs; the chosen path is written back into the active
  field.

Tests
- Service and route tests covering listing, Git-repo detection, hidden
  filtering, parent navigation, and the 400/403/404/501 error paths.

Security note: because there is no default root, the picker is inert
until REPO_BROWSE_ROOT is set, and it can never escape that root.

* fix(fs): reject symlink escapes in directory browse

Canonicalize root and requested path with fs.realpath() and gate on
path.relative() so symlinks inside REPO_BROWSE_ROOT pointing outside
are rejected with 403 before fs.stat/fs.readdir follow them. Handles
root '/' correctly. Adds regression test for symlink escape.

* fix(ui): make directory picker fill dialog height

Replace grid content-start packing with explicit grid-rows tracks so
the directory list grows to fill the dialog and scrolls while the
header, path, and footer stay fixed. Drops the ineffective flex-1
from the grid child on the scroll container.

---------

Co-authored-by: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com>
This commit is contained in:
Alexander DrewekeandChris Scott authored and GitHub committed 2026-07-26 21:12:13 -04:00
1 parent 8d5102dc90
commit f8e361c0b8
12 files changed
+536 -37

No files matched your search

+74 -36
View File
@@ -5,7 +5,8 @@ import { Button } from '@/components/ui/button'
import { Input } from '@/components/ui/input'
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'
import { Tabs, TabsList, TabsTrigger } from '@/components/ui/tabs'
import { Loader2 } from 'lucide-react'
import { DirectoryPickerDialog } from './DirectoryPickerDialog'
import { Loader2, FolderSearch } from 'lucide-react'
import { showToast } from '@/lib/toast'
import { invalidateRepoListCaches } from '@/lib/queryInvalidation'
import { getRepoBaseDirectoryName, getRepoDirectoryNameError, getRepoNameFromUrl, normalizeRepoUrlForCompare, sanitizeRepoDirectoryName } from '@opencode-manager/shared/utils'
@@ -25,6 +26,7 @@ export function AddRepoDialog({ open, onOpenChange }: AddRepoDialogProps) {
const [directoryName, setDirectoryName] = useState('')
const [branch, setBranch] = useState('')
const [skipSSHVerification, setSkipSSHVerification] = useState(false)
const [pickerOpen, setPickerOpen] = useState(false)
const directoryTouched = useRef(false)
const queryClient = useQueryClient()
@@ -143,17 +145,17 @@ export function AddRepoDialog({ open, onOpenChange }: AddRepoDialogProps) {
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent mobileFullscreen mobileSwipeToClose className="content-start gap-0 sm:max-w-[500px] sm:max-h-[80vh] sm:h-auto sm:top-[50%] sm:translate-y-[-50%] bg-[#141414] border-[#2a2a2a]">
<DialogContent mobileFullscreen mobileSwipeToClose className="content-start gap-0 sm:max-w-[500px] sm:max-h-[80vh] sm:h-auto sm:top-[50%] sm:translate-y-[-50%] bg-card border-border">
<DialogHeader className="px-4 sm:px-6 pt-2 sm:pt-6 pb-2 sm:pb-3 h-fit">
<DialogTitle className="text-xl bg-gradient-to-r from-white to-gray-300 bg-clip-text text-transparent">
<DialogTitle className="text-xl text-foreground">
Add Repository
</DialogTitle>
</DialogHeader>
<form onSubmit={handleSubmit} className="space-y-4 px-4 sm:px-6">
<div className="space-y-2">
<label className="text-sm text-zinc-400">Repository Type</label>
<label className="text-sm text-muted-foreground">Repository Type</label>
<Tabs value={repoType} onValueChange={(value) => setRepoType(value as 'remote' | 'local' | 'folder')}>
<TabsList className="grid w-full grid-cols-3 bg-[#1a1a1a]">
<TabsList className="grid w-full grid-cols-3 bg-muted">
<TabsTrigger value="remote">Remote</TabsTrigger>
<TabsTrigger value="local">Local</TabsTrigger>
<TabsTrigger value="folder">Folder</TabsTrigger>
@@ -163,43 +165,67 @@ export function AddRepoDialog({ open, onOpenChange }: AddRepoDialogProps) {
{repoType === 'remote' ? (
<div className="space-y-2">
<label className="text-sm text-zinc-400">Repository URL</label>
<label className="text-sm text-muted-foreground">Repository URL</label>
<Input
placeholder="owner/repo or https://github.com/user/repo.git"
value={repoUrl}
onChange={(e) => handleRepoUrlChange(e.target.value)}
disabled={mutation.isPending}
className="bg-[#1a1a1a] border-[#2a2a2a] text-white placeholder:text-zinc-500 min-h-[44px] text-base"
className="bg-muted border-border text-foreground placeholder:text-muted-foreground min-h-[44px] text-base"
/>
<p className="text-xs text-zinc-500">
<p className="text-xs text-muted-foreground">
Full URL or shorthand format (owner/repo for GitHub)
</p>
</div>
) : repoType === 'local' ? (
<div className="space-y-2">
<label className="text-sm text-zinc-400">Local Path</label>
<Input
placeholder="my-local-project OR /absolute/path/to/git-repo"
value={localPath}
onChange={(e) => setLocalPath(e.target.value)}
disabled={mutation.isPending}
className="bg-[#1a1a1a] border-[#2a2a2a] text-white placeholder:text-zinc-500 min-h-[44px] text-base"
/>
<p className="text-xs text-zinc-500">
<label className="text-sm text-muted-foreground">Local Path</label>
<div className="flex gap-2">
<Input
placeholder="my-local-project OR /absolute/path/to/git-repo"
value={localPath}
onChange={(e) => setLocalPath(e.target.value)}
disabled={mutation.isPending}
className="bg-muted border-border text-foreground placeholder:text-muted-foreground min-h-[44px] text-base"
/>
<Button
type="button"
variant="outline"
onClick={() => setPickerOpen(true)}
disabled={mutation.isPending}
className="min-h-[44px] shrink-0 border-border bg-muted px-3 text-muted-foreground hover:bg-accent"
aria-label="Browse for folder"
>
<FolderSearch className="h-4 w-4" />
</Button>
</div>
<p className="text-xs text-muted-foreground">
Directory name for a new repo, or an absolute path to link an existing Git repo
</p>
</div>
) : (
<div className="space-y-2">
<label className="text-sm text-zinc-400">Folder Path</label>
<Input
placeholder="/absolute/path/to/projects"
value={folderPath}
onChange={(e) => setFolderPath(e.target.value)}
disabled={mutation.isPending}
className="bg-[#1a1a1a] border-[#2a2a2a] text-white placeholder:text-zinc-500 min-h-[44px] text-base"
/>
<p className="text-xs text-zinc-500">
<label className="text-sm text-muted-foreground">Folder Path</label>
<div className="flex gap-2">
<Input
placeholder="/absolute/path/to/projects"
value={folderPath}
onChange={(e) => setFolderPath(e.target.value)}
disabled={mutation.isPending}
className="bg-muted border-border text-foreground placeholder:text-muted-foreground min-h-[44px] text-base"
/>
<Button
type="button"
variant="outline"
onClick={() => setPickerOpen(true)}
disabled={mutation.isPending}
className="min-h-[44px] shrink-0 border-border bg-muted px-3 text-muted-foreground hover:bg-accent"
aria-label="Browse for folder"
>
<FolderSearch className="h-4 w-4" />
</Button>
</div>
<p className="text-xs text-muted-foreground">
Scans the folder for nested Git repositories and links each one
</p>
</div>
@@ -207,13 +233,13 @@ export function AddRepoDialog({ open, onOpenChange }: AddRepoDialogProps) {
{showDirectoryName && (
<div className="space-y-2">
<label className="text-sm text-zinc-400">Directory Name</label>
<label className="text-sm text-muted-foreground">Directory Name</label>
<Input
placeholder="Auto-detected from URL"
value={directoryName}
onChange={(e) => handleDirectoryNameChange(e.target.value)}
disabled={mutation.isPending}
className="bg-[#1a1a1a] border-[#2a2a2a] text-white placeholder:text-zinc-500 min-h-[44px] text-base"
className="bg-muted border-border text-foreground placeholder:text-muted-foreground min-h-[44px] text-base"
/>
{directoryNameError ? (
<p className="text-xs text-amber-400">
@@ -229,7 +255,7 @@ export function AddRepoDialog({ open, onOpenChange }: AddRepoDialogProps) {
{' '}Choose a different directory name to clone this fork.
</p>
) : (
<p className="text-xs text-zinc-500">
<p className="text-xs text-muted-foreground">
Custom directory name for the cloned repository
</p>
)}
@@ -237,15 +263,15 @@ export function AddRepoDialog({ open, onOpenChange }: AddRepoDialogProps) {
)}
<div className="space-y-2">
<label className="text-sm text-zinc-400">Branch (optional)</label>
<label className="text-sm text-muted-foreground">Branch (optional)</label>
<Input
placeholder="Uses default if empty"
value={branch}
onChange={(e) => setBranch(e.target.value)}
disabled={mutation.isPending || repoType === 'folder'}
className="bg-[#1a1a1a] border-[#2a2a2a] text-white placeholder:text-zinc-500 min-h-[44px] text-base"
className="bg-muted border-border text-foreground placeholder:text-muted-foreground min-h-[44px] text-base"
/>
<p className="text-xs text-zinc-500">
<p className="text-xs text-muted-foreground">
{repoType === 'folder'
? 'Links each repository on its current branch'
: branch
@@ -263,13 +289,13 @@ export function AddRepoDialog({ open, onOpenChange }: AddRepoDialogProps) {
checked={skipSSHVerification}
onChange={(e) => setSkipSSHVerification(e.target.checked)}
disabled={mutation.isPending}
className="mt-1 h-5 w-5 rounded border-[#2a2a2a] bg-[#1a1a1a] text-blue-600 focus:ring-blue-600"
className="mt-1 h-5 w-5 rounded border-border bg-muted text-primary focus:ring-primary"
/>
<div className="flex-1">
<label htmlFor="skip-ssh-verification" className="cursor-pointer text-sm text-white">
<label htmlFor="skip-ssh-verification" className="cursor-pointer text-sm text-foreground">
Skip SSH host key verification
</label>
<p className="text-xs text-zinc-500">
<p className="text-xs text-muted-foreground">
Auto-accept the SSH host key for self-hosted or internal servers
</p>
</div>
@@ -279,7 +305,7 @@ export function AddRepoDialog({ open, onOpenChange }: AddRepoDialogProps) {
<Button
type="submit"
disabled={(!repoUrl && repoType === 'remote') || (!localPath && repoType === 'local') || (!folderPath && repoType === 'folder') || mutation.isPending || (showDirectoryName && (!!directoryNameError || !!directoryCollision))}
className="w-full min-h-[48px] bg-blue-600 hover:bg-blue-700 text-white text-base font-medium"
className="w-full min-h-[48px] bg-primary hover:bg-primary-hover text-primary-foreground text-base font-medium"
>
{mutation.isPending ? (
<>
@@ -297,6 +323,18 @@ export function AddRepoDialog({ open, onOpenChange }: AddRepoDialogProps) {
)}
</form>
</DialogContent>
<DirectoryPickerDialog
open={pickerOpen}
onOpenChange={setPickerOpen}
title={repoType === 'folder' ? 'Select Folder to Scan' : 'Select Local Repository'}
onSelect={(path) => {
if (repoType === 'folder') {
setFolderPath(path)
} else {
setLocalPath(path)
}
}}
/>
</Dialog>
)
}
@@ -0,0 +1,125 @@
import { useState, useCallback, useEffect } from 'react'
import { useQuery } from '@tanstack/react-query'
import { browseDirectory } from '@/api/filesystem'
import { Button } from '@/components/ui/button'
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from '@/components/ui/dialog'
import { FetchError } from '@/api/fetchWrapper'
import { Folder, FolderGit2, ChevronUp, Loader2, FolderX } from 'lucide-react'
interface DirectoryPickerDialogProps {
open: boolean
onOpenChange: (open: boolean) => void
onSelect: (path: string) => void
title?: string
}
export function DirectoryPickerDialog({ open, onOpenChange, onSelect, title = 'Select Folder' }: DirectoryPickerDialogProps) {
const [currentPath, setCurrentPath] = useState<string | undefined>(undefined)
useEffect(() => {
if (open) {
setCurrentPath(undefined)
}
}, [open])
const { data, isLoading, isError, error } = useQuery({
queryKey: ['filesystem-browse', currentPath],
queryFn: () => browseDirectory(currentPath),
enabled: open,
retry: false,
})
const isDisabled = error instanceof FetchError && error.statusCode === 501
const navigateTo = useCallback((path: string) => {
setCurrentPath(path)
}, [])
const handleSelect = useCallback(() => {
if (data?.path) {
onSelect(data.path)
onOpenChange(false)
}
}, [data?.path, onSelect, onOpenChange])
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent mobileFullscreen className="grid-rows-[auto_auto_minmax(0,1fr)_auto] gap-0 sm:max-w-[560px] sm:max-h-[80vh]">
<DialogHeader className="px-4 sm:px-6 pt-4 sm:pt-6 pb-2 h-fit">
<DialogTitle className="text-lg">{title}</DialogTitle>
</DialogHeader>
<div className="px-4 sm:px-6 pb-2">
<p className="truncate rounded bg-muted px-3 py-2 text-xs text-muted-foreground" title={data?.path}>
{data?.path ?? 'Loading...'}
</p>
</div>
<div className="mx-4 sm:mx-6 mb-4 min-h-0 overflow-y-auto rounded border border-border bg-muted">
{isLoading ? (
<div className="flex h-[240px] items-center justify-center text-muted-foreground">
<Loader2 className="h-5 w-5 animate-spin" />
</div>
) : isDisabled ? (
<div className="flex h-[240px] flex-col items-center justify-center gap-3 px-6 text-center">
<FolderX className="h-8 w-8 text-muted-foreground" />
<p className="text-sm text-foreground">Folder browsing is not enabled</p>
<p className="text-xs text-muted-foreground">
Ask your administrator to set <code className="rounded bg-accent px-1 py-0.5 text-foreground">REPO_BROWSE_ROOT</code> in the
server environment, then enter the path manually for now.
</p>
</div>
) : isError ? (
<div className="flex h-[240px] items-center justify-center px-4 text-center text-sm text-destructive">
{error instanceof Error ? error.message : 'Failed to load directory'}
</div>
) : (
<ul className="divide-y divide-border">
{data && !data.isRoot && data.parentPath !== null && (
<li>
<button
type="button"
onClick={() => navigateTo(data.parentPath!)}
className="flex w-full items-center gap-2 px-3 py-2.5 text-left text-sm text-muted-foreground hover:bg-accent"
>
<ChevronUp className="h-4 w-4 shrink-0 text-muted-foreground" />
..
</button>
</li>
)}
{data?.entries.length === 0 && (
<li className="px-3 py-4 text-center text-sm text-muted-foreground">No subfolders</li>
)}
{data?.entries.map((entry) => (
<li key={entry.path}>
<button
type="button"
onClick={() => navigateTo(entry.path)}
className="flex w-full items-center gap-2 px-3 py-2.5 text-left text-sm text-foreground hover:bg-accent"
>
{entry.isGitRepo ? (
<FolderGit2 className="h-4 w-4 shrink-0 text-primary" />
) : (
<Folder className="h-4 w-4 shrink-0 text-muted-foreground" />
)}
<span className="truncate">{entry.name}</span>
{entry.isGitRepo && <span className="ml-auto text-xs text-primary">git</span>}
</button>
</li>
))}
</ul>
)}
</div>
<DialogFooter className="px-4 sm:px-6 pb-4 sm:pb-6 gap-2">
<Button type="button" variant="outline" onClick={() => onOpenChange(false)}>
Cancel
</Button>
<Button type="button" onClick={handleSelect} disabled={!data?.path}>
Select This Folder
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)
}