]> git.proxmox.com Git - libgit2.git/blob - tests/index/racy.c
Merge pull request #3281 from ethomson/wildcard_filters
[libgit2.git] / tests / index / racy.c
1 #include "clar_libgit2.h"
2 #include "../checkout/checkout_helpers.h"
3
4 #include "buffer.h"
5 #include "index.h"
6 #include "repository.h"
7
8 static git_repository *g_repo;
9
10 void test_index_racy__initialize(void)
11 {
12 cl_git_pass(git_repository_init(&g_repo, "diff_racy", false));
13 }
14
15 void test_index_racy__cleanup(void)
16 {
17 git_repository_free(g_repo);
18 g_repo = NULL;
19
20 cl_fixture_cleanup("diff_racy");
21 }
22
23 void test_index_racy__diff(void)
24 {
25 git_index *index;
26 git_diff *diff;
27 git_buf path = GIT_BUF_INIT;
28
29 cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "A"));
30 cl_git_mkfile(path.ptr, "A");
31
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));
36
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));
39 git_diff_free(diff);
40
41 /* Change its contents quickly, so we get the same timestamp */
42 cl_git_mkfile(path.ptr, "B");
43
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));
46
47 git_index_free(index);
48 git_diff_free(diff);
49 git_buf_free(&path);
50 }
51
52 void test_index_racy__write_index_just_after_file(void)
53 {
54 git_index *index;
55 git_diff *diff;
56 git_buf path = GIT_BUF_INIT;
57 struct timeval times[2];
58
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));
62
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 + 1;
67 times[0].tv_usec = 0;
68 times[1].tv_sec = index->stamp.mtime + 1;
69 times[1].tv_usec = 0;
70 cl_git_pass(p_utimes(path.ptr, times));
71
72 /*
73 * Put 'A' into the index, the size field will be filled,
74 * because the index' on-disk timestamp does not match the
75 * file's timestamp.
76 */
77 cl_git_pass(git_index_add_bypath(index, "A"));
78 cl_git_pass(git_index_write(index));
79
80 cl_git_mkfile(path.ptr, "B");
81 /*
82 * Pretend this index' modification happend a second after the
83 * file update, and rewrite the file in that same second.
84 */
85 times[0].tv_sec = index->stamp.mtime + 2;
86 times[0].tv_usec = 0;
87 times[1].tv_sec = index->stamp.mtime + 2;
88 times[0].tv_usec = 0;
89
90 cl_git_pass(p_utimes(git_index_path(index), times));
91 cl_git_pass(p_utimes(path.ptr, times));
92
93 cl_git_pass(git_index_read(index, true));
94
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));
97
98 git_buf_free(&path);
99 git_diff_free(diff);
100 git_index_free(index);
101 }
102
103 void test_index_racy__empty_file_after_smudge(void)
104 {
105 git_index *index;
106 git_diff *diff;
107 git_buf path = GIT_BUF_INIT;
108 int i, found_race = 0;
109 const git_index_entry *entry;
110
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));
114
115 cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "A"));
116
117 /* Make sure writing the file, adding and rewriting happen in the same second */
118 for (i = 0; i < 10; i++) {
119 struct stat st;
120 cl_git_mkfile(path.ptr, "A");
121
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));
125
126 cl_git_mkfile(path.ptr, "");
127
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) {
131 found_race = 1;
132 break;
133 }
134
135 }
136
137 if (!found_race)
138 cl_fail("failed to find race after 10 attempts");
139
140 cl_assert_equal_i(0, entry->file_size);
141
142 cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, NULL));
143 cl_assert_equal_i(1, git_diff_num_deltas(diff));
144
145 git_buf_free(&path);
146 git_diff_free(diff);
147 }