Skip to content

Add git submodules support to github linking #1270

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

Merged
merged 2 commits into from
Jul 31, 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
20 changes: 15 additions & 5 deletions __tests__/lib/git/find_git.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@ const findGit = require('../../../src/git/find_git');

test('findGit', function() {
mock(mockRepo.master);
const root =
path.parse(__dirname).root + path.join('my', 'repository', 'path');
const masterPaths = findGit(path.join(root, 'index.js'));
mock.restore();

const root = path.parse(__dirname).root;

expect(
findGit(root + path.join('my', 'repository', 'path', 'index.js'))
).toBe(root + path.join('my', 'repository', 'path', '.git'));
expect(masterPaths).toEqual({
git: path.join(root, '.git'),
root
});

mock(mockRepo.submodule);
const submodulePaths = findGit(path.join(root, 'index.js'));
mock.restore();

expect(submodulePaths).toEqual({
git: path.join(path.dirname(root), '.git', 'modules', 'path'),
root
});
});
25 changes: 19 additions & 6 deletions __tests__/lib/git/url_prefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,33 @@ const parsePackedRefs = getGithubURLPrefix.parsePackedRefs;

test('getGithubURLPrefix', function() {
mock(mockRepo.master);

expect(getGithubURLPrefix('/my/repository/path/')).toBe(
'https://github.com/foo/bar/blob/this_is_the_sha/'
);

const masterUrl = getGithubURLPrefix({
git: '/my/repository/path/.git',
root: '/my/repository/path'
});
mock.restore();

expect(masterUrl).toBe('https://github.com/foo/bar/blob/this_is_the_sha/');

mock(mockRepo.detached);
const detachedUrl = getGithubURLPrefix({
git: '/my/repository/path/.git',
root: '/my/repository/path'
});
mock.restore();

expect(getGithubURLPrefix('/my/repository/path/')).toBe(
expect(detachedUrl).toBe(
'https://github.com/foo/bar/blob/e4cb2ffe677571d0503e659e4e64e01f45639c62/'
);

mock(mockRepo.submodule);
const submoduleUrl = getGithubURLPrefix({
git: '/my/repository/.git/modules/path',
root: '/my/repository/path'
});
mock.restore();

expect(submoduleUrl).toBe('https://github.com/foo/bar/blob/this_is_the_sha/');
});

test('parsePackedRefs', function() {
Expand Down
26 changes: 26 additions & 0 deletions __tests__/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,32 @@ module.exports.mockRepo = {
}
}
},
submodule: {
'/my': {
repository: {
path: {
'.git': 'gitdir: ../.git/modules/path',
'index.js': 'module.exports = 42;'
},
'.git': {
config:
'[submodule "path"]\n' +
'url = https://github.com/foo/bar\n' +
'active = true',
modules: {
path: {
HEAD: 'ref: refs/heads/master',
refs: {
heads: {
master: 'this_is_the_sha'
}
}
}
}
}
}
}
},
malformed: {
'/my': {
repository: {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"glob": "^7.1.2",
"globals-docs": "^2.4.0",
"highlight.js": "^9.15.5",
"ini": "^1.3.5",
"js-yaml": "^3.10.0",
"lodash": "^4.17.10",
"mdast-util-inject": "^1.1.0",
Expand All @@ -58,7 +59,6 @@
"remark-html": "^8.0.0",
"remark-reference-links": "^4.0.1",
"remark-toc": "^5.0.0",
"remote-origin-url": "0.4.0",
"resolve": "^1.8.1",
"stream-array": "^1.1.2",
"strip-json-comments": "^2.0.1",
Expand Down
23 changes: 15 additions & 8 deletions src/git/find_git.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@ const fs = require('fs');
* Given a full path to a single file, iterate upwards through the filesystem
* to find a directory with a .git file indicating that it is a git repository
* @param filename any file within a repository
* @returns repository path
* @returns repository root & its .git folder paths
*/
function findGit(filename) {
const paths = filename.split(path.sep);
for (let i = paths.length; i > 0; i--) {
const p = path.resolve(
paths.slice(0, i).join(path.sep) + path.sep + '.git'
);
if (fs.existsSync(p)) {
return p;
let root = path.resolve(filename);
while (root) {
root = path.dirname(root);
let git = path.join(root, '.git');
if (!fs.existsSync(git)) continue;

if (fs.statSync(git).isFile()) {
// git submodule
const matches = fs.readFileSync(git, 'utf8').match(/gitdir: (.*)/);
if (!matches) return null;
git = path.join(root, matches[1]);
}

return { root, git };
}
return null;
}

module.exports = findGit;
24 changes: 18 additions & 6 deletions src/git/url_prefix.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('fs');
const path = require('path');
const gitUrlParse = require('git-url-parse');
const getRemoteOrigin = require('remote-origin-url');
const ini = require('ini');

/**
* Sometimes git will [pack refs](https://git-scm.com/docs/git-pack-refs)
Expand Down Expand Up @@ -32,15 +32,15 @@ function parsePackedRefs(packedRefs, branchName) {
* @returns {string} base HTTPS url of the GitHub repository
* @throws {Error} if the root is not a git repo
*/
function getGithubURLPrefix(root) {
function getGithubURLPrefix({ git, root }) {
let sha;
try {
const head = fs.readFileSync(path.join(root, '.git', 'HEAD'), 'utf8');
const head = fs.readFileSync(path.join(git, 'HEAD'), 'utf8');
const branch = head.match(/ref: (.*)/);
if (branch) {
const branchName = branch[1];
const branchFileName = path.join(root, '.git', branchName);
const packedRefsName = path.join(root, '.git', 'packed-refs');
const branchFileName = path.join(git, branchName);
const packedRefsName = path.join(git, 'packed-refs');
if (fs.existsSync(branchFileName)) {
sha = fs.readFileSync(branchFileName, 'utf8');
} else if (fs.existsSync(packedRefsName)) {
Expand All @@ -57,7 +57,19 @@ function getGithubURLPrefix(root) {
sha = head;
}
if (sha) {
const parsed = gitUrlParse(getRemoteOrigin.sync(root));
let origin;
if (git.indexOf(root) === 0) {
const config = ini.parse(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For documentation.js's purposes this probably isn't important, but git configuration is a little different than ini and the underlying parse-git-config module did a find/replace to preprocess it so it would work as an ini.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, good point! I did have parse-git-config here first, but then I upgraded its version to the latest and everything broke, and I didn't have enough patience to get to the bottom of it, so wrote without a dep instead. Will make sure it doesn't break when submodule name contains dots, and if it is, I'll submit another PR. Thanks!

fs.readFileSync(path.join(git, 'config'), 'utf8')
);
origin = config['remote "origin"'].url;
} else {
const config = ini.parse(
fs.readFileSync(path.join(git, '..', '..', 'config'), 'utf8')
);
origin = config[`submodule "${path.basename(git)}"`].url;
}
const parsed = gitUrlParse(origin);
parsed.git_suffix = false; // eslint-disable-line
return parsed.toString('https') + '/blob/' + sha.trim() + '/';
}
Expand Down
15 changes: 8 additions & 7 deletions src/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ const getGithubURLPrefix = require('./git/url_prefix');
* @returns {Object} comment with github inferred
*/
module.exports = function(comment) {
const repoPath = findGit(comment.context.file);
const root = repoPath ? path.dirname(repoPath) : '.';
const urlPrefix = getGithubURLPrefix(root);
const fileRelativePath = comment.context.file
.replace(root + path.sep, '')
.split(path.sep)
.join('/');
const paths = findGit(comment.context.file);

const urlPrefix = paths && getGithubURLPrefix(paths);

if (urlPrefix) {
const fileRelativePath = comment.context.file
.replace(paths.root + path.sep, '')
.split(path.sep)
.join('/');

let startLine;
let endLine;

Expand Down
14 changes: 1 addition & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3076,7 +3076,7 @@ inherits@^2.0.1, inherits@^2.0.3, inherits@~2.0.1, inherits@~2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"

ini@^1.3.2, ini@^1.3.3, ini@~1.3.0:
ini@^1.3.2, ini@^1.3.5, ini@~1.3.0:
version "1.3.5"
resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.5.tgz#eee25f56db1c9ec6085e0c22778083f596abf927"

Expand Down Expand Up @@ -4868,12 +4868,6 @@ parse-filepath@^1.0.2:
map-cache "^0.2.0"
path-root "^0.1.1"

parse-git-config@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/parse-git-config/-/parse-git-config-0.2.0.tgz#272833fdd15fea146fb75d336d236b963b6ff706"
dependencies:
ini "^1.3.3"

parse-github-repo-url@^1.3.0:
version "1.4.1"
resolved "https://registry.yarnpkg.com/parse-github-repo-url/-/parse-github-repo-url-1.4.1.tgz#9e7d8bb252a6cb6ba42595060b7bf6df3dbc1f50"
Expand Down Expand Up @@ -5420,12 +5414,6 @@ remark@^9.0.0:
remark-stringify "^5.0.0"
unified "^6.0.0"

[email protected]:
version "0.4.0"
resolved "https://registry.yarnpkg.com/remote-origin-url/-/remote-origin-url-0.4.0.tgz#4d3e2902f34e2d37d1c263d87710b77eb4086a30"
dependencies:
parse-git-config "^0.2.0"

remove-bom-buffer@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/remove-bom-buffer/-/remove-bom-buffer-3.0.0.tgz#c2bf1e377520d324f623892e33c10cac2c252b53"
Expand Down