Skip to content

Commit

Permalink
Merge pull request #42 from trivialkettle/patch-4
Browse files Browse the repository at this point in the history
Improve URL normalization in bin.ts
  • Loading branch information
oscarotero authored Nov 18, 2023
2 parents 7015fff + b256182 commit 3fe3643
Showing 1 changed file with 15 additions and 10 deletions.
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

0 comments on commit 3fe3643

Please sign in to comment.