Skip to content

Commit fab464f

Browse files
committed
Add git committer/author info
1 parent 2ccc487 commit fab464f

File tree

2 files changed

+134
-12
lines changed

2 files changed

+134
-12
lines changed

index.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ module.exports = function(gitPath) {
125125
abbreviatedSha: null,
126126
branch: null,
127127
tag: null,
128+
committer: null,
129+
committerDate: null,
130+
author: null,
131+
authorDate: null,
132+
commitMessage: null,
128133
root: null
129134
};
130135

@@ -160,6 +165,15 @@ module.exports = function(gitPath) {
160165

161166
result.abbreviatedSha = result.sha.slice(0,10);
162167

168+
// Find commit data
169+
var commitData = getCommitData(gitPath, result.sha);
170+
if (commitData) {
171+
result = Object.keys(commitData).reduce(function(r, key) {
172+
result[key] = commitData[key];
173+
return result;
174+
}, result);
175+
}
176+
163177
// Find tag
164178
var tag = findTag(gitPath, result.sha);
165179
if (tag) {
@@ -180,3 +194,49 @@ module.exports = function(gitPath) {
180194
module.exports._suppressErrors = true;
181195
module.exports._findRepo = findRepo;
182196
module.exports._changeGitDir = changeGitDir;
197+
198+
function getCommitData(gitPath, sha) {
199+
var objectPath = path.join(gitPath, 'objects', sha.slice(0, 2), sha.slice(2));
200+
201+
if (zlib.inflateSync && fs.existsSync(objectPath)) {
202+
var objectContents = zlib.inflateSync(fs.readFileSync(objectPath)).toString();
203+
204+
return objectContents.split(/\0|\n/)
205+
.filter(function(item) {
206+
return !!item;
207+
})
208+
.reduce(function(data, section) {
209+
var part = section.slice(0, section.indexOf(' ')).trim();
210+
211+
switch(part) {
212+
case 'commit':
213+
case 'tag':
214+
case 'object':
215+
case 'type':
216+
case 'tree':
217+
case 'parent':
218+
//ignore these for now
219+
break;
220+
case 'author':
221+
case 'committer':
222+
var parts = section.match(/^(?:author|committer)\s(.+)\s(\d+\s(?:\+|\-)\d{4})$/);
223+
224+
if (parts) {
225+
data[part] = parts[1];
226+
data[part + 'Date'] = parseDate(parts[2]);
227+
}
228+
break;
229+
default:
230+
//should just be the commit message left
231+
data.commitMessage = section;
232+
}
233+
234+
return data;
235+
}, {});
236+
}
237+
}
238+
239+
function parseDate(d) {
240+
var epoch = d.split(' ')[0];
241+
return new Date(epoch * 1000).toISOString();
242+
}

tests/index.js

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ describe('git-repo-info', function() {
7171
sha: '5359aabd3872d9ffd160712e9615c5592dfe6745',
7272
abbreviatedSha: '5359aabd38',
7373
tag: null,
74+
committer: null,
75+
committerDate: null,
76+
author: null,
77+
authorDate: null,
78+
commitMessage: null,
7479
root: repoRoot
7580
};
7681

@@ -86,6 +91,11 @@ describe('git-repo-info', function() {
8691
sha: '9dac893d5a83c02344d91e79dad8904889aeacb1',
8792
abbreviatedSha: '9dac893d5a',
8893
tag: null,
94+
committer: null,
95+
committerDate: null,
96+
author: null,
97+
authorDate: null,
98+
commitMessage: null,
8999
root: repoRoot
90100
};
91101

@@ -101,6 +111,11 @@ describe('git-repo-info', function() {
101111
sha: 'd670460b4b4aece5915caf5c68d12f560a9fe3e4',
102112
abbreviatedSha: 'd670460b4b',
103113
tag: null,
114+
committer: null,
115+
committerDate: null,
116+
author: null,
117+
authorDate: null,
118+
commitMessage: null,
104119
root: repoRoot
105120
};
106121

@@ -116,26 +131,58 @@ describe('git-repo-info', function() {
116131
sha: '5359aabd3872d9ffd160712e9615c5592dfe6745',
117132
abbreviatedSha: '5359aabd38',
118133
tag: 'my-tag',
134+
committer: null,
135+
committerDate: null,
136+
author: null,
137+
authorDate: null,
138+
commitMessage: null,
119139
root: repoRoot
120140
};
121141

122142
assert.deepEqual(result, expected);
123143
});
124144

125-
it('returns an object with repo info, including the tag (unpacked tags)', function() {
126-
var repoRoot = path.join(testFixturesPath, 'tagged-commit-unpacked');
127-
var result = repoInfo(path.join(repoRoot, gitDir));
145+
if (zlib.inflateSync) {
146+
it('returns an object with repo info, including the tag (unpacked tags)', function() {
147+
var repoRoot = path.join(testFixturesPath, 'tagged-commit-unpacked');
148+
var result = repoInfo(path.join(repoRoot, gitDir));
128149

129-
var expected = {
130-
branch: 'master',
131-
sha: 'c1ee41c325d54f410b133e0018c7a6b1316f6cda',
132-
abbreviatedSha: 'c1ee41c325',
133-
tag: 'awesome-tag',
134-
root: repoRoot
135-
};
150+
var expected = {
151+
branch: 'master',
152+
sha: 'c1ee41c325d54f410b133e0018c7a6b1316f6cda',
153+
abbreviatedSha: 'c1ee41c325',
154+
tag: 'awesome-tag',
155+
committer: 'Robert Jackson <[email protected]>',
156+
committerDate: '2015-04-15T12:10:06.000Z',
157+
author: 'Robert Jackson <[email protected]>',
158+
authorDate: '2015-04-15T12:10:06.000Z',
159+
commitMessage: 'Initial commit.',
160+
root: repoRoot
161+
};
136162

137-
assert.deepEqual(result, expected);
138-
});
163+
assert.deepEqual(result, expected);
164+
});
165+
} else {
166+
it('returns an object with repo info, including the tag (unpacked tags)', function() {
167+
var repoRoot = path.join(testFixturesPath, 'tagged-commit-unpacked');
168+
var result = repoInfo(path.join(repoRoot, gitDir));
169+
170+
var expected = {
171+
branch: 'master',
172+
sha: 'c1ee41c325d54f410b133e0018c7a6b1316f6cda',
173+
abbreviatedSha: 'c1ee41c325',
174+
tag: 'awesome-tag',
175+
committer: null,
176+
committerDate: null,
177+
author: null,
178+
authorDate: null,
179+
commitMessage: null,
180+
root: repoRoot
181+
};
182+
183+
assert.deepEqual(result, expected);
184+
});
185+
}
139186

140187
it('returns an object with repo info, including the tag (unpacked tags) when a tag object does not exist', function() {
141188
var repoRoot = path.join(testFixturesPath, 'tagged-commit-unpacked-no-object');
@@ -146,6 +193,11 @@ describe('git-repo-info', function() {
146193
sha: 'c1ee41c325d54f410b133e0018c7a6b1316f6cda',
147194
abbreviatedSha: 'c1ee41c325',
148195
tag: 'awesome-tag',
196+
committer: null,
197+
committerDate: null,
198+
author: null,
199+
authorDate: null,
200+
commitMessage: null,
149201
root: repoRoot
150202
};
151203

@@ -162,6 +214,11 @@ describe('git-repo-info', function() {
162214
sha: 'c1ee41c325d54f410b133e0018c7a6b1316f6cda',
163215
abbreviatedSha: 'c1ee41c325',
164216
tag: 'awesome-tag',
217+
committer: 'Robert Jackson <[email protected]>',
218+
committerDate: '2015-04-15T12:10:06.000Z',
219+
author: 'Robert Jackson <[email protected]>',
220+
authorDate: '2015-04-15T12:10:06.000Z',
221+
commitMessage: 'Initial commit.',
165222
root: repoRoot
166223
};
167224

@@ -178,6 +235,11 @@ describe('git-repo-info', function() {
178235
sha: '5359aabd3872d9ffd160712e9615c5592dfe6745',
179236
abbreviatedSha: '5359aabd38',
180237
tag: null,
238+
committer: null,
239+
committerDate: null,
240+
author: null,
241+
authorDate: null,
242+
commitMessage: null,
181243
root: repoRoot
182244
};
183245

0 commit comments

Comments
 (0)