From c4d17b759be2a3e740031a4c2d4690882351ab1a Mon Sep 17 00:00:00 2001 From: Martin Lenders Date: Fri, 8 Jun 2012 12:17:37 +0200 Subject: [PATCH] Create Config type --- include/pygit2/config.h | 8 ++++ include/pygit2/types.h | 1 + src/pygit2.c | 7 +++ src/pygit2/config.c | 97 +++++++++++++++++++++++++++++++++++++++++ test/__init__.py | 4 +- test/test_config.py | 50 +++++++++++++++++++++ 6 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 include/pygit2/config.h create mode 100644 src/pygit2/config.c create mode 100644 test/test_config.py diff --git a/include/pygit2/config.h b/include/pygit2/config.h new file mode 100644 index 0000000..29e56ad --- /dev/null +++ b/include/pygit2/config.h @@ -0,0 +1,8 @@ +#ifndef INCLUDE_pygit2_config_h +#define INCLUDE_pygit2_config_h + +#define PY_SSIZE_T_CLEAN +#include +#include + +#endif diff --git a/include/pygit2/types.h b/include/pygit2/types.h index 44e4f31..5393f3c 100644 --- a/include/pygit2/types.h +++ b/include/pygit2/types.h @@ -30,6 +30,7 @@ OBJECT_STRUCT(Tag, git_tag, tag) OBJECT_STRUCT(Index, git_index, index) OBJECT_STRUCT(Walker, git_revwalk, walk) OBJECT_STRUCT(Diff, git_diff_list, diff) +OBJECT_STRUCT(Config, git_config, config) typedef struct { PyObject_HEAD diff --git a/src/pygit2.c b/src/pygit2.c index eede316..8ecc0d2 100644 --- a/src/pygit2.c +++ b/src/pygit2.c @@ -52,6 +52,7 @@ extern PyTypeObject IndexType; extern PyTypeObject IndexEntryType; extern PyTypeObject IndexIterType; extern PyTypeObject WalkerType; +extern PyTypeObject ConfigType; extern PyTypeObject ReferenceType; extern PyTypeObject RefLogIterType; extern PyTypeObject RefLogEntryType; @@ -160,6 +161,9 @@ moduleinit(PyObject* m) TreeBuilderType.tp_new = PyType_GenericNew; if (PyType_Ready(&TreeBuilderType) < 0) return NULL; + ConfigType.tp_new = PyType_GenericNew; + if (PyType_Ready(&ConfigType) < 0) + return NULL; WalkerType.tp_new = PyType_GenericNew; if (PyType_Ready(&WalkerType) < 0) return NULL; @@ -190,6 +194,9 @@ moduleinit(PyObject* m) Py_INCREF(&TreeType); PyModule_AddObject(m, "Tree", (PyObject *)&TreeType); + Py_INCREF(&ConfigType); + PyModule_AddObject(m, "Config", (PyObject *)&ConfigType); + Py_INCREF(&BlobType); PyModule_AddObject(m, "Blob", (PyObject *)&BlobType); diff --git a/src/pygit2/config.c b/src/pygit2/config.c new file mode 100644 index 0000000..a0deadf --- /dev/null +++ b/src/pygit2/config.c @@ -0,0 +1,97 @@ +#define PY_SSIZE_T_CLEAN +#include +#include +#include +#include +#include + +extern PyTypeObject ConfigType; + +int +Config_init(Config *self, PyObject *args, PyObject *kwds) +{ + char *path; + int err; + + if (kwds) { + PyErr_SetString(PyExc_TypeError, + "Repository takes no keyword arguments"); + return -1; + } + + if (PySequence_Length(args) > 0) { + if (!PyArg_ParseTuple(args, "s", &path)) { + return -1; + } + err = git_config_open_ondisk(&self->config, path); + if (err < 0) { + Error_set_str(err, path); + return -1; + } + } else { + err = git_config_new(&self->config); + if (err < 0) { + Error_set(err); + return -1; + } + } + return 0; +} + +void +Config_dealloc(Config *self) +{ + PyObject_GC_UnTrack(self); + Py_XDECREF(self->repo); + git_config_free(self->config); + PyObject_GC_Del(self); +} + +int +Config_traverse(Config *self, visitproc visit, void *arg) +{ + Py_VISIT(self->repo); + return 0; +} + +PyTypeObject ConfigType = { + PyVarObject_HEAD_INIT(NULL, 0) + "_pygit2.Config", /* tp_name */ + sizeof(Config), /* tp_basicsize */ + 0, /* tp_itemsize */ + (destructor)Config_dealloc, /* tp_dealloc */ + 0, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_compare */ + 0, /* tp_repr */ + 0, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + 0, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT | + Py_TPFLAGS_HAVE_GC, /* tp_flags */ + "Configuration management", /* tp_doc */ + (traverseproc)Config_traverse, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + 0, /* tp_methods */ + 0, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + (initproc)Config_init, /* tp_init */ + 0, /* tp_alloc */ + 0, /* tp_new */ +}; diff --git a/test/__init__.py b/test/__init__.py index fcb82c5..2cf6bb9 100644 --- a/test/__init__.py +++ b/test/__init__.py @@ -35,8 +35,8 @@ import sys import unittest -names = ['blob', 'commit', 'index', 'refs', 'repository', 'revwalk', 'tag', - 'tree', 'signature', 'status', 'treebuilder', 'diff'] +names = ['blob', 'commit', 'config', 'index', 'refs', 'repository', 'revwalk', + 'tag', 'tree', 'signature', 'status', 'treebuilder'] def test_suite(): modules = ['test.test_%s' % n for n in names] return unittest.defaultTestLoader.loadTestsFromNames(modules) diff --git a/test/test_config.py b/test/test_config.py new file mode 100644 index 0000000..cc2623c --- /dev/null +++ b/test/test_config.py @@ -0,0 +1,50 @@ +# -*- coding: UTF-8 -*- +# +# Copyright 2012 elego +# +# 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 Index files.""" + +import os +import unittest + +import pygit2 +from . import utils + + +__author__ = 'mlenders@elegosoft.com (M. Lenders)' + +config_filename = "test_config" + +class ConfigTest(utils.RepoTestCase): + + def test_new(self): + config_write = pygit2.Config(config_filename) + + self.assertNotEqual(config_write, None) + + +if __name__ == '__main__': + unittest.main()