Skip to content

Add flexibility when dealing with index entries #325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/working-copy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Index write::
>>> del index['path/to/file'] # git rm
>>> index.write() # don't forget to save the changes

Custom entries::
>>> entry = pygit2.IndexEntry('README.md', blob_id, blob_filemode)
>>> repo.index.add(entry)

The Index type
====================
Expand Down
126 changes: 110 additions & 16 deletions src/index.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ extern PyTypeObject TreeType;
extern PyTypeObject DiffType;
extern PyTypeObject IndexIterType;
extern PyTypeObject IndexEntryType;
extern PyTypeObject OidType;
extern PyTypeObject RepositoryType;

int
Index_init(Index *self, PyObject *args, PyObject *kwds)
Expand Down Expand Up @@ -81,7 +83,7 @@ Index_traverse(Index *self, visitproc visit, void *arg)


PyDoc_STRVAR(Index_add__doc__,
"add(path)\n"
"add([path|entry])\n"
"\n"
"Add or update an index entry from a file in disk.");

Expand All @@ -90,6 +92,16 @@ Index_add(Index *self, PyObject *args)
{
int err;
const char *path;
IndexEntry *py_entry;

if (PyArg_ParseTuple(args, "O!", &IndexEntryType, &py_entry)) {
err = git_index_add(self->index, &py_entry->entry);
if (err < 0)
return Error_set(err);

Py_RETURN_NONE;
}
PyErr_Clear();

if (!PyArg_ParseTuple(args, "s", &path))
return NULL;
Expand Down Expand Up @@ -314,8 +326,15 @@ wrap_index_entry(const git_index_entry *entry, Index *index)
IndexEntry *py_entry;

py_entry = PyObject_New(IndexEntry, &IndexEntryType);
if (py_entry)
py_entry->entry = entry;
if (!py_entry)
return NULL;

memcpy(&py_entry->entry, entry, sizeof(struct git_index_entry));
py_entry->entry.path = strdup(entry->path);
if (!py_entry->entry.path) {
Py_CLEAR(py_entry);
return NULL;
}

return (PyObject*)py_entry;
}
Expand Down Expand Up @@ -410,17 +429,26 @@ Index_read_tree(Index *self, PyObject *value)


PyDoc_STRVAR(Index_write_tree__doc__,
"write_tree() -> Oid\n"
"write_tree([repo]) -> Oid\n"
"\n"
"Create a tree object from the index file, return its oid.");
"Create a tree object from the index file, return its oid.\n"
"If 'repo' is passed, write to that repository's odb.");

