Skip to content

Commit

Permalink
Add refs /cc #170
Browse files Browse the repository at this point in the history
  • Loading branch information
IonicaBizau committed Aug 22, 2024
1 parent 7bfc730 commit 44ddaa2
Showing 1 changed file with 33 additions and 29 deletions.
62 changes: 33 additions & 29 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const gitUp = require("git-up");
* @name gitUrlParse
* @function
* @param {String} url The Git url to parse.
* @param {Array} refs An array of strings representing the refs. This is
* helpful in the context of the URLs that contain branches with slashes.
* If user wants to identify the branch, he should pass all branch names
* of the project as part of refs parameter
* @return {GitUrl} The `GitUrl` object containing:
*
* - `protocols` (Array): An array with the url protocols (usually it has one element).
Expand All @@ -33,14 +37,14 @@ const gitUp = require("git-up");
* - `git_suffix` (Boolean): Whether to add the `.git` suffix or not.
*
*/
function gitUrlParse(url, refs=undefined) {
function gitUrlParse(url, refs) {
refs = refs || []

if (typeof url !== "string") {
throw new Error("The url must be a string.");
}

if (!refs.every(item => typeof item === 'string')) {
if (!refs.every(item => typeof item === "string")) {
throw new Error("The refs should contain only strings")
}

Expand All @@ -67,7 +71,7 @@ function gitUrlParse(url, refs=undefined) {
// Note: Some hosting services (e.g. Visual Studio Team Services) allow whitespace characters
// in the repository and owner names so we decode the URL pieces to get the correct result
urlInfo.git_suffix = /\.git$/.test(urlInfo.pathname);
urlInfo.name = decodeURIComponent((urlInfo.pathname || urlInfo.href).replace(/(^\/)|(\/$)/g, '').replace(/\.git$/, ""));
urlInfo.name = decodeURIComponent((urlInfo.pathname || urlInfo.href).replace(/(^\/)|(\/$)/g, "").replace(/\.git$/, ""));
urlInfo.owner = decodeURIComponent(urlInfo.user);

switch (urlInfo.source) {
Expand All @@ -78,44 +82,44 @@ function gitUrlParse(url, refs=undefined) {
break;
case "visualstudio.com":
// Handle VSTS SSH URLs
if (urlInfo.resource === 'vs-ssh.visualstudio.com') {
if (urlInfo.resource === "vs-ssh.visualstudio.com") {
splits = urlInfo.name.split("/");
if (splits.length === 4) {
urlInfo.organization = splits[1];
urlInfo.owner = splits[2];
urlInfo.name = splits[3];
urlInfo.full_name = splits[2] + '/' + splits[3];
urlInfo.full_name = splits[2] + "/" + splits[3];
}
break;
} else {
splits = urlInfo.name.split("/");
if (splits.length === 2) {
urlInfo.owner = splits[1];
urlInfo.name = splits[1];
urlInfo.full_name = '_git/' + urlInfo.name;
urlInfo.full_name = "_git/" + urlInfo.name;
} else if (splits.length === 3) {
urlInfo.name = splits[2];
if (splits[0] === 'DefaultCollection') {
if (splits[0] === "DefaultCollection") {
urlInfo.owner = splits[2];
urlInfo.organization = splits[0];
urlInfo.full_name = urlInfo.organization + '/_git/' + urlInfo.name;
urlInfo.full_name = urlInfo.organization + "/_git/" + urlInfo.name;
} else {
urlInfo.owner = splits[0];
urlInfo.full_name = urlInfo.owner + '/_git/' + urlInfo.name;
urlInfo.full_name = urlInfo.owner + "/_git/" + urlInfo.name;
}
} else if (splits.length === 4) {
urlInfo.organization = splits[0];
urlInfo.owner = splits[1];
urlInfo.name = splits[3];
urlInfo.full_name = urlInfo.organization + '/' + urlInfo.owner + '/_git/' + urlInfo.name;
urlInfo.full_name = urlInfo.organization + "/" + urlInfo.owner + "/_git/" + urlInfo.name;
}
break;
}

// Azure DevOps (formerly Visual Studio Team Services)
case "dev.azure.com":
case "azure.com":
if (urlInfo.resource === 'ssh.dev.azure.com') {
if (urlInfo.resource === "ssh.dev.azure.com") {
splits = urlInfo.name.split("/");
if (splits.length === 4) {
urlInfo.organization = splits[1];
Expand All @@ -129,28 +133,28 @@ function gitUrlParse(url, refs=undefined) {
urlInfo.organization = splits[0];
urlInfo.owner = splits[1];
urlInfo.name = splits[4];
urlInfo.full_name = '_git/' + urlInfo.name;
urlInfo.full_name = "_git/" + urlInfo.name;
} else if (splits.length === 3) {
urlInfo.name = splits[2];
if (splits[0] === 'DefaultCollection') {
if (splits[0] === "DefaultCollection") {
urlInfo.owner = splits[2];
urlInfo.organization = splits[0];
urlInfo.full_name = urlInfo.organization + '/_git/' + urlInfo.name;
urlInfo.full_name = urlInfo.organization + "/_git/" + urlInfo.name;
} else {
urlInfo.owner = splits[0];
urlInfo.full_name = urlInfo.owner + '/_git/' + urlInfo.name;
urlInfo.full_name = urlInfo.owner + "/_git/" + urlInfo.name;
}
} else if (splits.length === 4) {
urlInfo.organization = splits[0];
urlInfo.owner = splits[1];
urlInfo.name = splits[3];
urlInfo.full_name = urlInfo.organization + '/' + urlInfo.owner + '/_git/' + urlInfo.name;
urlInfo.full_name = urlInfo.organization + "/" + urlInfo.owner + "/_git/" + urlInfo.name;
}
if(urlInfo.query && urlInfo.query['path']) {
urlInfo.filepath = urlInfo.query['path'].replace(/^\/+/g, ''); // Strip leading slash (/)
if(urlInfo.query && urlInfo.query["path"]) {
urlInfo.filepath = urlInfo.query["path"].replace(/^\/+/g, ""); // Strip leading slash (/)
}
if(urlInfo.query && urlInfo.query['version']) { // version=GB<branch>
urlInfo.ref = urlInfo.query['version'].replace(/^GB/, ''); // remove GB
if(urlInfo.query && urlInfo.query["version"]) { // version=GB<branch>
urlInfo.ref = urlInfo.query["version"].replace(/^GB/, ""); // remove GB
}
break;
}
Expand All @@ -177,7 +181,7 @@ function gitUrlParse(url, refs=undefined) {
: editIndex > 0 ? editIndex - 1
: nameIndex;

urlInfo.owner = splits.slice(0, nameIndex).join('/');
urlInfo.owner = splits.slice(0, nameIndex).join("/");
urlInfo.name = splits[nameIndex];
if (commitIndex && issuesIndex < 0) {
urlInfo.commit = splits[nameIndex + 2]
Expand All @@ -193,7 +197,7 @@ function gitUrlParse(url, refs=undefined) {
urlInfo.filepathtype = splits[offsetNameIndex + 1];
urlInfo.ref = splits[offsetNameIndex + 2];
if (splits.length > offsetNameIndex + 3) {
urlInfo.filepath = splits.slice(offsetNameIndex + 3).join('/');
urlInfo.filepath = splits.slice(offsetNameIndex + 3).join("/");
}
}
urlInfo.organization = urlInfo.owner;
Expand Down Expand Up @@ -233,7 +237,7 @@ function gitUrlParse(url, refs=undefined) {
if(["raw","browse"].indexOf(splits[1]) >= 0) {
urlInfo.filepathtype = splits[1];
if (splits.length > 2) {
urlInfo.filepath = splits.slice(2).join('/');
urlInfo.filepath = splits.slice(2).join("/");
}
} else if(splits[1] === "commits" && splits.length > 2) {
urlInfo.commit = splits[2];
Expand Down Expand Up @@ -267,9 +271,9 @@ function gitUrlParse(url, refs=undefined) {
* @return {String} The stringified url.
*/
gitUrlParse.stringify = function (obj, type) {
type = type || ((obj.protocols && obj.protocols.length) ? obj.protocols.join('+') : obj.protocol);
const port = obj.port ? `:${obj.port}` : '';
const user = obj.user || 'git';
type = type || ((obj.protocols && obj.protocols.length) ? obj.protocols.join("+") : obj.protocol);
const port = obj.port ? `:${obj.port}` : "";
const user = obj.user || "git";
const maybeGitSuffix = obj.git_suffix ? ".git" : ""
switch (type) {
case "ssh":
Expand All @@ -285,7 +289,7 @@ gitUrlParse.stringify = function (obj, type) {
case "http":
case "https":
const auth = obj.token
? buildToken(obj) : obj.user && (obj.protocols.includes('http') || obj.protocols.includes('https'))
? buildToken(obj) : obj.user && (obj.protocols.includes("http") || obj.protocols.includes("https"))
? `${obj.user}@` : "";
return `${type}://${auth}${obj.resource}${port}/${buildPath(obj)}${maybeGitSuffix}`;
default:
Expand Down Expand Up @@ -318,9 +322,9 @@ function buildPath(obj) {
default:
// Note: Re-encode the repository and owner names for hosting services that allow whitespace characters
const encoded_full_name = obj.full_name
.split('/')
.split("/")
.map(x => encodeURIComponent(x))
.join('/');
.join("/");

return encoded_full_name;
}
Expand Down

0 comments on commit 44ddaa2

Please sign in to comment.