commit: add test to ensure predictability of generation of commit, tree and blob object ids
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
diff --git a/tests-clay/clay.h b/tests-clay/clay.h
index bdb11e8..3f09d4f 100644
--- a/tests-clay/clay.h
+++ b/tests-clay/clay.h
@@ -129,6 +129,9 @@ extern void test_network_remotes__initialize(void);
extern void test_network_remotes__parsing(void);
extern void test_network_remotes__refspec_parsing(void);
extern void test_network_remotes__transform(void);
+extern void test_object_commit_commitstagedfile__cleanup(void);
+extern void test_object_commit_commitstagedfile__generate_predictable_object_ids(void);
+extern void test_object_commit_commitstagedfile__initialize(void);
extern void test_object_raw_chars__build_valid_oid_from_raw_bytes(void);
extern void test_object_raw_chars__find_invalid_chars_in_oid(void);
extern void test_object_raw_compare__compare_allocfmt_oids(void);
@@ -159,9 +162,6 @@ extern void test_object_raw_size__validate_oid_size(void);
extern void test_object_raw_type2string__check_type_is_loose(void);
extern void test_object_raw_type2string__convert_string_to_type(void);
extern void test_object_raw_type2string__convert_type_to_string(void);
-extern void test_object_tree_buildfromindex__cleanup(void);
-extern void test_object_tree_buildfromindex__generate_predictable_object_ids(void);
-extern void test_object_tree_buildfromindex__initialize(void);
extern void test_object_tree_diff__addition(void);
extern void test_object_tree_diff__cleanup(void);
extern void test_object_tree_diff__deletion(void);
diff --git a/tests-clay/clay_main.c b/tests-clay/clay_main.c
index 93e15f4..ce3b66b 100644
--- a/tests-clay/clay_main.c
+++ b/tests-clay/clay_main.c
@@ -200,6 +200,9 @@ static const struct clay_func _clay_cb_network_remotes[] = {
{"refspec_parsing", &test_network_remotes__refspec_parsing},
{"transform", &test_network_remotes__transform}
};
+static const struct clay_func _clay_cb_object_commit_commitstagedfile[] = {
+ {"generate_predictable_object_ids", &test_object_commit_commitstagedfile__generate_predictable_object_ids}
+};
static const struct clay_func _clay_cb_object_raw_chars[] = {
{"build_valid_oid_from_raw_bytes", &test_object_raw_chars__build_valid_oid_from_raw_bytes},
{"find_invalid_chars_in_oid", &test_object_raw_chars__find_invalid_chars_in_oid}
@@ -246,9 +249,6 @@ static const struct clay_func _clay_cb_object_raw_type2string[] = {
{"convert_string_to_type", &test_object_raw_type2string__convert_string_to_type},
{"convert_type_to_string", &test_object_raw_type2string__convert_type_to_string}
};
-static const struct clay_func _clay_cb_object_tree_buildfromindex[] = {
- {"generate_predictable_object_ids", &test_object_tree_buildfromindex__generate_predictable_object_ids}
-};
static const struct clay_func _clay_cb_object_tree_diff[] = {
{"addition", &test_object_tree_diff__addition},
{"deletion", &test_object_tree_diff__deletion},
@@ -400,6 +400,12 @@ static const struct clay_suite _clay_suites[] = {
_clay_cb_network_remotes, 4
},
{
+ "object::commit::commitstagedfile",
+ {"initialize", &test_object_commit_commitstagedfile__initialize},
+ {"cleanup", &test_object_commit_commitstagedfile__cleanup},
+ _clay_cb_object_commit_commitstagedfile, 1
+ },
+ {
"object::raw::chars",
{NULL, NULL},
{NULL, NULL},
@@ -448,12 +454,6 @@ static const struct clay_suite _clay_suites[] = {
_clay_cb_object_raw_type2string, 3
},
{
- "object::tree::buildfromindex",
- {"initialize", &test_object_tree_buildfromindex__initialize},
- {"cleanup", &test_object_tree_buildfromindex__cleanup},
- _clay_cb_object_tree_buildfromindex, 1
- },
- {
"object::tree::diff",
{"initialize", &test_object_tree_diff__initialize},
{"cleanup", &test_object_tree_diff__cleanup},
diff --git a/tests-clay/object/commit/commitstagedfile.c b/tests-clay/object/commit/commitstagedfile.c
new file mode 100644
index 0000000..b673005
--- /dev/null
+++ b/tests-clay/object/commit/commitstagedfile.c
@@ -0,0 +1,118 @@
+#include "clay_libgit2.h"
+#include "posix.h"
+
+static git_repository *repo;
+
+static void file_create(const char *filename, const char *content)
+{
+ int fd;
+
+ fd = p_creat(filename, 0666);
+ cl_assert(fd != 0);
+ cl_git_pass(p_write(fd, content, strlen(content)));
+ cl_git_pass(p_close(fd));
+}
+
+void test_object_commit_commitstagedfile__initialize(void)
+{
+ cl_fixture("treebuilder");
+ cl_git_pass(git_repository_init(&repo, "treebuilder/", 0));
+ cl_git_pass(git_repository_open(&repo, "treebuilder/.git"));
+ cl_assert(repo != NULL);
+}
+
+void test_object_commit_commitstagedfile__cleanup(void)
+{
+ git_repository_free(repo);
+ cl_fixture_cleanup("treebuilder");
+}
+
+void test_object_commit_commitstagedfile__generate_predictable_object_ids(void)
+{
+ git_index *index;
+ git_index_entry *entry;
+ git_oid expected_blob_oid, tree_oid, expected_tree_oid, commit_oid, expected_commit_oid;
+ git_signature *signature;
+ git_tree *tree;
+
+ /*
+ * The test below replicates the following git scenario
+ *
+ * $ echo "test" > test.txt
+ * $ git hash-object test.txt
+ * 9daeafb9864cf43055ae93beb0afd6c7d144bfa4
+ *
+ * $ git add .
+ * $ git commit -m "Initial commit"
+ *
+ * $ git log
+ * commit 1fe3126578fc4eca68c193e4a3a0a14a0704624d
+ * Author: nulltoken <emeric.fermas@gmail.com>
+ * Date: Wed Dec 14 08:29:03 2011 +0100
+ *
+ * Initial commit
+ *
+ * $ git show 1fe3 --format=raw
+ * commit 1fe3126578fc4eca68c193e4a3a0a14a0704624d
+ * tree 2b297e643c551e76cfa1f93810c50811382f9117
+ * author nulltoken <emeric.fermas@gmail.com> 1323847743 +0100
+ * committer nulltoken <emeric.fermas@gmail.com> 1323847743 +0100
+ *
+ * Initial commit
+ *
+ * diff --git a/test.txt b/test.txt
+ * new file mode 100644
+ * index 0000000..9daeafb
+ * --- /dev/null
+ * +++ b/test.txt
+ * @@ -0,0 +1 @@
+ * +test
+ *
+ * $ git ls-tree 2b297
+ * 100644 blob 9daeafb9864cf43055ae93beb0afd6c7d144bfa4 test.txt
+ */
+
+ cl_git_pass(git_oid_fromstr(&expected_commit_oid, "1fe3126578fc4eca68c193e4a3a0a14a0704624d"));
+ cl_git_pass(git_oid_fromstr(&expected_tree_oid, "2b297e643c551e76cfa1f93810c50811382f9117"));
+ cl_git_pass(git_oid_fromstr(&expected_blob_oid, "9daeafb9864cf43055ae93beb0afd6c7d144bfa4"));
+
+ /*
+ * Add a new file to the index
+ */
+ file_create("treebuilder/test.txt", "test\n");
+ cl_git_pass(git_repository_index(&index, repo));
+ cl_git_pass(git_index_add(index, "test.txt", 0));
+
+ entry = git_index_get(index, 0);
+
+ cl_assert(git_oid_cmp(&expected_blob_oid, &entry->oid) == 0);
+
+ /*
+ * Build the tree from the index
+ */
+ cl_git_pass(git_tree_create_fromindex(&tree_oid, index));
+
+ cl_assert(git_oid_cmp(&expected_tree_oid, &tree_oid) == 0);
+
+ /*
+ * Commit the staged file
+ */
+ cl_git_pass(git_signature_new(&signature, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60));
+ cl_git_pass(git_tree_lookup(&tree, repo, &tree_oid));
+ cl_git_pass(git_commit_create_v(
+ &commit_oid,
+ repo,
+ "HEAD",
+ signature,
+ signature,
+ NULL,
+ "Initial commit\n", // Note: the trailing linefeed is mandatory to replicate git behavior
+ tree,
+ 0));
+
+ cl_assert(git_oid_cmp(&expected_commit_oid, &commit_oid) == 0);
+
+ git_signature_free(signature);
+ git_tree_free(tree);
+ git_index_free(index);
+}
diff --git a/tests-clay/object/tree/buildfromindex.c b/tests-clay/object/tree/buildfromindex.c
deleted file mode 100644
index d4a5206..0000000
--- a/tests-clay/object/tree/buildfromindex.c
+++ /dev/null
@@ -1,60 +0,0 @@
-#include "clay_libgit2.h"
-#include "posix.h"
-
-static git_repository *repo;
-
-static void file_create(const char *filename, const char *content)
-{
- int fd;
-
- fd = p_creat(filename, 0666);
- cl_assert(fd != 0);
- cl_git_pass(p_write(fd, content, strlen(content)));
- cl_git_pass(p_close(fd));
-}
-
-void test_object_tree_buildfromindex__initialize(void)
-{
- cl_fixture("treebuilder");
- cl_git_pass(git_repository_init(&repo, "treebuilder/", 0));
- cl_git_pass(git_repository_open(&repo, "treebuilder/.git"));
- cl_assert(repo != NULL);
-}
-
-void test_object_tree_buildfromindex__cleanup(void)
-{
- git_repository_free(repo);
- cl_fixture_cleanup("treebuilder");
-}
-
-void test_object_tree_buildfromindex__generate_predictable_object_ids(void)
-{
- git_index *index;
- git_oid blob_oid, tree_oid, expected_tree_oid;
- git_index_entry *entry;
-
- /*
- * Add a new file to the index
- */
- cl_git_pass(git_repository_index(&index, repo));
-
- file_create("treebuilder/test.txt", "test\n");
- cl_git_pass(git_index_add(index, "test.txt", 0));
-
- entry = git_index_get(index, 0);
-
- /* $ echo "test" | git hash-object --stdin */
- cl_git_pass(git_oid_fromstr(&blob_oid, "9daeafb9864cf43055ae93beb0afd6c7d144bfa4"));
-
- cl_assert(git_oid_cmp(&blob_oid, &entry->oid) == 0);
-
- /*
- * Build the tree from the index
- */
- cl_git_pass(git_tree_create_fromindex(&tree_oid, index));
-
- cl_git_pass(git_oid_fromstr(&expected_tree_oid, "2b297e643c551e76cfa1f93810c50811382f9117"));
- cl_assert(git_oid_cmp(&expected_tree_oid, &tree_oid) == 0);
-
- git_index_free(index);
-}