Skip to content

Commit c50436a

Browse files
minkezhangclayreimann
authored andcommitted
fix(repository): unbreak repo.move
BREAKING CHANGE: the argument list to repository#move has changed
1 parent bfa8690 commit c50436a

File tree

2 files changed

+50
-37
lines changed

2 files changed

+50
-37
lines changed

lib/Repository.js

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ class Repository extends Requestable {
286286
* Add a commit to the repository
287287
* @see https://developer.github.com/v3/git/commits/#create-a-commit
288288
* @param {string} parent - the SHA of the parent commit
289-
* @param {Object} tree - the tree that describes this commit
289+
* @param {string} tree - the SHA of the tree for this commit
290290
* @param {string} message - the commit message
291291
* @param {Function} cb - will receive the commit that is created
292292
* @return {Promise} - the promise for the http request
@@ -300,7 +300,7 @@ class Repository extends Requestable {
300300

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

320321
/**
@@ -500,41 +501,38 @@ class Repository extends Requestable {
500501
});
501502
}
502503

503-
// Move a file to a new location
504-
// -------
505-
move(branch, path, newPath, cb) {
506-
return this._updateTree(branch, function(err, latestCommit) {
507-
this.getTree(latestCommit + '?recursive=true', function(err, tree) {
508-
// Update Tree
509-
tree.forEach(function(ref) {
510-
if (ref.path === path) {
511-
ref.path = newPath;
512-
}
513-
514-
if (ref.type === 'tree') {
515-
delete ref.sha;
516-
}
517-
});
518-
519-
this.createTree(tree, function(err, rootTree) {
520-
this.commit(latestCommit, rootTree, 'Deleted ' + path, function(err, commit) {
521-
this.updateHead(branch, commit, cb);
504+
/**
505+
* Change all references in a repo from old_path to new_path
506+
* @param {string} branch - the branch to carry out the reference change, or the default branch if not specified
507+
* @param {string} old_path - original path
508+
* @param {string} new_path - new reference path
509+
* @param {Function} cb - will receive the commit in which the move occurred
510+
* @return {Promise} - the promise for the http request
511+
*/
512+
move(branch, old_path, new_path, cb) {
513+
return this.getRef(`heads/${branch}`)
514+
.then((response) => {
515+
return this.getTree(`${response.data.object.sha}?recursive=true`)
516+
.then((response) => {
517+
var _resp = response;
518+
response.data.tree.forEach((ref) => {
519+
if (ref.path === old_path) {
520+
ref.path = new_path;
521+
}
522+
if (ref.type === 'tree') {
523+
delete ref.sha;
524+
}
525+
});
526+
return this.createTree(response.data.tree).then(
527+
(response) => {
528+
return this.commit(_resp.data.sha, response.data.sha, `Renamed '${old_path}' to '${new_path}'`)
529+
.then((response) => {
530+
return this.updateHead(`heads/${branch}`, response.data.sha, true, cb);
531+
});
532+
}
533+
);
522534
});
523-
});
524535
});
525-
});
526-
}
527-
528-
_updateTree(branch, cb) {
529-
if (branch === this.__currentTree.branch && this.__currentTree.sha) {
530-
return cb(null, this.__currentTree.sha);
531-
}
532-
533-
return this.getRef(`heads/${branch}`, function(err, sha) {
534-
this.__currentTree.branch = branch;
535-
this.__currentTree.sha = sha;
536-
cb(err, sha);
537-
});
538536
}
539537

540538
/**

test/repository.spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,21 @@ describe('Repository', function() {
287287
}));
288288
});
289289

290+
it('should rename files', function(done) {
291+
remoteRepo.writeFile('master', fileName, initialText, initialMessage, assertSuccessful(done, function() {
292+
remoteRepo.move('master', fileName, 'new_name', assertSuccessful(done, function() {
293+
remoteRepo.getContents('master', fileName, 'raw', assertFailure(done, function(err) {
294+
expect(err.status).to.be(404);
295+
remoteRepo.getContents('master', 'new_name', 'raw', assertSuccessful(done, function(err, fileText) {
296+
expect(fileText).to.be(initialText);
297+
298+
done();
299+
}));
300+
}));
301+
}));
302+
}));
303+
});
304+
290305
it('should create a new branch', function(done) {
291306
remoteRepo.createBranch('master', 'dev', assertSuccessful(done, function(err, branch) {
292307
expect(branch).to.have.property('ref', 'refs/heads/dev');

0 commit comments

Comments
 (0)