Skip to content

Commit 4ef9d4a

Browse files
author
Chris Watts
committed
Add the ability to get a packed ref if the branchPath does not exist
1 parent 3093960 commit 4ef9d4a

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

index.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ function findPackedTag(gitPath, sha) {
4444
}
4545
}
4646

47+
function findPackedCommit(gitPath, refPath) {
48+
var packedRefsFilePath = path.join(gitPath, 'packed-refs');
49+
if (fs.existsSync(packedRefsFilePath)) {
50+
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];
54+
55+
if (shaLine) {
56+
return shaLine.split(' ')[0];
57+
}
58+
}
59+
}
60+
4761
function commitForTag(gitPath, tag) {
4862
var tagPath = path.join(gitPath, 'refs', 'tags', tag);
4963
var taggedObject = fs.readFileSync(tagPath, { encoding: 'utf8' }).trim();
@@ -112,10 +126,18 @@ module.exports = function(gitPath) {
112126

113127
// Find branch and SHA
114128
if (refPath) {
115-
var branchPath = path.join(gitPath, refPath.trim());
129+
refPath = refPath.trim();
130+
var branchPath = path.join(gitPath, refPath);
116131

117132
result.branch = branchName;
118-
result.sha = fs.readFileSync(branchPath, {encoding: 'utf8' }).trim();
133+
try {
134+
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+
}
140+
}
119141
} else {
120142
result.sha = branchName;
121143
}
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)