Commit b29e8f19300bb95b9f8307f1438193271233adc8

Vicent Marti 2011-01-29T02:12:59

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>

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);