Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/git/remoteUrl.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {getRemoteHost} from "./remoteUrl";

describe("getRemoteHost", () => {
it("parses https remotes", () => {
expect(getRemoteHost("https://github.com/owner/repo.git")).toBe("github.com");
});

it("parses ssh:// remotes", () => {
expect(getRemoteHost("ssh://git@github.com/owner/repo.git")).toBe("github.com");
expect(getRemoteHost("ssh://git@github.com:22/owner/repo.git")).toBe("github.com");
});

it("parses scp-style remotes", () => {
expect(getRemoteHost("git@github.com:owner/repo.git")).toBe("github.com");
expect(getRemoteHost("git@bitbucket.org:owner/repo.git")).toBe("bitbucket.org");
expect(getRemoteHost("git@myorg.ghe.com:owner/repo.git")).toBe("myorg.ghe.com");
});

it("parses scp-style remotes without a user", () => {
expect(getRemoteHost("github.com:owner/repo.git")).toBe("github.com");
});

it("lower-cases the host", () => {
expect(getRemoteHost("git@GitHub.com:owner/repo.git")).toBe("github.com");
expect(getRemoteHost("https://GitHub.com/owner/repo.git")).toBe("github.com");
});

it("returns undefined for unparseable remotes instead of throwing", () => {
expect(getRemoteHost("")).toBeUndefined();
expect(getRemoteHost("not a url")).toBeUndefined();
expect(getRemoteHost("/local/path/to/repo.git")).toBeUndefined();
});
});
23 changes: 23 additions & 0 deletions src/git/remoteUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Extract the host name from a git remote URL.
*
* Handles both URL-style remotes (`https://host/owner/repo.git`, `ssh://git@host/owner/repo.git`)
* and scp-style remotes (`git@host:owner/repo.git`), which `new URL()` rejects.
*
* Returns `undefined` when the host cannot be determined instead of throwing, so a single
* unparseable remote in a multi-root workspace does not abort extension activation.
*/
export function getRemoteHost(remoteUrl: string): string | undefined {
// scp-style: [user@]host:path (no scheme, no leading slash in path)
const scpMatch = /^(?:[^@/]+@)?([^:/]+):(?!\/\/)/.exec(remoteUrl);
if (scpMatch) {
return scpMatch[1].toLowerCase();
}

try {
const host = new URL(remoteUrl).hostname;
return host ? host.toLowerCase() : undefined;
} catch {
return undefined;
}
}
28 changes: 22 additions & 6 deletions src/git/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {getSession} from "../auth/auth";
import {getGitHubApiUri, getRemoteName, useEnterprise} from "../configuration/configuration";
import {Protocol} from "../external/protocol";
import {logDebug, logError} from "../log";
import {getRemoteHost} from "./remoteUrl";
import {API, GitExtension, RefType, RepositoryState} from "../typings/git";
import {RepositoryPermission, getRepositoryPermission} from "./repository-permissions";

Expand Down Expand Up @@ -54,6 +55,26 @@ export async function getGitHead(): Promise<string | undefined> {
}
}

function isGitHubRemote(pushUrl: string | undefined): boolean {
if (!pushUrl) {
return false;
}

if (pushUrl.indexOf("github.com") !== -1) {
return true;
}

if (useEnterprise() && pushUrl.indexOf(new URL(getGitHubApiUri()).host) !== -1) {
return true;
}

// Remotes for other hosts (e.g. a Bitbucket repo in a multi-root workspace) may use the
// scp-style `git@host:owner/repo.git` form, which `new URL()` rejects. Parse defensively so
// one such remote does not abort activation for the whole workspace.
const host = getRemoteHost(pushUrl);
return host !== undefined && host.endsWith(".ghe.com");
Comment thread
chriswydra marked this conversation as resolved.
Resolved
}

export async function getGitHubUrls(): Promise<GitHubUrls[] | null> {
const git = await getGitExtension();
if (git && git.repositories.length > 0) {
Expand All @@ -74,12 +95,7 @@ export async function getGitHubUrls(): Promise<GitHubUrls[] | null> {
remote = [r.state.remotes[0]];
}

if (
remote.length > 0 &&
(remote[0].pushUrl?.indexOf("github.com") !== -1 ||
(useEnterprise() && remote[0].pushUrl?.indexOf(new URL(getGitHubApiUri()).host) !== -1) ||
(remote[0].pushUrl ? new URL(remote[0].pushUrl).host.endsWith(".ghe.com") : false))
) {
if (remote.length > 0 && isGitHubRemote(remote[0].pushUrl)) {
const url = remote[0].pushUrl;

return {
Expand Down