Return the created entry in git_tree_add_entry() Yes, we are breaking the API. Alpha software, deal with it. We need a way of getting a pointer to each newly added entry to the index, because manually looking up the entry after creation is outrageously expensive. Signed-off-by: Vicent Marti <tanoku@gmail.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
diff --git a/src/git2/tree.h b/src/git2/tree.h
index 3e003de..9ebb265 100644
--- a/src/git2/tree.h
+++ b/src/git2/tree.h
@@ -132,18 +132,21 @@ GIT_EXTERN(const git_oid *) git_tree_entry_id(git_tree_entry *entry);
GIT_EXTERN(int) git_tree_entry_2object(git_object **object, git_tree_entry *entry);
/**
- * Add a new entry to a tree.
+ * Add a new entry to a tree and return the new entry.
*
* This will mark the tree as modified; the new entry will
* be written back to disk on the next git_object_write()
*
+ * @param entry_out Pointer to the entry that just got
+ * created. May be NULL if you are not interested on
+ * getting the new entry
* @param tree Tree object to store the entry
* @iparam id OID for the tree entry
* @param filename Filename for the tree entry
* @param attributes UNIX file attributes for the entry
* @return 0 on success; otherwise error code
*/
-GIT_EXTERN(int) git_tree_add_entry(git_tree *tree, const git_oid *id, const char *filename, int attributes);
+GIT_EXTERN(int) git_tree_add_entry(git_tree_entry **entry_out, git_tree *tree, const git_oid *id, const char *filename, int attributes);
/**
* Remove an entry by its index.
diff --git a/src/tree.c b/src/tree.c
index d160a79..dffe872 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -174,7 +174,7 @@ size_t git_tree_entrycount(git_tree *tree)
return tree->entries.length;
}
-int git_tree_add_entry(git_tree *tree, const git_oid *id, const char *filename, int attributes)
+int git_tree_add_entry(git_tree_entry **entry_out, git_tree *tree, const git_oid *id, const char *filename, int attributes)
{
git_tree_entry *entry;
@@ -195,6 +195,9 @@ int git_tree_add_entry(git_tree *tree, const git_oid *id, const char *filename,
git_vector_sort(&tree->entries);
+ if (entry_out != NULL)
+ *entry_out = entry;
+
tree->object.modified = 1;
return GIT_SUCCESS;
}
diff --git a/tests/t0902-modify.c b/tests/t0902-modify.c
index 893b9b3..870e07b 100644
--- a/tests/t0902-modify.c
+++ b/tests/t0902-modify.c
@@ -22,8 +22,11 @@ BEGIN_TEST(tree_in_memory_add_test)
git_oid_mkstr(&entry_id, tree_oid);
for (i = 0; i < entry_count; ++i) {
char filename[32];
+ git_tree_entry *ent = NULL;
+
sprintf(filename, "file%d.txt", i);
- must_pass(git_tree_add_entry(tree, &entry_id, filename, 040000));
+ must_pass(git_tree_add_entry(&ent, tree, &entry_id, filename, 040000));
+ must_be_true(ent != NULL);
}
must_be_true(git_tree_entrycount(tree) == entry_count);
@@ -51,8 +54,10 @@ BEGIN_TEST(tree_add_entry_test)
must_be_true(git_tree_entrycount(tree) == 3);
- git_tree_add_entry(tree, &id, "zzz_test_entry.dat", 0);
- git_tree_add_entry(tree, &id, "01_test_entry.txt", 0);
+ /* check there is NP if we don't want the
+ * created entry back */
+ git_tree_add_entry(NULL, tree, &id, "zzz_test_entry.dat", 0);
+ git_tree_add_entry(NULL, tree, &id, "01_test_entry.txt", 0);
must_be_true(git_tree_entrycount(tree) == 5);