Skip to content

Commit

Permalink
fix: Git submodule support for repo names with a dot
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Aug 1, 2019
1 parent 43cd57d commit c94717c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
12 changes: 9 additions & 3 deletions __tests__/lib/git/find_git.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
});
4 changes: 2 additions & 2 deletions __tests__/lib/git/url_prefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
8 changes: 4 additions & 4 deletions __tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
18 changes: 12 additions & 6 deletions src/git/url_prefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -78,5 +74,15 @@ function getGithubURLPrefix({ git, root }) {
}
}

function parseGitConfig(configPath) {
const str = fs
.readFileSync(configPath, 'utf8')
.replace(
/\[(\S+) "(.+)"\]/g,
(match, key, value) => `[${key} "${value.split('.').join('\\.')}"]`
);
return ini.parse(str);
}

module.exports = getGithubURLPrefix;
module.exports.parsePackedRefs = parsePackedRefs;

0 comments on commit c94717c

Please sign in to comment.