Implement Branch.remote_name.

This commit is contained in:
Daniel Rodríguez Troitiño
2013-05-17 23:03:40 +02:00
parent 761d23bb2d
commit 8d9f59edec
2 changed files with 45 additions and 0 deletions

View File

@@ -123,6 +123,44 @@ PyObject* Branch_branch_name__get__(Branch *self)
} }
PyDoc_STRVAR(Branch_remote_name__doc__,
"The name of the remote that the remote tracking branch belongs to.");
PyObject* Branch_remote_name__get__(Branch *self)
{
int err;
const char *branch_name;
char *c_name = NULL;
CHECK_REFERENCE(self);
branch_name = git_reference_name(self->reference);
// get the length of the remote name
err = git_branch_remote_name(NULL, 0, self->repo->repo, branch_name);
if (err < GIT_OK)
return Error_set(err);
// get the actual remote name
c_name = calloc(err, sizeof(char));
if (c_name == NULL)
return PyErr_NoMemory();
err = git_branch_remote_name(c_name,
err * sizeof(char),
self->repo->repo,
branch_name);
if (err < GIT_OK) {
free(c_name);
return Error_set(err);
}
PyObject *py_name = to_unicode(c_name, NULL, NULL);
free(c_name);
return py_name;
}
PyMethodDef Branch_methods[] = { PyMethodDef Branch_methods[] = {
METHOD(Branch, delete, METH_NOARGS), METHOD(Branch, delete, METH_NOARGS),
METHOD(Branch, is_head, METH_NOARGS), METHOD(Branch, is_head, METH_NOARGS),
@@ -132,6 +170,7 @@ PyMethodDef Branch_methods[] = {
PyGetSetDef Branch_getseters[] = { PyGetSetDef Branch_getseters[] = {
GETTER(Branch, branch_name), GETTER(Branch, branch_name),
GETTER(Branch, remote_name),
{NULL} {NULL}
}; };

View File

@@ -140,6 +140,12 @@ class BranchesEmptyRepoTestCase(utils.EmptyRepoTestCase):
branches = sorted(self.repo.listall_branches(pygit2.GIT_BRANCH_REMOTE)) branches = sorted(self.repo.listall_branches(pygit2.GIT_BRANCH_REMOTE))
self.assertEqual(branches, ['origin/master']) self.assertEqual(branches, ['origin/master'])
def test_branch_remote_name(self):
self.repo.remotes[0].fetch()
branch = self.repo.lookup_branch('origin/master',
pygit2.GIT_BRANCH_REMOTE)
self.assertEqual(branch.remote_name, 'origin')
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()