Skip to content

Commit 5d4d1c9

Browse files
author
Chris Watts
committed
Address PR comments
Refactored packed refs into smaller more readable functions Using fs.existsStat instead of try catch
1 parent 4ef9d4a commit 5d4d1c9

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

index.js

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,34 +30,48 @@ function findRepo(startingPath) {
3030
return null;
3131
}
3232

33-
function findPackedTag(gitPath, sha) {
34-
var packedRefsFilePath = path.join(gitPath, 'packed-refs');
35-
if (fs.existsSync(packedRefsFilePath)) {
36-
var packedRefsFile = fs.readFileSync(packedRefsFilePath, {encoding: 'utf8'});
37-
var tagLine = packedRefsFile.split('\n').filter(function(line) {
38-
return line.indexOf('refs/tags') > -1 && line.indexOf(sha) > -1;
39-
})[0];
40-
41-
if (tagLine) {
42-
return tagLine.split('tags/')[1];
43-
}
44-
}
33+
function findPackedTag(gitPath, refPath) {
34+
return getPackedRefsForType(gitPath, refPath, 'tag');
4535
}
4636

4737
function findPackedCommit(gitPath, refPath) {
38+
return getPackedRefsForType(gitPath, refPath, 'commit');
39+
}
40+
41+
function getPackedRefsForType(gitPath, refPath, type) {
4842
var packedRefsFilePath = path.join(gitPath, 'packed-refs');
4943
if (fs.existsSync(packedRefsFilePath)) {
5044
var packedRefsFile = fs.readFileSync(packedRefsFilePath, { encoding: 'utf8' });
51-
var shaLine = packedRefsFile.split('\n').filter(function(line) {
52-
return line.indexOf('refs/heads') > -1 && line.indexOf(refPath) > -1;
53-
})[0];
45+
var shaLine = getLineForRefPath(packedRefsFile, type, refPath);
5446

5547
if (shaLine) {
56-
return shaLine.split(' ')[0];
48+
return getShaBasedOnType(type, shaLine);
5749
}
5850
}
5951
}
6052

53+
function getLineForRefPath(packedRefsFile, type, refPath) {
54+
return packedRefsFile.split('\n').filter(function(line) {
55+
return doesLineMatchRefPath(type, line, refPath);
56+
})[0];
57+
}
58+
59+
function doesLineMatchRefPath(type, line, refPath) {
60+
var refPrefix = type === 'tag' ? 'refs/tags' : 'refs/heads';
61+
return line.indexOf(refPrefix) > -1 && line.indexOf(refPath) > -1;
62+
}
63+
64+
function getShaBasedOnType(type, shaLine) {
65+
var shaResult = '';
66+
if (type === 'tag') {
67+
shaResult = shaLine.split('tags/')[1];
68+
} else if (type === 'commit') {
69+
shaResult = shaLine.split(' ')[0];
70+
}
71+
72+
return shaResult;
73+
}
74+
6175
function commitForTag(gitPath, tag) {
6276
var tagPath = path.join(gitPath, 'refs', 'tags', tag);
6377
var taggedObject = fs.readFileSync(tagPath, { encoding: 'utf8' }).trim();
@@ -71,7 +85,8 @@ function commitForTag(gitPath, tag) {
7185

7286
var objectContents = zlib.inflateSync(fs.readFileSync(objectPath)).toString();
7387

74-
// 'tag 172\u0000object c1ee41c325d54f410b133e0018c7a6b1316f6cda\ntype commit\ntag awesome-tag\ntagger Robert Jackson <[email protected]> 1429100021 -0400\n\nI am making an annotated tag.\n'
88+
// 'tag 172\u0000object c1ee41c325d54f410b133e0018c7a6b1316f6cda\ntype commit\ntag awesome-tag\ntagger Robert Jackson
89+
// <[email protected]> 1429100021 -0400\n\nI am making an annotated tag.\n'
7590
if (objectContents.slice(0,3) === 'tag') {
7691
var sections = objectContents.split(/\0|\n/);
7792
var sha = sections[1].slice(7);
@@ -130,13 +145,10 @@ module.exports = function(gitPath) {
130145
var branchPath = path.join(gitPath, refPath);
131146

132147
result.branch = branchName;
133-
try {
148+
if (fs.existsSync(branchPath)) {
134149
result.sha = fs.readFileSync(branchPath, { encoding: 'utf8' }).trim();
135-
} catch (err) {
136-
if (err.code === 'ENOENT') {
137-
console.log('Could not find ' + branchPath + ' trying to find ref in packed-refs instead.');
138-
result.sha = findPackedCommit(gitPath, refPath);
139-
}
150+
} else {
151+
result.sha = findPackedCommit(gitPath, refPath);
140152
}
141153
} else {
142154
result.sha = branchName;

0 commit comments

Comments
 (0)