Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve URL normalization in bin.ts #42

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Changes from all commits
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
25 changes: 15 additions & 10 deletions bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,20 @@ function green(message: string) {
return "\u001b[" + 32 + "m" + message + "\u001b[" + 39 + "m";
}

function normalizeUrl(url: string, https: boolean) {
// remove .git suffix
url = url.replace(".git", "")

// normalize git@host urls
if (url.startsWith("git@")) {
url = url.replace(/^git@([^:]+):(.*)$/, (https ? "https" : "http") + "://$1/$2");
}

// remove trailing slashes
url = url.replace(/\/+$/, "")
return new URL(url)
}

function getRemoteUrl(https = true) {
try {
const file = join(Deno.cwd(), ".git", "config");
Expand All @@ -141,16 +155,7 @@ function getRemoteUrl(https = true) {
return;
}

const remoteUrl = new URL(
url.replace(/^git@([^:]+):(.*)\.git$/, "https://$1/$2"),
);

if (https) {
remoteUrl.protocol = "https:";
}

// remove trailing slashes
return remoteUrl.href.replace(/\/+$/, "");
return normalizeUrl(url, https).href;
} catch (err) {
console.error(red(err.message));
// Ignore
Expand Down