Skip to content

Commit e09f34d

Browse files
tkuhemiyaclaude
andcommitted
fix(client): pasting a repository page URL clones the repository
A URL copied from the browser is often a page inside the repository, such as `/tree/main`. Add project → Git URL passed it to git unchanged and named the folder after the branch, so the clone failed on GitHub, GitLab and Forgejo, and cloned into a folder named after the branch on Bitbucket. Each source control definition now maps its own page URLs back to the repository URL, and normalizePastedCloneUrl uses that before cloning. Fixes #32667 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 25bf84a commit e09f34d

8 files changed

Lines changed: 68 additions & 3 deletions

File tree

‎apps/web/src/components/CommandPalette.tsx‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,7 +1966,7 @@ function OpenCommandPaletteDialog(props: {
19661966
function getDefaultCloneDestination(
19671967
environmentId: EnvironmentId,
19681968
remoteUrl: string,
1969-
repository: string,
1969+
repository = remoteUrl,
19701970
): string {
19711971
const environment = environments.find((candidate) => candidate.environmentId === environmentId);
19721972
return getCloneDestinationPath(
@@ -2021,7 +2021,6 @@ function OpenCommandPaletteDialog(props: {
20212021
const destinationPath = getDefaultCloneDestination(
20222022
addProjectCloneFlow.environmentId,
20232023
remoteUrl,
2024-
rawRepository,
20252024
);
20262025
setAddProjectCloneFlow({
20272026
step: "confirm",

‎packages/client-runtime/src/operations/projects.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,30 @@ describe("add project shared logic", () => {
6363
);
6464
expect(normalizePastedCloneUrl("group/subgroup/project")).toBe("group/subgroup/project");
6565
expect(normalizePastedCloneUrl("/srv/git/repo.git")).toBe("/srv/git/repo.git");
66+
expect(normalizePastedCloneUrl("https://gitlab.example.com/group/tree/main")).toBe(
67+
"https://gitlab.example.com/group/tree/main",
68+
);
69+
});
70+
71+
it.each([
72+
["https://github.com/owner/repo/tree/main", "https://github.com/owner/repo"],
73+
["https://github.com/owner/repo/tree/feature/x/src", "https://github.com/owner/repo"],
74+
["https://github.com/owner/repo/blob/main/README.md", "https://github.com/owner/repo"],
75+
["https://github.example.com/owner/repo/tree/main", "https://github.example.com/owner/repo"],
76+
["https://gitlab.com/group/sub/project/-/tree/main", "https://gitlab.com/group/sub/project"],
77+
[
78+
"https://gitlab.example.com/group/project/-/blob/main/x",
79+
"https://gitlab.example.com/group/project",
80+
],
81+
["https://codeberg.org/owner/repo/src/branch/main", "https://codeberg.org/owner/repo"],
82+
["https://codeberg.org/owner/repo/src/tag/v1/docs", "https://codeberg.org/owner/repo"],
83+
["https://bitbucket.org/workspace/repo/src/master/", "https://bitbucket.org/workspace/repo"],
84+
["https://[email protected]/owner/repo/tree/main?x=1#L2", "https://[email protected]/owner/repo"],
85+
])("clones the repository a pasted page belongs to: %s", (pageUrl, repositoryUrl) => {
86+
expect(normalizePastedCloneUrl(pageUrl)).toBe(repositoryUrl);
87+
expect(getCloneDirectoryName(normalizePastedCloneUrl(pageUrl))).toBe(
88+
repositoryUrl.split("/").at(-1),
89+
);
6690
});
6791

6892
it("uses HTTPS for repositories selected through a provider", () => {

‎packages/client-runtime/src/operations/projects.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ export function githubIncompleteOwnerRepositoryMessage(
128128

129129
export function normalizePastedCloneUrl(input: string): string {
130130
const trimmed = input.trim();
131-
return REPOSITORY_SHORTHAND.test(trimmed) ? (defaultHostCloneUrl(trimmed) ?? trimmed) : trimmed;
131+
if (REPOSITORY_SHORTHAND.test(trimmed)) return defaultHostCloneUrl(trimmed) ?? trimmed;
132+
// A URL copied from the browser is often a page inside the repository, which git can't clone.
133+
return sourceControlClients.repositoryUrlFromPageUrl(trimmed) ?? trimmed;
132134
}
133135

134136
export function getDefaultCloneUrl(

‎packages/source-control-bitbucket/src/shared/definition.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
parseChangeRequestPath,
1616
parseCommitPath,
1717
projectChangeRequestHost,
18+
repositoryUrlFromPagePath,
1819
} from "@t3tools/source-control-core/shared/definition";
1920
import { icon } from "./icon.ts";
2021

@@ -23,6 +24,7 @@ const repositoryName = /^[A-Za-z0-9._-]+\/[A-Za-z0-9._-]+$/;
2324

2425
const CHANGE_REQUEST_PATH = /^\/([^/]+\/[^/]+)\/pull-requests\/(\d+)(?:\/|$)/u;
2526
const COMMIT_PATH = /^\/([^/]+\/[^/]+)\/commits\/([0-9a-f]+)\/?$/iu;
27+
const PAGE_PATH = /^\/([^/]+\/[^/]+)\/src\//u;
2628

2729
const KIND = SourceControlProviderKind.make("bitbucket");
2830

@@ -93,6 +95,8 @@ export const definition = defineSourceControl({
9395
: `https://${host}/${repository}/pull-requests/${number}`,
9496
parseChangeRequestUrl: (url) =>
9597
isBitbucketHost(url.hostname) ? parseChangeRequestPath(url, CHANGE_REQUEST_PATH) : null,
98+
repositoryUrlFromPageUrl: (url) =>
99+
isBitbucketHost(url.hostname) ? repositoryUrlFromPagePath(url, PAGE_PATH) : null,
96100
commitUrl: (input) => buildCommitUrl(input, "commits"),
97101
parseCommitUrl: (url) =>
98102
isBitbucketHost(url.hostname) ? parseCommitPath(url, COMMIT_PATH) : null,

‎packages/source-control-core/src/shared/definition.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ export interface SourceControlDefinition {
258258
readonly parseCommitUrl?: (url: URL, known: boolean) => CommitLink | null;
259259
/** Null when the URL is not one of this host's change requests. */
260260
readonly parseChangeRequestUrl: (url: URL) => ParsedChangeRequestUrl | null;
261+
/** The repository's URL when `url` is a page inside it, such as a branch's file tree. */
262+
readonly repositoryUrlFromPageUrl?: (url: URL) => string | null;
261263
/**
262264
* The change request URL a `reference` autolink might name, such as a GitHub `#123` written
263265
* through the issue route. Only a candidate: the caller must read it as a change request
@@ -410,6 +412,18 @@ export function parseChangeRequestPath(url: URL, route: RegExp): ParsedChangeReq
410412
: null;
411413
}
412414

415+
/** Reads the repository `url` is a page of with `route`, whose first group is the repository path. */
416+
export function repositoryUrlFromPagePath(url: URL, route: RegExp): string | null {
417+
const repository = route.exec(url.pathname)?.[1];
418+
if (!repository) return null;
419+
// Keeps any credentials in the pasted URL, which a clone still needs.
420+
const repositoryUrl = new URL(url);
421+
repositoryUrl.pathname = `/${repository}`;
422+
repositoryUrl.search = "";
423+
repositoryUrl.hash = "";
424+
return repositoryUrl.toString();
425+
}
426+
413427
export function changeRequestUrlBelow(webUrl: string, route: string): string {
414428
return `${webUrl.replace(/\/+$/u, "")}/${route}`;
415429
}
@@ -611,6 +625,8 @@ export interface SourceControlDefinitionRegistry {
611625
/** The first host, in registry order, that reads `url` as one of its change requests. */
612626
readonly parseChangeRequestUrl: (url: string) => ParsedChangeRequest | null;
613627
readonly parseCommitUrl: (url: string, kind?: SourceControlProviderKind) => ParsedCommit | null;
628+
/** The first host, in registry order, that reads `url` as a page inside a repository. */
629+
readonly repositoryUrlFromPageUrl: (url: string) => string | null;
614630
readonly findByChangeRequestUrl: (url: string) => SourceControlDefinition | undefined;
615631
readonly hostLabelForChangeRequestUrl: (url: string) => string;
616632
readonly isSameChangeRequest: (left: string, right: string) => boolean;
@@ -720,6 +736,15 @@ export function makeSourceControlDefinitionRegistry(
720736
}
721737
return null;
722738
},
739+
repositoryUrlFromPageUrl: (value) => {
740+
const url = httpUrl(value);
741+
if (url === null) return null;
742+
for (const definition of claimOrder) {
743+
const repositoryUrl = definition.repositoryUrlFromPageUrl?.(url);
744+
if (repositoryUrl != null) return repositoryUrl;
745+
}
746+
return null;
747+
},
723748
findByChangeRequestUrl: (url) => parseChangeRequestUrl(url)?.definition,
724749
hostLabelForChangeRequestUrl: (url) =>
725750
parseChangeRequestUrl(url)?.definition.label ?? "the host",

‎packages/source-control-forgejo/src/shared/definition.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
parseCommitPath,
1313
projectChangeRequestHost,
1414
remoteWebOrigin,
15+
repositoryUrlFromPagePath,
1516
} from "@t3tools/source-control-core/shared/definition";
1617
import { icon } from "./icon.ts";
1718

@@ -22,6 +23,7 @@ const CHANGE_REQUEST_REFERENCE =
2223
// Forgejo and Gitea run on any hostname, possibly mounted below a path.
2324
const CHANGE_REQUEST_PATH = /^\/([^/]+(?:\/[^/]+)+)\/pulls\/(\d+)(?:\/|$)/u;
2425
const COMMIT_PATH = /^\/([^/]+(?:\/[^/]+)+)\/commit\/([0-9a-f]+)\/?$/iu;
26+
const PAGE_PATH = /^\/([^/]+(?:\/[^/]+)+?)\/src\/(?:branch|tag|commit)\//u;
2527

2628
const KIND = SourceControlProviderKind.make("forgejo");
2729

@@ -84,6 +86,7 @@ export const definition = defineSourceControl({
8486
const link = parseChangeRequestPath(url, CHANGE_REQUEST_PATH);
8587
return link === null ? null : { ...link, authority: url.host.toLowerCase() };
8688
},
89+
repositoryUrlFromPageUrl: (url) => repositoryUrlFromPagePath(url, PAGE_PATH),
8790
changeRequestUrlFromReferenceAutolink: () => null,
8891
commitUrl: (input) => buildCommitUrl(input, "commit"),
8992
parseCommitUrl: (url) => parseCommitPath(url, COMMIT_PATH),

‎packages/source-control-github/src/shared/definition.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
parseChangeRequestPath,
1010
parseCommitPath,
1111
projectChangeRequestHost,
12+
repositoryUrlFromPagePath,
1213
remoteWebOrigin,
1314
type SourceControlAppSignIn,
1415
} from "@t3tools/source-control-core/shared/definition";
@@ -26,6 +27,7 @@ const CHANGE_REQUEST_REFERENCE =
2627
const CHANGE_REQUEST_PATH = /^\/([^/]+\/[^/]+)\/pull\/(\d+)(?:\/|$)/u;
2728
const COMMIT_PATH = /^\/([^/]+\/[^/]+)\/(?:commit|pull\/\d+\/commits)\/([0-9a-f]+)\/?$/iu;
2829
const ISSUE_PATH = /^\/([^/]+\/[^/]+)\/issues\/(\d+)(?:\/|$)/u;
30+
const PAGE_PATH = /^\/([^/]+\/[^/]+)\/(?:tree|blob)\/./u;
2931

3032
const KIND = SourceControlProviderKind.make("github");
3133

@@ -98,6 +100,9 @@ export const definition = defineSourceControl({
98100
parseCommitUrl: (url, known) =>
99101
known || isGitHubHost(url.hostname) ? parseCommitPath(url, COMMIT_PATH) : null,
100102
parseChangeRequestUrl: (url) => parseChangeRequestPath(url, CHANGE_REQUEST_PATH),
103+
// `tree` and `blob` are also ordinary group names on other hosts, so only GitHub's hosts read them.
104+
repositoryUrlFromPageUrl: (url) =>
105+
isGitHubHost(url.hostname) ? repositoryUrlFromPagePath(url, PAGE_PATH) : null,
101106
// GitHub writes every bare reference through `/issues/`, including pull requests.
102107
changeRequestUrlFromReferenceAutolink: (url) => {
103108
const match = isGitHubHost(url.hostname) ? ISSUE_PATH.exec(url.pathname) : null;

‎packages/source-control-gitlab/src/shared/definition.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
parseChangeRequestPath,
99
parseCommitPath,
1010
projectChangeRequestHost,
11+
repositoryUrlFromPagePath,
1112
remoteWebOrigin,
1213
} from "@t3tools/source-control-core/shared/definition";
1314
import { icon } from "./icon.ts";
@@ -20,6 +21,7 @@ const CHANGE_REQUEST_REFERENCE =
2021
// Self-managed GitLab runs on any hostname and nests projects in groups.
2122
const CHANGE_REQUEST_PATH = /^\/([^/]+(?:\/[^/]+)+)\/-\/merge_requests\/(\d+)(?:\/|$)/u;
2223
const COMMIT_PATH = /^\/([^/]+(?:\/[^/]+)+)\/-\/commit\/([0-9a-f]+)\/?$/iu;
24+
const PAGE_PATH = /^\/([^/]+(?:\/[^/]+)+?)\/-\//u;
2325

2426
const KIND = SourceControlProviderKind.make("gitlab");
2527

@@ -45,6 +47,7 @@ export const definition = defineSourceControl({
4547
? changeRequestUrlBelow(webUrl, `-/merge_requests/${number}`)
4648
: `${remoteWebOrigin(host, remoteUrl)}/${repository}/-/merge_requests/${number}`,
4749
parseChangeRequestUrl: (url) => parseChangeRequestPath(url, CHANGE_REQUEST_PATH),
50+
repositoryUrlFromPageUrl: (url) => repositoryUrlFromPagePath(url, PAGE_PATH),
4851
commitUrl: (input) => buildCommitUrl(input, "-/commit"),
4952
parseCommitUrl: (url) => parseCommitPath(url, COMMIT_PATH),
5053
// `!12` already links the merge request itself; `#12` is always an issue.

0 commit comments

Comments
 (0)