Implement Repository.lookup_branch. Constants for local and remote branches.

This commit is contained in:
Daniel Rodríguez Troitiño 2013-05-17 22:00:52 +02:00
parent f2b1644510
commit 1062ab638b
4 changed files with 96 additions and 0 deletions

View File

@ -30,6 +30,7 @@ from string import hexdigits
# Import from pygit2
from _pygit2 import Repository as _Repository
from _pygit2 import GIT_BRANCH_LOCAL, GIT_BRANCH_REMOTE
from _pygit2 import Oid, GIT_OID_HEXSZ, GIT_OID_MINPREFIXLEN
from _pygit2 import GIT_CHECKOUT_SAFE_CREATE, GIT_DIFF_NORMAL
from _pygit2 import Reference, Tree, Commit, Blob

View File

@ -337,6 +337,8 @@ moduleinit(PyObject* m)
*/
INIT_TYPE(BranchType, &ReferenceType, PyType_GenericNew);
ADD_TYPE(m, Branch)
ADD_CONSTANT_INT(m, GIT_BRANCH_LOCAL)
ADD_CONSTANT_INT(m, GIT_BRANCH_REMOTE)
/*
* Index & Working copy

View File

@ -36,6 +36,7 @@
#include "note.h"
#include "repository.h"
#include "remote.h"
#include "branch.h"
#include <git2/odb_backend.h>
extern PyObject *GitError;
@ -274,6 +275,33 @@ Repository_git_object_lookup_prefix(Repository *self, PyObject *key)
}
PyDoc_STRVAR(Repository_lookup_branch__doc__,
"lookup_branch(branch_name, [branch_type]) -> Object\n"
"\n"
"Returns the Git reference for the given branch name (local or remote).");
PyObject *
Repository_lookup_branch(Repository *self, PyObject *args)
{
git_reference *c_reference;
const char *c_name;
git_branch_t branch_type = GIT_BRANCH_LOCAL;
int err;
if (!PyArg_ParseTuple(args, "s|I", &c_name, &branch_type))
return NULL;
err = git_branch_lookup(&c_reference, self->repo, c_name, branch_type);
if (err == 0)
return wrap_branch(c_reference, self);
if (err == GIT_ENOTFOUND)
Py_RETURN_NONE;
return Error_set(err);
}
PyDoc_STRVAR(Repository_revparse_single__doc__,
"revparse_single(revision) -> Object\n"
"\n"
@ -1321,6 +1349,7 @@ PyMethodDef Repository_methods[] = {
METHOD(Repository, create_note, METH_VARARGS),
METHOD(Repository, lookup_note, METH_VARARGS),
METHOD(Repository, git_object_lookup_prefix, METH_O),
METHOD(Repository, lookup_branch, METH_VARARGS),
{NULL}
};

64
test/test_branch.py Normal file
View File

@ -0,0 +1,64 @@
# -*- coding: UTF-8 -*-
#
# Copyright 2010-2013 The pygit2 contributors
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2,
# as published by the Free Software Foundation.
#
# In addition to the permissions in the GNU General Public License,
# the authors give you unlimited permission to link the compiled
# version of this file into combinations with other programs,
# and to distribute those combinations without any restriction
# coming from the use of this file. (The General Public License
# restrictions do apply in other respects; for example, they cover
# modification of the file, and distribution when not linked into
# a combined executable.)
#
# This file is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to
# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
"""Tests for branch methods."""
from __future__ import absolute_import
from __future__ import unicode_literals
import unittest
import pygit2
from . import utils
LAST_COMMIT = '2be5719152d4f82c7302b1c0932d8e5f0a4a0e98'
I18N_LAST_COMMIT = '5470a671a80ac3789f1a6a8cefbcf43ce7af0563'
ORIGIN_MASTER_COMMIT = '784855caf26449a1914d2cf62d12b9374d76ae78'
class BranchesTestCase(utils.RepoTestCase):
def test_lookup_branch_local(self):
branch = self.repo.lookup_branch('master')
self.assertEqual(branch.target.hex, LAST_COMMIT)
branch = self.repo.lookup_branch('i18n', pygit2.GIT_BRANCH_LOCAL)
self.assertEqual(branch.target.hex, I18N_LAST_COMMIT)
self.assertTrue(self.repo.lookup_branch('not-exists') is None)
class BranchesEmptyRepoTestCase(utils.EmptyRepoTestCase):
def test_lookup_branch_remote(self):
remote = self.repo.remotes[0]
remote.fetch()
branch = self.repo.lookup_branch('origin/master', pygit2.GIT_BRANCH_REMOTE)
self.assertEqual(branch.target.hex, ORIGIN_MASTER_COMMIT)
self.assertTrue(self.repo.lookup_branch('origin/not-exists', pygit2.GIT_BRANCH_REMOTE) is None)
if __name__ == '__main__':
unittest.main()