Skip to content

Commit 0d8fcc0

Browse files
committed
Don't crash if .git is there but .git/HEAD isn't. Fixes #329
1 parent df96427 commit 0d8fcc0

File tree

4 files changed

+47
-12
lines changed

4 files changed

+47
-12
lines changed

lib/git/url_prefix.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@ var getRemoteOrigin = require('remote-origin-url');
1212
* @throws {Error} if the root is not a git repo
1313
*/
1414
function getGithubURLPrefix(root) {
15-
var head = fs.readFileSync(path.join(root, '.git', 'HEAD'), 'utf8');
16-
var branch = head.match(/ref\: (.*)/);
17-
if (branch) {
18-
var sha = fs.readFileSync(path.join(root, '.git', branch[1]), 'utf8');
19-
} else {
20-
sha = head;
15+
try {
16+
var head = fs.readFileSync(path.join(root, '.git', 'HEAD'), 'utf8');
17+
var branch = head.match(/ref\: (.*)/);
18+
if (branch) {
19+
var sha = fs.readFileSync(path.join(root, '.git', branch[1]), 'utf8');
20+
} else {
21+
sha = head;
22+
}
23+
return urlFromGit(getRemoteOrigin.sync(root)) + '/blob/' + sha.trim() + '/';
24+
} catch (e) {
25+
return null;
2126
}
22-
return urlFromGit(getRemoteOrigin.sync(root)) + '/blob/' + sha.trim() + '/';
2327
}
2428

2529
module.exports = getGithubURLPrefix;

lib/github.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ function getFileRoot(file) {
1818
module.exports = function (comment) {
1919
var root = getFileRoot(comment.context.file);
2020
var urlPrefix = getGithubURLPrefix(root);
21-
comment.context.path = comment.context.file.replace(root + '/', '');
22-
comment.context.github = urlPrefix +
23-
comment.context.file.replace(root + '/', '') +
24-
'#L' + comment.context.loc.start.line + '-' +
25-
'L' + comment.context.loc.end.line;
21+
if (urlPrefix) {
22+
comment.context.path = comment.context.file.replace(root + '/', '');
23+
comment.context.github = urlPrefix +
24+
comment.context.file.replace(root + '/', '') +
25+
'#L' + comment.context.loc.start.line + '-' +
26+
'L' + comment.context.loc.end.line;
27+
}
2628
return comment;
2729
};

test/lib/git/mock_repo.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,15 @@ module.exports = {
3333
}
3434
}
3535
}
36+
},
37+
malformed: {
38+
'/my': {
39+
repository: {
40+
path: {
41+
'.git': {},
42+
'index.js': 'module.exports = 42;'
43+
}
44+
}
45+
}
3646
}
3747
};

test/lib/github.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,22 @@ test('github', function (t) {
3939

4040
t.end();
4141
});
42+
43+
test('malformed repository', function (t) {
44+
45+
mock(mockRepo.malformed);
46+
47+
t.equal(evaluate(function () {
48+
/**
49+
* get one
50+
* @returns {number} one
51+
*/
52+
function getOne() {
53+
return 1;
54+
}
55+
})[0].context.github, undefined, 'does not crash');
56+
57+
mock.restore();
58+
59+
t.end();
60+
});

0 commit comments

Comments
 (0)