From 9c95cb056048c53efbcaf955c648d397770e227f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Tue, 21 Jan 2014 02:58:30 +0100 Subject: [PATCH] IndexEntry: keep a copy of the underlying git_index_entry The tree entries exist more or less independently of the index they were retrieved from. The idea behind them is that they can be kept by a binding without needing to refer to the original anymore, which may disappear at any moment if the index is modified. Keep a copy of the data in TreeEntry instead of pointing to the one retrieved from the index, which is not safe to keep around. --- src/index.c | 19 +++++++++++++------ src/types.h | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/index.c b/src/index.c index ce47f13..d9a12d8 100644 --- a/src/index.c +++ b/src/index.c @@ -314,8 +314,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; } @@ -569,7 +576,7 @@ PyDoc_STRVAR(IndexEntry_mode__doc__, "Mode."); PyObject * IndexEntry_mode__get__(IndexEntry *self) { - return PyLong_FromLong(self->entry->mode); + return PyLong_FromLong(self->entry.mode); } @@ -578,7 +585,7 @@ PyDoc_STRVAR(IndexEntry_path__doc__, "Path."); PyObject * IndexEntry_path__get__(IndexEntry *self) { - return to_path(self->entry->path); + return to_path(self->entry.path); } @@ -587,7 +594,7 @@ 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); } @@ -596,7 +603,7 @@ 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[] = { diff --git a/src/types.h b/src/types.h index 63a5672..1918c9d 100644 --- a/src/types.h +++ b/src/types.h @@ -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 {