Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

fix(clients): default clone destination to folder plus repo name (#5989)

Inaya Yousfiec141c125726
diff --git a/apps/mobile/src/features/projects/AddProjectDestinationRoute.tsx b/apps/mobile/src/features/projects/AddProjectDestinationRoute.tsx
index 04e2e236b..59b0e6c56 100644
--- a/apps/mobile/src/features/projects/AddProjectDestinationRoute.tsx
+++ b/apps/mobile/src/features/projects/AddProjectDestinationRoute.tsx
@@ -6,6 +6,7 @@ type AddProjectDestinationRouteParams = {
readonly source?: string | string[];
readonly remoteUrl?: string | string[];
readonly repositoryTitle?: string | string[];
+ readonly repositoryName?: string | string[];
};
export function AddProjectDestinationRoute({
diff --git a/apps/mobile/src/features/projects/AddProjectScreen.tsx b/apps/mobile/src/features/projects/AddProjectScreen.tsx
index 747a919a0..c7e6b534a 100644
--- a/apps/mobile/src/features/projects/AddProjectScreen.tsx
+++ b/apps/mobile/src/features/projects/AddProjectScreen.tsx
@@ -7,6 +7,9 @@ import {
canCreateProjectInEnvironment,
findExistingAddProject,
getAddProjectInitialQuery,
+ getCloneDestinationBrowsePath,
+ getCloneDestinationPath,
+ getCloneDirectoryName,
resolveAddProjectPath,
sortAddProjectProviderSources,
type AddProjectRemoteSource,
@@ -23,8 +26,8 @@ import {
} from "@t3tools/client-runtime/state/filesystem";
import {
appendBrowsePathSegment,
- ensureBrowseDirectoryPath,
inferProjectTitleFromPath,
+ isWindowsPlatform,
} from "@t3tools/client-runtime/state/projects";
import { CommandId, type EnvironmentId, ProjectId } from "@t3tools/contracts";
import { CommonActions, StackActions, useNavigation } from "@react-navigation/native";
@@ -236,11 +239,18 @@ function ProjectPathInput(props: {
);
}
-function useBrowsePathInput(environment: EnvironmentOption | null) {
+// `pinnedDirectoryName` is the repository folder the clone destination keeps
+// appended to whatever folder the user browses to. The plain add-project flow
+// passes nothing, so it keeps proposing the browsed folder itself.
+function useBrowsePathInput(environment: EnvironmentOption | null, pinnedDirectoryName = "") {
const environmentId = environment?.environmentId ?? null;
const environmentBaseDirectory = environment?.baseDirectory ?? null;
+ const clonePathCaseSensitive = !isWindowsPlatform(environment?.platform ?? "");
const [pathInput, commitPathInput] = useState(() =>
- getAddProjectInitialQuery(environmentBaseDirectory),
+ getCloneDestinationPath(
+ getAddProjectInitialQuery(environmentBaseDirectory),
+ pinnedDirectoryName,
+ ),
);
const previousEnvironmentIdRef = useRef(environmentId);
const environmentRuntime = useRemoteEnvironmentRuntime(environmentId);
@@ -259,33 +269,60 @@ function useBrowsePathInput(environment: EnvironmentOption | null) {
[browseNavigation],
);
const navigateToBrowsePath = useCallback(
- async (path: string) => {
+ async (input: {
+ readonly browseDirectoryPath: string;
+ readonly selectedDirectoryName?: string;
+ }) => {
+ const selectedDirectoryPath = input.selectedDirectoryName
+ ? appendBrowsePathSegment(input.browseDirectoryPath, input.selectedDirectoryName)
+ : input.browseDirectoryPath;
+ const nextPathInput =
+ pinnedDirectoryName && input.selectedDirectoryName
+ ? getCloneDestinationBrowsePath({
+ browseDirectoryPath: input.browseDirectoryPath,
+ selectedDirectoryName: input.selectedDirectoryName,
+ cloneDirectoryName: pinnedDirectoryName,
+ caseSensitive: clonePathCaseSensitive,
+ })
+ : getCloneDestinationPath(selectedDirectoryPath, pinnedDirectoryName);
setIsBrowseNavigating(true);
const committed = await browseNavigation.run(
async () => {
if (environment && canPreloadBrowsePath(environmentRuntime?.connectionState)) {
await loadBrowsePath({
environmentId: environment.environmentId,
- input: { partialPath: path },
+ input: { partialPath: selectedDirectoryPath },
});
}
},
- () => commitPathInput(path),
+ () => commitPathInput(nextPathInput),
);
if (committed) {
setIsBrowseNavigating(false);
}
return committed;
},
- [browseNavigation, environment, environmentRuntime?.connectionState, loadBrowsePath],
+ [
+ browseNavigation,
+ clonePathCaseSensitive,
+ environment,
+ environmentRuntime?.connectionState,
+ loadBrowsePath,
+ pinnedDirectoryName,
+ ],
);
useEffect(() => {
if (environmentId !== null && environmentId !== previousEnvironmentIdRef.current) {
previousEnvironmentIdRef.current = environmentId;
- setPathInput(getAddProjectInitialQuery(environmentBaseDirectory));
+ setPathInput(
+ getCloneDestinationPath(
+ getAddProjectInitialQuery(environmentBaseDirectory),
+ pinnedDirectoryName,
+ ),
+ );
}
- }, [environmentBaseDirectory, environmentId, setPathInput]);
+ }, [environmentBaseDirectory, environmentId, pinnedDirectoryName, setPathInput]);
useEffect(
() => () => {
@@ -632,6 +669,7 @@ export function AddProjectRepositoryScreen(props: {
source,
remoteUrl,
repositoryTitle: remoteUrl,
+ repositoryName: getCloneDirectoryName(remoteUrl),
}),
);
setIsSubmitting(false);
@@ -655,6 +693,7 @@ export function AddProjectRepositoryScreen(props: {
source,
remoteUrl: repository.sshUrl,
repositoryTitle: repository.nameWithOwner,
+ repositoryName: getCloneDirectoryName(repository.nameWithOwner),
}),
);
}
@@ -698,7 +737,11 @@ function FolderBrowser(props: {
readonly environment: EnvironmentOption;
readonly pathInput: string;
readonly setPathInput: (path: string) => void;
- readonly navigateToBrowsePath: (path: string) => Promise<boolean>;
+ readonly navigateToBrowsePath: (input: {
+ readonly browseDirectoryPath: string;
+ readonly selectedDirectoryName?: string;
+ }) => Promise<boolean>;
+ readonly pinnedDirectoryName?: string;
}) {
const accentColor = useThemeColor("--color-icon-muted");
const browsePath = useMemo(
@@ -717,9 +760,16 @@ function FolderBrowser(props: {
input: browseInput,
}),
);
+ // A pinned repository folder does not exist yet, so filtering the listing by
+ // it would empty the folder picker. Anything the user typed still filters.
+ const pinnedDirectoryName = props.pinnedDirectoryName ?? "";
+ const pinnedDirectoryMatches = isWindowsPlatform(props.environment.platform)
+ ? browsePath.filterQuery.toLowerCase() === pinnedDirectoryName.toLowerCase()
+ : browsePath.filterQuery === pinnedDirectoryName;
+ const browseFilterQuery = pinnedDirectoryMatches ? "" : browsePath.filterQuery;
const { visibleEntries: visibleBrowseEntries } = useMemo(
- () => filterFilesystemBrowseEntries(browseState.data?.entries ?? [], browsePath.filterQuery),
- [browsePath.filterQuery, browseState.data?.entries],
+ () => filterFilesystemBrowseEntries(browseState.data?.entries ?? [], browseFilterQuery),
+ [browseFilterQuery, browseState.data?.entries],
);
return (
@@ -747,7 +797,9 @@ function FolderBrowser(props: {
right={null}
onPress={() => {
if (browsePath.parentPath) {
- void props.navigateToBrowsePath(browsePath.parentPath);
+ void props.navigateToBrowsePath({
+ browseDirectoryPath: browsePath.parentPath,
+ });
}
}}
/>
@@ -760,11 +812,10 @@ function FolderBrowser(props: {
isFirst={index === 0 && !browsePath.canBrowseUp}
right={null}
onPress={() => {
- const nextPath =
- browsePath.directoryPath.length > 0
- ? appendBrowsePathSegment(browsePath.directoryPath, entry.name)
- : ensureBrowseDirectoryPath(entry.fullPath);
- void props.navigateToBrowsePath(nextPath);
+ void props.navigateToBrowsePath({
+ browseDirectoryPath: browsePath.directoryPath,
+ selectedDirectoryName: entry.name,
+ });
}}
/>
))}
@@ -836,6 +887,7 @@ export function AddProjectDestinationScreen(props: {
readonly environmentId?: string | string[];
readonly remoteUrl?: string | string[];
readonly repositoryTitle?: string | string[];
+ readonly repositoryName?: string | string[];
}) {
const cloneRepository = useAtomCommand(sourceControlEnvironment.cloneRepository, {
reportFailure: false,
@@ -844,8 +896,15 @@ export function AddProjectDestinationScreen(props: {
const createProject = useCreateProject(environment);
const remoteUrl = stringParam(props.remoteUrl);
const repositoryTitle = stringParam(props.repositoryTitle);
- const { isBrowseNavigating, navigateToBrowsePath, pathInput, setPathInput } =
- useBrowsePathInput(environment);
+ // A lookup derives this from "owner/repo", a pasted clone URL from its own
+ // last segment. Older links without the param keep the browsed folder.
+ // Trim once here: the path input and the folder-list filter must compare the
+ // same value, or a deep link with a padded param empties the folder picker.
+ const repositoryName = stringParam(props.repositoryName)?.trim() ?? "";
+ const { isBrowseNavigating, navigateToBrowsePath, pathInput, setPathInput } = useBrowsePathInput(
+ environment,
+ repositoryName,
+ );
const [isSubmitting, setIsSubmitting] = useState(false);
const [error, setError] = useState<string | null>(null);
@@ -918,6 +977,7 @@ export function AddProjectDestinationScreen(props: {
navigateToBrowsePath={navigateToBrowsePath}
pathInput={pathInput}
setPathInput={setPathInput}
+ pinnedDirectoryName={repositoryName}
/>
</>
) : (
diff --git a/apps/web/src/components/CommandPalette.logic.test.ts b/apps/web/src/components/CommandPalette.logic.test.ts
index 17949b7c9..9bae9c58a 100644
--- a/apps/web/src/components/CommandPalette.logic.test.ts
+++ b/apps/web/src/components/CommandPalette.logic.test.ts
@@ -6,6 +6,7 @@ import {
buildBrowseGroups,
buildThreadActionItems,
enumerateCommandPaletteItems,
+ filterPinnedBrowseEntries,
filterCommandPaletteGroups,
reduceCommandPaletteUiState,
type CommandPaletteGroup,
@@ -371,3 +372,39 @@ describe("buildBrowseGroups", () => {
expect(actionSettled).toBe(true);
});
});
+
+describe("filterPinnedBrowseEntries", () => {
+ const entries = [
+ { name: "repo", fullPath: "/projects/repo" },
+ { name: "work", fullPath: "/projects/work" },
+ ];
+
+ it("shows sibling folders without losing an existing pinned destination", () => {
+ expect(
+ filterPinnedBrowseEntries({
+ browseEntries: entries,
+ filterQuery: "repo",
+ pinnedDirectoryName: "repo",
+ caseSensitive: true,
+ }),
+ ).toEqual({ visibleEntries: entries, exactEntry: entries[0] });
+ });
+
+ it("matches an existing pinned destination without Windows casing", () => {
+ const windowsEntries = [
+ { name: "Repo", fullPath: "C:\\projects\\Repo" },
+ { name: "work", fullPath: "C:\\projects\\work" },
+ ];
+ expect(
+ filterPinnedBrowseEntries({
+ browseEntries: windowsEntries,
+ filterQuery: "repo",
+ pinnedDirectoryName: "repo",
+ caseSensitive: false,
+ }),
+ ).toEqual({
+ visibleEntries: windowsEntries,
+ exactEntry: windowsEntries[0],
+ });
+ });
+});
diff --git a/apps/web/src/components/CommandPalette.logic.ts b/apps/web/src/components/CommandPalette.logic.ts
index 95d7a91b7..ed758830f 100644
--- a/apps/web/src/components/CommandPalette.logic.ts
+++ b/apps/web/src/components/CommandPalette.logic.ts
@@ -3,6 +3,7 @@ import {
type KeybindingCommand,
THREAD_JUMP_KEYBINDING_COMMANDS,
} from "@t3tools/contracts";
+import { filterFilesystemBrowseEntries } from "@t3tools/client-runtime/state/filesystem";
import type { SidebarThreadSortOrder } from "@t3tools/contracts/settings";
import * as Arr from "effect/Array";
import * as Result from "effect/Result";
@@ -397,6 +398,25 @@ export function buildBrowseGroups(input: {
return [{ value: "directories", label: "Directories", items }];
}
+export function filterPinnedBrowseEntries(input: {
+ browseEntries: ReadonlyArray<FilesystemBrowseEntry>;
+ filterQuery: string;
+ pinnedDirectoryName: string;
+ caseSensitive: boolean;
+}): ReturnType<typeof filterFilesystemBrowseEntries> {
+ const namesMatch = (left: string, right: string) =>
+ input.caseSensitive ? left === right : left.toLowerCase() === right.toLowerCase();
+ const visibleFilterQuery = namesMatch(input.filterQuery, input.pinnedDirectoryName)
+ ? ""
+ : input.filterQuery;
+ const { visibleEntries } = filterFilesystemBrowseEntries(input.browseEntries, visibleFilterQuery);
+ const exactEntry =
+ input.filterQuery.length > 0
+ ? (input.browseEntries.find((entry) => namesMatch(entry.name, input.filterQuery)) ?? null)
+ : null;
+ return { visibleEntries, exactEntry };
+}
+
export function getCommandPaletteMode(input: {
currentView: CommandPaletteView | null;
isBrowsing: boolean;
diff --git a/apps/web/src/components/CommandPalette.tsx b/apps/web/src/components/CommandPalette.tsx
index 413ebca30..410be73b4 100644
--- a/apps/web/src/components/CommandPalette.tsx
+++ b/apps/web/src/components/CommandPalette.tsx
@@ -1,7 +1,12 @@
"use client";
import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";
-import { canCreateProjectInEnvironment } from "@t3tools/client-runtime/operations/projects";
+import {
+ canCreateProjectInEnvironment,
+ getCloneDestinationBrowsePath,
+ getCloneDestinationPath,
+ getCloneDirectoryName,
+} from "@t3tools/client-runtime/operations/projects";
import { connectionStatusText } from "@t3tools/client-runtime/connection";
import { threadSearchMatchKey } from "@t3tools/client-runtime/state/thread-search";
import {
@@ -109,6 +114,7 @@ import {
type CommandPaletteSubmenuItem,
type CommandPaletteView,
filterCommandPaletteGroups,
+ filterPinnedBrowseEntries,
getCommandPaletteInputPlaceholder,
getCommandPaletteMode,
ITEM_ICON_CLASS,
@@ -807,6 +813,16 @@ function OpenCommandPaletteDialog(props: {
);
const isRemoteProjectCloneFlow = addProjectCloneFlow !== null;
const isRemoteProjectRepositoryStep = addProjectCloneFlow?.step === "repository";
+ // The destination step pins the repository folder onto the browsed path, so
+ // the proposed clone target is "<chosen folder>/<repo>" instead of the bare
+ // folder. A lookup reports "owner/repo"; a pasted clone URL falls back to its
+ // own last segment, minus ".git".
+ const pinnedCloneDirectoryName =
+ addProjectCloneFlow?.step === "confirm"
+ ? getCloneDirectoryName(
+ addProjectCloneFlow.repository?.nameWithOwner ?? addProjectCloneFlow.remoteUrl,
+ )
+ : "";
const browsePath = useMemo(
() => getFilesystemBrowsePath(query, browseEnvironmentPlatform, !isRemoteProjectRepositoryStep),
[browseEnvironmentPlatform, isRemoteProjectRepositoryStep, query],
@@ -879,8 +895,16 @@ function OpenCommandPaletteDialog(props: {
const isBrowsePending = browseQuery.isPending;
const browseEntries = browseResult?.entries ?? EMPTY_BROWSE_ENTRIES;
const { visibleEntries: visibleBrowseEntries, exactEntry: exactBrowseEntry } = useMemo(
- () => filterFilesystemBrowseEntries(browseEntries, browsePath.filterQuery),
- [browseEntries, browsePath.filterQuery],
+ () =>
+ pinnedCloneDirectoryName
+ ? filterPinnedBrowseEntries({
+ browseEntries,
+ filterQuery: browsePath.filterQuery,
+ pinnedDirectoryName: pinnedCloneDirectoryName,
+ caseSensitive: !isWindowsPlatform(browseEnvironmentPlatform),
+ })
+ : filterFilesystemBrowseEntries(browseEntries, browsePath.filterQuery),
+ [browseEntries, browseEnvironmentPlatform, browsePath.filterQuery, pinnedCloneDirectoryName],
);
const prefetchBrowsePath = useCallback(
@@ -1793,7 +1817,10 @@ function OpenCommandPaletteDialog(props: {
const provider = remoteProjectSourceProvider(addProjectCloneFlow.source);
if (!provider) {
- const destinationPath = getDefaultCloneParentPath(addProjectCloneFlow.environmentId);
+ const destinationPath = getCloneDestinationPath(
+ getDefaultCloneParentPath(addProjectCloneFlow.environmentId),
+ getCloneDirectoryName(rawRepository),
+ );
setAddProjectCloneFlow({
step: "confirm",
environmentId: addProjectCloneFlow.environmentId,
@@ -1830,7 +1857,10 @@ function OpenCommandPaletteDialog(props: {
return;
}
const repository = lookupResult.value;
- const destinationPath = getDefaultCloneParentPath(addProjectCloneFlow.environmentId);
+ const destinationPath = getCloneDestinationPath(
+ getDefaultCloneParentPath(addProjectCloneFlow.environmentId),
+ getCloneDirectoryName(repository.nameWithOwner),
+ );
setAddProjectCloneFlow({
step: "confirm",
environmentId: addProjectCloneFlow.environmentId,
@@ -1906,7 +1936,14 @@ function OpenCommandPaletteDialog(props: {
const browseTo = useCallback(
async (name: string): Promise<void> => {
- const nextQuery = appendBrowsePathSegment(query, name);
+ const nextQuery = pinnedCloneDirectoryName
+ ? getCloneDestinationBrowsePath({
+ browseDirectoryPath: browsePath.directoryPath,
+ selectedDirectoryName: name,
+ cloneDirectoryName: pinnedCloneDirectoryName,
+ caseSensitive: !isWindowsPlatform(browseEnvironmentPlatform),
+ })
+ : appendBrowsePathSegment(query, name);
await browseNavigation.run(
() => prefetchBrowsePath(getBrowseDirectoryPath(nextQuery)),
() => {
@@ -1916,7 +1953,14 @@ function OpenCommandPaletteDialog(props: {
},
);
},
- [browseNavigation, prefetchBrowsePath, query],
+ [
+ browseNavigation,
+ browseEnvironmentPlatform,
+ browsePath.directoryPath,
+ pinnedCloneDirectoryName,
+ prefetchBrowsePath,
+ query,
+ ],
);
const browseUp = useCallback(async (): Promise<void> => {
@@ -1925,15 +1969,16 @@ function OpenCommandPaletteDialog(props: {
return;
}
+ const nextQuery = getCloneDestinationPath(parentPath, pinnedCloneDirectoryName);
await browseNavigation.run(
() => prefetchBrowsePath(parentPath),
() => {
setHighlightedItemValue(null);
- setQuery(parentPath);
+ setQuery(nextQuery);
setBrowseGeneration((generation) => generation + 1);
},
);
- }, [browseNavigation, browsePath.parentPath, prefetchBrowsePath]);
+ }, [browseNavigation, browsePath.parentPath, pinnedCloneDirectoryName, prefetchBrowsePath]);
// Resolve the add-project path from browse data when available. When the
// query has a trailing separator (e.g. "~/projects/foo/"), parentPath is the
diff --git a/packages/client-runtime/src/operations/projects.test.ts b/packages/client-runtime/src/operations/projects.test.ts
index 4cca703c1..60cc1bacd 100644
--- a/packages/client-runtime/src/operations/projects.test.ts
+++ b/packages/client-runtime/src/operations/projects.test.ts
@@ -13,6 +13,9 @@ import {
canCreateProjectInEnvironment,
findExistingAddProject,
getAddProjectInitialQuery,
+ getCloneDestinationBrowsePath,
+ getCloneDestinationPath,
+ getCloneDirectoryName,
resolveAddProjectPath,
sortAddProjectProviderSources,
} from "./projects.ts";
@@ -34,6 +37,79 @@ describe("add project shared logic", () => {
expect(getAddProjectInitialQuery("C:\\work")).toBe("C:\\work\\");
});
+ it("derives the clone folder name from the repository name with owner", () => {
+ expect(getCloneDirectoryName("owner/repo")).toBe("repo");
+ expect(getCloneDirectoryName("org/project/repo")).toBe("repo");
+ expect(getCloneDirectoryName("repo")).toBe("repo");
+ expect(getCloneDirectoryName("owner/repo/")).toBe("repo");
+ expect(getCloneDirectoryName("")).toBe("");
+ expect(getCloneDirectoryName(null)).toBe("");
+ });
+
+ it("derives the clone folder name from any pasted clone URL", () => {
+ expect(getCloneDirectoryName("https://github.com/owner/repo.git")).toBe("repo");
+ expect(getCloneDirectoryName("https://github.com/owner/repo")).toBe("repo");
+ expect(getCloneDirectoryName("https://github.com/owner/repo/")).toBe("repo");
+ expect(getCloneDirectoryName("[email protected]:owner/repo.git")).toBe("repo");
+ expect(getCloneDirectoryName("ssh://[email protected]:22/owner/repo.git")).toBe("repo");
+ expect(getCloneDirectoryName("https://[email protected]/owner/repo.git")).toBe("repo");
+ expect(getCloneDirectoryName("https://dev.azure.com/org/project/_git/repo")).toBe("repo");
+ expect(getCloneDirectoryName("https://github.com/owner/repo.git?ref=main#readme")).toBe("repo");
+ expect(getCloneDirectoryName("/srv/git/repo.git")).toBe("repo");
+ expect(getCloneDirectoryName("C:\\src\\repo.git")).toBe("repo");
+ expect(getCloneDirectoryName("[email protected]:repo.git")).toBe("repo");
+ expect(getCloneDirectoryName(" https://github.com/owner/repo.git ")).toBe("repo");
+ });
+
+ it("keeps a numeric repository name that sits on a path", () => {
+ expect(getCloneDirectoryName("https://github.com/acme/123.git")).toBe("123");
+ expect(getCloneDirectoryName("https://github.com/acme/123")).toBe("123");
+ expect(getCloneDirectoryName("[email protected]:acme/123.git")).toBe("123");
+ });
+
+ it("proposes no clone folder for a link that names no repository", () => {
+ expect(getCloneDirectoryName("https://github.com/")).toBe("");
+ expect(getCloneDirectoryName("https://github.com")).toBe("");
+ expect(getCloneDirectoryName("[email protected]:")).toBe("");
+ expect(getCloneDirectoryName("ssh://[email protected]:22")).toBe("");
+ expect(getCloneDirectoryName("https://")).toBe("");
+ });
+
+ it("proposes the clone destination inside the selected directory", () => {
+ expect(getCloneDestinationPath("~/Projects/", "repo")).toBe("~/Projects/repo");
+ expect(getCloneDestinationPath("~/Projects", "repo")).toBe("~/Projects/repo");
+ expect(getCloneDestinationPath("C:\\work\\", "repo")).toBe("C:\\work\\repo");
+ expect(getCloneDestinationPath("~/Projects/", null)).toBe("~/Projects/");
+ expect(getCloneDestinationPath("~/Projects/", "")).toBe("~/Projects/");
+ });
+
+ it("keeps pinned clone destinations anchored to the browsed directory", () => {
+ expect(
+ getCloneDestinationBrowsePath({
+ browseDirectoryPath: "~/Projects/",
+ selectedDirectoryName: "work",
+ cloneDirectoryName: "repo",
+ caseSensitive: true,
+ }),
+ ).toBe("~/Projects/work/repo");
+ expect(
+ getCloneDestinationBrowsePath({
+ browseDirectoryPath: "~/Projects/",
+ selectedDirectoryName: "repo",
+ cloneDirectoryName: "repo",
+ caseSensitive: true,
+ }),
+ ).toBe("~/Projects/repo/");
+ expect(
+ getCloneDestinationBrowsePath({
+ browseDirectoryPath: "C:\\Projects\\",
+ selectedDirectoryName: "Repo",
+ cloneDirectoryName: "repo",
+ caseSensitive: false,
+ }),
+ ).toBe("C:\\Projects\\Repo\\");
+ });
+
it("rejects unsupported windows paths on non-windows environments", () => {
expect(
resolveAddProjectPath({
diff --git a/packages/client-runtime/src/operations/projects.ts b/packages/client-runtime/src/operations/projects.ts
index 056f96b21..914931689 100644
--- a/packages/client-runtime/src/operations/projects.ts
+++ b/packages/client-runtime/src/operations/projects.ts
@@ -13,6 +13,7 @@ import * as Option from "effect/Option";
import * as Order from "effect/Order";
import {
+ appendBrowsePathSegment,
ensureBrowseDirectoryPath,
findProjectByPath,
inferProjectTitleFromPath,
@@ -176,6 +177,76 @@ export function getAddProjectInitialQuery(baseDirectory: string | null | undefin
return trimmed.length === 0 ? "~/" : ensureBrowseDirectoryPath(trimmed);
}
+/**
+ * Folder name `git clone` would pick, from either a looked-up repository or a
+ * pasted clone URL. Providers report `owner/repo`, Azure DevOps reports
+ * `org/project/repo`, and a URL can arrive in any form: `https://host/owner/
+ * repo.git`, `ssh://git@host:22/owner/repo`, `git@host:owner/repo.git`, with
+ * or without a query, a fragment or a trailing slash. The repository is always
+ * the last segment, minus the `.git` suffix.
+ */
+export function getCloneDirectoryName(repositoryOrRemoteUrl: string | null | undefined): string {
+ const withoutQuery = (repositoryOrRemoteUrl ?? "").split(/[?#]/)[0]?.trim() ?? "";
+ const schemeIndex = withoutQuery.indexOf("://");
+ // A remote URL carries a host before the repository path. The host is never
+ // the repository, so a link that stops at the host, or at a port, names
+ // nothing and the destination falls back to the browsed folder.
+ const hasHost = schemeIndex >= 0 || /^[^/\\:]+@[^/\\:]+:/.test(withoutQuery);
+ const pathPart = schemeIndex >= 0 ? withoutQuery.slice(schemeIndex + "://".length) : withoutQuery;
+ const segments = pathPart.split(/[/\\:]+/).filter((segment) => segment.trim().length > 0);
+ if (hasHost && segments.length < 2) {
+ return "";
+ }
+
+ const lastSegment = segments.at(-1)?.trim() ?? "";
+ // A port can only sit directly behind the authority, so it is a port only
+ // when nothing follows it. Deeper segments are path, even when numeric: the
+ // repository in `https://host/acme/123` really is named `123`.
+ if (hasHost && segments.length === 2 && /^\d+$/.test(lastSegment)) {
+ return "";
+ }
+ return lastSegment.endsWith(".git") ? lastSegment.slice(0, -".git".length) : lastSegment;
+}
+
+/**
+ * Clone destination proposed for a directory: the directory the user picked
+ * plus the repository folder inside it. Without a name the directory is the
+ * destination, which is what the raw clone URL flow keeps doing.
+ */
+export function getCloneDestinationPath(
+ directoryPath: string,
+ directoryName: string | null | undefined,
+): string {
+ const name = directoryName?.trim() ?? "";
+ if (name.length === 0) {
+ return directoryPath;
+ }
+ return `${ensureBrowseDirectoryPath(directoryPath)}${name}`;
+}
+
+/**
+ * Destination query after choosing a directory while the clone folder is
+ * pinned in the path input. Selecting an existing directory with the pinned
+ * name uses that directory directly instead of producing `repo/repo`.
+ */
+export function getCloneDestinationBrowsePath(input: {
+ readonly browseDirectoryPath: string;
+ readonly selectedDirectoryName: string;
+ readonly cloneDirectoryName: string;
+ readonly caseSensitive: boolean;
+}): string {
+ const selectedDirectoryPath = appendBrowsePathSegment(
+ input.browseDirectoryPath,
+ input.selectedDirectoryName,
+ );
+ const selectedDirectoryMatches = input.caseSensitive
+ ? input.selectedDirectoryName === input.cloneDirectoryName
+ : input.selectedDirectoryName.toLowerCase() === input.cloneDirectoryName.toLowerCase();
+ return selectedDirectoryMatches
+ ? selectedDirectoryPath
+ : getCloneDestinationPath(selectedDirectoryPath, input.cloneDirectoryName);
+}
+
export function resolveAddProjectPath(input: {
readonly rawPath: string;
readonly currentProjectCwd?: string | null;
diff --git a/packages/client-runtime/src/state/projects.ts b/packages/client-runtime/src/state/projects.ts
index 31f5ce111..10ec5025f 100644
--- a/packages/client-runtime/src/state/projects.ts
+++ b/packages/client-runtime/src/state/projects.ts
@@ -9,7 +9,7 @@ import {
export { normalizeProjectPathForComparison, normalizeProjectPathForDispatch };
-const isWindowsPlatform = (platform: string): boolean => {
+export const isWindowsPlatform = (platform: string): boolean => {
return /^win(dows)?/i.test(platform);
};