Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Git submodule support for repo names with a dot #1271

Merged
merged 1 commit into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;