From 44cd040b92f2b62557211f1b175c3e2d33ef9d6b Mon Sep 17 00:00:00 2001 From: Minke Zhang Date: Thu, 5 May 2016 12:56:47 -0700 Subject: [PATCH 01/14] Fixed scope resolution issues in Repo.move() --- lib/Repository.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/Repository.js b/lib/Repository.js index db1f2509..aacf24a5 100644 --- a/lib/Repository.js +++ b/lib/Repository.js @@ -504,9 +504,9 @@ class Repository extends Requestable { // ------- move(branch, path, newPath, cb) { return this._updateTree(branch, function(err, latestCommit) { - this.getTree(latestCommit + '?recursive=true', function(err, tree) { + this.getTree(latestCommit + '?recursive=true', (err, tree) => { // Update Tree - tree.forEach(function(ref) { + tree.forEach((ref) => { if (ref.path === path) { ref.path = newPath; } @@ -516,8 +516,8 @@ class Repository extends Requestable { } }); - this.postTree(tree, function(err, rootTree) { - this.commit(latestCommit, rootTree, 'Deleted ' + path, function(err, commit) { + this.postTree(tree, (err, rootTree) => { + this.commit(latestCommit, rootTree, 'Deleted ' + path, (err, commit) => { this.updateHead(branch, commit, cb); }); }); @@ -530,7 +530,7 @@ class Repository extends Requestable { return cb(null, this.__currentTree.sha); } - this.getRef(`heads/${branch}`, function(err, sha) { + this.getRef(`heads/${branch}`, => (err, sha) { this.__currentTree.branch = branch; this.__currentTree.sha = sha; cb(err, sha); From f87ed93083c844b8922b2673ab7e13c671caefc2 Mon Sep 17 00:00:00 2001 From: Minke Zhang Date: Thu, 5 May 2016 13:22:00 -0700 Subject: [PATCH 02/14] added Repo.move() test --- lib/Repository.js | 4 ++-- test/repository.spec.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/Repository.js b/lib/Repository.js index aacf24a5..894e6f6a 100644 --- a/lib/Repository.js +++ b/lib/Repository.js @@ -503,7 +503,7 @@ class Repository extends Requestable { // Move a file to a new location // ------- move(branch, path, newPath, cb) { - return this._updateTree(branch, function(err, latestCommit) { + return this._updateTree(branch, (err, latestCommit) => { this.getTree(latestCommit + '?recursive=true', (err, tree) => { // Update Tree tree.forEach((ref) => { @@ -530,7 +530,7 @@ class Repository extends Requestable { return cb(null, this.__currentTree.sha); } - this.getRef(`heads/${branch}`, => (err, sha) { + this.getRef(`heads/${branch}`, (err, sha) => { this.__currentTree.branch = branch; this.__currentTree.sha = sha; cb(err, sha); diff --git a/test/repository.spec.js b/test/repository.spec.js index d097d431..681267b9 100644 --- a/test/repository.spec.js +++ b/test/repository.spec.js @@ -281,6 +281,18 @@ describe('Repository', function() { })); }); + it('should move file', function(done) { + remoteRepo.writeFile('master', 'old_loc.txt', initialText, initialMessage, assertSuccessful(done, function() { + remoteRepo.move('master', 'old_loc.txt', 'new_loc.txt', assertSuccessful(done, function() { + remoteRepo.getContents('master', 'new_loc.txt', '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'); From 447017b32287277614392ed7f8b1ce2f7c219d57 Mon Sep 17 00:00:00 2001 From: Minke Zhang Date: Thu, 5 May 2016 13:51:03 -0700 Subject: [PATCH 03/14] updated references --- lib/Repository.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Repository.js b/lib/Repository.js index 894e6f6a..3a9c69cf 100644 --- a/lib/Repository.js +++ b/lib/Repository.js @@ -504,9 +504,9 @@ class Repository extends Requestable { // ------- move(branch, path, newPath, cb) { return this._updateTree(branch, (err, latestCommit) => { - this.getTree(latestCommit + '?recursive=true', (err, tree) => { + this.getTree(latestCommit.ref + '?recursive=true', (err, tree) => { // Update Tree - tree.forEach((ref) => { + tree.tree.forEach((ref) => { if (ref.path === path) { ref.path = newPath; } @@ -516,7 +516,7 @@ class Repository extends Requestable { } }); - this.postTree(tree, (err, rootTree) => { + this.createTree(tree, (err, rootTree) => { this.commit(latestCommit, rootTree, 'Deleted ' + path, (err, commit) => { this.updateHead(branch, commit, cb); }); From f2b824a80ec2526497bdd87ab4d561c66b6c94bd Mon Sep 17 00:00:00 2001 From: Minke Zhang Date: Thu, 5 May 2016 14:18:29 -0700 Subject: [PATCH 04/14] continue updating refs --- lib/Repository.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/Repository.js b/lib/Repository.js index 3a9c69cf..a839dc3f 100644 --- a/lib/Repository.js +++ b/lib/Repository.js @@ -516,11 +516,13 @@ class Repository extends Requestable { } }); - this.createTree(tree, (err, rootTree) => { - this.commit(latestCommit, rootTree, 'Deleted ' + path, (err, commit) => { - this.updateHead(branch, commit, cb); - }); - }); + this.createTree(tree, tree.sha).catch( + (err) => { + this.commit(latestCommit, tree.sha, 'Deleted ' + path, (err, commit) => { + this.updateHead(branch, commit, cb); + }); + } + ); }); }); } From e8f6d989420648e4b323fad2c43cfe00f317eb54 Mon Sep 17 00:00:00 2001 From: Minke Zhang Date: Thu, 5 May 2016 12:56:47 -0700 Subject: [PATCH 05/14] Fixed scope resolution issues in Repo.move() --- lib/Repository.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/Repository.js b/lib/Repository.js index 8b302558..925baa7a 100644 --- a/lib/Repository.js +++ b/lib/Repository.js @@ -504,9 +504,9 @@ class Repository extends Requestable { // ------- move(branch, path, newPath, cb) { return this._updateTree(branch, function(err, latestCommit) { - this.getTree(latestCommit + '?recursive=true', function(err, tree) { + this.getTree(latestCommit + '?recursive=true', (err, tree) => { // Update Tree - tree.forEach(function(ref) { + tree.forEach((ref) => { if (ref.path === path) { ref.path = newPath; } @@ -516,8 +516,8 @@ class Repository extends Requestable { } }); - this.createTree(tree, function(err, rootTree) { - this.commit(latestCommit, rootTree, 'Deleted ' + path, function(err, commit) { + this.postTree(tree, (err, rootTree) => { + this.commit(latestCommit, rootTree, 'Deleted ' + path, (err, commit) => { this.updateHead(branch, commit, cb); }); }); @@ -530,7 +530,7 @@ class Repository extends Requestable { return cb(null, this.__currentTree.sha); } - return this.getRef(`heads/${branch}`, function(err, sha) { + return this.getRef(`heads/${branch}`, (err, sha) => { this.__currentTree.branch = branch; this.__currentTree.sha = sha; cb(err, sha); From 36a7a55bb9614102b7b6f7cb8282fb2af116f93f Mon Sep 17 00:00:00 2001 From: Minke Zhang Date: Thu, 5 May 2016 13:22:00 -0700 Subject: [PATCH 06/14] added Repo.move() test --- lib/Repository.js | 2 +- test/repository.spec.js | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/Repository.js b/lib/Repository.js index 925baa7a..6f311bcc 100644 --- a/lib/Repository.js +++ b/lib/Repository.js @@ -503,7 +503,7 @@ class Repository extends Requestable { // Move a file to a new location // ------- move(branch, path, newPath, cb) { - return this._updateTree(branch, function(err, latestCommit) { + return this._updateTree(branch, (err, latestCommit) => { this.getTree(latestCommit + '?recursive=true', (err, tree) => { // Update Tree tree.forEach((ref) => { diff --git a/test/repository.spec.js b/test/repository.spec.js index 93c394a4..453cee3a 100644 --- a/test/repository.spec.js +++ b/test/repository.spec.js @@ -284,6 +284,18 @@ describe('Repository', function() { })); }); + it('should move file', function(done) { + remoteRepo.writeFile('master', 'old_loc.txt', initialText, initialMessage, assertSuccessful(done, function() { + remoteRepo.move('master', 'old_loc.txt', 'new_loc.txt', assertSuccessful(done, function() { + remoteRepo.getContents('master', 'new_loc.txt', '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'); From d6d995e90d476f4ce6371af7e5373423cf44942b Mon Sep 17 00:00:00 2001 From: Minke Zhang Date: Thu, 5 May 2016 13:51:03 -0700 Subject: [PATCH 07/14] updated references --- lib/Repository.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Repository.js b/lib/Repository.js index 6f311bcc..3ccfa6f2 100644 --- a/lib/Repository.js +++ b/lib/Repository.js @@ -504,9 +504,9 @@ class Repository extends Requestable { // ------- move(branch, path, newPath, cb) { return this._updateTree(branch, (err, latestCommit) => { - this.getTree(latestCommit + '?recursive=true', (err, tree) => { + this.getTree(latestCommit.ref + '?recursive=true', (err, tree) => { // Update Tree - tree.forEach((ref) => { + tree.tree.forEach((ref) => { if (ref.path === path) { ref.path = newPath; } @@ -516,7 +516,7 @@ class Repository extends Requestable { } }); - this.postTree(tree, (err, rootTree) => { + this.createTree(tree, (err, rootTree) => { this.commit(latestCommit, rootTree, 'Deleted ' + path, (err, commit) => { this.updateHead(branch, commit, cb); }); From ff674b8606fed28ac236690d5a087110030cd44b Mon Sep 17 00:00:00 2001 From: Minke Zhang Date: Thu, 5 May 2016 14:18:29 -0700 Subject: [PATCH 08/14] continue updating refs --- lib/Repository.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/Repository.js b/lib/Repository.js index 3ccfa6f2..702d3cf3 100644 --- a/lib/Repository.js +++ b/lib/Repository.js @@ -516,11 +516,13 @@ class Repository extends Requestable { } }); - this.createTree(tree, (err, rootTree) => { - this.commit(latestCommit, rootTree, 'Deleted ' + path, (err, commit) => { - this.updateHead(branch, commit, cb); - }); - }); + this.createTree(tree, tree.sha).catch( + (err) => { + this.commit(latestCommit, tree.sha, 'Deleted ' + path, (err, commit) => { + this.updateHead(branch, commit, cb); + }); + } + ); }); }); } From 8103c80f733cf93e2ba589f6ba9d1cd7329fb75d Mon Sep 17 00:00:00 2001 From: Minke Zhang Date: Sat, 14 May 2016 00:23:31 -0700 Subject: [PATCH 09/14] refactored Repo.move() to use promises --- lib/Repository.js | 65 +++++++++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/lib/Repository.js b/lib/Repository.js index 702d3cf3..c9d70329 100644 --- a/lib/Repository.js +++ b/lib/Repository.js @@ -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 @@ -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; }); } @@ -500,42 +500,53 @@ class Repository extends Requestable { }); } - // Move a file to a new location - // ------- - move(branch, path, newPath, cb) { - return this._updateTree(branch, (err, latestCommit) => { - this.getTree(latestCommit.ref + '?recursive=true', (err, tree) => { - // Update Tree - tree.tree.forEach((ref) => { - if (ref.path === path) { - ref.path = newPath; - } - - if (ref.type === 'tree') { - delete ref.sha; - } - }); - - this.createTree(tree, tree.sha).catch( - (err) => { - this.commit(latestCommit, tree.sha, 'Deleted ' + path, (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, response.data.sha).then( + (response) => { + return this.commit(_resp.data.sha, response.data.sha, 'Deleted ' + old_path) // minke + .then((response) => { + return this.updateHead(branch, response.data.sha, cb).catch((err) => {return err}); + }); + } + ); + }); }); - }); } _updateTree(branch, cb) { if (branch === this.__currentTree.branch && this.__currentTree.sha) { - return cb(null, this.__currentTree.sha); + if (cb != undefined) { + return cb(null, this.__currentTree.sha); + } } return this.getRef(`heads/${branch}`, (err, sha) => { this.__currentTree.branch = branch; this.__currentTree.sha = sha; - cb(err, sha); + if (cb != undefined) { + cb(err, sha); + } }); } From 5129126cff7ed04db6e3010d4a7594baccdf4824 Mon Sep 17 00:00:00 2001 From: Minke Zhang Date: Sat, 14 May 2016 00:32:17 -0700 Subject: [PATCH 10/14] removed extraneous Repo._UpdateTree function --- lib/Repository.js | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/lib/Repository.js b/lib/Repository.js index c9d70329..8c86bfcd 100644 --- a/lib/Repository.js +++ b/lib/Repository.js @@ -524,7 +524,7 @@ class Repository extends Requestable { }); return this.createTree(response.data.tree, response.data.sha).then( (response) => { - return this.commit(_resp.data.sha, response.data.sha, 'Deleted ' + old_path) // minke + return this.commit(_resp.data.sha, response.data.sha, 'Deleted ' + old_path) .then((response) => { return this.updateHead(branch, response.data.sha, cb).catch((err) => {return err}); }); @@ -534,22 +534,6 @@ class Repository extends Requestable { }); } - _updateTree(branch, cb) { - if (branch === this.__currentTree.branch && this.__currentTree.sha) { - if (cb != undefined) { - return cb(null, this.__currentTree.sha); - } - } - - return this.getRef(`heads/${branch}`, (err, sha) => { - this.__currentTree.branch = branch; - this.__currentTree.sha = sha; - if (cb != undefined) { - cb(err, sha); - } - }); - } - /** * Write a file to the repository * @see https://developer.github.com/v3/repos/contents/#update-a-file From eecdd78a4a4c42a1dd8a93388a60416eb0218209 Mon Sep 17 00:00:00 2001 From: Minke Zhang Date: Sat, 14 May 2016 11:44:47 -0700 Subject: [PATCH 11/14] move now renaming, but not yet deleted reference to old file --- lib/Repository.js | 7 ++++--- test/repository.spec.js | 15 +++++++++------ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/Repository.js b/lib/Repository.js index 8c86bfcd..6d64ff11 100644 --- a/lib/Repository.js +++ b/lib/Repository.js @@ -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); } /** @@ -526,7 +527,7 @@ class Repository extends Requestable { (response) => { return this.commit(_resp.data.sha, response.data.sha, 'Deleted ' + old_path) .then((response) => { - return this.updateHead(branch, response.data.sha, cb).catch((err) => {return err}); + return this.updateHead('heads/' + branch, response.data.sha, true, cb); }); } ); diff --git a/test/repository.spec.js b/test/repository.spec.js index 453cee3a..8b1009a6 100644 --- a/test/repository.spec.js +++ b/test/repository.spec.js @@ -284,13 +284,16 @@ describe('Repository', function() { })); }); - it('should move file', function(done) { - remoteRepo.writeFile('master', 'old_loc.txt', initialText, initialMessage, assertSuccessful(done, function() { - remoteRepo.move('master', 'old_loc.txt', 'new_loc.txt', assertSuccessful(done, function() { - remoteRepo.getContents('master', 'new_loc.txt', 'raw', assertSuccessful(done, function(err, fileText) { - expect(fileText).to.be(initialText); + 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(); + done(); + })); })); })); })); From 8d1bbc6ca3eb643541ce4eb1670da59f5e4698d5 Mon Sep 17 00:00:00 2001 From: Minke Zhang Date: Sat, 14 May 2016 12:01:13 -0700 Subject: [PATCH 12/14] Repo.move() deletes old file in same commit as creating new file; added test --- lib/Repository.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Repository.js b/lib/Repository.js index 6d64ff11..3d756171 100644 --- a/lib/Repository.js +++ b/lib/Repository.js @@ -523,9 +523,9 @@ class Repository extends Requestable { delete ref.sha; } }); - return this.createTree(response.data.tree, response.data.sha).then( + return this.createTree(response.data.tree).then( (response) => { - return this.commit(_resp.data.sha, response.data.sha, 'Deleted ' + old_path) + 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); }); From 53f92193f4f9d25cb716432342cdc301aa96e058 Mon Sep 17 00:00:00 2001 From: Minke Zhang Date: Sat, 14 May 2016 17:28:20 -0700 Subject: [PATCH 13/14] changed string formatting --- lib/Repository.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Repository.js b/lib/Repository.js index 3d756171..a0f95cf8 100644 --- a/lib/Repository.js +++ b/lib/Repository.js @@ -527,7 +527,7 @@ class Repository extends Requestable { (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); + return this.updateHead(`heads/${branch}`, response.data.sha, true, cb); }); } ); From 03b6df09f1f8722fd70b21d9e081230c2faa82da Mon Sep 17 00:00:00 2001 From: Minke Zhang Date: Sat, 14 May 2016 17:35:25 -0700 Subject: [PATCH 14/14] changed string formatting --- lib/Repository.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Repository.js b/lib/Repository.js index a0f95cf8..f74b620e 100644 --- a/lib/Repository.js +++ b/lib/Repository.js @@ -512,7 +512,7 @@ class Repository extends Requestable { move(branch, old_path, new_path, cb) { return this.getRef(`heads/${branch}`) .then((response) => { - return this.getTree(response.data.object.sha + '?recursive=true') + return this.getTree(`${response.data.object.sha}?recursive=true`) .then((response) => { var _resp = response; response.data.tree.forEach((ref) => {