Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed parsing branch with slashes in its name #170

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,17 @@ const gitUp = require("git-up");
* - `git_suffix` (Boolean): Whether to add the `.git` suffix or not.
*
*/
function gitUrlParse(url) {
function gitUrlParse(url, refs=undefined) {
refs = refs || []

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

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

const shorthandRe = /^([a-z\d-]{1,39})\/([-\.\w]{1,100})$/i

if (shorthandRe.test(url)) {
Expand Down Expand Up @@ -242,6 +247,12 @@ function gitUrlParse(url) {
urlInfo.ref = "";
}
}

if (refs.length !== 0 && urlInfo.ref) {
urlInfo.ref = findLongestMatchingSubstring(urlInfo.href, refs) || urlInfo.ref
urlInfo.filepath = urlInfo.href.split(urlInfo.ref + "/")[1]
}

return urlInfo;
}

Expand Down Expand Up @@ -315,4 +326,16 @@ function buildPath(obj) {
}
}

function findLongestMatchingSubstring(string, array) {
let longestMatch = "";

array.forEach(item => {
if (string.includes(item) && item.length > longestMatch.length) {
longestMatch = item;
}
});

return longestMatch;
}

module.exports = gitUrlParse;
13 changes: 13 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,4 +532,17 @@ tester.describe("parse urls", test => {
test.expect(res.filepath).toBe("pkg/blob/data.yaml");
test.expect(res.toString()).toBe("https://github.com/owner/id");
});

test.should("parse branch names with slashes, when refs are provided", () => {
var res = gitUrlParse("https://github.com/owner/id/blob/branch1/branch2/data.yaml", ["branch1/branch2", "branch1"]);
test.expect(res.pathname).toBe("/owner/id/blob/branch1/branch2/data.yaml");
test.expect(res.ref).toBe("branch1/branch2");
test.expect(res.filepath).toBe("data.yaml");
var res = gitUrlParse("https://github.com/owner/id/blob/branch1/branch2/branch3/folder/data.yaml", ["branch1/branch2/branch3", "branch1"]);
test.expect(res.ref).toBe("branch1/branch2/branch3");
test.expect(res.filepath).toBe("folder/data.yaml");
var res = gitUrlParse("https://github.com/owner/id/blob/main/folder/data.yaml", ["branch1/branch2/branch3", "main"]);
test.expect(res.ref).toBe("main");
test.expect(res.filepath).toBe("folder/data.yaml");
});
});