1 #include "clar_libgit2.h"
2 #include "../checkout/checkout_helpers.h"
6 #include "repository.h"
8 static git_repository
*g_repo
;
10 void test_index_racy__initialize(void)
12 cl_git_pass(git_repository_init(&g_repo
, "diff_racy", false));
15 void test_index_racy__cleanup(void)
17 git_repository_free(g_repo
);
20 cl_fixture_cleanup("diff_racy");
23 void test_index_racy__diff(void)
27 git_buf path
= GIT_BUF_INIT
;
29 cl_git_pass(git_buf_joinpath(&path
, git_repository_workdir(g_repo
), "A"));
30 cl_git_mkfile(path
.ptr
, "A");
32 /* Put 'A' into the index */
33 cl_git_pass(git_repository_index(&index
, g_repo
));
34 cl_git_pass(git_index_add_bypath(index
, "A"));
35 cl_git_pass(git_index_write(index
));
37 cl_git_pass(git_diff_index_to_workdir(&diff
, g_repo
, index
, NULL
));
38 cl_assert_equal_i(0, git_diff_num_deltas(diff
));
41 /* Change its contents quickly, so we get the same timestamp */
42 cl_git_mkfile(path
.ptr
, "B");
44 cl_git_pass(git_diff_index_to_workdir(&diff
, g_repo
, index
, NULL
));
45 cl_assert_equal_i(1, git_diff_num_deltas(diff
));
47 git_index_free(index
);
52 void test_index_racy__write_index_just_after_file(void)
56 git_buf path
= GIT_BUF_INIT
;
57 struct timeval times
[2];
59 /* Make sure we do have a timestamp */
60 cl_git_pass(git_repository_index(&index
, g_repo
));
61 cl_git_pass(git_index_write(index
));
63 cl_git_pass(git_buf_joinpath(&path
, git_repository_workdir(g_repo
), "A"));
64 cl_git_mkfile(path
.ptr
, "A");
65 /* Force the file's timestamp to be a second after we wrote the index */
66 times
[0].tv_sec
= index
->stamp
.mtime
.tv_sec
+ 1;
67 times
[0].tv_usec
= index
->stamp
.mtime
.tv_nsec
/ 1000;
68 times
[1].tv_sec
= index
->stamp
.mtime
.tv_sec
+ 1;
69 times
[1].tv_usec
= index
->stamp
.mtime
.tv_nsec
/ 1000;
70 cl_git_pass(p_utimes(path
.ptr
, times
));
73 * Put 'A' into the index, the size field will be filled,
74 * because the index' on-disk timestamp does not match the
77 cl_git_pass(git_index_add_bypath(index
, "A"));
78 cl_git_pass(git_index_write(index
));
80 cl_git_mkfile(path
.ptr
, "B");
82 * Pretend this index' modification happend a second after the
83 * file update, and rewrite the file in that same second.
85 times
[0].tv_sec
= index
->stamp
.mtime
.tv_sec
+ 2;
86 times
[0].tv_usec
= index
->stamp
.mtime
.tv_nsec
/ 1000;
87 times
[1].tv_sec
= index
->stamp
.mtime
.tv_sec
+ 2;
88 times
[0].tv_usec
= index
->stamp
.mtime
.tv_nsec
/ 1000;
90 cl_git_pass(p_utimes(git_index_path(index
), times
));
91 cl_git_pass(p_utimes(path
.ptr
, times
));
93 cl_git_pass(git_index_read(index
, true));
95 cl_git_pass(git_diff_index_to_workdir(&diff
, g_repo
, index
, NULL
));
96 cl_assert_equal_i(1, git_diff_num_deltas(diff
));
100 git_index_free(index
);
104 static void setup_race(void)
106 git_buf path
= GIT_BUF_INIT
;
108 const git_index_entry
*entry
;
109 int i
, found_race
= 0;
111 /* Make sure we do have a timestamp */
112 cl_git_pass(git_repository_index__weakptr(&index
, g_repo
));
113 cl_git_pass(git_index_write(index
));
115 cl_git_pass(git_buf_joinpath(&path
, git_repository_workdir(g_repo
), "A"));
117 /* Make sure writing the file, adding and rewriting happen in the same second */
118 for (i
= 0; i
< 10; i
++) {
120 cl_git_mkfile(path
.ptr
, "A");
122 cl_git_pass(git_index_add_bypath(index
, "A"));
123 cl_git_mkfile(path
.ptr
, "B");
124 cl_git_pass(git_index_write(index
));
126 cl_git_mkfile(path
.ptr
, "");
128 cl_git_pass(p_stat(path
.ptr
, &st
));
129 cl_assert(entry
= git_index_get_bypath(index
, "A", 0));
130 if (entry
->mtime
.seconds
== (int32_t) st
.st_mtime
) {
137 cl_fail("failed to find race after 10 attempts");
142 void test_index_racy__smudges_index_entry_on_save(void)
145 const git_index_entry
*entry
;
149 /* write the index, which will smudge anything that had the same timestamp
150 * as the index when the index was loaded. that way future loads of the
151 * index (with the new timestamp) will know that these files were not
155 cl_git_pass(git_repository_index__weakptr(&index
, g_repo
));
156 cl_git_pass(git_index_write(index
));
158 cl_assert(entry
= git_index_get_bypath(index
, "A", 0));
159 cl_assert_equal_i(0, entry
->file_size
);
162 void test_index_racy__detects_diff_of_change_in_identical_timestamp(void)
167 cl_git_pass(git_repository_index__weakptr(&index
, g_repo
));
171 cl_git_pass(git_diff_index_to_workdir(&diff
, g_repo
, index
, NULL
));
172 cl_assert_equal_i(1, git_diff_num_deltas(diff
));
177 static void setup_uptodate_files(void)
179 git_buf path
= GIT_BUF_INIT
;
181 git_index_entry new_entry
= {{0}};
183 cl_git_pass(git_repository_index(&index
, g_repo
));
185 cl_git_pass(git_buf_joinpath(&path
, git_repository_workdir(g_repo
), "A"));
186 cl_git_mkfile(path
.ptr
, "A");
188 /* Put 'A' into the index */
189 cl_git_pass(git_index_add_bypath(index
, "A"));
191 /* Put 'B' into the index */
192 new_entry
.path
= "B";
193 new_entry
.mode
= GIT_FILEMODE_BLOB
;
194 cl_git_pass(git_index_add(index
, &new_entry
));
196 /* Put 'C' into the index */
197 new_entry
.path
= "C";
198 new_entry
.mode
= GIT_FILEMODE_BLOB
;
199 cl_git_pass(git_index_add_frombuffer(index
, &new_entry
, "hello!\n", 7));
201 git_index_free(index
);
205 void test_index_racy__adding_to_index_is_uptodate(void)
208 const git_index_entry
*entry
;
210 setup_uptodate_files();
212 cl_git_pass(git_repository_index(&index
, g_repo
));
214 /* ensure that they're all uptodate */
215 cl_assert((entry
= git_index_get_bypath(index
, "A", 0)));
216 cl_assert_equal_i(GIT_IDXENTRY_UPTODATE
, (entry
->flags_extended
& GIT_IDXENTRY_UPTODATE
));
218 cl_assert((entry
= git_index_get_bypath(index
, "B", 0)));
219 cl_assert_equal_i(GIT_IDXENTRY_UPTODATE
, (entry
->flags_extended
& GIT_IDXENTRY_UPTODATE
));
221 cl_assert((entry
= git_index_get_bypath(index
, "C", 0)));
222 cl_assert_equal_i(GIT_IDXENTRY_UPTODATE
, (entry
->flags_extended
& GIT_IDXENTRY_UPTODATE
));
224 cl_git_pass(git_index_write(index
));
226 git_index_free(index
);
229 void test_index_racy__reading_clears_uptodate_bit(void)
232 const git_index_entry
*entry
;
234 setup_uptodate_files();
236 cl_git_pass(git_repository_index(&index
, g_repo
));
237 cl_git_pass(git_index_write(index
));
239 cl_git_pass(git_index_read(index
, true));
241 /* ensure that no files are uptodate */
242 cl_assert((entry
= git_index_get_bypath(index
, "A", 0)));
243 cl_assert_equal_i(0, (entry
->flags_extended
& GIT_IDXENTRY_UPTODATE
));
245 cl_assert((entry
= git_index_get_bypath(index
, "B", 0)));
246 cl_assert_equal_i(0, (entry
->flags_extended
& GIT_IDXENTRY_UPTODATE
));
248 cl_assert((entry
= git_index_get_bypath(index
, "C", 0)));
249 cl_assert_equal_i(0, (entry
->flags_extended
& GIT_IDXENTRY_UPTODATE
));
251 git_index_free(index
);
254 void test_index_racy__read_tree_clears_uptodate_bit(void)
258 const git_index_entry
*entry
;
261 setup_uptodate_files();
263 cl_git_pass(git_repository_index(&index
, g_repo
));
264 cl_git_pass(git_index_write_tree_to(&id
, index
, g_repo
));
265 cl_git_pass(git_tree_lookup(&tree
, g_repo
, &id
));
266 cl_git_pass(git_index_read_tree(index
, tree
));
268 /* ensure that no files are uptodate */
269 cl_assert((entry
= git_index_get_bypath(index
, "A", 0)));
270 cl_assert_equal_i(0, (entry
->flags_extended
& GIT_IDXENTRY_UPTODATE
));
272 cl_assert((entry
= git_index_get_bypath(index
, "B", 0)));
273 cl_assert_equal_i(0, (entry
->flags_extended
& GIT_IDXENTRY_UPTODATE
));
275 cl_assert((entry
= git_index_get_bypath(index
, "C", 0)));
276 cl_assert_equal_i(0, (entry
->flags_extended
& GIT_IDXENTRY_UPTODATE
));
279 git_index_free(index
);
282 void test_index_racy__read_index_smudges(void)
284 git_index
*index
, *newindex
;
285 const git_index_entry
*entry
;
287 /* if we are reading an index into our new index, ensure that any
288 * racy entries in the index that we're reading are smudged so that
289 * we don't propagate their timestamps without further investigation.
293 cl_git_pass(git_repository_index(&index
, g_repo
));
294 cl_git_pass(git_index_new(&newindex
));
295 cl_git_pass(git_index_read_index(newindex
, index
));
297 cl_assert(entry
= git_index_get_bypath(newindex
, "A", 0));
298 cl_assert_equal_i(0, entry
->file_size
);
300 git_index_free(index
);
301 git_index_free(newindex
);
304 void test_index_racy__read_index_clears_uptodate_bit(void)
306 git_index
*index
, *newindex
;
307 const git_index_entry
*entry
;
309 setup_uptodate_files();
311 cl_git_pass(git_repository_index(&index
, g_repo
));
312 cl_git_pass(git_index_new(&newindex
));
313 cl_git_pass(git_index_read_index(newindex
, index
));
315 /* ensure that files brought in from the other index are not uptodate */
316 cl_assert((entry
= git_index_get_bypath(newindex
, "A", 0)));
317 cl_assert_equal_i(0, (entry
->flags_extended
& GIT_IDXENTRY_UPTODATE
));
319 cl_assert((entry
= git_index_get_bypath(newindex
, "B", 0)));
320 cl_assert_equal_i(0, (entry
->flags_extended
& GIT_IDXENTRY_UPTODATE
));
322 cl_assert((entry
= git_index_get_bypath(newindex
, "C", 0)));
323 cl_assert_equal_i(0, (entry
->flags_extended
& GIT_IDXENTRY_UPTODATE
));
325 git_index_free(index
);
326 git_index_free(newindex
);