From 8c43f50c2d6035437a243a06b411e7f855db5f2e Mon Sep 17 00:00:00 2001 From: Chris Ball Date: Sat, 23 Apr 2016 17:17:21 -0400 Subject: [PATCH] Gracefully handle partial git info. On some host/git combinations, gitRepoInfo returns an object like the following: `{ sha: null, abbreviatedSha: null, branch: 'develop', tag: null }` When using the version-commit option, ember-cli-deploy would fail if the sha is empty. --- README.md | 2 + lib/data-generators/version-commit.js | 14 ++++- tests/fixtures/repo/dotgit-branch-only/HEAD | 1 + tests/fixtures/repo/dotgit-branch-only/config | 7 +++ tests/fixtures/repo/dotgit-branch-only/index | Bin 0 -> 225 bytes .../41/d41f081b45ad50935c08b1203220737d9739b4 | 2 + .../77/06f9e51d51a2f357b2c2078cdd04e4acf48f93 | Bin 0 -> 41 bytes .../a5/10e8069cc7eaa8b8262a9c071a702fbd52dbea | Bin 0 -> 41 bytes .../d4/6bba1991f218165a93efe79024e16da2a213fc | Bin 0 -> 89 bytes .../repo/dotgit-branch-only/refs/heads/master | 0 .../version-commit-nodetest.js | 53 ++++++++++++++++++ 11 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/repo/dotgit-branch-only/HEAD create mode 100644 tests/fixtures/repo/dotgit-branch-only/config create mode 100644 tests/fixtures/repo/dotgit-branch-only/index create mode 100644 tests/fixtures/repo/dotgit-branch-only/objects/41/d41f081b45ad50935c08b1203220737d9739b4 create mode 100644 tests/fixtures/repo/dotgit-branch-only/objects/77/06f9e51d51a2f357b2c2078cdd04e4acf48f93 create mode 100644 tests/fixtures/repo/dotgit-branch-only/objects/a5/10e8069cc7eaa8b8262a9c071a702fbd52dbea create mode 100644 tests/fixtures/repo/dotgit-branch-only/objects/d4/6bba1991f218165a93efe79024e16da2a213fc create mode 100644 tests/fixtures/repo/dotgit-branch-only/refs/heads/master diff --git a/README.md b/README.md index 1c520f2..b64c96a 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,8 @@ The unique identifier of this build based on the version in the `package.json`, 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`. +`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`. + ##### timestamp The timestamp of the current deploy diff --git a/lib/data-generators/version-commit.js b/lib/data-generators/version-commit.js index a5af9ac..4f1fdfc 100644 --- a/lib/data-generators/version-commit.js +++ b/lib/data-generators/version-commit.js @@ -21,18 +21,26 @@ module.exports = CoreObject.extend({ } var info = gitRepoInfo(path); - var sha = info.sha.slice(0, 8); + var sha = (info.sha || '').slice(0, 8); + var log = this._plugin.log; return readFile(versionFile) .then(function(contents) { var json = JSON.parse(contents); - if (!json.version || !sha) { + if (!json.version) { return Promise.reject('Could not build revision with version `' + json.version + '` and commit hash `' + sha + '`'); } + var versionString = json.version; + if (sha) { + versionString = versionString + '+' + sha; + } else { + log('Missing git commit sha, using package version as revisionKey', { color: 'yellow', verbose: true }); + } + return { - revisionKey: json.version + '+' + sha, + revisionKey: versionString, timestamp: new Date().toISOString() }; }); diff --git a/tests/fixtures/repo/dotgit-branch-only/HEAD b/tests/fixtures/repo/dotgit-branch-only/HEAD new file mode 100644 index 0000000..cb089cd --- /dev/null +++ b/tests/fixtures/repo/dotgit-branch-only/HEAD @@ -0,0 +1 @@ +ref: refs/heads/master diff --git a/tests/fixtures/repo/dotgit-branch-only/config b/tests/fixtures/repo/dotgit-branch-only/config new file mode 100644 index 0000000..6c9406b --- /dev/null +++ b/tests/fixtures/repo/dotgit-branch-only/config @@ -0,0 +1,7 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = false + logallrefupdates = true + ignorecase = true + precomposeunicode = true diff --git a/tests/fixtures/repo/dotgit-branch-only/index b/tests/fixtures/repo/dotgit-branch-only/index new file mode 100644 index 0000000000000000000000000000000000000000..910060a72df49a118b833bd29d56f46494a4441e GIT binary patch literal 225 zcmZ?q402{*U|<5_(35tYK$-zYGcqu+FiSZ;W?*Ps!oa}z6(}VF#FFJ~KcC75F8Un4 z=@5I*U6v0)`BXM=(dY0@huC}W vvOHPyrGGM1U0G^Tab|uVMBP$>7i@Emzgn?FO=}LjRDu59pxdtivF;;`nIS0Q literal 0 HcmV?d00001 diff --git a/tests/fixtures/repo/dotgit-branch-only/refs/heads/master b/tests/fixtures/repo/dotgit-branch-only/refs/heads/master new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/lib/data-generators/version-commit-nodetest.js b/tests/unit/lib/data-generators/version-commit-nodetest.js index 83e76d9..4bc9326 100644 --- a/tests/unit/lib/data-generators/version-commit-nodetest.js +++ b/tests/unit/lib/data-generators/version-commit-nodetest.js @@ -21,6 +21,59 @@ describe('the version-commit data generator', function() { }); describe('#generate', function() { + describe('with partial commit data', function() { + before(function() { + gitRepoInfo._changeGitDir('dotgit-branch-only'); + }); + + after(function() { + gitRepoInfo._changeGitDir('dotgit'); + }); + + it('only adds `+revision` if it can be read', function() { + process.chdir('tests/fixtures/repo'); + + var plugin = { + stubConfig: { + versionFile: 'package.json' + }, + readConfig: function(key) { return this.stubConfig[key]; }, + log: function() {} + }; + + var subject = new DataGenerator({ + plugin: plugin + }); + + return assert.isFulfilled(subject.generate()) + .then(function(data) { + var path = gitRepoInfo._findRepo(); + assert.equal(data.revisionKey, '3.2.1'); + }); + }); + + it('logs a warning if no git sha found', function() { + process.chdir('tests/fixtures/repo'); + + var expectedMessage = /missing git commit sha/i; + var plugin = { + stubConfig: { + versionFile: 'package.json' + }, + readConfig: function(key) { return this.stubConfig[key]; }, + log: function(message) { + assert.ok(message.match(expectedMessage)); + } + }; + + var subject = new DataGenerator({ + plugin: plugin + }); + + return assert.isFulfilled(subject.generate()) + }) + }); + it('concatenates the package version and the git commit hash', function() { process.chdir('tests/fixtures/repo');