Add git_treebuilder_insert test and clarify doc This wasn't being tested and since it has a callback, I fixed it even though the return value of this callback is not treated like any of the other callbacks in the API.
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
diff --git a/include/git2/tree.h b/include/git2/tree.h
index d94b446..4223656 100644
--- a/include/git2/tree.h
+++ b/include/git2/tree.h
@@ -332,11 +332,18 @@ GIT_EXTERN(int) git_treebuilder_insert(
GIT_EXTERN(int) git_treebuilder_remove(
git_treebuilder *bld, const char *filename);
+/**
+ * Callback for git_treebuilder_filter
+ *
+ * The return value is treated as a boolean, with zero indicating that the
+ * entry should be left alone and any non-zero value meaning that the
+ * entry should be removed from the treebuilder list (i.e. filtered out).
+ */
typedef int (*git_treebuilder_filter_cb)(
const git_tree_entry *entry, void *payload);
/**
- * Filter the entries in the tree
+ * Selectively remove entries in the tree
*
* The `filter` callback will be called for each entry in the tree with a
* pointer to the entry and the provided `payload`; if the callback returns
@@ -344,7 +351,7 @@ typedef int (*git_treebuilder_filter_cb)(
*
* @param bld Tree builder
* @param filter Callback to filter entries
- * @param payload Extra data to pass to filter
+ * @param payload Extra data to pass to filter callback
*/
GIT_EXTERN(void) git_treebuilder_filter(
git_treebuilder *bld,
diff --git a/tests/object/tree/write.c b/tests/object/tree/write.c
index 468c0cc..3bea0ed 100644
--- a/tests/object/tree/write.c
+++ b/tests/object/tree/write.c
@@ -164,24 +164,25 @@ void test_object_tree_write__sorted_subtrees(void)
git_treebuilder_free(builder);
}
+static struct {
+ unsigned int attr;
+ const char *filename;
+} _entries[] = {
+ { GIT_FILEMODE_BLOB, "aardvark" },
+ { GIT_FILEMODE_BLOB, ".first" },
+ { GIT_FILEMODE_BLOB, "apple" },
+ { GIT_FILEMODE_BLOB, "last"},
+ { GIT_FILEMODE_BLOB, "apple_after"},
+ { GIT_FILEMODE_BLOB, "after_aardvark"},
+ { 0, NULL },
+};
+
void test_object_tree_write__removing_and_re_adding_in_treebuilder(void)
{
git_treebuilder *builder;
- int i, aardvark_i, apple_i, apple_after_i, apple_extra_i, last_i;
+ int i, aardvark_i, apple_i, apple_after_i, apple_extra_i, last_i;
git_oid blank_oid, tree_oid;
git_tree *tree;
- struct {
- unsigned int attr;
- const char *filename;
- } entries[] = {
- { GIT_FILEMODE_BLOB, "aardvark" },
- { GIT_FILEMODE_BLOB, ".first" },
- { GIT_FILEMODE_BLOB, "apple" },
- { GIT_FILEMODE_BLOB, "last"},
- { GIT_FILEMODE_BLOB, "apple_after"},
- { GIT_FILEMODE_BLOB, "after_aardvark"},
- { 0, NULL },
- };
memset(&blank_oid, 0x0, sizeof(blank_oid));
@@ -189,9 +190,9 @@ void test_object_tree_write__removing_and_re_adding_in_treebuilder(void)
cl_assert_equal_i(0, (int)git_treebuilder_entrycount(builder));
- for (i = 0; entries[i].filename; ++i)
+ for (i = 0; _entries[i].filename; ++i)
cl_git_pass(git_treebuilder_insert(NULL,
- builder, entries[i].filename, &blank_oid, entries[i].attr));
+ builder, _entries[i].filename, &blank_oid, _entries[i].attr));
cl_assert_equal_i(6, (int)git_treebuilder_entrycount(builder));
@@ -260,3 +261,56 @@ void test_object_tree_write__removing_and_re_adding_in_treebuilder(void)
git_tree_free(tree);
}
+
+static int treebuilder_filter_prefixed(
+ const git_tree_entry *entry, void *payload)
+{
+ return !git__prefixcmp(git_tree_entry_name(entry), payload);
+}
+
+void test_object_tree_write__filtering(void)
+{
+ git_treebuilder *builder;
+ int i;
+ git_oid blank_oid, tree_oid;
+ git_tree *tree;
+
+ memset(&blank_oid, 0x0, sizeof(blank_oid));
+
+ cl_git_pass(git_treebuilder_create(&builder, NULL));
+
+ for (i = 0; _entries[i].filename; ++i)
+ cl_git_pass(git_treebuilder_insert(NULL,
+ builder, _entries[i].filename, &blank_oid, _entries[i].attr));
+
+ cl_assert_equal_i(6, (int)git_treebuilder_entrycount(builder));
+
+ cl_assert(git_treebuilder_get(builder, "apple") != NULL);
+ cl_assert(git_treebuilder_get(builder, "aardvark") != NULL);
+ cl_assert(git_treebuilder_get(builder, "last") != NULL);
+
+ git_treebuilder_filter(builder, treebuilder_filter_prefixed, "apple");
+
+ cl_assert_equal_i(4, (int)git_treebuilder_entrycount(builder));
+
+ cl_assert(git_treebuilder_get(builder, "apple") == NULL);
+ cl_assert(git_treebuilder_get(builder, "aardvark") != NULL);
+ cl_assert(git_treebuilder_get(builder, "last") != NULL);
+
+ git_treebuilder_filter(builder, treebuilder_filter_prefixed, "a");
+
+ cl_assert_equal_i(2, (int)git_treebuilder_entrycount(builder));
+
+ cl_assert(git_treebuilder_get(builder, "aardvark") == NULL);
+ cl_assert(git_treebuilder_get(builder, "last") != NULL);
+
+ cl_git_pass(git_treebuilder_write(&tree_oid, g_repo, builder));
+
+ git_treebuilder_free(builder);
+
+ cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_oid));
+
+ cl_assert_equal_i(2, (int)git_tree_entrycount(tree));
+
+ git_tree_free(tree);
+}