Skip to content

Commit bfdb246

Browse files
authored
Merge pull request #37 from luxferresum/lastTag
Show the lastTag and the noOfAdditionalCommits
2 parents 3f25daa + cd50141 commit bfdb246

File tree

14 files changed

+150
-20
lines changed

14 files changed

+150
-20
lines changed

.travis.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
language: node_js
2+
node_js:
3+
- "4"
4+
- "6"
5+
- "8"
6+
- "9"
7+
28
sudo: false
39
cache:
410
directories:
511
- node_modules
612

7-
env:
8-
- NODE_VERSION=0.10
9-
- NODE_VERSION=0.12
10-
- NODE_VERSION=iojs
11-
1213
branches:
1314
only:
1415
- master

index.js

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,40 @@ function findTag(gitPath, sha) {
149149
return tags.length ? tags[0] : false;
150150
}
151151

152+
var LAST_TAG_CACHE = {};
153+
154+
function findLastTagCached(gitPath, sha, commitsSinceLastTag) {
155+
if(!LAST_TAG_CACHE[gitPath]) {
156+
LAST_TAG_CACHE[gitPath] = {};
157+
}
158+
159+
if(!LAST_TAG_CACHE[gitPath][sha]) {
160+
LAST_TAG_CACHE[gitPath][sha] = findLastTag(gitPath, sha, commitsSinceLastTag);
161+
}
162+
163+
return LAST_TAG_CACHE[gitPath][sha];
164+
}
165+
166+
function findLastTag(gitPath, sha, commitsSinceLastTag) {
167+
commitsSinceLastTag = commitsSinceLastTag || 0;
168+
var tag = findTag(gitPath, sha);
169+
if(!tag) {
170+
var commitData = getCommitData(gitPath, sha);
171+
if(!commitData) {
172+
return { tag: null, commitsSinceLastTag: Infinity };
173+
}
174+
return commitData.parents
175+
.map(parent => findLastTag(gitPath, parent, commitsSinceLastTag + 1))
176+
.reduce((a,b) => {
177+
return a.commitsSinceLastTag < b.commitsSinceLastTag ? a : b;
178+
}, { commitsSinceLastTag: Infinity });
179+
}
180+
return {
181+
tag: tag,
182+
commitsSinceLastTag: commitsSinceLastTag
183+
};
184+
}
185+
152186
function findUnpackedTags(gitPath, sha) {
153187
var unpackedTags = [];
154188
var tags = findLooseRefsForType(gitPath, 'tags');
@@ -179,7 +213,9 @@ module.exports = function(gitPath) {
179213
author: null,
180214
authorDate: null,
181215
commitMessage: null,
182-
root: null
216+
root: null,
217+
lastTag: null,
218+
commitsSinceLastTag: 0,
183219
};
184220

185221
if (!gitPathInfo) { return result; }
@@ -228,6 +264,10 @@ module.exports = function(gitPath) {
228264
if (tag) {
229265
result.tag = tag;
230266
}
267+
268+
var lastTagInfo = findLastTagCached(gitPathInfo.commonGitDir, result.sha);
269+
result.lastTag = lastTagInfo.tag;
270+
result.commitsSinceLastTag = lastTagInfo.commitsSinceLastTag;
231271
}
232272
} catch (e) {
233273
if (!module.exports._suppressErrors) {
@@ -263,7 +303,6 @@ function getCommitData(gitPath, sha) {
263303
case 'object':
264304
case 'type':
265305
case 'tree':
266-
case 'parent':
267306
//ignore these for now
268307
break;
269308
case 'author':
@@ -275,6 +314,9 @@ function getCommitData(gitPath, sha) {
275314
data[part + 'Date'] = parseDate(parts[2]);
276315
}
277316
break;
317+
case 'parent':
318+
data.parents = section.split('\n').map(p => p.split(' ')[1]);
319+
break;
278320
default:
279321
//should just be the commit message left
280322
data.commitMessage = section;

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,8 @@
2727
"devDependencies": {
2828
"mocha": "^1.21.4",
2929
"mocha-jshint": "^1.1.0"
30+
},
31+
"engines": {
32+
"node": ">= 4.0"
3033
}
3134
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
b60d665ae0978a7b46e2447f4c13d7909997f56c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4f5c726a1528fdfb1ec7c9537e4b1b2dbaacbcc4
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master
65 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)