Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getRootHash and getCommitObject on project API #118

Merged
merged 1 commit into from
Aug 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 48 additions & 3 deletions src/common/storage/project/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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} <b>branchHash</b>.<br>
* {@link module:Storage~CommitHash} <b>branchHash</b>.<br>
* On error the promise will be rejected with {@link Error} <b>error</b>.
*/
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} <b>rootHash</b>.<br>
* On error the promise will be rejected with {@link Error} <b>error</b>.
*/
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.
Expand Down Expand Up @@ -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} <b>commitObject</b>.<br>
* On error the promise will be rejected with {@link Error} <b>error</b>.
*/
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).
* <br> The result is ordered by the rules (applied in order)
Expand Down
59 changes: 56 additions & 3 deletions test/common/storage/project.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
38 changes: 38 additions & 0 deletions test/server/storage/userproject.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down