1 #include "clar_libgit2.h"
4 static git_repository
*repo
;
6 void test_object_commit_commitstagedfile__initialize(void)
8 cl_fixture("treebuilder");
9 cl_git_pass(git_repository_init(&repo
, "treebuilder/", 0));
10 cl_assert(repo
!= NULL
);
13 void test_object_commit_commitstagedfile__cleanup(void)
15 git_repository_free(repo
);
18 cl_fixture_cleanup("treebuilder");
21 void test_object_commit_commitstagedfile__generate_predictable_object_ids(void)
24 const git_index_entry
*entry
;
25 git_oid expected_blob_oid
, tree_oid
, expected_tree_oid
, commit_oid
, expected_commit_oid
;
26 git_signature
*signature
;
28 git_buf buffer
= GIT_BUF_INIT
;
31 * The test below replicates the following git scenario
33 * $ echo "test" > test.txt
34 * $ git hash-object test.txt
35 * 9daeafb9864cf43055ae93beb0afd6c7d144bfa4
38 * $ git commit -m "Initial commit"
41 * commit 1fe3126578fc4eca68c193e4a3a0a14a0704624d
42 * Author: nulltoken <emeric.fermas@gmail.com>
43 * Date: Wed Dec 14 08:29:03 2011 +0100
47 * $ git show 1fe3 --format=raw
48 * commit 1fe3126578fc4eca68c193e4a3a0a14a0704624d
49 * tree 2b297e643c551e76cfa1f93810c50811382f9117
50 * author nulltoken <emeric.fermas@gmail.com> 1323847743 +0100
51 * committer nulltoken <emeric.fermas@gmail.com> 1323847743 +0100
55 * diff --git a/test.txt b/test.txt
56 * new file mode 100644
57 * index 0000000..9daeafb
64 * 100644 blob 9daeafb9864cf43055ae93beb0afd6c7d144bfa4 test.txt
67 cl_git_pass(git_oid_fromstr(&expected_commit_oid
, "1fe3126578fc4eca68c193e4a3a0a14a0704624d"));
68 cl_git_pass(git_oid_fromstr(&expected_tree_oid
, "2b297e643c551e76cfa1f93810c50811382f9117"));
69 cl_git_pass(git_oid_fromstr(&expected_blob_oid
, "9daeafb9864cf43055ae93beb0afd6c7d144bfa4"));
72 * Add a new file to the index
74 cl_git_mkfile("treebuilder/test.txt", "test\n");
75 cl_git_pass(git_repository_index(&index
, repo
));
76 cl_git_pass(git_index_add_bypath(index
, "test.txt"));
78 entry
= git_index_get_byindex(index
, 0);
80 cl_assert(git_oid_cmp(&expected_blob_oid
, &entry
->id
) == 0);
83 * Information about index entry should match test file
87 cl_must_pass(p_lstat("treebuilder/test.txt", &st
));
88 cl_assert(entry
->file_size
== st
.st_size
);
91 * Windows doesn't populate these fields, and the signage is
92 * wrong in the Windows version of the struct, so lets avoid
93 * the "comparing signed and unsigned" compilation warning in
96 cl_assert(entry
->uid
== st
.st_uid
);
97 cl_assert(entry
->gid
== st
.st_gid
);
102 * Build the tree from the index
104 cl_git_pass(git_index_write_tree(&tree_oid
, index
));
106 cl_assert(git_oid_cmp(&expected_tree_oid
, &tree_oid
) == 0);
109 * Commit the staged file
111 cl_git_pass(git_signature_new(&signature
, "nulltoken", "emeric.fermas@gmail.com", 1323847743, 60));
112 cl_git_pass(git_tree_lookup(&tree
, repo
, &tree_oid
));
114 cl_git_pass(git_message_prettify(&buffer
, "Initial commit", 0, '#'));
116 cl_git_pass(git_commit_create_v(
127 cl_assert(git_oid_cmp(&expected_commit_oid
, &commit_oid
) == 0);
129 git_buf_dispose(&buffer
);
130 git_signature_free(signature
);
132 git_index_free(index
);
135 static void assert_commit_tree_has_n_entries(git_commit
*c
, int count
)
138 cl_git_pass(git_commit_tree(&tree
, c
));
139 cl_assert_equal_i(count
, git_tree_entrycount(tree
));
143 static void assert_commit_is_head_(git_commit
*c
, const char *file
, const char *func
, int line
)
146 cl_git_pass(git_revparse_single((git_object
**)&head
, repo
, "HEAD"));
147 clar__assert(git_oid_equal(git_commit_id(c
), git_commit_id(head
)), file
, func
, line
, "Commit is not the HEAD", NULL
, 1);
148 git_commit_free(head
);
150 #define assert_commit_is_head(C) assert_commit_is_head_((C),__FILE__,__func__,__LINE__)
152 void test_object_commit_commitstagedfile__amend_commit(void)
155 git_oid old_oid
, new_oid
, tree_oid
;
156 git_commit
*old_commit
, *new_commit
;
161 cl_git_mkfile("treebuilder/myfile", "This is a file\n");
162 cl_git_pass(git_repository_index(&index
, repo
));
163 cl_git_pass(git_index_add_bypath(index
, "myfile"));
164 cl_repo_commit_from_index(&old_oid
, repo
, NULL
, 0, "first commit");
166 cl_git_pass(git_commit_lookup(&old_commit
, repo
, &old_oid
));
168 cl_assert_equal_i(0, git_commit_parentcount(old_commit
));
169 assert_commit_tree_has_n_entries(old_commit
, 1);
170 assert_commit_is_head(old_commit
);
172 /* let's amend the message of the HEAD commit */
174 cl_git_pass(git_commit_amend(
175 &new_oid
, old_commit
, "HEAD", NULL
, NULL
, NULL
, "Initial commit", NULL
));
177 /* fail because the commit isn't the tip of the branch anymore */
178 cl_git_fail(git_commit_amend(
179 &new_oid
, old_commit
, "HEAD", NULL
, NULL
, NULL
, "Initial commit", NULL
));
181 cl_git_pass(git_commit_lookup(&new_commit
, repo
, &new_oid
));
183 cl_assert_equal_i(0, git_commit_parentcount(new_commit
));
184 assert_commit_tree_has_n_entries(new_commit
, 1);
185 assert_commit_is_head(new_commit
);
187 git_commit_free(old_commit
);
189 old_commit
= new_commit
;
191 /* let's amend the tree of that last commit */
193 cl_git_mkfile("treebuilder/anotherfile", "This is another file\n");
194 cl_git_pass(git_index_add_bypath(index
, "anotherfile"));
195 cl_git_pass(git_index_write_tree(&tree_oid
, index
));
196 cl_git_pass(git_tree_lookup(&tree
, repo
, &tree_oid
));
197 cl_assert_equal_i(2, git_tree_entrycount(tree
));
199 /* fail to amend on a ref which does not exist */
200 cl_git_fail_with(GIT_ENOTFOUND
, git_commit_amend(
201 &new_oid
, old_commit
, "refs/heads/nope", NULL
, NULL
, NULL
, "Initial commit", tree
));
203 cl_git_pass(git_commit_amend(
204 &new_oid
, old_commit
, "HEAD", NULL
, NULL
, NULL
, "Initial commit", tree
));
207 cl_git_pass(git_commit_lookup(&new_commit
, repo
, &new_oid
));
209 cl_assert_equal_i(0, git_commit_parentcount(new_commit
));
210 assert_commit_tree_has_n_entries(new_commit
, 2);
211 assert_commit_is_head(new_commit
);
215 git_commit_free(old_commit
);
216 git_commit_free(new_commit
);
217 git_index_free(index
);