diff --git a/include/pygit2/config.h b/include/pygit2/config.h index b07849e..62dbc83 100644 --- a/include/pygit2/config.h +++ b/include/pygit2/config.h @@ -34,7 +34,7 @@ PyObject* Config_get_global_config(void); PyObject* Config_get_system_config(void); -PyObject* Config_add_file(Config *self, PyObject *args); +PyObject* Config_add_file(Config *self, PyObject *args, PyObject *kwds); PyObject* Config_getitem(Config *self, PyObject *key); PyObject* Config_foreach(Config *self, PyObject *args); PyObject* Config_get_multivar(Config *self, PyObject *args); diff --git a/setup.py b/setup.py index 7a8dce8..59f5bc3 100644 --- a/setup.py +++ b/setup.py @@ -30,6 +30,7 @@ from __future__ import print_function +import codecs import os from subprocess import Popen, PIPE import sys @@ -162,7 +163,7 @@ classifiers = [ "Topic :: Software Development :: Version Control"] -with open('README.rst') as readme: +with codecs.open('README.rst', 'r', 'utf-8') as readme: long_description = readme.read() setup(name='pygit2', diff --git a/src/pygit2/config.c b/src/pygit2/config.c index d850ba8..0983bac 100644 --- a/src/pygit2/config.c +++ b/src/pygit2/config.c @@ -214,8 +214,7 @@ Config_setitem(Config *self, PyObject *py_key, PyObject *py_value) } int -Config_foreach_callback_wrapper(const char *c_name, const char *c_value, - void *c_payload) +Config_foreach_callback_wrapper(const git_config_entry *entry, void *c_payload) { PyObject *args = (PyObject *)c_payload; PyObject *py_callback = NULL; @@ -224,18 +223,20 @@ Config_foreach_callback_wrapper(const char *c_name, const char *c_value, int c_result; if (!PyArg_ParseTuple(args, "O|O", &py_callback, &py_payload)) - return 0; + return -1; if (py_payload) - args = Py_BuildValue("ssO", c_name, c_value, py_payload); + args = Py_BuildValue("ssO", entry->name, entry->value, py_payload); else - args = Py_BuildValue("ss", c_name, c_value); + args = Py_BuildValue("ss", entry->name, entry->value); + if (!args) + return -1; if (!(py_result = PyObject_CallObject(py_callback,args))) - return 0; + return -1; - if (!(c_result = PyLong_AsLong(py_result))) - return 0; + if ((c_result = PyLong_AsLong(py_result) == -1)) + return -1; return c_result; } @@ -264,16 +265,20 @@ Config_foreach(Config *self, PyObject *args) } PyObject * -Config_add_file(Config *self, PyObject *args) +Config_add_file(Config *self, PyObject *args, PyObject *kwds) { + char *keywords[] = {"path", "level", "force", NULL}; int err; char *path; - int priority; + unsigned int level = 0; + int force = 0; - if (!PyArg_ParseTuple(args, "si", &path, &priority)) + if (!PyArg_ParseTupleAndKeywords( + args, kwds, "s|Ii", keywords, + &path, &level, &force)) return NULL; - err = git_config_add_file_ondisk(self->config, path, priority); + err = git_config_add_file_ondisk(self->config, path, level, force); if (err < 0) { Error_set_str(err, path); return NULL; @@ -283,12 +288,12 @@ Config_add_file(Config *self, PyObject *args) } int -Config_get_multivar_fn_wrapper(const char *value, void *data) +Config_get_multivar_fn_wrapper(const git_config_entry *value, void *data) { PyObject *list = (PyObject *)data; PyObject *item = NULL; - if (!(item = PyUnicode_FromString(value))) + if (!(item = PyUnicode_FromString(value->value))) return -2; PyList_Append(list, item); @@ -353,7 +358,7 @@ PyMethodDef Config_methods[] = { "and value of each variable in the config backend, and an optional " "payload passed to this method. As soon as one of the callbacks returns " "an integer other than 0, this function returns that value."}, - {"add_file", (PyCFunction)Config_add_file, METH_VARARGS, + {"add_file", (PyCFunction)Config_add_file, METH_VARARGS | METH_KEYWORDS, "Add a config file instance to an existing config."}, {"get_multivar", (PyCFunction)Config_get_multivar, METH_VARARGS, "Get each value of a multivar ''name'' as a list. The optional ''regex'' " diff --git a/src/pygit2/diff.c b/src/pygit2/diff.c index 4fc44eb..d010442 100644 --- a/src/pygit2/diff.c +++ b/src/pygit2/diff.c @@ -42,8 +42,8 @@ extern PyTypeObject HunkType; static int diff_data_cb( void *cb_data, - git_diff_delta *delta, - git_diff_range *range, + const git_diff_delta *delta, + const git_diff_range *range, char line_origin, const char *content, size_t content_len) @@ -75,8 +75,8 @@ static int diff_data_cb( static int diff_hunk_cb( void *cb_data, - git_diff_delta *delta, - git_diff_range *range, + const git_diff_delta *delta, + const git_diff_range *range, const char *header, size_t header_len) { @@ -158,7 +158,8 @@ static int diff_hunk_cb( return 0; }; -static int diff_file_cb(void *cb_data, git_diff_delta *delta, float progress) +static int diff_file_cb(void *cb_data, const git_diff_delta *delta, + float progress) { PyObject *files, *file; @@ -201,8 +202,8 @@ Diff_changes(Diff *self) static int diff_print_cb( void *cb_data, - git_diff_delta *delta, - git_diff_range *range, + const git_diff_delta *delta, + const git_diff_range *range, char usage, const char *line, size_t line_len) diff --git a/src/pygit2/index.c b/src/pygit2/index.c index d0de48e..8681bd3 100644 --- a/src/pygit2/index.c +++ b/src/pygit2/index.c @@ -84,12 +84,11 @@ Index_add(Index *self, PyObject *args) { int err; const char *path; - int stage=0; - if (!PyArg_ParseTuple(args, "s|i", &path, &stage)) + if (!PyArg_ParseTuple(args, "s", &path)) return NULL; - err = git_index_add(self->index, path, stage); + err = git_index_add_from_workdir(self->index, path); if (err < 0) return Error_set_str(err, path); @@ -281,7 +280,7 @@ Index_getitem(Index *self, PyObject *value) if (idx == -1) return NULL; - index_entry = git_index_get(self->index, idx); + index_entry = git_index_get_byindex(self->index, idx); if (!index_entry) { PyErr_SetObject(PyExc_KeyError, value); return NULL; @@ -290,27 +289,35 @@ Index_getitem(Index *self, PyObject *value) return wrap_index_entry(index_entry, self); } +PyObject * +Index_remove(Index *self, PyObject *args) +{ + int err; + const char *path; + + if (!PyArg_ParseTuple(args, "s", &path)) + return NULL; + + err = git_index_remove(self->index, path, 0); + if (err < 0) { + Error_set(err); + return NULL; + } + + Py_RETURN_NONE; +} + int Index_setitem(Index *self, PyObject *key, PyObject *value) { - int err; - int idx; - - if (value) { + if (value != NULL) { PyErr_SetString(PyExc_NotImplementedError, "set item on index not yet implemented"); return -1; } - idx = Index_get_position(self, key); - if (idx == -1) - return -1; - - err = git_index_remove(self->index, idx); - if (err < 0) { - Error_set(err); - return -1; - } + if(Index_remove(self, Py_BuildValue("(N)", key)) == NULL) + return -1; return 0; } @@ -331,7 +338,7 @@ Index_read_tree(Index *self, PyObject *value) if (err < 0) return Error_set(err); - err = git_index_read_tree(self->index, tree, NULL); + err = git_index_read_tree(self->index, tree); if (err < 0) return Error_set(err); @@ -344,7 +351,7 @@ Index_write_tree(Index *self) git_oid oid; int err; - err = git_tree_create_fromindex(&oid, self->index); + err = git_index_write_tree(&oid, self->index); if (err < 0) return Error_set(err); @@ -354,6 +361,8 @@ Index_write_tree(Index *self) PyMethodDef Index_methods[] = { {"add", (PyCFunction)Index_add, METH_VARARGS, "Add or update an index entry from a file in disk."}, + {"remove", (PyCFunction)Index_remove, METH_VARARGS, + "Removes an entry from index."}, {"clear", (PyCFunction)Index_clear, METH_NOARGS, "Clear the contents (all the entries) of an index object."}, {"diff", (PyCFunction)Index_diff_tree, METH_VARARGS, @@ -447,7 +456,7 @@ IndexIter_iternext(IndexIter *self) { git_index_entry *index_entry; - index_entry = git_index_get(self->owner->index, self->i); + index_entry = git_index_get_byindex(self->owner->index, self->i); if (!index_entry) return NULL; diff --git a/test/test_config.py b/test/test_config.py index c2bd9f2..8656436 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -39,6 +39,7 @@ config_filename = "test_config" def foreach_test_wrapper(key, name, lst): lst[key] = name return 0 +foreach_test_wrapper.__test__ = False class ConfigTest(utils.RepoTestCase): @@ -150,7 +151,7 @@ class ConfigTest(utils.RepoTestCase): new_file.write("[this]\n\tthat = foobar\n\tthat = foobeer\n") new_file.close() - config.add_file(config_filename, 0) + config.add_file(config_filename, 5) self.assertTrue('this.that' in config) config.set_multivar('this.that', '^.*beer', 'fool') l = config.get_multivar('this.that', 'fool') diff --git a/test/test_index.py b/test/test_index.py index da77840..8950cbc 100644 --- a/test/test_index.py +++ b/test/test_index.py @@ -80,7 +80,7 @@ class IndexTest(utils.RepoTestCase): def test_write(self): index = self.repo.index - index.add('bye.txt', 0) + index.add('bye.txt') index.write() index.clear() @@ -133,7 +133,15 @@ class IndexTest(utils.RepoTestCase): self.assertEqual([x.hex for x in index], [x.hex for x in self.repo.index]) - self.assertRaises(pygit2.GitError, lambda: index.add('bye.txt', 0)) + self.assertRaises(pygit2.GitError, lambda: index.add('bye.txt')) + + def test_del(self): + index = self.repo.index + del index['hello.txt'] + + def test_remove(self): + index = self.repo.index + index.remove('hello.txt') if __name__ == '__main__': unittest.main()