Skip to content

Commit

Permalink
Add support for Bitbucket Cloud raw and source urls
Browse files Browse the repository at this point in the history
  • Loading branch information
goober committed Oct 21, 2020
1 parent c92ee78 commit 5f2f4da
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/index.js
Expand Up @@ -148,10 +148,14 @@ function gitUrlParse(url) {
const blobIndex = splits.indexOf("blob", 2);
const treeIndex = splits.indexOf("tree", 2);
const commitIndex = splits.indexOf("commit", 2);
const srcIndex = splits.indexOf("src", 2);
const rawIndex = splits.indexOf("raw", 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
: nameIndex;

urlInfo.owner = splits.slice(0, nameIndex).join('/');
Expand All @@ -165,7 +169,7 @@ function gitUrlParse(url) {
urlInfo.filepathtype = "";
urlInfo.filepath = "";
const offsetNameIndex = splits.length > nameIndex && splits[nameIndex+1] === "-" ? nameIndex + 1 : nameIndex;
if ((splits.length > offsetNameIndex + 2) && (["blob", "tree"].indexOf(splits[offsetNameIndex + 1]) >= 0)) {
if ((splits.length > offsetNameIndex + 2) && (["raw","src","blob", "tree"].indexOf(splits[offsetNameIndex + 1]) >= 0)) {
urlInfo.filepathtype = splits[offsetNameIndex + 1];
urlInfo.ref = splits[offsetNameIndex + 2];
if (splits.length > offsetNameIndex + 3) {
Expand Down
20 changes: 20 additions & 0 deletions test/index.js
Expand Up @@ -133,6 +133,26 @@ tester.describe("parse urls", test => {
test.expect(res.name).toBe("name");
});

// bitbucket cloud src file
test.should("parse Bitbucket Cloud src file", () => {
var res = gitUrlParse("https://bitbucket.org/owner/name/src/master/README.md");
test.expect(res.owner).toBe("owner");
test.expect(res.name).toBe("name");
test.expect(res.filepath).toBe("README.md");
test.expect(res.ref).toBe("master");
test.expect(res.filepathtype).toBe("src");
});

// bitbucket cloud raw file
test.should("parse Bitbucket Cloud raw file", () => {
var res = gitUrlParse("https://bitbucket.org/owner/name/raw/master/README.md");
test.expect(res.owner).toBe("owner");
test.expect(res.name).toBe("name");
test.expect(res.filepath).toBe("README.md");
test.expect(res.ref).toBe("master");
test.expect(res.filepathtype).toBe("raw");
});

// https bitbucket server
test.should("parse Bitbucket Server clone over http", () => {
var res = gitUrlParse("https://user@bitbucket.companyname.com/scm/owner/name.git");
Expand Down

0 comments on commit 5f2f4da

Please sign in to comment.