Skip to content

Commit 44ddaa2

Browse files
committed
Add refs /cc #170
1 parent 7bfc730 commit 44ddaa2

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

lib/index.js

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ const gitUp = require("git-up");
99
* @name gitUrlParse
1010
* @function
1111
* @param {String} url The Git url to parse.
12+
* @param {Array} refs An array of strings representing the refs. This is
13+
* helpful in the context of the URLs that contain branches with slashes.
14+
* If user wants to identify the branch, he should pass all branch names
15+
* of the project as part of refs parameter
1216
* @return {GitUrl} The `GitUrl` object containing:
1317
*
1418
* - `protocols` (Array): An array with the url protocols (usually it has one element).
@@ -33,14 +37,14 @@ const gitUp = require("git-up");
3337
* - `git_suffix` (Boolean): Whether to add the `.git` suffix or not.
3438
*
3539
*/
36-
function gitUrlParse(url, refs=undefined) {
40+
function gitUrlParse(url, refs) {
3741
refs = refs || []
3842

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

43-
if (!refs.every(item => typeof item === 'string')) {
47+
if (!refs.every(item => typeof item === "string")) {
4448
throw new Error("The refs should contain only strings")
4549
}
4650

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

7377
switch (urlInfo.source) {
@@ -78,44 +82,44 @@ function gitUrlParse(url, refs=undefined) {
7882
break;
7983
case "visualstudio.com":
8084
// Handle VSTS SSH URLs
81-
if (urlInfo.resource === 'vs-ssh.visualstudio.com') {
85+
if (urlInfo.resource === "vs-ssh.visualstudio.com") {
8286
splits = urlInfo.name.split("/");
8387
if (splits.length === 4) {
8488
urlInfo.organization = splits[1];
8589
urlInfo.owner = splits[2];
8690
urlInfo.name = splits[3];
87-
urlInfo.full_name = splits[2] + '/' + splits[3];
91+
urlInfo.full_name = splits[2] + "/" + splits[3];
8892
}
8993
break;
9094
} else {
9195
splits = urlInfo.name.split("/");
9296
if (splits.length === 2) {
9397
urlInfo.owner = splits[1];
9498
urlInfo.name = splits[1];
95-
urlInfo.full_name = '_git/' + urlInfo.name;
99+
urlInfo.full_name = "_git/" + urlInfo.name;
96100
} else if (splits.length === 3) {
97101
urlInfo.name = splits[2];
98-
if (splits[0] === 'DefaultCollection') {
102+
if (splits[0] === "DefaultCollection") {
99103
urlInfo.owner = splits[2];
100104
urlInfo.organization = splits[0];
101-
urlInfo.full_name = urlInfo.organization + '/_git/' + urlInfo.name;
105+
urlInfo.full_name = urlInfo.organization + "/_git/" + urlInfo.name;
102106
} else {
103107
urlInfo.owner = splits[0];
104-
urlInfo.full_name = urlInfo.owner + '/_git/' + urlInfo.name;
108+
urlInfo.full_name = urlInfo.owner + "/_git/" + urlInfo.name;
105109
}
106110
} else if (splits.length === 4) {
107111
urlInfo.organization = splits[0];
108112
urlInfo.owner = splits[1];
109113
urlInfo.name = splits[3];
110-
urlInfo.full_name = urlInfo.organization + '/' + urlInfo.owner + '/_git/' + urlInfo.name;
114+
urlInfo.full_name = urlInfo.organization + "/" + urlInfo.owner + "/_git/" + urlInfo.name;
111115
}
112116
break;
113117
}
114118

115119
// Azure DevOps (formerly Visual Studio Team Services)
116120
case "dev.azure.com":
117121
case "azure.com":
118-
if (urlInfo.resource === 'ssh.dev.azure.com') {
122+
if (urlInfo.resource === "ssh.dev.azure.com") {
119123
splits = urlInfo.name.split("/");
120124
if (splits.length === 4) {
121125
urlInfo.organization = splits[1];
@@ -129,28 +133,28 @@ function gitUrlParse(url, refs=undefined) {
129133
urlInfo.organization = splits[0];
130134
urlInfo.owner = splits[1];
131135
urlInfo.name = splits[4];
132-
urlInfo.full_name = '_git/' + urlInfo.name;
136+
urlInfo.full_name = "_git/" + urlInfo.name;
133137
} else if (splits.length === 3) {
134138
urlInfo.name = splits[2];
135-
if (splits[0] === 'DefaultCollection') {
139+
if (splits[0] === "DefaultCollection") {
136140
urlInfo.owner = splits[2];
137141
urlInfo.organization = splits[0];
138-
urlInfo.full_name = urlInfo.organization + '/_git/' + urlInfo.name;
142+
urlInfo.full_name = urlInfo.organization + "/_git/" + urlInfo.name;
139143
} else {
140144
urlInfo.owner = splits[0];
141-
urlInfo.full_name = urlInfo.owner + '/_git/' + urlInfo.name;
145+
urlInfo.full_name = urlInfo.owner + "/_git/" + urlInfo.name;
142146
}
143147
} else if (splits.length === 4) {
144148
urlInfo.organization = splits[0];
145149
urlInfo.owner = splits[1];
146150
urlInfo.name = splits[3];
147-
urlInfo.full_name = urlInfo.organization + '/' + urlInfo.owner + '/_git/' + urlInfo.name;
151+
urlInfo.full_name = urlInfo.organization + "/" + urlInfo.owner + "/_git/" + urlInfo.name;
148152
}
149-
if(urlInfo.query && urlInfo.query['path']) {
150-
urlInfo.filepath = urlInfo.query['path'].replace(/^\/+/g, ''); // Strip leading slash (/)
153+
if(urlInfo.query && urlInfo.query["path"]) {
154+
urlInfo.filepath = urlInfo.query["path"].replace(/^\/+/g, ""); // Strip leading slash (/)
151155
}
152-
if(urlInfo.query && urlInfo.query['version']) { // version=GB<branch>
153-
urlInfo.ref = urlInfo.query['version'].replace(/^GB/, ''); // remove GB
156+
if(urlInfo.query && urlInfo.query["version"]) { // version=GB<branch>
157+
urlInfo.ref = urlInfo.query["version"].replace(/^GB/, ""); // remove GB
154158
}
155159
break;
156160
}
@@ -177,7 +181,7 @@ function gitUrlParse(url, refs=undefined) {
177181
: editIndex > 0 ? editIndex - 1
178182
: nameIndex;
179183

180-
urlInfo.owner = splits.slice(0, nameIndex).join('/');
184+
urlInfo.owner = splits.slice(0, nameIndex).join("/");
181185
urlInfo.name = splits[nameIndex];
182186
if (commitIndex && issuesIndex < 0) {
183187
urlInfo.commit = splits[nameIndex + 2]
@@ -193,7 +197,7 @@ function gitUrlParse(url, refs=undefined) {
193197
urlInfo.filepathtype = splits[offsetNameIndex + 1];
194198
urlInfo.ref = splits[offsetNameIndex + 2];
195199
if (splits.length > offsetNameIndex + 3) {
196-
urlInfo.filepath = splits.slice(offsetNameIndex + 3).join('/');
200+
urlInfo.filepath = splits.slice(offsetNameIndex + 3).join("/");
197201
}
198202
}
199203
urlInfo.organization = urlInfo.owner;
@@ -233,7 +237,7 @@ function gitUrlParse(url, refs=undefined) {
233237
if(["raw","browse"].indexOf(splits[1]) >= 0) {
234238
urlInfo.filepathtype = splits[1];
235239
if (splits.length > 2) {
236-
urlInfo.filepath = splits.slice(2).join('/');
240+
urlInfo.filepath = splits.slice(2).join("/");
237241
}
238242
} else if(splits[1] === "commits" && splits.length > 2) {
239243
urlInfo.commit = splits[2];
@@ -267,9 +271,9 @@ function gitUrlParse(url, refs=undefined) {
267271
* @return {String} The stringified url.
268272
*/
269273
gitUrlParse.stringify = function (obj, type) {
270-
type = type || ((obj.protocols && obj.protocols.length) ? obj.protocols.join('+') : obj.protocol);
271-
const port = obj.port ? `:${obj.port}` : '';
272-
const user = obj.user || 'git';
274+
type = type || ((obj.protocols && obj.protocols.length) ? obj.protocols.join("+") : obj.protocol);
275+
const port = obj.port ? `:${obj.port}` : "";
276+
const user = obj.user || "git";
273277
const maybeGitSuffix = obj.git_suffix ? ".git" : ""
274278
switch (type) {
275279
case "ssh":
@@ -285,7 +289,7 @@ gitUrlParse.stringify = function (obj, type) {
285289
case "http":
286290
case "https":
287291
const auth = obj.token
288-
? buildToken(obj) : obj.user && (obj.protocols.includes('http') || obj.protocols.includes('https'))
292+
? buildToken(obj) : obj.user && (obj.protocols.includes("http") || obj.protocols.includes("https"))
289293
? `${obj.user}@` : "";
290294
return `${type}://${auth}${obj.resource}${port}/${buildPath(obj)}${maybeGitSuffix}`;
291295
default:
@@ -318,9 +322,9 @@ function buildPath(obj) {
318322
default:
319323
// Note: Re-encode the repository and owner names for hosting services that allow whitespace characters
320324
const encoded_full_name = obj.full_name
321-
.split('/')
325+
.split("/")
322326
.map(x => encodeURIComponent(x))
323-
.join('/');
327+
.join("/");
324328

325329
return encoded_full_name;
326330
}

0 commit comments

Comments
 (0)