]>
Commit | Line | Data |
---|---|---|
aa13bf05 RB |
1 | #include "clar_libgit2.h" |
2 | #include "posix.h" | |
3 | #include "path.h" | |
4 | #include "submodule_helpers.h" | |
5f4a61ae | 5 | #include "fileops.h" |
aa13bf05 RB |
6 | |
7 | static git_repository *g_repo = NULL; | |
8 | ||
9 | void test_submodule_status__initialize(void) | |
10 | { | |
11 | g_repo = cl_git_sandbox_init("submod2"); | |
12 | ||
13 | cl_fixture_sandbox("submod2_target"); | |
14 | p_rename("submod2_target/.gitted", "submod2_target/.git"); | |
15 | ||
16 | /* must create submod2_target before rewrite so prettify will work */ | |
17 | rewrite_gitmodules(git_repository_workdir(g_repo)); | |
18 | p_rename("submod2/not_submodule/.gitted", "submod2/not_submodule/.git"); | |
19 | } | |
20 | ||
21 | void test_submodule_status__cleanup(void) | |
22 | { | |
23 | cl_git_sandbox_cleanup(); | |
24 | cl_fixture_cleanup("submod2_target"); | |
25 | } | |
26 | ||
27 | void test_submodule_status__unchanged(void) | |
28 | { | |
5f4a61ae RB |
29 | unsigned int status, expected; |
30 | git_submodule *sm; | |
31 | ||
32 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged")); | |
33 | cl_git_pass(git_submodule_status(&status, sm)); | |
34 | cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); | |
35 | ||
36 | expected = GIT_SUBMODULE_STATUS_IN_HEAD | | |
37 | GIT_SUBMODULE_STATUS_IN_INDEX | | |
38 | GIT_SUBMODULE_STATUS_IN_CONFIG | | |
39 | GIT_SUBMODULE_STATUS_IN_WD; | |
40 | ||
41 | cl_assert(status == expected); | |
aa13bf05 RB |
42 | } |
43 | ||
5f4a61ae RB |
44 | /* 4 values of GIT_SUBMODULE_IGNORE to check */ |
45 | ||
46 | void test_submodule_status__ignore_none(void) | |
aa13bf05 | 47 | { |
5f4a61ae RB |
48 | unsigned int status; |
49 | git_submodule *sm; | |
50 | git_buf path = GIT_BUF_INIT; | |
51 | ||
52 | cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "sm_unchanged")); | |
331e7de9 | 53 | cl_git_pass(git_futils_rmdir_r(git_buf_cstr(&path), NULL, GIT_RMDIR_REMOVE_FILES)); |
5f4a61ae RB |
54 | |
55 | cl_git_fail(git_submodule_lookup(&sm, g_repo, "not_submodule")); | |
56 | ||
57 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_index")); | |
58 | cl_git_pass(git_submodule_status(&status, sm)); | |
59 | cl_assert((status & GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED) != 0); | |
60 | ||
61 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head")); | |
62 | cl_git_pass(git_submodule_status(&status, sm)); | |
63 | cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0); | |
64 | ||
65 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_file")); | |
66 | cl_git_pass(git_submodule_status(&status, sm)); | |
67 | cl_assert((status & GIT_SUBMODULE_STATUS_WD_WD_MODIFIED) != 0); | |
68 | ||
69 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_untracked_file")); | |
70 | cl_git_pass(git_submodule_status(&status, sm)); | |
71 | cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNTRACKED) != 0); | |
72 | ||
73 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_missing_commits")); | |
74 | cl_git_pass(git_submodule_status(&status, sm)); | |
75 | cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0); | |
76 | ||
77 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_added_and_uncommited")); | |
78 | cl_git_pass(git_submodule_status(&status, sm)); | |
79 | cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_ADDED) != 0); | |
80 | ||
81 | /* removed sm_unchanged for deleted workdir */ | |
82 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged")); | |
83 | cl_git_pass(git_submodule_status(&status, sm)); | |
84 | cl_assert((status & GIT_SUBMODULE_STATUS_WD_DELETED) != 0); | |
85 | ||
86 | /* now mkdir sm_unchanged to test uninitialized */ | |
87 | cl_git_pass(git_futils_mkdir(git_buf_cstr(&path), NULL, 0755, 0)); | |
88 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged")); | |
89 | cl_git_pass(git_submodule_reload(sm)); | |
90 | cl_git_pass(git_submodule_status(&status, sm)); | |
91 | cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNINITIALIZED) != 0); | |
92 | ||
93 | /* update sm_changed_head in index */ | |
94 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head")); | |
95 | cl_git_pass(git_submodule_add_to_index(sm, true)); | |
96 | /* reload is not needed because add_to_index updates the submodule data */ | |
97 | cl_git_pass(git_submodule_status(&status, sm)); | |
98 | cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_MODIFIED) != 0); | |
99 | ||
100 | /* remove sm_changed_head from index */ | |
101 | { | |
102 | git_index *index; | |
11d9f6b3 | 103 | size_t pos; |
5f4a61ae RB |
104 | |
105 | cl_git_pass(git_repository_index(&index, g_repo)); | |
11d9f6b3 | 106 | cl_assert(!git_index_find(&pos, index, "sm_changed_head")); |
f45ec1a0 | 107 | cl_git_pass(git_index_remove(index, "sm_changed_head", 0)); |
5f4a61ae RB |
108 | cl_git_pass(git_index_write(index)); |
109 | ||
110 | git_index_free(index); | |
111 | } | |
aa13bf05 | 112 | |
5f4a61ae RB |
113 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head")); |
114 | cl_git_pass(git_submodule_reload(sm)); | |
115 | cl_git_pass(git_submodule_status(&status, sm)); | |
116 | cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_DELETED) != 0); | |
117 | ||
118 | git_buf_free(&path); | |
aa13bf05 RB |
119 | } |
120 | ||
5f4a61ae RB |
121 | static int set_sm_ignore(git_submodule *sm, const char *name, void *payload) |
122 | { | |
123 | git_submodule_ignore_t ignore = *(git_submodule_ignore_t *)payload; | |
124 | GIT_UNUSED(name); | |
125 | git_submodule_set_ignore(sm, ignore); | |
126 | return 0; | |
127 | } | |
128 | ||
129 | void test_submodule_status__ignore_untracked(void) | |
130 | { | |
131 | unsigned int status; | |
132 | git_submodule *sm; | |
133 | git_buf path = GIT_BUF_INIT; | |
134 | git_submodule_ignore_t ign = GIT_SUBMODULE_IGNORE_UNTRACKED; | |
135 | ||
136 | cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "sm_unchanged")); | |
331e7de9 | 137 | cl_git_pass(git_futils_rmdir_r(git_buf_cstr(&path), NULL, GIT_RMDIR_REMOVE_FILES)); |
5f4a61ae RB |
138 | |
139 | cl_git_pass(git_submodule_foreach(g_repo, set_sm_ignore, &ign)); | |
140 | ||
141 | cl_git_fail(git_submodule_lookup(&sm, g_repo, "not_submodule")); | |
142 | ||
143 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_index")); | |
144 | cl_git_pass(git_submodule_status(&status, sm)); | |
145 | cl_assert((status & GIT_SUBMODULE_STATUS_WD_INDEX_MODIFIED) != 0); | |
146 | ||
147 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head")); | |
148 | cl_git_pass(git_submodule_status(&status, sm)); | |
149 | cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0); | |
150 | ||
151 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_file")); | |
152 | cl_git_pass(git_submodule_status(&status, sm)); | |
153 | cl_assert((status & GIT_SUBMODULE_STATUS_WD_WD_MODIFIED) != 0); | |
154 | ||
155 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_untracked_file")); | |
156 | cl_git_pass(git_submodule_status(&status, sm)); | |
157 | cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); | |
158 | ||
159 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_missing_commits")); | |
160 | cl_git_pass(git_submodule_status(&status, sm)); | |
161 | cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0); | |
162 | ||
163 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_added_and_uncommited")); | |
164 | cl_git_pass(git_submodule_status(&status, sm)); | |
165 | cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_ADDED) != 0); | |
166 | ||
167 | /* removed sm_unchanged for deleted workdir */ | |
168 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged")); | |
169 | cl_git_pass(git_submodule_status(&status, sm)); | |
170 | cl_assert((status & GIT_SUBMODULE_STATUS_WD_DELETED) != 0); | |
171 | ||
172 | /* now mkdir sm_unchanged to test uninitialized */ | |
173 | cl_git_pass(git_futils_mkdir(git_buf_cstr(&path), NULL, 0755, 0)); | |
174 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged")); | |
175 | cl_git_pass(git_submodule_reload(sm)); | |
176 | cl_git_pass(git_submodule_status(&status, sm)); | |
177 | cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNINITIALIZED) != 0); | |
178 | ||
179 | /* update sm_changed_head in index */ | |
180 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head")); | |
181 | cl_git_pass(git_submodule_add_to_index(sm, true)); | |
182 | /* reload is not needed because add_to_index updates the submodule data */ | |
183 | cl_git_pass(git_submodule_status(&status, sm)); | |
184 | cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_MODIFIED) != 0); | |
185 | ||
186 | git_buf_free(&path); | |
187 | } | |
188 | ||
189 | void test_submodule_status__ignore_dirty(void) | |
190 | { | |
191 | unsigned int status; | |
192 | git_submodule *sm; | |
193 | git_buf path = GIT_BUF_INIT; | |
194 | git_submodule_ignore_t ign = GIT_SUBMODULE_IGNORE_DIRTY; | |
195 | ||
196 | cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "sm_unchanged")); | |
331e7de9 | 197 | cl_git_pass(git_futils_rmdir_r(git_buf_cstr(&path), NULL, GIT_RMDIR_REMOVE_FILES)); |
5f4a61ae RB |
198 | |
199 | cl_git_pass(git_submodule_foreach(g_repo, set_sm_ignore, &ign)); | |
200 | ||
201 | cl_git_fail(git_submodule_lookup(&sm, g_repo, "not_submodule")); | |
202 | ||
203 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_index")); | |
204 | cl_git_pass(git_submodule_status(&status, sm)); | |
205 | cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); | |
206 | ||
207 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head")); | |
208 | cl_git_pass(git_submodule_status(&status, sm)); | |
209 | cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0); | |
210 | ||
211 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_file")); | |
212 | cl_git_pass(git_submodule_status(&status, sm)); | |
213 | cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); | |
214 | ||
215 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_untracked_file")); | |
216 | cl_git_pass(git_submodule_status(&status, sm)); | |
217 | cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); | |
218 | ||
219 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_missing_commits")); | |
220 | cl_git_pass(git_submodule_status(&status, sm)); | |
221 | cl_assert((status & GIT_SUBMODULE_STATUS_WD_MODIFIED) != 0); | |
222 | ||
223 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_added_and_uncommited")); | |
224 | cl_git_pass(git_submodule_status(&status, sm)); | |
225 | cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_ADDED) != 0); | |
226 | ||
227 | /* removed sm_unchanged for deleted workdir */ | |
228 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged")); | |
229 | cl_git_pass(git_submodule_status(&status, sm)); | |
230 | cl_assert((status & GIT_SUBMODULE_STATUS_WD_DELETED) != 0); | |
231 | ||
232 | /* now mkdir sm_unchanged to test uninitialized */ | |
233 | cl_git_pass(git_futils_mkdir(git_buf_cstr(&path), NULL, 0755, 0)); | |
234 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged")); | |
235 | cl_git_pass(git_submodule_reload(sm)); | |
236 | cl_git_pass(git_submodule_status(&status, sm)); | |
237 | cl_assert((status & GIT_SUBMODULE_STATUS_WD_UNINITIALIZED) != 0); | |
238 | ||
239 | /* update sm_changed_head in index */ | |
240 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head")); | |
241 | cl_git_pass(git_submodule_add_to_index(sm, true)); | |
242 | /* reload is not needed because add_to_index updates the submodule data */ | |
243 | cl_git_pass(git_submodule_status(&status, sm)); | |
244 | cl_assert((status & GIT_SUBMODULE_STATUS_INDEX_MODIFIED) != 0); | |
245 | ||
246 | git_buf_free(&path); | |
247 | } | |
248 | ||
249 | void test_submodule_status__ignore_all(void) | |
250 | { | |
251 | unsigned int status; | |
252 | git_submodule *sm; | |
253 | git_buf path = GIT_BUF_INIT; | |
254 | git_submodule_ignore_t ign = GIT_SUBMODULE_IGNORE_ALL; | |
255 | ||
256 | cl_git_pass(git_buf_joinpath(&path, git_repository_workdir(g_repo), "sm_unchanged")); | |
331e7de9 | 257 | cl_git_pass(git_futils_rmdir_r(git_buf_cstr(&path), NULL, GIT_RMDIR_REMOVE_FILES)); |
5f4a61ae RB |
258 | |
259 | cl_git_pass(git_submodule_foreach(g_repo, set_sm_ignore, &ign)); | |
260 | ||
261 | cl_git_fail(git_submodule_lookup(&sm, g_repo, "not_submodule")); | |
262 | ||
263 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_index")); | |
264 | cl_git_pass(git_submodule_status(&status, sm)); | |
265 | cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); | |
266 | ||
267 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head")); | |
268 | cl_git_pass(git_submodule_status(&status, sm)); | |
269 | cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); | |
270 | ||
271 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_file")); | |
272 | cl_git_pass(git_submodule_status(&status, sm)); | |
273 | cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); | |
274 | ||
275 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_untracked_file")); | |
276 | cl_git_pass(git_submodule_status(&status, sm)); | |
277 | cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); | |
278 | ||
279 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_missing_commits")); | |
280 | cl_git_pass(git_submodule_status(&status, sm)); | |
281 | cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); | |
282 | ||
283 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_added_and_uncommited")); | |
284 | cl_git_pass(git_submodule_status(&status, sm)); | |
285 | cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); | |
286 | ||
287 | /* removed sm_unchanged for deleted workdir */ | |
288 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged")); | |
289 | cl_git_pass(git_submodule_status(&status, sm)); | |
290 | cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); | |
291 | ||
292 | /* now mkdir sm_unchanged to test uninitialized */ | |
293 | cl_git_pass(git_futils_mkdir(git_buf_cstr(&path), NULL, 0755, 0)); | |
294 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_unchanged")); | |
295 | cl_git_pass(git_submodule_reload(sm)); | |
296 | cl_git_pass(git_submodule_status(&status, sm)); | |
297 | cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); | |
298 | ||
299 | /* update sm_changed_head in index */ | |
300 | cl_git_pass(git_submodule_lookup(&sm, g_repo, "sm_changed_head")); | |
301 | cl_git_pass(git_submodule_add_to_index(sm, true)); | |
302 | /* reload is not needed because add_to_index updates the submodule data */ | |
303 | cl_git_pass(git_submodule_status(&status, sm)); | |
304 | cl_assert(GIT_SUBMODULE_STATUS_IS_UNMODIFIED(status)); | |
305 | ||
306 | git_buf_free(&path); | |
307 | } |