Skip to content

Commit

Permalink
Merge branch 'master' of github.com:ggdaltoso/git-url-parse into new-…
Browse files Browse the repository at this point in the history
…version
  • Loading branch information
IonicaBizau committed Sep 16, 2022
2 parents be9b6d4 + 7ee8abd commit 1ca18e9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
7 changes: 5 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,14 @@ function gitUrlParse(url) {
const commitIndex = splits.indexOf("commit", 2);
const srcIndex = splits.indexOf("src", 2);
const rawIndex = splits.indexOf("raw", 2);
const editIndex = splits.indexOf("edit", 2);
nameIndex = dashIndex > 0 ? dashIndex - 1
: blobIndex > 0 ? blobIndex - 1
: treeIndex > 0 ? treeIndex - 1
: commitIndex > 0 ? commitIndex - 1
: srcIndex > 0 ? srcIndex - 1
: rawIndex > 0 ? rawIndex - 1
: editIndex > 0 ? editIndex - 1
: nameIndex;

urlInfo.owner = splits.slice(0, nameIndex).join('/');
Expand All @@ -178,8 +180,9 @@ function gitUrlParse(url) {
urlInfo.filepathtype = "";
urlInfo.filepath = "";
const offsetNameIndex = splits.length > nameIndex && splits[nameIndex+1] === "-" ? nameIndex + 1 : nameIndex;
if ((splits.length > offsetNameIndex + 2) && (["raw","src","blob", "tree"].indexOf(splits[offsetNameIndex + 1]) >= 0)) {
urlInfo.filepathtype = splits[offsetNameIndex + 1];

if ((splits.length > offsetNameIndex + 2) && (["raw", "src", "blob", "tree", "edit"].indexOf(splits[offsetNameIndex + 1]) >= 0)) {
urlInfo.filepathtype = splits[offsetNameIndex + 1];
urlInfo.ref = splits[offsetNameIndex + 2];
if (splits.length > offsetNameIndex + 3) {
urlInfo.filepath = splits.slice(offsetNameIndex + 3).join('/');
Expand Down
34 changes: 15 additions & 19 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,25 +398,21 @@ tester.describe("parse urls", test => {
test.expect(res.filepathtype).toBe("blob");
test.expect(res.filepath).toBe("test/index.js");

res = gitUrlParse("https://gitlab.com/a/b/c/d/blob/master/test/index.js");
test.expect(res.protocol).toBe("https");
test.expect(res.source).toBe("gitlab.com");
test.expect(res.owner).toBe("a/b/c");
test.expect(res.name).toBe("d");
test.expect(res.href).toBe("https://gitlab.com/a/b/c/d/blob/master/test/index.js");
test.expect(res.ref).toBe("master");
test.expect(res.filepathtype).toBe("blob");
test.expect(res.filepath).toBe("test/index.js");

res = gitUrlParse("https://gitlab.com/a/b/c/d/-/blob/master/test/index.js");
test.expect(res.protocol).toBe("https");
test.expect(res.source).toBe("gitlab.com");
test.expect(res.owner).toBe("a/b/c");
test.expect(res.name).toBe("d");
test.expect(res.href).toBe("https://gitlab.com/a/b/c/d/-/blob/master/test/index.js");
test.expect(res.ref).toBe("master");
test.expect(res.filepathtype).toBe("blob");
test.expect(res.filepath).toBe("test/index.js");
function testForFilepathtypeURL(type) {
res = gitUrlParse(`https://gitlab.com/a/b/c/d/${type}/master/test/index.js`);

test.expect(res.protocol).toBe("https");
test.expect(res.source).toBe("gitlab.com");
test.expect(res.owner).toBe("a/b/c");
test.expect(res.name).toBe("d");
test.expect(res.href).toBe(`https://gitlab.com/a/b/c/d/${type}/master/test/index.js`);
test.expect(res.ref).toBe("master");
test.expect(res.filepathtype).toBe(type);
test.expect(res.filepath).toBe("test/index.js");
}

// execute for raw, src, blob, and tree
['raw', 'blob', 'tree', 'edit'].forEach(testForFilepathtypeURL);
});

// shorthand urls
Expand Down

0 comments on commit 1ca18e9

Please sign in to comment.