Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show the lastTag and the noOfAdditionalCommits
Browse files Browse the repository at this point in the history
luxzeitlos committed Nov 13, 2017
1 parent 3f25daa commit bf078ac
Showing 11 changed files with 141 additions and 15 deletions.
46 changes: 44 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -149,6 +149,40 @@ function findTag(gitPath, sha) {
return tags.length ? tags[0] : false;
}

var LAST_TAG_CACHE = {};

function findLastTagCached(gitPath, sha, commitsSinceLastTag) {
if(!LAST_TAG_CACHE[gitPath]) {
LAST_TAG_CACHE[gitPath] = {};
}

if(!LAST_TAG_CACHE[gitPath][sha]) {
LAST_TAG_CACHE[gitPath][sha] = findLastTag(gitPath, sha, commitsSinceLastTag);
}

return LAST_TAG_CACHE[gitPath][sha];
}

function findLastTag(gitPath, sha, commitsSinceLastTag) {
commitsSinceLastTag = commitsSinceLastTag || 0;
var tag = findTag(gitPath, sha);
if(!tag) {
var commitData = getCommitData(gitPath, sha);
if(!commitData) {
return { tag: null, commitsSinceLastTag: Infinity };
}
return commitData.parents
.map(parent => findLastTag(gitPath, parent, commitsSinceLastTag + 1))
.reduce((a,b) => {
return a.commitsSinceLastTag < b.commitsSinceLastTag ? a : b;
}, { commitsSinceLastTag: Infinity });
}
return {
tag: tag,
commitsSinceLastTag: commitsSinceLastTag
};
}

