Skip to content

Commit 51910bc

Browse files
committed
Merge pull request #33 from cball/handle-empty-git-info
Gracefully handle partial git info.
2 parents 5d232cd + 8c43f50 commit 51910bc

File tree

11 files changed

+76
-3
lines changed

11 files changed

+76
-3
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ The unique identifier of this build based on the version in the `package.json`,
156156

157157
For example, if your package.json version is `v2.0.3`, and the current commit is `0993043d49f9e0[...]`, this generator will return a revision of `v2.0.3+0993043d`.
158158

159+
`Note:` Some environments (like CircleCI) may return partial git information. If the current commit hash cannot be determined, the generator will return only the package.json version (`v2.0.3`) as the `revisionKey`.
160+
159161
##### timestamp
160162

161163
The timestamp of the current deploy

lib/data-generators/version-commit.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,26 @@ module.exports = CoreObject.extend({
2121
}
2222

2323
var info = gitRepoInfo(path);
24-
var sha = info.sha.slice(0, 8);
24+
var sha = (info.sha || '').slice(0, 8);
25+
var log = this._plugin.log;
2526

2627
return readFile(versionFile)
2728
.then(function(contents) {
2829
var json = JSON.parse(contents);
2930

30-
if (!json.version || !sha) {
31+
if (!json.version) {
3132
return Promise.reject('Could not build revision with version `' + json.version + '` and commit hash `' + sha + '`');
3233
}
3334

35+
var versionString = json.version;
36+
if (sha) {
37+
versionString = versionString + '+' + sha;
38+
} else {
39+
log('Missing git commit sha, using package version as revisionKey', { color: 'yellow', verbose: true });
40+
}
41+
3442
return {
35-
revisionKey: json.version + '+' + sha,
43+
revisionKey: versionString,
3644
timestamp: new Date().toISOString()
3745
};
3846
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ref: refs/heads/master
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[core]
2+
repositoryformatversion = 0
3+
filemode = true
4+
bare = false
5+
logallrefupdates = true
6+
ignorecase = true
7+
precomposeunicode = true
225 Bytes
Binary file not shown.

tests/fixtures/repo/dotgit-branch-only/objects/41/d41f081b45ad50935c08b1203220737d9739b4

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
x��A
2+
1 @Q�=E��4�L����"Ӧ�Z��������������7H��g�0N�GN�샥A�'&&t9~�Gmp^��X��E�Mp�_;��ֶ��~\���ak�Z��.2��Ԯ����OB�

tests/fixtures/repo/dotgit-branch-only/refs/heads/master

Whitespace-only changes.

tests/unit/lib/data-generators/version-commit-nodetest.js

+53
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,59 @@ describe('the version-commit data generator', function() {
2121
});
2222

2323
describe('#generate', function() {
24+
describe('with partial commit data', function() {
25+
before(function() {
26+
gitRepoInfo._changeGitDir('dotgit-branch-only');
27+
});
28+
29+
after(function() {
30+
gitRepoInfo._changeGitDir('dotgit');
31+
});
32+
33+
it('only adds `+revision` if it can be read', function() {
34+
process.chdir('tests/fixtures/repo');
35+
36+
var plugin = {
37+
stubConfig: {
38+
versionFile: 'package.json'
39+
},
40+
readConfig: function(key) { return this.stubConfig[key]; },
41+
log: function() {}
42+
};
43+
44+
var subject = new DataGenerator({
45+
plugin: plugin
46+
});
47+
48+
return assert.isFulfilled(subject.generate())
49+
.then(function(data) {
50+
var path = gitRepoInfo._findRepo();
51+
assert.equal(data.revisionKey, '3.2.1');
52+
});
53+
});
54+
55+
it('logs a warning if no git sha found', function() {
56+
process.chdir('tests/fixtures/repo');
57+
58+
var expectedMessage = /missing git commit sha/i;
59+
var plugin = {
60+
stubConfig: {
61+
versionFile: 'package.json'
62+
},
63+
readConfig: function(key) { return this.stubConfig[key]; },
64+
log: function(message) {
65+
assert.ok(message.match(expectedMessage));
66+
}
67+
};
68+
69+
var subject = new DataGenerator({
70+
plugin: plugin
71+
});
72+
73+
return assert.isFulfilled(subject.generate())
74+
})
75+
});
76+
2477
it('concatenates the package version and the git commit hash', function() {
2578
process.chdir('tests/fixtures/repo');
2679

0 commit comments

Comments
 (0)