Skip to content

Commit c2fc2db

Browse files
committed
Merge pull request #2 from rwjblue/ensure-non-packed-tags-are-found
Ensure that unpacked tags are found.
2 parents f47d6d0 + 64af8e1 commit c2fc2db

File tree

8 files changed

+56
-13
lines changed

8 files changed

+56
-13
lines changed

index.js

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,37 @@ function findRepo(startingPath) {
2929
return null;
3030
}
3131

32+
function findPackedTag(gitPath, sha) {
33+
var packedRefsFilePath = path.join(gitPath, 'packed-refs');
34+
if (fs.existsSync(packedRefsFilePath)) {
35+
var packedRefsFile = fs.readFileSync(packedRefsFilePath, {encoding: 'utf8'});
36+
var tagLine = packedRefsFile.split('\n').filter(function(line) {
37+
return line.indexOf("refs/tags") > -1 && line.indexOf(sha) > -1;
38+
})[0];
39+
40+
if (tagLine) {
41+
return tagLine.split('tags/')[1];
42+
}
43+
}
44+
}
45+
46+
function findTag(gitPath, sha) {
47+
var tag = findPackedTag(gitPath, sha);
48+
if (tag) { return tag; }
49+
50+
var tagsPath = path.join(gitPath, 'refs', 'tags');
51+
if (!fs.existsSync(tagsPath)) { return; }
52+
53+
var tags = fs.readdirSync(tagsPath);
54+
55+
for (var i = 0, l = tags.length; i < l; i++) {
56+
var tagPath = path.join(tagsPath, tags[i]);
57+
if (fs.readFileSync(tagPath, { encoding: 'utf8' }).indexOf(sha) > -1) {
58+
return tags[i];
59+
}
60+
}
61+
}
62+
3263
module.exports = function(gitPath) {
3364
if (!gitPath) { gitPath = findRepo(); }
3465

@@ -61,16 +92,9 @@ module.exports = function(gitPath) {
6192
result.abbreviatedSha = result.sha.slice(0,10);
6293

6394
// Find tag
64-
var packedRefsFilePath = path.join(gitPath, 'packed-refs');
65-
if (fs.existsSync(packedRefsFilePath)) {
66-
var packedRefsFile = fs.readFileSync(packedRefsFilePath, {encoding: 'utf8'});
67-
var tagLine = packedRefsFile.split('\n').filter(function(line) {
68-
return line.indexOf("refs/tags") > -1 && line.indexOf(result.sha) > -1;
69-
})[0];
70-
71-
if (tagLine) {
72-
result.tag = tagLine.split('tags/')[1];
73-
}
95+
var tag = findTag(gitPath, result.sha);
96+
if (tag) {
97+
result.tag = tag;
7498
}
7599
}
76100
} catch (e) {
@@ -81,4 +105,4 @@ module.exports = function(gitPath) {
81105
};
82106

83107
module.exports._findRepo = findRepo;
84-
module.exports._changeGitDir = changeGitDir;
108+
module.exports._changeGitDir = changeGitDir;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
5359aabd3872d9ffd160712e9615c5592dfe6745 refs/remotes/origin/master
2+
c76753aa8651471fe082a3ecd0790ec54f5ec673 refs/tags/v1.0.0
3+
6f2abfab299ad8e302f3a3023f88483f4be3b402 refs/tags/v1.0.1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
5359aabd3872d9ffd160712e9615c5592dfe6745

tests/index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,22 @@ describe('git-repo-info', function() {
7979
});
8080

8181

82-
it('returns an object with repo info, including the tag', function() {
83-
var repoRoot = path.join(testFixturesPath, 'tagged-commit');
82+
it('returns an object with repo info, including the tag (packed tags)', function() {
83+
var repoRoot = path.join(testFixturesPath, 'tagged-commit-packed');
84+
var result = repoInfo(path.join(repoRoot, gitDir))
85+
86+
var expected = {
87+
branch: 'master',
88+
sha: '5359aabd3872d9ffd160712e9615c5592dfe6745',
89+
abbreviatedSha: '5359aabd38',
90+
tag: 'my-tag'
91+
}
92+
93+
assert.deepEqual(result, expected);
94+
});
95+
96+
it('returns an object with repo info, including the tag (unpacked tags)', function() {
97+
var repoRoot = path.join(testFixturesPath, 'tagged-commit-unpacked');
8498
var result = repoInfo(path.join(repoRoot, gitDir))
8599

86100
var expected = {

0 commit comments

Comments
 (0)