function findUnpackedTags(gitPath, sha) {
var unpackedTags = [];
var tags = findLooseRefsForType(gitPath, 'tags');
@@ -179,7 +213,9 @@ module.exports = function(gitPath) {
author: null,
authorDate: null,
commitMessage: null,
root: null
root: null,
lastTag: null,
commitsSinceLastTag: 0,
};

if (!gitPathInfo) { return result; }
@@ -228,6 +264,10 @@ module.exports = function(gitPath) {
if (tag) {
result.tag = tag;
}

var lastTagInfo = findLastTagCached(gitPathInfo.commonGitDir, result.sha);
result.lastTag = lastTagInfo.tag;
result.commitsSinceLastTag = lastTagInfo.commitsSinceLastTag;
}
} catch (e) {
if (!module.exports._suppressErrors) {
@@ -263,7 +303,6 @@ function getCommitData(gitPath, sha) {
case 'object':
case 'type':
case 'tree':
case 'parent':
//ignore these for now
break;
case 'author':
@@ -275,6 +314,9 @@ function getCommitData(gitPath, sha) {
data[part + 'Date'] = parseDate(parts[2]);
}
break;
case 'parent':
data.parents = section.split('\n').map(p => p.split(' ')[1]);
break;
default:
//should just be the commit message left
data.commitMessage = section;
1 change: 1 addition & 0 deletions tests/fixtures/tag-on-parent-before-merge/dot-git/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/master
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b60d665ae0978a7b46e2447f4c13d7909997f56c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4f5c726a1528fdfb1ec7c9537e4b1b2dbaacbcc4
1 change: 1 addition & 0 deletions tests/fixtures/tag-on-parent/dot-git/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/master
Binary file added tests/fixtures/tag-on-parent/dot-git/index
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x��A��0 Eg�Sx��<�qS !�p��q���A�ק�a��K��I������e�*P�>a�Ys��Q� �>ƚU�Gr���l�̵S�%r(��F
B5K�<uZ�T�.�6���==�҆Q8�����f4��>�4���M'���ā�C��u ���o�{�����^��p5����%&Y�
1 change: 1 addition & 0 deletions tests/fixtures/tag-on-parent/dot-git/refs/heads/master
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fb26504da0ed5cd9ed366f7428c06a8433fd76e6
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
e66f7ec2da3b5d06f0fe845c4fbc87247efacf62
102 changes: 89 additions & 13 deletions tests/index.js
Original file line number Diff line number Diff line change
@@ -121,7 +121,59 @@ describe('git-repo-info', function() {
author: null,
authorDate: null,
commitMessage: null,
root: repoRoot
root: repoRoot,
lastTag: null,
commitsSinceLastTag: Infinity
};

assert.deepEqual(result, expected);
});

it('returns an object with repo info including the parent tag', function() {
var repoRoot = path.join(testFixturesPath, 'tag-on-parent');
var result = repoInfo(path.join(repoRoot, gitDir));

var expected = {
branch: 'master',
sha: 'fb26504da0ed5cd9ed366f7428c06a8433fd76e6',
abbreviatedSha: 'fb26504da0',
tag: null,
committer: 'Lukas Kohler <lukas.kohler@ontheblueplanet.com>',
committerDate: '2017-10-14T02:02:43.000Z',
author: 'Lukas Kohler <lukas.kohler@ontheblueplanet.com>',
authorDate: '2017-10-14T02:02:43.000Z',
commitMessage: 'second commit without tag',
root: repoRoot,
parents: [
'e66f7ec2da3b5d06f0fe845c4fbc87247efacf62'
],
lastTag: 'parent-magic-tag',
commitsSinceLastTag: 1
};

assert.deepEqual(result, expected);
});

it('returns an object with repo info including the parent tag before a merge commit', function() {
var repoRoot = path.join(testFixturesPath, 'tag-on-parent-before-merge');
var result = repoInfo(path.join(repoRoot, gitDir));

var expected = {
branch: 'master',
sha: 'b60d665ae0978a7b46e2447f4c13d7909997f56c',
abbreviatedSha: 'b60d665ae0',
tag: null,
committer: 'Lukas Kohler <lukas.kohler@ontheblueplanet.com>',
committerDate: '2017-11-13T14:54:49.000Z',
author: 'Lukas Kohler <lukas.kohler@ontheblueplanet.com>',
authorDate: '2017-11-13T14:54:49.000Z',
commitMessage: 'merge red and blue',
root: repoRoot,
parents: [
'4f5c726a1528fdfb1ec7c9537e4b1b2dbaacbcc4'
],
lastTag: 'magic-tag',
commitsSinceLastTag: 1
};

assert.deepEqual(result, expected);
@@ -141,7 +193,9 @@ describe('git-repo-info', function() {
author: null,
authorDate: null,
commitMessage: null,
root: repoRoot
root: repoRoot,
lastTag: null,
commitsSinceLastTag: Infinity
};

assert.deepEqual(result, expected);
@@ -161,7 +215,9 @@ describe('git-repo-info', function() {
author: null,
authorDate: null,
commitMessage: null,
root: repoRoot
root: repoRoot,
lastTag: null,
commitsSinceLastTag: Infinity
};

assert.deepEqual(result, expected);
@@ -181,7 +237,9 @@ describe('git-repo-info', function() {
author: null,
authorDate: null,
commitMessage: null,
root: repoRoot
root: repoRoot,
lastTag: 'my-tag',
commitsSinceLastTag: 0
};

assert.deepEqual(result, expected);
@@ -201,7 +259,9 @@ describe('git-repo-info', function() {
author: null,
authorDate: null,
commitMessage: null,
root: repoRoot
root: repoRoot,
lastTag: 'example-annotated-tag',
commitsSinceLastTag: 0
};

assert.deepEqual(result, expected);
@@ -222,7 +282,9 @@ describe('git-repo-info', function() {
author: 'Robert Jackson <robert.w.jackson@me.com>',
authorDate: '2015-04-15T12:10:06.000Z',
commitMessage: 'Initial commit.',
root: repoRoot
root: repoRoot,
lastTag: 'awesome-tag',
commitsSinceLastTag: 0
};

assert.deepEqual(result, expected);
@@ -242,7 +304,9 @@ describe('git-repo-info', function() {
author: null,
authorDate: null,
commitMessage: null,
root: repoRoot
root: repoRoot,
lastTag: 'awesome-tag',
commitsSinceLastTag: 0
};

assert.deepEqual(result, expected);
@@ -263,7 +327,9 @@ describe('git-repo-info', function() {
author: null,
authorDate: null,
commitMessage: null,
root: repoRoot
root: repoRoot,
lastTag: 'awesome-tag',
commitsSinceLastTag: 0
};

assert.deepEqual(result, expected);
@@ -296,7 +362,9 @@ describe('git-repo-info', function() {
author: 'Robert Jackson <robert.w.jackson@me.com>',
authorDate: '2015-04-15T12:10:06.000Z',
commitMessage: 'Initial commit.',
root: repoRoot
root: repoRoot,
lastTag: 'awesome-tag',
commitsSinceLastTag: 0
};

assert.deepEqual(result, expected);
@@ -317,7 +385,9 @@ describe('git-repo-info', function() {
author: null,
authorDate: null,
commitMessage: null,
root: repoRoot
root: repoRoot,
lastTag: null,
commitsSinceLastTag: Infinity
};

assert.deepEqual(result, expected);
@@ -337,7 +407,9 @@ describe('git-repo-info', function() {
author: null,
authorDate: null,
commitMessage: null,
root: path.join(testFixturesPath, 'linked-worktree')
root: path.join(testFixturesPath, 'linked-worktree'),
lastTag: null,
commitsSinceLastTag: Infinity
};

assert.deepEqual(result, expected);
@@ -360,7 +432,9 @@ describe('git-repo-info', function() {
// This is a pretty meaningless "root" path. The other information is
// correct, but we do not have full support for submodules at the
// moment.
root: path.join(testFixturesPath, 'submodule', 'dot-git', 'modules')
root: path.join(testFixturesPath, 'submodule', 'dot-git', 'modules'),
lastTag: null,
commitsSinceLastTag: Infinity
};

assert.deepEqual(result, expected);
@@ -383,7 +457,9 @@ describe('git-repo-info', function() {
// This is a pretty meaningless "root" path. The other information is
// correct, but we do not have full support for submodules at the
// moment.
root: path.join(testFixturesPath, 'submodule', 'dot-git', 'modules')
root: path.join(testFixturesPath, 'submodule', 'dot-git', 'modules'),
lastTag: null,
commitsSinceLastTag: Infinity
};

assert.deepEqual(result, expected);

0 comments on commit bf078ac

Please sign in to comment.