Skip to content

Commit

Permalink
map custom git schemes to their supported one
Browse files Browse the repository at this point in the history
git+file is only used to distinguish between archive and repository file urls
  • Loading branch information
grob committed Aug 16, 2021
1 parent c52dd56 commit 09611c3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
43 changes: 24 additions & 19 deletions tools/admin/test/specs_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const assert = require("assert");
const specs = require("../utils/specs");
const constants = require("../constants");

const {URI} = java.net;

const GIT_URLS = {
negative: [
"http://example.com/xyz",
Expand Down Expand Up @@ -96,29 +98,32 @@ exports.testNewGitHubSpec = () => {

exports.testNewGitSpec = () => {
const schemes = [
"git",
"git+ssh",
"ssh",
"git+http",
"git+https",
"git+file"
{value: "git", expected: "git"},
{value: "git+ssh", expected: "ssh"},
{value: "ssh", expected: "ssh"},
{value: "git+http", expected: "http"},
{value: "git+https", expected: "https"},
{value: "git+file", expected: "file"}
];
const uris = [
"example.com/path/to/repo",
"example.com/path/to/repo#HEAD",
"example.com/path/to/repo#v2.x",
"example.com/path/to/repo#82116b6d1a474a37fb2783a127d4168190e61745",
"//example.com/path/to/repo",
"//example.com/path/to/repo#HEAD",
"//example.com/path/to/repo#v2.x",
"//example.com/path/to/repo#82116b6d1a474a37fb2783a127d4168190e61745",
];
schemes.forEach(scheme => {
uris.map(part => [scheme, part].join("://"))
.forEach(uri => {
const spec = specs.newGitSpec(uri);
const [url, treeish] = uri.split("#");
assert.strictEqual(spec.type, constants.TYPE_GIT, uri);
assert.strictEqual(spec.url, url, uri);
assert.strictEqual(spec.treeish, treeish || null, uri);
});
})
uris.forEach(uri => {
const gitUrl = [scheme.value, uri].join(":");
const spec = specs.newGitSpec(gitUrl);
const treeish = gitUrl.split("#")[1] || null;
const receivedUri = URI.create(spec.url);
const expectedUri = URI.create(gitUrl);
assert.strictEqual(spec.type, constants.TYPE_GIT, gitUrl);
assert.strictEqual(receivedUri.getScheme(), scheme.expected, gitUrl);
assert.strictEqual(receivedUri.getSchemeSpecificPart(), expectedUri.getSchemeSpecificPart(), gitUrl);
assert.strictEqual(spec.treeish, expectedUri.getFragment(), gitUrl);
});
});
};

exports.testNewArchiveSpec = () => {
Expand Down
10 changes: 9 additions & 1 deletion tools/admin/utils/specs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
const {URI} = java.net;
const constants = require("../constants");

const GIT_SCHEMES = {
"git+ssh": "ssh",
"git+http": "http",
"git+https": "https",
"git+file": "file"
};

exports.isValidUrl = (url) => {
try {
URI.create(url);
Expand Down Expand Up @@ -57,8 +64,9 @@ const newGitHubSpec = exports.newGitHubSpec = (url) => {

const newGitSpec = exports.newGitSpec = (url) => {
const uri = URI.create(url);
const scheme = GIT_SCHEMES[uri.getScheme()] || uri.getScheme();
// additional toString to prevent java ProcessBuilder complaints about "ConsString"
const uriString = (uri.getScheme() + ":" + uri.getSchemeSpecificPart()).toString();
const uriString = (scheme + ":" + uri.getSchemeSpecificPart()).toString();
const treeish = uri.getFragment();
return {
type: constants.TYPE_GIT,
Expand Down

0 comments on commit 09611c3

Please sign in to comment.