Skip to content

Commit 7d4fe1e

Browse files
committed
Merge pull request #18 from seawatts/master
Add the ability to get a packed ref if the branchPath does not exist
2 parents 79a7b0f + 5d4d1c9 commit 7d4fe1e

File tree

4 files changed

+64
-10
lines changed

4 files changed

+64
-10
lines changed

index.js

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

33-
function findPackedTag(gitPath, sha) {
33+
function findPackedTag(gitPath, refPath) {
34+
return getPackedRefsForType(gitPath, refPath, 'tag');
35+
}
36+
37+
function findPackedCommit(gitPath, refPath) {
38+
return getPackedRefsForType(gitPath, refPath, 'commit');
39+
}
40+
41+
function getPackedRefsForType(gitPath, refPath, type) {
3442
var packedRefsFilePath = path.join(gitPath, 'packed-refs');
3543
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];
44+
var packedRefsFile = fs.readFileSync(packedRefsFilePath, { encoding: 'utf8' });
45+
var shaLine = getLineForRefPath(packedRefsFile, type, refPath);
4046

41-
if (tagLine) {
42-
return tagLine.split('tags/')[1];
47+
if (shaLine) {
48+
return getShaBasedOnType(type, shaLine);
4349
}
4450
}
4551
}
4652

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+
4775
function commitForTag(gitPath, tag) {
4876
var tagPath = path.join(gitPath, 'refs', 'tags', tag);
4977
var taggedObject = fs.readFileSync(tagPath, { encoding: 'utf8' }).trim();
@@ -57,7 +85,8 @@ function commitForTag(gitPath, tag) {
5785

5886
var objectContents = zlib.inflateSync(fs.readFileSync(objectPath)).toString();
5987

60-
// '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'
6190
if (objectContents.slice(0,3) === 'tag') {
6291
var sections = objectContents.split(/\0|\n/);
6392
var sha = sections[1].slice(7);
@@ -112,10 +141,15 @@ module.exports = function(gitPath) {
112141

113142
// Find branch and SHA
114143
if (refPath) {
115-
var branchPath = path.join(gitPath, refPath.trim());
144+
refPath = refPath.trim();
145+
var branchPath = path.join(gitPath, refPath);
116146

117147
result.branch = branchName;
118-
result.sha = fs.readFileSync(branchPath, {encoding: 'utf8' }).trim();
148+
if (fs.existsSync(branchPath)) {
149+
result.sha = fs.readFileSync(branchPath, { encoding: 'utf8' }).trim();
150+
} else {
151+
result.sha = findPackedCommit(gitPath, refPath);
152+
}
119153
} else {
120154
result.sha = branchName;
121155
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/develop
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
5359aabd3872d9ffd160712e9615c5592dfe6745 refs/remotes/origin/master
2+
d670460b4b4aece5915caf5c68d12f560a9fe3e4 refs/heads/develop
3+
c76753aa8651471fe082a3ecd0790ec54f5ec673 refs/tags/v1.0.0
4+
5359aabd3872d9ffd160712e9615c5592dfe6745 refs/tags/my-tag
5+
6f2abfab299ad8e302f3a3023f88483f4be3b402 refs/tags/v1.0.1

tests/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ describe('git-repo-info', function() {
9292
assert.deepEqual(result, expected);
9393
});
9494

95+
it('returns an object with repo info (packed commit)', function() {
96+
var repoRoot = path.join(testFixturesPath, 'commit-packed');
97+
var result = repoInfo(path.join(repoRoot, gitDir));
98+
99+
var expected = {
100+
branch: 'develop',
101+
sha: 'd670460b4b4aece5915caf5c68d12f560a9fe3e4',
102+
abbreviatedSha: 'd670460b4b',
103+
tag: null,
104+
root: repoRoot
105+
};
106+
107+
assert.deepEqual(result, expected);
108+
});
95109

96110
it('returns an object with repo info, including the tag (packed tags)', function() {
97111
var repoRoot = path.join(testFixturesPath, 'tagged-commit-packed');

0 commit comments

Comments
 (0)