Skip to content

Commit dde7985

Browse files
committed
Merge pull request #1 from cibernox/detect_tags
Detect the tag of the current commit
2 parents 6f2abfa + 340465b commit dde7985

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

index.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ module.exports = function(gitPath) {
3535
var result = {
3636
sha: null,
3737
abbreviatedSha: null,
38-
branch: null
38+
branch: null,
39+
tag: null
3940
}
4041

4142
try {
@@ -47,6 +48,7 @@ module.exports = function(gitPath) {
4748
var branchName = headFile.split('/').slice(-1)[0].trim();
4849
var refPath = headFile.split(' ')[1];
4950

51+
// Find branch and SHA
5052
if (refPath) {
5153
var branchPath = path.join(gitPath, refPath.trim());
5254

@@ -57,6 +59,19 @@ module.exports = function(gitPath) {
5759
}
5860

5961
result.abbreviatedSha = result.sha.slice(0,10);
62+
63+
// 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+
}
74+
}
6075
}
6176
} catch (e) {
6277
// eat it
@@ -66,4 +81,4 @@ module.exports = function(gitPath) {
6681
};
6782

6883
module.exports._findRepo = findRepo;
69-
module.exports._changeGitDir = changeGitDir;
84+
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
5359aabd3872d9ffd160712e9615c5592dfe6745 refs/remotes/origin/master
2+
c76753aa8651471fe082a3ecd0790ec54f5ec673 refs/tags/v1.0.0
3+
5359aabd3872d9ffd160712e9615c5592dfe6745 refs/tags/my-tag
4+
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: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ describe('git-repo-info', function() {
5757
var expected = {
5858
branch: 'master',
5959
sha: '5359aabd3872d9ffd160712e9615c5592dfe6745',
60-
abbreviatedSha: '5359aabd38'
60+
abbreviatedSha: '5359aabd38',
61+
tag: null
6162
}
6263

6364
assert.deepEqual(result, expected);
@@ -70,7 +71,23 @@ describe('git-repo-info', function() {
7071
var expected = {
7172
branch: null,
7273
sha: '9dac893d5a83c02344d91e79dad8904889aeacb1',
73-
abbreviatedSha: '9dac893d5a'
74+
abbreviatedSha: '9dac893d5a',
75+
tag: null
76+
}
77+
78+
assert.deepEqual(result, expected);
79+
});
80+
81+
82+
it('returns an object with repo info, including the tag', function() {
83+
var repoRoot = path.join(testFixturesPath, 'tagged-commit');
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'
7491
}
7592

7693
assert.deepEqual(result, expected);

0 commit comments

Comments
 (0)