git-repo-info
Advanced tools
Comparing version 1.1.2 to 1.1.3
59
index.js
@@ -33,12 +33,18 @@ 'use strict'; | ||
function findPackedTag(gitPath, sha) { | ||
function findPackedTag(gitPath, refPath) { | ||
return getPackedRefsForType(gitPath, refPath, 'tag'); | ||
} | ||
function findPackedCommit(gitPath, refPath) { | ||
return getPackedRefsForType(gitPath, refPath, 'commit'); | ||
} | ||
function getPackedRefsForType(gitPath, refPath, type) { | ||
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]; | ||
var packedRefsFile = fs.readFileSync(packedRefsFilePath, { encoding: 'utf8' }); | ||
var shaLine = getLineForRefPath(packedRefsFile, type, refPath); | ||
if (tagLine) { | ||
return tagLine.split('tags/')[1]; | ||
if (shaLine) { | ||
return getShaBasedOnType(type, shaLine); | ||
} | ||
@@ -48,2 +54,24 @@ } | ||
function getLineForRefPath(packedRefsFile, type, refPath) { | ||
return packedRefsFile.split('\n').filter(function(line) { | ||
return doesLineMatchRefPath(type, line, refPath); | ||
})[0]; | ||
} | ||
function doesLineMatchRefPath(type, line, refPath) { | ||
var refPrefix = type === 'tag' ? 'refs/tags' : 'refs/heads'; | ||
return line.indexOf(refPrefix) > -1 && line.indexOf(refPath) > -1; | ||
} | ||
function getShaBasedOnType(type, shaLine) { | ||
var shaResult = ''; | ||
if (type === 'tag') { | ||
shaResult = shaLine.split('tags/')[1]; | ||
} else if (type === 'commit') { | ||
shaResult = shaLine.split(' ')[0]; | ||
} | ||
return shaResult; | ||
} | ||
function commitForTag(gitPath, tag) { | ||
@@ -62,3 +90,4 @@ var tagPath = path.join(gitPath, 'refs', 'tags', tag); | ||
// 'tag 172\u0000object c1ee41c325d54f410b133e0018c7a6b1316f6cda\ntype commit\ntag awesome-tag\ntagger Robert Jackson <robert.w.jackson@me.com> 1429100021 -0400\n\nI am making an annotated tag.\n' | ||
// 'tag 172\u0000object c1ee41c325d54f410b133e0018c7a6b1316f6cda\ntype commit\ntag awesome-tag\ntagger Robert Jackson | ||
// <robert.w.jackson@me.com> 1429100021 -0400\n\nI am making an annotated tag.\n' | ||
if (objectContents.slice(0,3) === 'tag') { | ||
@@ -95,3 +124,3 @@ var sections = objectContents.split(/\0|\n/); | ||
module.exports = function(gitPath) { | ||
if (!gitPath) { gitPath = findRepo(); } | ||
gitPath = findRepo(gitPath); | ||
@@ -102,3 +131,4 @@ var result = { | ||
branch: null, | ||
tag: null | ||
tag: null, | ||
root: path.resolve(gitPath, '..') | ||
}; | ||
@@ -119,6 +149,11 @@ | ||
if (refPath) { | ||
var branchPath = path.join(gitPath, refPath.trim()); | ||
refPath = refPath.trim(); | ||
var branchPath = path.join(gitPath, refPath); | ||
result.branch = branchName; | ||
result.sha = fs.readFileSync(branchPath, {encoding: 'utf8' }).trim(); | ||
if (fs.existsSync(branchPath)) { | ||
result.sha = fs.readFileSync(branchPath, { encoding: 'utf8' }).trim(); | ||
} else { | ||
result.sha = findPackedCommit(gitPath, refPath); | ||
} | ||
} else { | ||
@@ -125,0 +160,0 @@ result.sha = branchName; |
{ | ||
"name": "git-repo-info", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "Retrieve current sha and branch name from a git repo.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha tests" | ||
"test": "mocha tests", | ||
"preversion": "npm test", | ||
"postversion": "git push && git push --tags && npm publish" | ||
}, | ||
@@ -9,0 +11,0 @@ "repository": { |
@@ -55,2 +55,9 @@ 'use strict'; | ||
}); | ||
it('finds a repo 2 levels up (without an argument)', function() { | ||
process.chdir(path.join(repoRoot, 'foo', 'bar')); | ||
var foundPath = repoInfo._findRepo(); | ||
assert.equal(foundPath, path.join(repoRoot, gitDir)); | ||
}); | ||
}); | ||
@@ -67,3 +74,4 @@ | ||
abbreviatedSha: '5359aabd38', | ||
tag: null | ||
tag: null, | ||
root: repoRoot | ||
}; | ||
@@ -82,3 +90,4 @@ | ||
abbreviatedSha: '9dac893d5a', | ||
tag: null | ||
tag: null, | ||
root: repoRoot | ||
}; | ||
@@ -89,3 +98,17 @@ | ||
it('returns an object with repo info (packed commit)', function() { | ||
var repoRoot = path.join(testFixturesPath, 'commit-packed'); | ||
var result = repoInfo(path.join(repoRoot, gitDir)); | ||
var expected = { | ||
branch: 'develop', | ||
sha: 'd670460b4b4aece5915caf5c68d12f560a9fe3e4', | ||
abbreviatedSha: 'd670460b4b', | ||
tag: null, | ||
root: repoRoot | ||
}; | ||
assert.deepEqual(result, expected); | ||
}); | ||
it('returns an object with repo info, including the tag (packed tags)', function() { | ||
@@ -99,3 +122,4 @@ var repoRoot = path.join(testFixturesPath, 'tagged-commit-packed'); | ||
abbreviatedSha: '5359aabd38', | ||
tag: 'my-tag' | ||
tag: 'my-tag', | ||
root: repoRoot | ||
}; | ||
@@ -114,3 +138,4 @@ | ||
abbreviatedSha: 'c1ee41c325', | ||
tag: 'awesome-tag' | ||
tag: 'awesome-tag', | ||
root: repoRoot | ||
}; | ||
@@ -129,3 +154,4 @@ | ||
abbreviatedSha: 'c1ee41c325', | ||
tag: 'awesome-tag' | ||
tag: 'awesome-tag', | ||
root: repoRoot | ||
}; | ||
@@ -145,3 +171,4 @@ | ||
abbreviatedSha: 'c1ee41c325', | ||
tag: 'awesome-tag' | ||
tag: 'awesome-tag', | ||
root: repoRoot | ||
}; | ||
@@ -161,3 +188,4 @@ | ||
abbreviatedSha: '5359aabd38', | ||
tag: null | ||
tag: null, | ||
root: repoRoot | ||
}; | ||
@@ -168,2 +196,16 @@ | ||
}); | ||
describe('repoInfo().root', function() { | ||
var repoRoot = path.join(testFixturesPath, 'nested-repo'); | ||
it('finds a repo from cwd (2 levels up)', function() { | ||
process.chdir(path.join(repoRoot, 'foo', 'bar')); | ||
assert.equal(repoInfo().root, repoRoot); | ||
}); | ||
it('finds a repo with an argument', function() { | ||
assert.equal(repoInfo(path.join(repoRoot, 'foo', 'bar')).root, repoRoot); | ||
}); | ||
}); | ||
}); |
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
47731
297