Skip to content

Commit ce55779

Browse files
committed
refactor(repository): make repo#move more promise-y
1 parent 2d1c29d commit ce55779

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

lib/Repository.js

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -531,29 +531,24 @@ class Repository extends Requestable {
531531
* @return {Promise} - the promise for the http request
532532
*/
533533
move(branch, oldPath, newPath, cb) {
534+
let oldSha;
534535
return this.getRef(`heads/${branch}`)
535-
.then((response) => {
536-
return this.getTree(`${response.data.object.sha}?recursive=true`)
537-
.then((response) => {
538-
let _resp = response;
539-
response.data.tree.forEach((ref) => {
540-
if (ref.path === oldPath) {
541-
ref.path = newPath;
542-
}
543-
if (ref.type === 'tree') {
544-
delete ref.sha;
545-
}
546-
});
547-
return this.createTree(response.data.tree).then(
548-
(response) => {
549-
return this.commit(_resp.data.sha, response.data.sha, `Renamed '${oldPath}' to '${newPath}'`)
550-
.then((response) => {
551-
return this.updateHead(`heads/${branch}`, response.data.sha, true, cb);
552-
});
553-
}
554-
);
555-
});
556-
});
536+
.then(({data: {object}}) => this.getTree(`${object.sha}?recursive=true`))
537+
.then(({data: {tree, sha}}) => {
538+
oldSha = sha;
539+
let newTree = tree.map((ref) => {
540+
if (ref.path === oldPath) {
541+
ref.path = newPath;
542+
}
543+
if (ref.type === 'tree') {
544+
delete ref.sha;
545+
}
546+
return ref;
547+
});
548+
return this.createTree(newTree);
549+
})
550+
.then(({data: tree}) => this.commit(oldSha, tree.sha, `Renamed '${oldPath}' to '${newPath}'`))
551+
.then(({data: commit}) => this.updateHead(`heads/${branch}`, commit.sha, true, cb));
557552
}
558553

559554
/**

test/repository.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ describe('Repository', function() {
141141
it('should show repo contributors', function(done) {
142142
remoteRepo.getContributors(assertSuccessful(done, function(err, contributors) {
143143
if (!(contributors instanceof Array)) {
144-
console.log(contributors); // eslint-disable-line
144+
console.log(JSON.stringify(contributors, null, 2)); // eslint-disable-line
145145
}
146146
expect(contributors).to.be.an.array();
147147
expect(contributors.length).to.be.above(1);
@@ -258,7 +258,7 @@ describe('Repository', function() {
258258
it('should show repo collaborators', function(done) {
259259
remoteRepo.getCollaborators(assertSuccessful(done, function(err, collaborators) {
260260
if (!(collaborators instanceof Array)) {
261-
console.log(collaborators); // eslint-disable-line
261+
console.log(JSON.stringify(collaborators, null, 2)); // eslint-disable-line
262262
}
263263
expect(collaborators).to.be.an.array();
264264
expect(collaborators).to.have.length(1);

0 commit comments

Comments
 (0)