Add back reference from Repository to Config

This commit is contained in:
Martin Lenders 2012-06-08 16:10:33 +02:00
parent fcacea310b
commit e77d248713
4 changed files with 42 additions and 0 deletions

@ -20,6 +20,7 @@ PyObject* Repository_write(Repository *self, PyObject *args);
PyObject* Repository_get_index(Repository *self, void *closure);
PyObject* Repository_get_path(Repository *self, void *closure);
PyObject* Repository_get_workdir(Repository *self, void *closure);
PyObject* Repository_get_config(Repository *self, void *closure);
PyObject* Repository_walk(Repository *self, PyObject *args);
PyObject* Repository_create_blob(Repository *self, PyObject *args);
PyObject* Repository_create_commit(Repository *self, PyObject *args);

@ -10,6 +10,7 @@ typedef struct {
PyObject_HEAD
git_repository *repo;
PyObject *index; /* It will be None for a bare repository */
PyObject *config;
} Repository;
/* The structs for some of the object subtypes are identical except for

@ -17,6 +17,7 @@ extern PyTypeObject CommitType;
extern PyTypeObject BlobType;
extern PyTypeObject TagType;
extern PyTypeObject TreeBuilderType;
extern PyTypeObject ConfigType;
extern PyTypeObject DiffType;
git_otype
@ -326,6 +327,37 @@ Repository_get_workdir(Repository *self, void *closure)
return to_path(c_path);
}
PyObject *
Repository_get_config(Repository *self, void *closure)
{
int err;
git_config *config;
Config *py_config;
assert(self->repo);
if (self->config == NULL) {
err = git_repository_config(&config, self->repo);
if (err < 0)
return Error_set(err);
py_config = PyObject_GC_New(Config, &ConfigType);
if (!py_config) {
git_config_free(config);
return NULL;
}
Py_INCREF(self);
py_config->repo = self;
py_config->config = config;
PyObject_GC_Track(py_config);
self->config = (PyObject*)py_config;
}
Py_INCREF(self->config);
return self->config;
}
PyObject *
Repository_walk(Repository *self, PyObject *args)
{
@ -768,6 +800,11 @@ PyGetSetDef Repository_getseters[] = {
"The normalized path to the git repository.", NULL},
{"head", (getter)Repository_head, NULL,
"Current head reference of the repository.", NULL},
{"config", (getter)Repository_get_config, NULL,
"Get the configuration file for this repository.\n\n"
"If a configuration file has not been set, the default "
"config set for the repository will be returned, including "
"global and system configurations (if they are available).", NULL},
{"workdir", (getter)Repository_get_workdir, NULL,
"The normalized path to the working directory of the repository. "
"If the repository is bare, None will be returned.", NULL},

@ -40,6 +40,9 @@ config_filename = "test_config"
class ConfigTest(utils.RepoTestCase):
def test_config(self):
self.assertNotEqual(None, self.repo.config)
def test_global_config(self):
try:
self.assertNotEqual(None, pygit2.Config.get_global_config())