diff --git a/src/common/storage/project/interface.js b/src/common/storage/project/interface.js index 2296aed3d..ba60f882f 100644 --- a/src/common/storage/project/interface.js +++ b/src/common/storage/project/interface.js @@ -9,10 +9,12 @@ */ define([ + 'q', 'common/storage/project/cache', 'common/storage/constants', - 'common/storage/util' -], function (ProjectCache, CONSTANTS, UTIL) { + 'common/storage/util', + 'common/regexp', +], function (Q, ProjectCache, CONSTANTS, UTIL, REGEXP) { 'use strict'; /** @@ -228,13 +230,30 @@ define([ * @param {function} [callback] - if provided no promise will be returned. * * @return {external:Promise} On success the promise will be resolved with - * {module:Storage~CommitHash} branchHash.
+ * {@link module:Storage~CommitHash} branchHash.
* On error the promise will be rejected with {@link Error} error. */ this.getBranchHash = function (branchName, callback) { throw new Error('getBranchHash must be overridden in derived class'); }; + /** + * Retrieves the root hash at the provided branch or commit-hash. + * @param {string} branchNameOrCommitHash - Name of branch or a commit-hash. + * @param {function} [callback] - if provided no promise will be returned. + * + * @return {external:Promise} On success the promise will be resolved with + * {@link module:Core~ObjectHash} rootHash.
+ * On error the promise will be rejected with {@link Error} error. + */ + this.getRootHash = function (branchNameOrCommitHash, callback) { + return this.getCommitObject(branchNameOrCommitHash) + .then(function (commitObj) { + return commitObj.root; + }) + .nodeify(callback); + }; + /** * Creates a new branch with head pointing to the provided commit hash. * @param {string} branchName - Name of branch to create. @@ -275,6 +294,32 @@ define([ throw new Error('getBranches must be overridden in derived class'); }; + /** + * Retrieves the commit-object at the provided branch or commit-hash. + * @param {string} branchNameOrCommitHash - Name of branch or a commit-hash. + * @param {function} [callback] - if provided no promise will be returned. + * + * @return {external:Promise} On success the promise will be resolved with + * {@link module:Storage~CommitObject} commitObject.
+ * On error the promise will be rejected with {@link Error} error. + */ + this.getCommitObject = function (branchNameOrCommitHash, callback) { + var self = this, + commitDeferred; + + if (REGEXP.HASH.test(branchNameOrCommitHash)) { + commitDeferred = Q(branchNameOrCommitHash); + } else { + commitDeferred = this.getBranchHash(branchNameOrCommitHash); + } + + return commitDeferred + .then(function (commitHash) { + return Q.ninvoke(self, 'loadObject', commitHash); + }) + .nodeify(callback); + }; + /** * Retrieves an array of commits starting from a branch(es) and/or commitHash(es). *
The result is ordered by the rules (applied in order) diff --git a/test/common/storage/project.spec.js b/test/common/storage/project.spec.js index 3b12a3369..a932645fd 100644 --- a/test/common/storage/project.spec.js +++ b/test/common/storage/project.spec.js @@ -214,6 +214,56 @@ describe('common storage project', function () { .nodeify(done); }); + it('should getCommitObject from branch-name and commit-hash', function (done) { + var project, + returned; + + Q.nfcall(storage.openProject, projectName2Id(projectName)) + .then(function (result) { + project = result[0]; + + return project.getCommitObject('master'); + }) + .then(function (commitObj) { + returned = commitObj; + expect(typeof commitObj).to.equal('object'); + expect(typeof commitObj._id).to.equal('string'); + return project.getCommitObject(commitObj._id); + }) + .then(function (commitObj) { + expect(commitObj).to.deep.equal(returned); + }) + .nodeify(done); + }); + + it('should getRootHash from branch-name and commit-hash', function (done) { + var project, + rootHash; + + Q.nfcall(storage.openProject, projectName2Id(projectName)) + .then(function (result) { + project = result[0]; + + return project.getCommitObject('master'); + }) + .then(function (commitObj) { + rootHash = commitObj.root; + expect(typeof rootHash).to.equal('string'); + expect(rootHash).to.not.equal(''); + expect(rootHash[0]).to.equal('#'); + + return Q.allDone([ + project.getRootHash(commitObj._id), + project.getRootHash('master') + ]); + }) + .then(function (res) { + expect(res[0]).to.equal(rootHash); + expect(res[1]).to.equal(rootHash); + }) + .nodeify(done); + }); + it('should getHistory from branch', function (done) { var project; @@ -550,7 +600,8 @@ describe('common storage project', function () { }) .then(function () { branch = project.branches.queueCommitName; - branch.queueCommit(commitData, function () {}); + branch.queueCommit(commitData, function () { + }); expect(branch.getCommitQueue().length).to.equal(1); expect(branch.getFirstCommit()).to.equal(commitData); expect(branch.getCommitQueue().length).to.equal(1); @@ -599,8 +650,10 @@ describe('common storage project', function () { branch.updateHashes('hash', 'hash'); expect(branch.getCommitsForNewFork('hash_different')).to.equal(false); expect(branch.getCommitsForNewFork()).to.deep.equal({commitHash: 'hash', queue: []}); - branch.queueCommit(commitData, function () {}); - branch.queueCommit(commitData2, function () {}); + branch.queueCommit(commitData, function () { + }); + branch.queueCommit(commitData2, function () { + }); expect(branch.getCommitQueue().length).to.equal(2); expect(branch.getCommitQueue()[0]).to.equal(commitData); expect(branch.getCommitsForNewFork('asd2').queue.length).to.equal(1); diff --git a/test/server/storage/userproject.spec.js b/test/server/storage/userproject.spec.js index c9b7a4b41..d2cb2954d 100644 --- a/test/server/storage/userproject.spec.js +++ b/test/server/storage/userproject.spec.js @@ -83,6 +83,44 @@ describe('UserProject', function () { .nodeify(done); }); + it('should getCommitObject from branch-name and commit-hash', function (done) { + var returned; + + project.getCommitObject('master') + .then(function (commitObj) { + returned = commitObj; + expect(typeof commitObj).to.equal('object'); + expect(typeof commitObj._id).to.equal('string'); + return project.getCommitObject(commitObj._id); + }) + .then(function (commitObj) { + expect(commitObj).to.deep.equal(returned); + }) + .nodeify(done); + }); + + it('should getRootHash from branch-name and commit-hash', function (done) { + var rootHash; + + project.getCommitObject('master') + .then(function (commitObj) { + rootHash = commitObj.root; + expect(typeof rootHash).to.equal('string'); + expect(rootHash).to.not.equal(''); + expect(rootHash[0]).to.equal('#'); + + return Q.allDone([ + project.getRootHash(commitObj._id), + project.getRootHash('master') + ]); + }) + .then(function (res) { + expect(res[0]).to.equal(rootHash); + expect(res[1]).to.equal(rootHash); + }) + .nodeify(done); + }); + it('should getCommits', function (done) { project.getCommits((new Date()).getTime() + 100, 1) .then(function (commits) {