diff --git a/generate/input/descriptor.json b/generate/input/descriptor.json index d33298c30..4bf70fbb3 100644 --- a/generate/input/descriptor.json +++ b/generate/input/descriptor.json @@ -202,6 +202,32 @@ "git_branch_next": { "ignore": true }, + "git_branch_remote_name": { + "cppFunctionName": "RemoteName", + "jsFunctionName": "remoteName", + "isAsync": true, + "args": { + "out": { + "isReturn": true, + "cppClassName": "GitBuf", + "jsClassName": "Buffer", + "cType": "git_buf *" + }, + "repo": { + "cppClassName": "GitRepository", + "jsClassName": "Repo", + "cType": "git_repository *" + }, + "canonical_branch_name": { + "cppClassName": "String", + "jsClassName": "String", + "cType": "const char *" + } + }, + "return": { + "isErrorCode": true + } + }, "git_branch_set_upstream": { "isAsync": true, "args": { @@ -221,7 +247,10 @@ "isErrorCode": true } } - } + }, + "dependencies": [ + "../include/buf.h" + ] }, "buf": { "functions": { diff --git a/generate/input/libgit2-docs.json b/generate/input/libgit2-docs.json index 48099e8b0..fb9f175af 100644 --- a/generate/input/libgit2-docs.json +++ b/generate/input/libgit2-docs.json @@ -2632,6 +2632,38 @@ "comments": "

The name of the branch matches the definition of the name for git_branch_lookup. That is, if the returned name is given to git_branch_lookup() then the reference is returned that was given to this function.

\n", "group": "branch" }, + "git_branch_remote_name": { + "type": "function", + "file": "branch.h", + "line": 274, + "lineto": 277, + "args": [ + { + "name": "out", + "type": "git_buf *", + "comment": "where the name is stored." + }, + { + "name": "repo", + "type": "git_respository *", + "comment": "the repo to check." + }, + { + "name": "canonical_branch_name", + "type": "const char *", + "comment": "the ref name of the branch" + } + ], + "argline": "git_buf *out, git_repository *repo, const char *canonical_branch_name", + "sig": "git_buf *::git_repository *::const char *", + "return": { + "type": "int", + "comment": " 0 on success; otherwise an error code (e.g., if the\n ref is no local or remote branch)." + }, + "description": "

Return the name of the given remote branch.

\n", + "comments": "

\n", + "group": "branch" + }, "git_branch_upstream": { "type": "function", "file": "branch.h", @@ -36087,6 +36119,7 @@ "git_branch_name", "git_branch_next", "git_branch_set_upstream", + "git_branch_remote_name", "git_branch_upstream" ] ], @@ -37149,4 +37182,4 @@ "ex/HEAD/tag.html" ] ] -} \ No newline at end of file +} diff --git a/lib/branch.js b/lib/branch.js new file mode 100644 index 000000000..bc4b23443 --- /dev/null +++ b/lib/branch.js @@ -0,0 +1,19 @@ +var NodeGit = require("../"); +var Branch = NodeGit.Branch; + +var _remoteName = Branch.remoteName; + +/** + * Retrieve the Branch's Remote Name as a String. + * + * @async + * @param {Repository} repo The repo to get the remote name from + * @param {String} the refname of the branch + * @return {String} remote name as a string. + */ +Branch.remoteName = function(repo, remoteRef) { + return _remoteName.call(this, repo, remoteRef) + .then(function(remoteNameBuffer) { + return remoteNameBuffer.toString(); + }); +}; diff --git a/test/tests/branch.js b/test/tests/branch.js index 8d587c019..3900e4242 100644 --- a/test/tests/branch.js +++ b/test/tests/branch.js @@ -11,6 +11,7 @@ describe("Branch", function() { var branchName2 = "test-branch2"; var fullBranchName = "refs/heads/" + branchName; var fullBranchName2 = "refs/heads/" + branchName2; + var remoteName = "origin"; var upstreamName = "origin/master"; var fullUpstreamName = "refs/remotes/origin/master"; var nonHeadCommit = "c82fb078a192ea221c9f1093c64321c60d64aa0d"; @@ -85,6 +86,27 @@ describe("Branch", function() { }); }); + it("can get the remote name of a branch", function() { + var repo = this.repository; + + return NodeGit.Branch.remoteName(repo, fullUpstreamName) + .then(function(remoteNameToTest) { + assert.equal(remoteNameToTest, remoteName); + }); + }); + + it("cannot get remote name from a non-remote branch", function() { + var repo = this.repository; + + return NodeGit.Branch.remoteName(repo, fullBranchName) + .then(function() { + assert.fail("The ref should not have been a remote"); + }) + .catch(function(err) { + assert.strictEqual(err.errno, -1); + }); + }); + it("can rename a branch", function() { var branch = this.branch;