Skip to content

Commit 113493a

Browse files
authored
Merge pull request nodegit#1340 from cjhoward92/feature/add_branch_remote_name
Exposed libgit2 git_branch_remote_name method
2 parents 92a570f + f913d50 commit 113493a

File tree

4 files changed

+105
-2
lines changed

4 files changed

+105
-2
lines changed

generate/input/descriptor.json

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,32 @@
202202
"git_branch_next": {
203203
"ignore": true
204204
},
205+
"git_branch_remote_name": {
206+
"cppFunctionName": "RemoteName",
207+
"jsFunctionName": "remoteName",
208+
"isAsync": true,
209+
"args": {
210+
"out": {
211+
"isReturn": true,
212+
"cppClassName": "GitBuf",
213+
"jsClassName": "Buffer",
214+
"cType": "git_buf *"
215+
},
216+
"repo": {
217+
"cppClassName": "GitRepository",
218+
"jsClassName": "Repo",
219+
"cType": "git_repository *"
220+
},
221+
"canonical_branch_name": {
222+
"cppClassName": "String",
223+
"jsClassName": "String",
224+
"cType": "const char *"
225+
}
226+
},
227+
"return": {
228+
"isErrorCode": true
229+
}
230+
},
205231
"git_branch_set_upstream": {
206232
"isAsync": true,
207233
"args": {
@@ -221,7 +247,10 @@
221247
"isErrorCode": true
222248
}
223249
}
224-
}
250+
},
251+
"dependencies": [
252+
"../include/buf.h"
253+
]
225254
},
226255
"buf": {
227256
"functions": {

generate/input/libgit2-docs.json

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2632,6 +2632,38 @@
26322632
"comments": "<p>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.</p>\n",
26332633
"group": "branch"
26342634
},
2635+
"git_branch_remote_name": {
2636+
"type": "function",
2637+
"file": "branch.h",
2638+
"line": 274,
2639+
"lineto": 277,
2640+
"args": [
2641+
{
2642+
"name": "out",
2643+
"type": "git_buf *",
2644+
"comment": "where the name is stored."
2645+
},
2646+
{
2647+
"name": "repo",
2648+
"type": "git_respository *",
2649+
"comment": "the repo to check."
2650+
},
2651+
{
2652+
"name": "canonical_branch_name",
2653+
"type": "const char *",
2654+
"comment": "the ref name of the branch"
2655+
}
2656+
],
2657+
"argline": "git_buf *out, git_repository *repo, const char *canonical_branch_name",
2658+
"sig": "git_buf *::git_repository *::const char *",
2659+
"return": {
2660+
"type": "int",
2661+
"comment": " 0 on success; otherwise an error code (e.g., if the\n ref is no local or remote branch)."
2662+
},
2663+
"description": "<p>Return the name of the given remote branch.</p>\n",
2664+
"comments": "<p></p>\n",
2665+
"group": "branch"
2666+
},
26352667
"git_branch_upstream": {
26362668
"type": "function",
26372669
"file": "branch.h",
@@ -36087,6 +36119,7 @@
3608736119
"git_branch_name",
3608836120
"git_branch_next",
3608936121
"git_branch_set_upstream",
36122+
"git_branch_remote_name",
3609036123
"git_branch_upstream"
3609136124
]
3609236125
],
@@ -37149,4 +37182,4 @@
3714937182
"ex/HEAD/tag.html"
3715037183
]
3715137184
]
37152-
}
37185+
}

lib/branch.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var NodeGit = require("../");
2+
var Branch = NodeGit.Branch;
3+
4+
var _remoteName = Branch.remoteName;
5+
6+
/**
7+
* Retrieve the Branch's Remote Name as a String.
8+
*
9+
* @async
10+
* @param {Repository} repo The repo to get the remote name from
11+
* @param {String} the refname of the branch
12+
* @return {String} remote name as a string.
13+
*/
14+
Branch.remoteName = function(repo, remoteRef) {
15+
return _remoteName.call(this, repo, remoteRef)
16+
.then(function(remoteNameBuffer) {
17+
return remoteNameBuffer.toString();
18+
});
19+
};

test/tests/branch.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe("Branch", function() {
1111
var branchName2 = "test-branch2";
1212
var fullBranchName = "refs/heads/" + branchName;
1313
var fullBranchName2 = "refs/heads/" + branchName2;
14+
var remoteName = "origin";
1415
var upstreamName = "origin/master";
1516
var fullUpstreamName = "refs/remotes/origin/master";
1617
var nonHeadCommit = "c82fb078a192ea221c9f1093c64321c60d64aa0d";
@@ -85,6 +86,27 @@ describe("Branch", function() {
8586
});
8687
});
8788

89+
it("can get the remote name of a branch", function() {
90+
var repo = this.repository;
91+
92+
return NodeGit.Branch.remoteName(repo, fullUpstreamName)
93+
.then(function(remoteNameToTest) {
94+
assert.equal(remoteNameToTest, remoteName);
95+
});
96+
});
97+
98+
it("cannot get remote name from a non-remote branch", function() {
99+
var repo = this.repository;
100+
101+
return NodeGit.Branch.remoteName(repo, fullBranchName)
102+
.then(function() {
103+
assert.fail("The ref should not have been a remote");
104+
})
105+
.catch(function(err) {
106+
assert.strictEqual(err.errno, -1);
107+
});
108+
});
109+
88110
it("can rename a branch", function() {
89111
var branch = this.branch;
90112

0 commit comments

Comments
 (0)