From 0c378304984f26810f802ac76fa998a963987b91 Mon Sep 17 00:00:00 2001 From: Vladimir Agafonkin Date: Thu, 1 Aug 2019 12:25:13 +0300 Subject: [PATCH] fix: Git submodule support for repo names with a dot --- __tests__/lib/git/find_git.js | 12 +++++++++--- __tests__/lib/git/url_prefix.js | 4 ++-- __tests__/utils.js | 8 ++++---- src/git/url_prefix.js | 19 +++++++++++++------ 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/__tests__/lib/git/find_git.js b/__tests__/lib/git/find_git.js index dd9886669..d5f089947 100644 --- a/__tests__/lib/git/find_git.js +++ b/__tests__/lib/git/find_git.js @@ -16,11 +16,17 @@ test('findGit', function() { }); mock(mockRepo.submodule); - const submodulePaths = findGit(path.join(root, 'index.js')); + const submoduleRoot = path.join(root, '..', 'my.submodule'); + const submodulePaths = findGit(path.join(submoduleRoot, 'index.js')); mock.restore(); expect(submodulePaths).toEqual({ - git: path.join(path.dirname(root), '.git', 'modules', 'path'), - root + git: path.join( + path.dirname(submoduleRoot), + '.git', + 'modules', + 'my.submodule' + ), + root: submoduleRoot }); }); diff --git a/__tests__/lib/git/url_prefix.js b/__tests__/lib/git/url_prefix.js index 2a7f53e75..22be4bf6b 100644 --- a/__tests__/lib/git/url_prefix.js +++ b/__tests__/lib/git/url_prefix.js @@ -26,8 +26,8 @@ test('getGithubURLPrefix', function() { mock(mockRepo.submodule); const submoduleUrl = getGithubURLPrefix({ - git: '/my/repository/.git/modules/path', - root: '/my/repository/path' + git: '/my/repository/.git/modules/my.submodule', + root: '/my/repository/my.submodule' }); mock.restore(); diff --git a/__tests__/utils.js b/__tests__/utils.js index aec4d2c5c..001e3b4a7 100644 --- a/__tests__/utils.js +++ b/__tests__/utils.js @@ -77,17 +77,17 @@ module.exports.mockRepo = { submodule: { '/my': { repository: { - path: { - '.git': 'gitdir: ../.git/modules/path', + 'my.submodule': { + '.git': 'gitdir: ../.git/modules/my.submodule', 'index.js': 'module.exports = 42;' }, '.git': { config: - '[submodule "path"]\n' + + '[submodule "my.submodule"]\n' + 'url = https://github.com/foo/bar\n' + 'active = true', modules: { - path: { + 'my.submodule': { HEAD: 'ref: refs/heads/master', refs: { heads: { diff --git a/src/git/url_prefix.js b/src/git/url_prefix.js index 64bd4f0aa..15dbc57da 100644 --- a/src/git/url_prefix.js +++ b/src/git/url_prefix.js @@ -59,14 +59,10 @@ function getGithubURLPrefix({ git, root }) { if (sha) { let origin; if (git.indexOf(root) === 0) { - const config = ini.parse( - fs.readFileSync(path.join(git, 'config'), 'utf8') - ); + const config = parseGitConfig(path.join(git, 'config')); origin = config['remote "origin"'].url; } else { - const config = ini.parse( - fs.readFileSync(path.join(git, '..', '..', 'config'), 'utf8') - ); + const config = parseGitConfig(path.join(git, '..', '..', 'config')); origin = config[`submodule "${path.basename(git)}"`].url; } const parsed = gitUrlParse(origin); @@ -78,5 +74,16 @@ function getGithubURLPrefix({ git, root }) { } } +const configHeaderRe = /\[(\S+) "(.*)"\]/g; +const configReplacer = (m, $1, $2) => + 1 && $2 ? `[${$1} "${$2.split('.').join('\\.')}"]` : m; + +function parseGitConfig(configPath) { + const str = fs + .readFileSync(configPath, 'utf8') + .replace(configHeaderRe, configReplacer); + return ini.parse(str); +} + module.exports = getGithubURLPrefix; module.exports.parsePackedRefs = parsePackedRefs;