From 1062ab638b9b6dd145ea34cc8fb6b362e49b1af9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Rodri=CC=81guez=20Troitin=CC=83o?= Date: Fri, 17 May 2013 22:00:52 +0200 Subject: [PATCH] Implement Repository.lookup_branch. Constants for local and remote branches. --- pygit2/repository.py | 1 + src/pygit2.c | 2 ++ src/repository.c | 29 ++++++++++++++++++++ test/test_branch.py | 64 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 test/test_branch.py diff --git a/pygit2/repository.py b/pygit2/repository.py index d235b8d..fbb77ce 100644 --- a/pygit2/repository.py +++ b/pygit2/repository.py @@ -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 diff --git a/src/pygit2.c b/src/pygit2.c index 19a8387..a896dcb 100644 --- a/src/pygit2.c +++ b/src/pygit2.c @@ -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 diff --git a/src/repository.c b/src/repository.c index 77327b0..6b3b279 100644 --- a/src/repository.c +++ b/src/repository.c @@ -36,6 +36,7 @@ #include "note.h" #include "repository.h" #include "remote.h" +#include "branch.h" #include 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} }; diff --git a/test/test_branch.py b/test/test_branch.py new file mode 100644 index 0000000..8c6a45d --- /dev/null +++ b/test/test_branch.py @@ -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()