Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Index: accept a tree for read_tree()
An index may not have an associated repository, so giving it an id in
that case is useless. Raise an error in that case and accept a Tree
object to make the function useful then.
  • Loading branch information
carlosmn committed May 27, 2014
commit 9e91a390cc39625ebc72d609e1be6d04c71fab25
8 changes: 8 additions & 0 deletions docs/working-copy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ Custom entries::
>>> entry = pygit2.IndexEntry('README.md', blob_id, blob_filemode)
>>> repo.index.add(entry)

The index fulfills a dual role as the in-memory representation of the
index file and data structure which represents a flat list of a
tree. You can use it independently of the index file, e.g.

>>> index = pygit2.Index()
>>> entry = pygit2.IndexEntry('README.md', blob_id, blob_filemode)
>>> index.add(entry)

The Index type
====================

Expand Down
38 changes: 29 additions & 9 deletions src/index.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,26 +426,46 @@ Index_remove(Index *self, PyObject *args)
PyDoc_STRVAR(Index_read_tree__doc__,
"read_tree(tree)\n"
"\n"
"Update the index file from the tree identified by the given oid.");
"Update the index file from the specified tree. The tree can be a Tree object or an Oid.\n"
"Using an Oid is only possible if this index is associated with a repository");

PyObject *
Index_read_tree(Index *self, PyObject *value)
{
git_oid oid;
git_tree *tree;
int err;
git_tree *tree = NULL;
int err, need_free = 0;
size_t len;

len = py_oid_to_git_oid(value, &oid);
if (len == 0)
return NULL;
if (len == 0) {
Tree *py_tree;
if (!PyObject_TypeCheck(value, &TreeType)) {
return NULL;
}

err = git_tree_lookup_prefix(&tree, self->repo->repo, &oid, len);
if (err < 0)
return Error_set(err);
PyErr_Clear();
py_tree = (Tree *) value;
tree = py_tree->tree;
}

/*
* if the user passed in an id but we're not associated with a
* repo, we can't do anything
*/
if (tree == NULL && self->repo == NULL) {
PyErr_SetString(PyExc_TypeError, "id given but no associated repository");
return NULL;
} else if (tree == NULL) {
need_free = 1;
err = git_tree_lookup_prefix(&tree, self->repo->repo, &oid, len);
if (err < 0)
return Error_set(err);
}

err = git_index_read_tree(self->index, tree);
git_tree_free(tree);
if (need_free)
git_tree_free(tree);
if (err < 0)
return Error_set(err);

Expand Down