Skip to content

Commit 210dd4c

Browse files
fix(source-control): leave ambiguous src/branch paths on unknown hosts unchanged
On a host no definition recognizes, `/<path>/src/(branch|tag|commit)/<x>` is equally an extensionless clone URL of a self-managed GitLab project nested under groups named `src` and `branch`, so pasting it must not rewrite it. The page hook now takes `known`, like `parseCommitUrl`. Off a known Forgejo host, Forgejo answers with the page itself: alone that means no rewrite, and next to GitLab's `/-/` answer it disagrees, so that stays unchanged too. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent bb7780f commit 210dd4c

4 files changed

Lines changed: 20 additions & 9 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ describe("add project shared logic", () => {
6969
"https://gitlab.example.test/acme/tools/src/branch/worker.git",
7070
"https://gitlab.example.test/acme/tools/src/branch/worker",
7171
"https://scm.example.test/acme/tools/src/branch/worker.git",
72+
"https://scm.example.test/acme/tools/src/branch/worker",
7273
"https://scm.example.test/team/repo/src/branch/main/-/README.md",
74+
"https://code.company.test/team/repo/src/branch/main/docs",
7375
]) {
7476
expect(normalizePastedCloneUrl(gitlabProject)).toBe(gitlabProject);
7577
}
@@ -83,7 +85,6 @@ describe("add project shared logic", () => {
8385
["https://git.cafe/pingdotgg/t3code/tree/main", "/tree/", "t3code"],
8486
["https://gitlab.example.test/g/p/-/blob/main/src/branch/x", "/-/", "p"],
8587
["https://code.company.test/g/p/-/tree/main", "/-/", "p"],
86-
["https://code.company.test/team/repo/src/branch/main/docs", "/src/", "repo"],
8788
] as const) {
8889
const cloneUrl = normalizePastedCloneUrl(pageUrl);
8990
expect(cloneUrl).not.toContain(pageRoute);

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,11 @@ 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 URL when `url` is a page inside one of this host's repositories. */
262-
readonly repositoryUrlFromPage?: (url: URL) => string | null;
261+
/**
262+
* The repository URL when `url` is a page inside one of this host's repositories. `known` is set
263+
* when the caller already knows this host serves the URL's repository.
264+
*/
265+
readonly repositoryUrlFromPage?: (url: URL, known: boolean) => string | null;
263266
/**
264267
* The change request URL a `reference` autolink might name, such as a GitHub `#123` written
265268
* through the issue route. Only a candidate: the caller must read it as a change request
@@ -769,12 +772,15 @@ export function makeSourceControlDefinitionRegistry(
769772
// A recognized host's paths are its own, so another host's any-host route never rewrites them.
770773
const remote = parseGitRemoteHost(value);
771774
const owner = remote === null ? undefined : remoteOwner(remote);
772-
if (owner) return owner.repositoryUrlFromPage?.(url) ?? null;
773-
// On an unrecognized host, only rewrite when every route that matches agrees.
775+
if (owner) return owner.repositoryUrlFromPage?.(url, true) ?? null;
776+
// On an unrecognized host, only rewrite when every route that matches agrees. A route that
777+
// answers with the page itself says it may already be the clone URL.
774778
const [repositoryUrl, ...others] = new Set(
775-
claimOrder.flatMap((definition) => definition.repositoryUrlFromPage?.(url) ?? []),
779+
claimOrder.flatMap((definition) => definition.repositoryUrlFromPage?.(url, false) ?? []),
776780
);
777-
return repositoryUrl !== undefined && others.length === 0 ? repositoryUrl : null;
781+
return repositoryUrl !== undefined && repositoryUrl !== url.href && others.length === 0
782+
? repositoryUrl
783+
: null;
778784
},
779785
recognizeRemote: (remoteUrl) => {
780786
const remote = parseGitRemoteHost(remoteUrl);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ describe("Forgejo change request URLs", () => {
106106

107107
describe("Forgejo repository pages", () => {
108108
it("reads the repository from branch, tag and commit views", () => {
109-
const repositoryUrl = (url: string) => definition.repositoryUrlFromPage?.(new URL(url));
109+
const repositoryUrl = (url: string) => definition.repositoryUrlFromPage?.(new URL(url), true);
110110
expect(repositoryUrl("https://codeberg.org/forgejo/forgejo/src/branch/forgejo/README.md")).toBe(
111111
"https://codeberg.org/forgejo/forgejo",
112112
);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ export const definition = defineSourceControl({
8989
changeRequestUrlFromReferenceAutolink: () => null,
9090
commitUrl: (input) => buildCommitUrl(input, "commit"),
9191
parseCommitUrl: (url) => parseCommitPath(url, COMMIT_PATH),
92-
repositoryUrlFromPage: (url) => repositoryUrlFromPagePath(url, REPOSITORY_PAGE_PATH),
92+
repositoryUrlFromPage: (url, known) => {
93+
const repositoryUrl = repositoryUrlFromPagePath(url, REPOSITORY_PAGE_PATH);
94+
// Elsewhere the same path is a GitLab project nested under groups named `src` and `branch`.
95+
return repositoryUrl === null || known ? repositoryUrl : url.href;
96+
},
9397
changeRequestActions: new Set(["merge", "close", "reopen", "update-branch"] as const),
9498
// Neither `fj` nor `tea` checks out by number.
9599
checkoutCommand: ({ number, repositoryUrl }) =>

0 commit comments

Comments
 (0)