]>
Commit | Line | Data |
---|---|---|
3fd1520c | 1 | #include "clar_libgit2.h" |
8fae76c5 | 2 | #include "posix.h" |
3 | ||
4 | static git_repository *repo; | |
5 | ||
8fae76c5 | 6 | void test_object_commit_commitstagedfile__initialize(void) |
7 | { | |
8 | cl_fixture("treebuilder"); | |
9 | cl_git_pass(git_repository_init(&repo, "treebuilder/", 0)); | |
8fae76c5 | 10 | cl_assert(repo != NULL); |
11 | } | |
12 | ||
13 | void test_object_commit_commitstagedfile__cleanup(void) | |
14 | { | |
15 | git_repository_free(repo); | |
9094d30b SC |
16 | repo = NULL; |
17 | ||
8fae76c5 | 18 | cl_fixture_cleanup("treebuilder"); |
19 | } | |
20 | ||
21 | void test_object_commit_commitstagedfile__generate_predictable_object_ids(void) | |
22 | { | |
23 | git_index *index; | |
f45d51ff | 24 | const git_index_entry *entry; |
8fae76c5 | 25 | git_oid expected_blob_oid, tree_oid, expected_tree_oid, commit_oid, expected_commit_oid; |
26 | git_signature *signature; | |
27 | git_tree *tree; | |
e579e0f7 | 28 | git_buf buffer = GIT_BUF_INIT; |
8fae76c5 | 29 | |
30 | /* | |
31 | * The test below replicates the following git scenario | |
32 | * | |
33 | * $ echo "test" > test.txt | |
34 | * $ git hash-object test.txt | |
35 | * 9daeafb9864cf43055ae93beb0afd6c7d144bfa4 | |
36 | * | |
37 | * $ git add . | |
38 | * $ git commit -m "Initial commit" | |
39 | * | |
40 | * $ git log | |
41 | * commit 1fe3126578fc4eca68c193e4a3a0a14a0704624d | |
42 | * Author: nulltoken <emeric.fermas@gmail.com> | |
43 | * Date: Wed Dec 14 08:29:03 2011 +0100 | |
44 | * | |
45 | * Initial commit | |
46 | * | |
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 | |
22a2d3d5 | 52 | * |
8fae76c5 | 53 | * Initial commit |
22a2d3d5 | 54 | * |
8fae76c5 | 55 | * diff --git a/test.txt b/test.txt |
56 | * new file mode 100644 | |
57 | * index 0000000..9daeafb | |
58 | * --- /dev/null | |
59 | * +++ b/test.txt | |
60 | * @@ -0,0 +1 @@ | |
61 | * +test | |
62 | * | |
63 | * $ git ls-tree 2b297 | |
64 | * 100644 blob 9daeafb9864cf43055ae93beb0afd6c7d144bfa4 test.txt | |
65 | */ | |
66 | ||
743a4b3b | 67 | cl_git_pass(git_oid_fromstr(&expected_commit_oid, "1fe3126578fc4eca68c193e4a3a0a14a0704624d")); |
8fae76c5 | 68 | cl_git_pass(git_oid_fromstr(&expected_tree_oid, "2b297e643c551e76cfa1f93810c50811382f9117")); |
69 | cl_git_pass(git_oid_fromstr(&expected_blob_oid, "9daeafb9864cf43055ae93beb0afd6c7d144bfa4")); | |
70 | ||
71 | /* | |
72 | * Add a new file to the index | |
73 | */ | |
1d415455 | 74 | cl_git_mkfile("treebuilder/test.txt", "test\n"); |
8fae76c5 | 75 | cl_git_pass(git_repository_index(&index, repo)); |
25743bd7 | 76 | cl_git_pass(git_index_add_bypath(index, "test.txt")); |
8fae76c5 | 77 | |
f45ec1a0 | 78 | entry = git_index_get_byindex(index, 0); |
8fae76c5 | 79 | |
d541170c | 80 | cl_assert(git_oid_cmp(&expected_blob_oid, &entry->id) == 0); |
8fae76c5 | 81 | |
be00b00d RB |
82 | /* |
83 | * Information about index entry should match test file | |
84 | */ | |
85 | { | |
86 | struct stat st; | |
87 | cl_must_pass(p_lstat("treebuilder/test.txt", &st)); | |
88 | cl_assert(entry->file_size == st.st_size); | |
35cdd261 SG |
89 | #ifndef _WIN32 |
90 | /* | |
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 | |
94 | * that case. | |
95 | */ | |
be00b00d RB |
96 | cl_assert(entry->uid == st.st_uid); |
97 | cl_assert(entry->gid == st.st_gid); | |
35cdd261 | 98 | #endif |
be00b00d RB |
99 | } |
100 | ||
8fae76c5 | 101 | /* |
102 | * Build the tree from the index | |
103 | */ | |
43eeca04 | 104 | cl_git_pass(git_index_write_tree(&tree_oid, index)); |
8fae76c5 | 105 | |
106 | cl_assert(git_oid_cmp(&expected_tree_oid, &tree_oid) == 0); | |
107 | ||
108 | /* | |
109 | * Commit the staged file | |
110 | */ | |
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)); | |
743a4b3b | 113 | |
49e369b2 | 114 | cl_git_pass(git_message_prettify(&buffer, "Initial commit", 0, '#')); |
743a4b3b | 115 | |
8fae76c5 | 116 | cl_git_pass(git_commit_create_v( |
117 | &commit_oid, | |
118 | repo, | |
119 | "HEAD", | |
120 | signature, | |
121 | signature, | |
122 | NULL, | |
e1d7f003 | 123 | buffer.ptr, |
8fae76c5 | 124 | tree, |
125 | 0)); | |
126 | ||
127 | cl_assert(git_oid_cmp(&expected_commit_oid, &commit_oid) == 0); | |
128 | ||
ac3d33df | 129 | git_buf_dispose(&buffer); |
8fae76c5 | 130 | git_signature_free(signature); |
131 | git_tree_free(tree); | |
132 | git_index_free(index); | |
133 | } | |
80c29fe9 RB |
134 | |
135 | static void assert_commit_tree_has_n_entries(git_commit *c, int count) | |
136 | { | |
137 | git_tree *tree; | |
138 | cl_git_pass(git_commit_tree(&tree, c)); | |
139 | cl_assert_equal_i(count, git_tree_entrycount(tree)); | |
140 | git_tree_free(tree); | |
141 | } | |
142 | ||
22a2d3d5 | 143 | static void assert_commit_is_head_(git_commit *c, const char *file, const char *func, int line) |
80c29fe9 RB |
144 | { |
145 | git_commit *head; | |
146 | cl_git_pass(git_revparse_single((git_object **)&head, repo, "HEAD")); | |
22a2d3d5 | 147 | clar__assert(git_oid_equal(git_commit_id(c), git_commit_id(head)), file, func, line, "Commit is not the HEAD", NULL, 1); |
80c29fe9 RB |
148 | git_commit_free(head); |
149 | } | |
22a2d3d5 | 150 | #define assert_commit_is_head(C) assert_commit_is_head_((C),__FILE__,__func__,__LINE__) |
80c29fe9 RB |
151 | |
152 | void test_object_commit_commitstagedfile__amend_commit(void) | |
153 | { | |
154 | git_index *index; | |
155 | git_oid old_oid, new_oid, tree_oid; | |
156 | git_commit *old_commit, *new_commit; | |
157 | git_tree *tree; | |
158 | ||
159 | /* make a commit */ | |
160 | ||
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"); | |
165 | ||
166 | cl_git_pass(git_commit_lookup(&old_commit, repo, &old_oid)); | |
167 | ||
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); | |
171 | ||
172 | /* let's amend the message of the HEAD commit */ | |
173 | ||
174 | cl_git_pass(git_commit_amend( | |
175 | &new_oid, old_commit, "HEAD", NULL, NULL, NULL, "Initial commit", NULL)); | |
176 | ||
217c029b CMN |
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)); | |
180 | ||
80c29fe9 RB |
181 | cl_git_pass(git_commit_lookup(&new_commit, repo, &new_oid)); |
182 | ||
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); | |
186 | ||
187 | git_commit_free(old_commit); | |
217c029b | 188 | |
80c29fe9 RB |
189 | old_commit = new_commit; |
190 | ||
191 | /* let's amend the tree of that last commit */ | |
192 | ||
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)); | |
198 | ||
217c029b CMN |
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)); | |
202 | ||
80c29fe9 RB |
203 | cl_git_pass(git_commit_amend( |
204 | &new_oid, old_commit, "HEAD", NULL, NULL, NULL, "Initial commit", tree)); | |
205 | git_tree_free(tree); | |
206 | ||
207 | cl_git_pass(git_commit_lookup(&new_commit, repo, &new_oid)); | |
208 | ||
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); | |
212 | ||
213 | /* cleanup */ | |
214 | ||
215 | git_commit_free(old_commit); | |
216 | git_commit_free(new_commit); | |
217 | git_index_free(index); | |
218 | } |