git-repo-info
Advanced tools
Comparing version 1.0.3 to 1.0.4
46
index.js
@@ -32,2 +32,33 @@ 'use strict'; | ||
function findPackedTag(gitPath, sha) { | ||
var packedRefsFilePath = path.join(gitPath, 'packed-refs'); | ||
if (fs.existsSync(packedRefsFilePath)) { | ||
var packedRefsFile = fs.readFileSync(packedRefsFilePath, {encoding: 'utf8'}); | ||
var tagLine = packedRefsFile.split('\n').filter(function(line) { | ||
return line.indexOf("refs/tags") > -1 && line.indexOf(sha) > -1; | ||
})[0]; | ||
if (tagLine) { | ||
return tagLine.split('tags/')[1]; | ||
} | ||
} | ||
} | ||
function findTag(gitPath, sha) { | ||
var tag = findPackedTag(gitPath, sha); | ||
if (tag) { return tag; } | ||
var tagsPath = path.join(gitPath, 'refs', 'tags'); | ||
if (!fs.existsSync(tagsPath)) { return; } | ||
var tags = fs.readdirSync(tagsPath); | ||
for (var i = 0, l = tags.length; i < l; i++) { | ||
var tagPath = path.join(tagsPath, tags[i]); | ||
if (fs.readFileSync(tagPath, { encoding: 'utf8' }).indexOf(sha) > -1) { | ||
return tags[i]; | ||
} | ||
} | ||
} | ||
module.exports = function(gitPath) { | ||
@@ -65,12 +96,5 @@ if (!gitPath) { gitPath = findRepo(); } | ||
// Find tag | ||
var packedRefsFilePath = path.join(gitPath, 'packed-refs'); | ||
if (fs.existsSync(packedRefsFilePath)) { | ||
var packedRefsFile = fs.readFileSync(packedRefsFilePath, {encoding: 'utf8'}); | ||
var tagLine = packedRefsFile.split('\n').filter(function(line) { | ||
return line.indexOf("refs/tags") > -1 && line.indexOf(result.sha) > -1; | ||
})[0]; | ||
if (tagLine) { | ||
result.tag = tagLine.split('tags/')[1]; | ||
} | ||
var tag = findTag(gitPath, result.sha); | ||
if (tag) { | ||
result.tag = tag; | ||
} | ||
@@ -86,2 +110,2 @@ } | ||
module.exports._findRepo = findRepo; | ||
module.exports._changeGitDir = changeGitDir; | ||
module.exports._changeGitDir = changeGitDir; |
{ | ||
"name": "git-repo-info", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "Retrieve current sha and branch name from a git repo.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -82,4 +82,4 @@ 'use strict'; | ||
it('returns an object with repo info, including the tag', function() { | ||
var repoRoot = path.join(testFixturesPath, 'tagged-commit'); | ||
it('returns an object with repo info, including the tag (packed tags)', function() { | ||
var repoRoot = path.join(testFixturesPath, 'tagged-commit-packed'); | ||
var result = repoInfo(path.join(repoRoot, gitDir)) | ||
@@ -96,3 +96,17 @@ | ||
}); | ||
it('returns an object with repo info, including the tag (unpacked tags)', function() { | ||
var repoRoot = path.join(testFixturesPath, 'tagged-commit-unpacked'); | ||
var result = repoInfo(path.join(repoRoot, gitDir)) | ||
var expected = { | ||
branch: 'master', | ||
sha: '5359aabd3872d9ffd160712e9615c5592dfe6745', | ||
abbreviatedSha: '5359aabd38', | ||
tag: 'my-tag' | ||
} | ||
assert.deepEqual(result, expected); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7661
18
167