PyObject *
Index_write_tree(Index *self)
Index_write_tree(Index *self, PyObject *args)
{
git_oid oid;
Repository *repo = NULL;
int err;

err = git_index_write_tree(&oid, self->index);
if (!PyArg_ParseTuple(args, "|O!", &RepositoryType, &repo))
return NULL;

if (repo)
err = git_index_write_tree_to(&oid, self->index, repo->repo);
else
err = git_index_write_tree(&oid, self->index);

if (err < 0)
return Error_set(err);

Expand All @@ -437,7 +465,7 @@ PyMethodDef Index_methods[] = {
METHOD(Index, read, METH_VARARGS),
METHOD(Index, write, METH_NOARGS),
METHOD(Index, read_tree, METH_O),
METHOD(Index, write_tree, METH_NOARGS),
METHOD(Index, write_tree, METH_VARARGS),
{NULL}
};

Expand Down Expand Up @@ -557,6 +585,31 @@ PyTypeObject IndexIterType = {
(iternextfunc)IndexIter_iternext, /* tp_iternext */
};

int
IndexEntry_init(IndexEntry *self, PyObject *args, PyObject *kwds)
{
char *c_path = NULL;
Oid *id = NULL;
unsigned int mode;
char *keywords[] = {"path", "oid", "mode", NULL};

if (!PyArg_ParseTupleAndKeywords(args, kwds, "sO!I", keywords,
&c_path, &OidType, &id, &mode))
return -1;

memset(&self->entry, 0, sizeof(struct git_index_entry));
if (c_path)
self->entry.path = c_path;

if (id)
git_oid_cpy(&self->entry.oid, &id->oid);

if (mode)
self->entry.mode = mode;

return 0;
}

void
IndexEntry_dealloc(IndexEntry *self)
{
Expand All @@ -569,40 +622,81 @@ PyDoc_STRVAR(IndexEntry_mode__doc__, "Mode.");
PyObject *
IndexEntry_mode__get__(IndexEntry *self)
{
return PyLong_FromLong(self->entry->mode);
return PyLong_FromLong(self->entry.mode);
}

int
IndexEntry_mode__set__(IndexEntry *self, PyObject *py_mode)
{
long c_val;

c_val = PyLong_AsLong(py_mode);
if (c_val == -1 && PyErr_Occurred())
return -1;

self->entry.mode = (unsigned int) c_val;

return 0;
}

PyDoc_STRVAR(IndexEntry_path__doc__, "Path.");

PyObject *
IndexEntry_path__get__(IndexEntry *self)
{
return to_path(self->entry->path);
return to_path(self->entry.path);
}

int
IndexEntry_path__set__(IndexEntry *self, PyObject *py_path)
{
char *c_inner, *c_path;

c_inner = py_str_to_c_str(py_path, NULL);
if (!c_inner)
return -1;

c_path = strdup(c_inner);
if (!c_path) {
PyErr_NoMemory();
return -1;
}

free(self->entry.path);
self->entry.path = c_path;

return 0;
}

PyDoc_STRVAR(IndexEntry_oid__doc__, "Object id.");

PyObject *
IndexEntry_oid__get__(IndexEntry *self)
{
return git_oid_to_python(&self->entry->oid);
return git_oid_to_python(&self->entry.oid);
}

int
IndexEntry_oid__set__(IndexEntry *self, PyObject *py_id)
{
if (!py_oid_to_git_oid(py_id, &self->entry.oid))
return -1;

return 0;
}

PyDoc_STRVAR(IndexEntry_hex__doc__, "Hex id.");

PyObject *
IndexEntry_hex__get__(IndexEntry *self)
{
return git_oid_to_py_str(&self->entry->oid);
return git_oid_to_py_str(&self->entry.oid);
}

PyGetSetDef IndexEntry_getseters[] = {
GETTER(IndexEntry, mode),
GETTER(IndexEntry, path),
GETTER(IndexEntry, oid),
GETSET(IndexEntry, mode),
GETSET(IndexEntry, path),
GETSET(IndexEntry, oid),
GETTER(IndexEntry, hex),
{NULL},
};
Expand Down Expand Up @@ -645,7 +739,7 @@ PyTypeObject IndexEntryType = {
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
(initproc)IndexEntry_init, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
};
2 changes: 1 addition & 1 deletion src/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ PyObject* Index_write(Index *self);
PyObject* Index_iter(Index *self);
PyObject* Index_getitem(Index *self, PyObject *value);
PyObject* Index_read_tree(Index *self, PyObject *value);
PyObject* Index_write_tree(Index *self);
PyObject* Index_write_tree(Index *self, PyObject *args);
Py_ssize_t Index_len(Index *self);
int Index_setitem(Index *self, PyObject *key, PyObject *value);

Expand Down
2 changes: 1 addition & 1 deletion src/pygit2.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ moduleinit(PyObject* m)
* Index & Working copy
*/
INIT_TYPE(IndexType, NULL, PyType_GenericNew)
INIT_TYPE(IndexEntryType, NULL, NULL)
INIT_TYPE(IndexEntryType, NULL, PyType_GenericNew)
INIT_TYPE(IndexIterType, NULL, NULL)
ADD_TYPE(m, Index)
ADD_TYPE(m, IndexEntry)
Expand Down
2 changes: 1 addition & 1 deletion src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ SIMPLE_TYPE(Index, git_index, index)

typedef struct {
PyObject_HEAD
const git_index_entry *entry;
git_index_entry entry;
} IndexEntry;

typedef struct {
Expand Down
31 changes: 31 additions & 0 deletions test/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from __future__ import unicode_literals
import os
import unittest
import tempfile

import pygit2
from . import utils
Expand Down Expand Up @@ -139,6 +140,36 @@ def test_remove(self):
index.remove('hello.txt')
self.assertFalse('hello.txt' in index)

def test_change_attributes(self):
index = self.repo.index
entry = index['hello.txt']
ign_entry = index['.gitignore']
self.assertNotEqual(ign_entry.oid, entry.oid)
self.assertNotEqual(entry.mode, pygit2.GIT_FILEMODE_BLOB_EXECUTABLE)
entry.path = 'foo.txt'
entry.oid = ign_entry.oid
entry.mode = pygit2.GIT_FILEMODE_BLOB_EXECUTABLE
self.assertEqual('foo.txt', entry.path)
self.assertEqual(ign_entry.oid, entry.oid)
self.assertEqual(pygit2.GIT_FILEMODE_BLOB_EXECUTABLE, entry.mode)

def test_write_tree_to(self):
path = tempfile.mkdtemp()
pygit2.init_repository(path)
nrepo = pygit2.Repository(path)

id = self.repo.index.write_tree(nrepo)
self.assertNotEqual(None, nrepo[id])

class IndexEntryTest(utils.RepoTestCase):

def test_create_entry(self):
index = self.repo.index
hello_entry = index['hello.txt']
entry = pygit2.IndexEntry('README.md', hello_entry.oid, hello_entry.mode)
index.add(entry)
tree_id = index.write_tree()
self.assertEqual('60e769e57ae1d6a2ab75d8d253139e6260e1f912', str(tree_id))

if __name__ == '__main__':
unittest.main()