+
Skip to content

Fixed Repo.move() and added test #330

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

Merged
merged 15 commits into from
May 19, 2016
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
72 changes: 35 additions & 37 deletions lib/Repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ class Repository extends Requestable {
* Add a commit to the repository
* @see https://developer.github.com/v3/git/commits/#create-a-commit
* @param {string} parent - the SHA of the parent commit
* @param {Object} tree - the tree that describes this commit
* @param {string} tree - the SHA of the tree for this commit
* @param {string} message - the commit message
* @param {Function} cb - will receive the commit that is created
* @return {Promise} - the promise for the http request
Expand All @@ -300,7 +300,7 @@ class Repository extends Requestable {

return this._request('POST', `/repos/${this.__fullname}/git/commits`, data, cb)
.then((response) => {
this.__currentTree.sha = response.sha; // Update latest commit
this.__currentTree.sha = response.data.sha; // Update latest commit
return response;
});
}
Expand All @@ -310,11 +310,12 @@ class Repository extends Requestable {
* @see https://developer.github.com/v3/git/refs/#update-a-reference
* @param {string} ref - the ref to update
* @param {string} commitSHA - the SHA to point the reference to
* @param {boolean} force - indicates whether to force or ensure a fast-forward update
* @param {Function} cb - will receive the updated ref back
* @return {Promise} - the promise for the http request
*/
updateHead(ref, commitSHA, cb) {
return this._request('PATCH', `/repos/${this.__fullname}/git/refs/${ref}`, {sha: commitSHA}, cb);
updateHead(ref, commitSHA, force, cb) {
return this._request('PATCH', `/repos/${this.__fullname}/git/refs/${ref}`, {sha: commitSHA, force: force}, cb);
}

/**
Expand Down Expand Up @@ -500,41 +501,38 @@ class Repository extends Requestable {
});
}

// Move a file to a new location
// -------
move(branch, path, newPath, cb) {
return this._updateTree(branch, function(err, latestCommit) {
this.getTree(latestCommit + '?recursive=true', function(err, tree) {
// Update Tree
tree.forEach(function(ref) {
if (ref.path === path) {
ref.path = newPath;
}

if (ref.type === 'tree') {
delete ref.sha;
}
});

this.createTree(tree, function(err, rootTree) {
this.commit(latestCommit, rootTree, 'Deleted ' + path, function(err, commit) {
this.updateHead(branch, commit, cb);
/**
* Change all references in a repo from old_path to new_path
* @param {string} branch - the branch to carry out the reference change, or the default branch if not specified
* @param {string} old_path - original path
* @param {string} new_path - new reference path
* @param {Function} cb - will receive the commit in which the move occurred
* @return {Promise} - the promise for the http request
*/
move(branch, old_path, new_path, cb) {
return this.getRef(`heads/${branch}`)
.then((response) => {
return this.getTree(`${response.data.object.sha}?recursive=true`)
.then((response) => {
var _resp = response;
response.data.tree.forEach((ref) => {
if (ref.path === old_path) {
ref.path = new_path;
}
if (ref.type === 'tree') {
delete ref.sha;
}
});
return this.createTree(response.data.tree).then(
(response) => {
return this.commit(_resp.data.sha, response.data.sha, `Renamed '${old_path}' to '${new_path}'`)
.then((response) => {
return this.updateHead(`heads/${branch}`, response.data.sha, true, cb);
});
}
);
});
});
});
});
}

_updateTree(branch, cb) {
if (branch === this.__currentTree.branch && this.__currentTree.sha) {
return cb(null, this.__currentTree.sha);
}

return this.getRef(`heads/${branch}`, function(err, sha) {
this.__currentTree.branch = branch;
this.__currentTree.sha = sha;
cb(err, sha);
});
}

/**
Expand Down
15 changes: 15 additions & 0 deletions test/repository.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,21 @@ describe('Repository', function() {
}));
});

it('should rename files', function(done) {
remoteRepo.writeFile('master', fileName, initialText, initialMessage, assertSuccessful(done, function() {
remoteRepo.move('master', fileName, 'new_name', assertSuccessful(done, function() {
remoteRepo.getContents('master', fileName, 'raw', assertFailure(done, function(err) {
expect(err.status).to.be(404);
remoteRepo.getContents('master', 'new_name', 'raw', assertSuccessful(done, function(err, fileText) {
expect(fileText).to.be(initialText);

done();
}));
}));
}));
}));
});

it('should create a new branch', function(done) {
remoteRepo.createBranch('master', 'dev', assertSuccessful(done, function(err, branch) {
expect(branch).to.have.property('ref', 'refs/heads/dev');
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载