From 9fa1a3260bae7d8673a60d2c78a20f73ca76fa62 Mon Sep 17 00:00:00 2001 From: Remy Suen Date: Sat, 28 Jan 2017 19:25:56 +0900 Subject: [PATCH 1/2] Make `Branch.createFromAnnotated` async The function was originally synchronous which meant it would return something on regardless of whether it succeeded (a branch) or failed (an error code). This forces the client to have to check whether the returned object is an error code or an object. Change the function to be asynchronous instead so that clients can use the resolved or rejected promise to easily determine whether the branch was successfully created or not. --- generate/input/descriptor.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/generate/input/descriptor.json b/generate/input/descriptor.json index 044757eda..bc628cb3d 100644 --- a/generate/input/descriptor.json +++ b/generate/input/descriptor.json @@ -183,6 +183,7 @@ } }, "git_branch_create_from_annotated": { + "isAsync": true, "args": { "ref_out": { "isReturn": true @@ -193,6 +194,9 @@ "force": { "isOptional": true } + }, + "return": { + "isErrorCode": true } }, "git_branch_next": { From b88d332d9ca5b51194d336bcb503e9288995655c Mon Sep 17 00:00:00 2001 From: Remy Suen Date: Fri, 6 Jan 2017 16:14:46 +0900 Subject: [PATCH 2/2] Added tests for Branch move lookup createFromAnnotated --- test/tests/branch.js | 66 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/test/tests/branch.js b/test/tests/branch.js index 8e69020a8..8d587c019 100644 --- a/test/tests/branch.js +++ b/test/tests/branch.js @@ -6,9 +6,14 @@ describe("Branch", function() { var NodeGit = require("../../"); var Repository = NodeGit.Repository; var Branch = NodeGit.Branch; + var AnnotatedCommit = NodeGit.AnnotatedCommit; var branchName = "test-branch"; + var branchName2 = "test-branch2"; var fullBranchName = "refs/heads/" + branchName; + var fullBranchName2 = "refs/heads/" + branchName2; var upstreamName = "origin/master"; + var fullUpstreamName = "refs/remotes/origin/master"; + var nonHeadCommit = "c82fb078a192ea221c9f1093c64321c60d64aa0d"; var reposPath = local("../repos/workdir"); @@ -27,6 +32,8 @@ describe("Branch", function() { }) .then(function(branch) { test.branch = branch; + return test.repository.createBranch( + branchName2, test.masterCommit, true); }); }); @@ -77,4 +84,63 @@ describe("Branch", function() { assert.equal(branchNameToTest, branchName); }); }); + + it("can rename a branch", function() { + var branch = this.branch; + + // don't force the move + return Branch.move(branch, branchName2, 0) + .then(function(branch) { + return Promise.reject(new Error( + "should not be able to rename the branch")); + }, function(error) { + return Promise.resolve() + .then(function() { + // force the move + return Branch.move(branch, branchName2, 1); + }) + .then(function(branch) { + assert.equal(branch.name(), fullBranchName2); + }); + }); + }); + + it("can lookup a branch", function() { + var repo = this.repository; + + return Branch.lookup(repo, branchName, Branch.BRANCH.LOCAL) + .then(function(branch) { + assert.equal(branch.name(), fullBranchName); + return Branch.lookup(repo, upstreamName, Branch.BRANCH.REMOTE); + }) + .then(function(branch) { + assert.equal(branch.name(), fullUpstreamName); + }); + }); + + it("can create branch from annotated commit", function() { + var repo = this.repository; + var annotatedCommit = null; + + return AnnotatedCommit.fromRevspec(repo, nonHeadCommit) + .then(function(theAnnotatedCommit) { + annotatedCommit = theAnnotatedCommit; + return Branch.createFromAnnotated( + repo, branchName, annotatedCommit, 0); + }) + .then(function(branch) { + return Promise.reject(new Error( + "should not be able to create the branch")); + }, function(error) { + return Promise.resolve() + .then(function() { + // force the branch creation + return Branch.createFromAnnotated( + repo, branchName, annotatedCommit, 1); + }) + .then(function(branch) { + assert.equal(branch.name(), fullBranchName); + }); + }); + }); });