Skip to content

Commit 02121e2

Browse files
committed
Add git-commit data generator
1 parent d35c81c commit 02121e2

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ For detailed information on how configuration of plugins works, please refer to
5555
The type of [Data Generator](#data-generators) to be used.
5656

5757
*Default:* `'file-hash'`
58-
*Alternatives:* `'git-tag-commit'`, `'version-commit'`
58+
*Alternatives:* `'git-tag-commit'`, `'git-commit'`, `'version-commit'`
5959

6060
## Data Generators
6161

@@ -111,6 +111,22 @@ For example, if your most recent git tag is `v2.0.3`, and the current commit is
111111

112112
The timestamp of the current deploy
113113

114+
### Git Commit generator
115+
116+
Constructs a revision key based on the most recent git commit.
117+
118+
#### Data fields returned
119+
120+
##### revisionKey
121+
122+
The unique identifier of this build based on the first 7 characters of the current commit hash.
123+
124+
For example, if the current commit is `0993043d49f9e0[...]`, this generator will return a revision of `0993043`.
125+
126+
##### timestamp
127+
128+
The timestamp of the current deploy
129+
114130
### Version Commit generator
115131

116132
Similar to the Git Tag Commit generator but uses the `package.json` version string to construct the revision key instead of the git tag.

lib/data-generators/git-commit.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var CoreObject = require('core-object');
2+
var gitRepoInfo = require('git-repo-info');
3+
var Promise = require('ember-cli/lib/ext/promise');
4+
5+
module.exports = CoreObject.extend({
6+
generate: function() {
7+
var path = gitRepoInfo._findRepo();
8+
9+
if (path === null) {
10+
return Promise.reject('Could not find git repository');
11+
}
12+
13+
var info = gitRepoInfo(path);
14+
var sha = info.sha.slice(0, 7);
15+
16+
if (!sha) {
17+
return Promise.reject('Could not build revision with commit hash `' + sha + '`');
18+
}
19+
20+
return Promise.resolve({
21+
revisionKey: sha,
22+
timestamp: new Date().toISOString()
23+
});
24+
}
25+
});

lib/data-generators/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = {
22
"file-hash": require('./file-hash'),
33
"git-tag-commit": require('./git-tag-commit'),
4+
"git-commit": require('./git-commit'),
45
"version-commit": require('./version-commit')
56
};
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
var assert = require('ember-cli/tests/helpers/assert');
4+
var gitRepoInfo = require('git-repo-info');
5+
6+
describe('the git-commit data generator', function() {
7+
var DataGenerator;
8+
var cwd;
9+
10+
before(function() {
11+
DataGenerator = require('../../../../lib/data-generators/git-commit');
12+
gitRepoInfo._changeGitDir('dotgit');
13+
});
14+
15+
beforeEach(function() {
16+
cwd = process.cwd();
17+
});
18+
19+
afterEach(function() {
20+
process.chdir(cwd);
21+
});
22+
23+
describe('#generate', function() {
24+
it('revision key contains first 7 characters of git commit hash', function() {
25+
process.chdir('tests/fixtures/repo');
26+
27+
var subject = new DataGenerator();
28+
29+
return assert.isFulfilled(subject.generate())
30+
.then(function(data) {
31+
assert.equal(data.revisionKey, '41d41f0');
32+
});
33+
});
34+
35+
it('returns a timestamp', function() {
36+
process.chdir('tests/fixtures/repo');
37+
38+
var subject = new DataGenerator();
39+
40+
return assert.isFulfilled(subject.generate())
41+
.then(function(data) {
42+
assert.isNotNull(data.timestamp);
43+
});
44+
});
45+
46+
it('rejects if no repository found', function() {
47+
process.chdir('tests/fixtures/not-a-repo');
48+
49+
var subject = new DataGenerator();
50+
51+
return assert.isRejected(subject.generate())
52+
.then(function(error) {
53+
assert.equal(error, 'Could not find git repository');
54+
});
55+
});
56+
});
57+
});

0 commit comments

Comments
 (0)