Skip to content

Commit

Permalink
Merge pull request #100 from neumann-d/master
Browse files Browse the repository at this point in the history
Feature: Allows cloning packages using the `file://` protocol. Closes #101.

Thanks to @neumann-d!
  • Loading branch information
pomek authored Mar 7, 2019
2 parents d0aa351 + f8b0088 commit d0aa893
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/utils/parserepositoryurl.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ const url = require( 'url' );
*
* @param {String} repositoryUrl The repository URL in formats supported by `mgit.json`.
* @param {Object} options
* @param {String} options.urlTemplate The URL template.
* @param {String} [options.urlTemplate] The URL template.
* Used if `repositoryUrl` defines only `'<organization>/<repositoryName>'`.
* @param {String} options.defaultBranch The default branch name to be used if the
* @param {String} [options.defaultBranch='master'] The default branch name to be used if the
* repository URL doesn't specify it.
* @returns {Repository}
*/
module.exports = function parseRepositoryUrl( repositoryUrl, options = {} ) {
const parsedUrl = url.parse( repositoryUrl );
const branch = parsedUrl.hash ? parsedUrl.hash.slice( 1 ) : options.defaultBranch;
const branch = parsedUrl.hash ? parsedUrl.hash.slice( 1 ) : options.defaultBranch || 'master';
let repoUrl;

if ( repositoryUrl.match( /^https?:\/\// ) || repositoryUrl.match( /^git@/ ) ) {
if ( repositoryUrl.match( /^(file|https?):\/\// ) || repositoryUrl.match( /^git@/ ) ) {
parsedUrl.hash = null;

repoUrl = url.format( parsedUrl );
Expand All @@ -37,7 +37,7 @@ module.exports = function parseRepositoryUrl( repositoryUrl, options = {} ) {
return {
url: repoUrl,
branch,
directory: repoUrl.match( /[:/]([^/]+)\.git$/ )[ 1 ]
directory: repoUrl.replace( /\.git$/, '' ).match( /[:/]([^/]+)\/?$/ )[ 1 ]
};
};

Expand Down
112 changes: 112 additions & 0 deletions tests/utils/parserepositoryurl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/

/* jshint mocha:true */

'use strict';

const expect = require( 'chai' ).expect;
const parseRepositoryUrl = require( '../../lib/utils/parserepositoryurl' );

describe( 'utils', () => {
describe( 'parseRepositoryUrl()', () => {
it( 'returns "master" branch if "options.defaultBranch" was not specified', () => {
const repository = parseRepositoryUrl( 'foo/bar', {
urlTemplate: 'https://github.com/${ path }.git'
} );

expect( repository ).to.deep.equal( {
url: 'https://github.com/foo/bar.git',
branch: 'master',
directory: 'bar'
} );
} );

it( 'allows modifying the branch using hash in "<organization>/<repositoryName>" template', () => {
const repository = parseRepositoryUrl( 'foo/bar#stable', {
urlTemplate: 'https://github.com/${ path }.git'
} );

expect( repository ).to.deep.equal( {
url: 'https://github.com/foo/bar.git',
branch: 'stable',
directory: 'bar'
} );
} );

it( 'ignores "options.defaultBranch" if branch is defined in specified repository', () => {
const repository = parseRepositoryUrl( 'foo/bar#stable', {
urlTemplate: 'https://github.com/${ path }.git',
defaultBranch: 'master'
} );

expect( repository ).to.deep.equal( {
url: 'https://github.com/foo/bar.git',
branch: 'stable',
directory: 'bar'
} );
} );

it( 'extracts all parameters basing on specified "http" URL', () => {
const repository = parseRepositoryUrl( 'http://github.com/foo/bar.git' );

expect( repository ).to.deep.equal( {
url: 'http://github.com/foo/bar.git',
branch: 'master',
directory: 'bar'
} );
} );

it( 'extracts all parameters basing on specified "https" URL', () => {
const repository = parseRepositoryUrl( 'https://github.com/foo/bar.git' );

expect( repository ).to.deep.equal( {
url: 'https://github.com/foo/bar.git',
branch: 'master',
directory: 'bar'
} );
} );

it( 'extracts all parameters basing on specified "file" (Unix path)', () => {
const repository = parseRepositoryUrl( 'file:///Users/Workspace/Projects/foo/bar' );

expect( repository ).to.deep.equal( {
url: 'file:///Users/Workspace/Projects/foo/bar',
branch: 'master',
directory: 'bar'
} );
} );

it( 'extracts all parameters basing on specified "file" (Windows path)', () => {
const repository = parseRepositoryUrl( 'file://C:/Users/Workspace/Projects/foo/bar' );

expect( repository ).to.deep.equal( {
url: 'file://c/Users/Workspace/Projects/foo/bar',
branch: 'master',
directory: 'bar'
} );
} );

it( 'extracts all parameters basing on specified "git" URL', () => {
const repository = parseRepositoryUrl( 'git@github.com:foo/bar.git' );

expect( repository ).to.deep.equal( {
url: 'git@github.com:foo/bar.git',
branch: 'master',
directory: 'bar'
} );
} );

it( 'allows modifying the branch using hash in the URL', () => {
const repository = parseRepositoryUrl( 'https://github.com/foo/bar.git#stable' );

expect( repository ).to.deep.equal( {
url: 'https://github.com/foo/bar.git',
branch: 'stable',
directory: 'bar'
} );
} );
} );
} );

0 comments on commit d0aa893

Please sign in to comment.