Skip to content

Commit

Permalink
Handle file paths and dots in git urls
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Aug 31, 2022
1 parent 1851650 commit 055a4ff
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
5 changes: 4 additions & 1 deletion lib/util.js
Expand Up @@ -48,7 +48,10 @@ const rejectAfter = (ms, error) =>

const parseGitUrl = remoteUrl => {
if (!remoteUrl) return { host: null, owner: null, project: null, protocol: null, remote: null, repository: null };
const normalizedUrl = (remoteUrl || '').replace(/^[A-Z]:/, '').replace(/\\/g, '/'); // Hacky workaround for file protocol in Windows
const normalizedUrl = (remoteUrl || '')
.replace(/^[A-Z]:\\\\/, 'file://') // Assume file protocol for Windows drive letters
.replace(/^\//, 'file://') // Assume file protocol if only /path is given
.replace(/\\+/g, '/'); // Replace forward with backslashes
const parsedUrl = gitUrlParse(normalizedUrl);
const { resource: host, name: project, protocol, href: remote } = parsedUrl;
const owner = protocol === 'file' ? _.last(parsedUrl.owner.split('/')) : parsedUrl.owner; // Fix owner for file protocol
Expand Down
2 changes: 1 addition & 1 deletion test/git.js
Expand Up @@ -274,7 +274,7 @@ test.serial('should reset files', async t => {

test.serial('should roll back when cancelled', async t => {
sh.exec('git init');
sh.exec(`git remote add origin foo`);
sh.exec(`git remote add origin file://foo`);
const version = '1.2.3';
gitAdd(`{"version":"${version}"}`, 'package.json', 'Add package.json');
const options = { git: { requireCleanWorkingDir: true, commit: true, tag: true, tagName: 'v${version}' } };
Expand Down
31 changes: 29 additions & 2 deletions test/utils.js
Expand Up @@ -47,12 +47,39 @@ test('parseGitUrl', t => {
repository: 'org/sub-group/repo-in-sub-group'
});

t.deepEqual(parseGitUrl('git@github.com:org/example.com.git'), {
host: 'github.com',
owner: 'org',
project: 'example.com',
protocol: 'ssh',
remote: 'git@github.com:org/example.com.git',
repository: 'org/example.com'
});

t.deepEqual(parseGitUrl('file://Users/john/doe/owner/project'), {
host: 'users',
owner: 'owner',
project: 'project',
protocol: 'file',
remote: 'file://users/john/doe/owner/project',
repository: 'owner/project'
});

t.deepEqual(parseGitUrl('/Users/john/doe/owner/project'), {
host: '',
host: 'users',
owner: 'owner',
project: 'project',
protocol: 'file',
remote: 'file://users/john/doe/owner/project',
repository: 'owner/project'
});

t.deepEqual(parseGitUrl('C:\\\\Users\\john\\doe\\owner\\project'), {
host: 'users',
owner: 'owner',
project: 'project',
protocol: 'file',
remote: '/Users/john/doe/owner/project',
remote: 'file://users/john/doe/owner/project',
repository: 'owner/project'
});
});
Expand Down

0 comments on commit 055a4ff

Please sign in to comment.