]>
Commit | Line | Data |
---|---|---|
74fa4bfa RB |
1 | #include "clar_libgit2.h" |
2 | #include "diff_helpers.h" | |
3 | ||
4 | static git_repository *g_repo = NULL; | |
5 | ||
6 | void test_diff_workdir__initialize(void) | |
7 | { | |
74fa4bfa RB |
8 | } |
9 | ||
10 | void test_diff_workdir__cleanup(void) | |
11 | { | |
854eccbb | 12 | cl_git_sandbox_cleanup(); |
74fa4bfa RB |
13 | } |
14 | ||
15 | void test_diff_workdir__to_index(void) | |
16 | { | |
17 | git_diff_options opts = {0}; | |
18 | git_diff_list *diff = NULL; | |
19 | diff_expects exp; | |
f335ecd6 | 20 | int use_iterator; |
74fa4bfa | 21 | |
0abd7244 RB |
22 | g_repo = cl_git_sandbox_init("status"); |
23 | ||
74fa4bfa RB |
24 | opts.context_lines = 3; |
25 | opts.interhunk_lines = 1; | |
26 | opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED; | |
27 | ||
74fa4bfa RB |
28 | cl_git_pass(git_diff_workdir_to_index(g_repo, &opts, &diff)); |
29 | ||
f335ecd6 RB |
30 | for (use_iterator = 0; use_iterator <= 1; use_iterator++) { |
31 | memset(&exp, 0, sizeof(exp)); | |
32 | ||
33 | if (use_iterator) | |
34 | cl_git_pass(diff_foreach_via_iterator( | |
35 | diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
36 | else | |
37 | cl_git_pass(git_diff_foreach( | |
38 | diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
39 | ||
40 | /* to generate these values: | |
41 | * - cd to tests/resources/status, | |
42 | * - mv .gitted .git | |
43 | * - git diff --name-status | |
44 | * - git diff | |
45 | * - mv .git .gitted | |
46 | */ | |
47 | cl_assert_equal_i(13, exp.files); | |
48 | cl_assert_equal_i(0, exp.file_adds); | |
49 | cl_assert_equal_i(4, exp.file_dels); | |
50 | cl_assert_equal_i(4, exp.file_mods); | |
51 | cl_assert_equal_i(1, exp.file_ignored); | |
52 | cl_assert_equal_i(4, exp.file_untracked); | |
53 | ||
54 | cl_assert_equal_i(8, exp.hunks); | |
55 | ||
56 | cl_assert_equal_i(14, exp.lines); | |
57 | cl_assert_equal_i(5, exp.line_ctxt); | |
58 | cl_assert_equal_i(4, exp.line_adds); | |
59 | cl_assert_equal_i(5, exp.line_dels); | |
60 | } | |
74fa4bfa RB |
61 | |
62 | git_diff_list_free(diff); | |
63 | } | |
64 | ||
65 | void test_diff_workdir__to_tree(void) | |
66 | { | |
67 | /* grabbed a couple of commit oids from the history of the attr repo */ | |
68 | const char *a_commit = "26a125ee1bf"; /* the current HEAD */ | |
69 | const char *b_commit = "0017bd4ab1ec3"; /* the start */ | |
0abd7244 | 70 | git_tree *a, *b; |
74fa4bfa RB |
71 | git_diff_options opts = {0}; |
72 | git_diff_list *diff = NULL; | |
73 | git_diff_list *diff2 = NULL; | |
74 | diff_expects exp; | |
f335ecd6 | 75 | int use_iterator; |
74fa4bfa | 76 | |
0abd7244 RB |
77 | g_repo = cl_git_sandbox_init("status"); |
78 | ||
79 | a = resolve_commit_oid_to_tree(g_repo, a_commit); | |
80 | b = resolve_commit_oid_to_tree(g_repo, b_commit); | |
81 | ||
74fa4bfa RB |
82 | opts.context_lines = 3; |
83 | opts.interhunk_lines = 1; | |
84 | opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED; | |
85 | ||
74fa4bfa RB |
86 | /* You can't really generate the equivalent of git_diff_workdir_to_tree() |
87 | * using C git. It really wants to interpose the index into the diff. | |
88 | * | |
89 | * To validate the following results with command line git, I ran the | |
90 | * following: | |
91 | * - git ls-tree 26a125 | |
92 | * - find . ! -path ./.git/\* -a -type f | git hash-object --stdin-paths | |
93 | * The results are documented at the bottom of this file in the | |
94 | * long comment entitled "PREPARATION OF TEST DATA". | |
95 | */ | |
96 | cl_git_pass(git_diff_workdir_to_tree(g_repo, &opts, a, &diff)); | |
97 | ||
f335ecd6 RB |
98 | for (use_iterator = 0; use_iterator <= 1; use_iterator++) { |
99 | memset(&exp, 0, sizeof(exp)); | |
74fa4bfa | 100 | |
f335ecd6 RB |
101 | if (use_iterator) |
102 | cl_git_pass(diff_foreach_via_iterator( | |
103 | diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
104 | else | |
105 | cl_git_pass(git_diff_foreach( | |
106 | diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
107 | ||
108 | cl_assert_equal_i(14, exp.files); | |
109 | cl_assert_equal_i(0, exp.file_adds); | |
110 | cl_assert_equal_i(4, exp.file_dels); | |
111 | cl_assert_equal_i(4, exp.file_mods); | |
112 | cl_assert_equal_i(1, exp.file_ignored); | |
113 | cl_assert_equal_i(5, exp.file_untracked); | |
114 | } | |
74fa4bfa RB |
115 | |
116 | /* Since there is no git diff equivalent, let's just assume that the | |
117 | * text diffs produced by git_diff_foreach are accurate here. We will | |
118 | * do more apples-to-apples test comparison below. | |
119 | */ | |
120 | ||
121 | git_diff_list_free(diff); | |
122 | diff = NULL; | |
123 | memset(&exp, 0, sizeof(exp)); | |
124 | ||
125 | /* This is a compatible emulation of "git diff <sha>" which looks like | |
126 | * a workdir to tree diff (even though it is not really). This is what | |
127 | * you would get from "git diff --name-status 26a125ee1bf" | |
128 | */ | |
129 | cl_git_pass(git_diff_index_to_tree(g_repo, &opts, a, &diff)); | |
130 | cl_git_pass(git_diff_workdir_to_index(g_repo, &opts, &diff2)); | |
131 | cl_git_pass(git_diff_merge(diff, diff2)); | |
132 | git_diff_list_free(diff2); | |
133 | ||
f335ecd6 RB |
134 | for (use_iterator = 0; use_iterator <= 1; use_iterator++) { |
135 | memset(&exp, 0, sizeof(exp)); | |
136 | ||
137 | if (use_iterator) | |
138 | cl_git_pass(diff_foreach_via_iterator( | |
139 | diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
140 | else | |
141 | cl_git_pass(git_diff_foreach( | |
142 | diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
74fa4bfa | 143 | |
f335ecd6 RB |
144 | cl_assert_equal_i(15, exp.files); |
145 | cl_assert_equal_i(2, exp.file_adds); | |
146 | cl_assert_equal_i(5, exp.file_dels); | |
147 | cl_assert_equal_i(4, exp.file_mods); | |
148 | cl_assert_equal_i(1, exp.file_ignored); | |
149 | cl_assert_equal_i(3, exp.file_untracked); | |
74fa4bfa | 150 | |
f335ecd6 | 151 | cl_assert_equal_i(11, exp.hunks); |
74fa4bfa | 152 | |
f335ecd6 RB |
153 | cl_assert_equal_i(17, exp.lines); |
154 | cl_assert_equal_i(4, exp.line_ctxt); | |
155 | cl_assert_equal_i(8, exp.line_adds); | |
156 | cl_assert_equal_i(5, exp.line_dels); | |
157 | } | |
74fa4bfa RB |
158 | |
159 | git_diff_list_free(diff); | |
160 | diff = NULL; | |
161 | memset(&exp, 0, sizeof(exp)); | |
162 | ||
163 | /* Again, emulating "git diff <sha>" for testing purposes using | |
164 | * "git diff --name-status 0017bd4ab1ec3" instead. | |
165 | */ | |
166 | cl_git_pass(git_diff_index_to_tree(g_repo, &opts, b, &diff)); | |
167 | cl_git_pass(git_diff_workdir_to_index(g_repo, &opts, &diff2)); | |
168 | cl_git_pass(git_diff_merge(diff, diff2)); | |
169 | git_diff_list_free(diff2); | |
170 | ||
f335ecd6 RB |
171 | for (use_iterator = 0; use_iterator <= 1; use_iterator++) { |
172 | memset(&exp, 0, sizeof(exp)); | |
74fa4bfa | 173 | |
f335ecd6 RB |
174 | if (use_iterator) |
175 | cl_git_pass(diff_foreach_via_iterator( | |
176 | diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
177 | else | |
178 | cl_git_pass(git_diff_foreach( | |
179 | diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
74fa4bfa | 180 | |
f335ecd6 RB |
181 | cl_assert_equal_i(16, exp.files); |
182 | cl_assert_equal_i(5, exp.file_adds); | |
183 | cl_assert_equal_i(4, exp.file_dels); | |
184 | cl_assert_equal_i(3, exp.file_mods); | |
185 | cl_assert_equal_i(1, exp.file_ignored); | |
186 | cl_assert_equal_i(3, exp.file_untracked); | |
74fa4bfa | 187 | |
f335ecd6 RB |
188 | cl_assert_equal_i(12, exp.hunks); |
189 | ||
190 | cl_assert_equal_i(19, exp.lines); | |
191 | cl_assert_equal_i(3, exp.line_ctxt); | |
192 | cl_assert_equal_i(12, exp.line_adds); | |
193 | cl_assert_equal_i(4, exp.line_dels); | |
194 | } | |
74fa4bfa | 195 | |
c19bc93c RB |
196 | git_diff_list_free(diff); |
197 | ||
74fa4bfa RB |
198 | git_tree_free(a); |
199 | git_tree_free(b); | |
200 | } | |
201 | ||
14a513e0 RB |
202 | void test_diff_workdir__to_index_with_pathspec(void) |
203 | { | |
204 | git_diff_options opts = {0}; | |
205 | git_diff_list *diff = NULL; | |
206 | diff_expects exp; | |
207 | char *pathspec = NULL; | |
f335ecd6 | 208 | int use_iterator; |
14a513e0 | 209 | |
0abd7244 RB |
210 | g_repo = cl_git_sandbox_init("status"); |
211 | ||
14a513e0 RB |
212 | opts.context_lines = 3; |
213 | opts.interhunk_lines = 1; | |
214 | opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED; | |
215 | opts.pathspec.strings = &pathspec; | |
216 | opts.pathspec.count = 1; | |
217 | ||
14a513e0 | 218 | cl_git_pass(git_diff_workdir_to_index(g_repo, &opts, &diff)); |
14a513e0 | 219 | |
f335ecd6 RB |
220 | for (use_iterator = 0; use_iterator <= 1; use_iterator++) { |
221 | memset(&exp, 0, sizeof(exp)); | |
222 | ||
223 | if (use_iterator) | |
224 | cl_git_pass(diff_foreach_via_iterator( | |
225 | diff, &exp, diff_file_fn, NULL, NULL)); | |
226 | else | |
227 | cl_git_pass(git_diff_foreach(diff, &exp, diff_file_fn, NULL, NULL)); | |
228 | ||
229 | cl_assert_equal_i(13, exp.files); | |
230 | cl_assert_equal_i(0, exp.file_adds); | |
231 | cl_assert_equal_i(4, exp.file_dels); | |
232 | cl_assert_equal_i(4, exp.file_mods); | |
233 | cl_assert_equal_i(1, exp.file_ignored); | |
234 | cl_assert_equal_i(4, exp.file_untracked); | |
235 | } | |
14a513e0 RB |
236 | |
237 | git_diff_list_free(diff); | |
238 | ||
14a513e0 RB |
239 | pathspec = "modified_file"; |
240 | ||
241 | cl_git_pass(git_diff_workdir_to_index(g_repo, &opts, &diff)); | |
14a513e0 | 242 | |
f335ecd6 RB |
243 | for (use_iterator = 0; use_iterator <= 1; use_iterator++) { |
244 | memset(&exp, 0, sizeof(exp)); | |
245 | ||
246 | if (use_iterator) | |
247 | cl_git_pass(diff_foreach_via_iterator( | |
248 | diff, &exp, diff_file_fn, NULL, NULL)); | |
249 | else | |
250 | cl_git_pass(git_diff_foreach(diff, &exp, diff_file_fn, NULL, NULL)); | |
251 | ||
252 | cl_assert_equal_i(1, exp.files); | |
253 | cl_assert_equal_i(0, exp.file_adds); | |
254 | cl_assert_equal_i(0, exp.file_dels); | |
255 | cl_assert_equal_i(1, exp.file_mods); | |
256 | cl_assert_equal_i(0, exp.file_ignored); | |
257 | cl_assert_equal_i(0, exp.file_untracked); | |
258 | } | |
14a513e0 RB |
259 | |
260 | git_diff_list_free(diff); | |
261 | ||
14a513e0 RB |
262 | pathspec = "subdir"; |
263 | ||
264 | cl_git_pass(git_diff_workdir_to_index(g_repo, &opts, &diff)); | |
14a513e0 | 265 | |
f335ecd6 RB |
266 | for (use_iterator = 0; use_iterator <= 1; use_iterator++) { |
267 | memset(&exp, 0, sizeof(exp)); | |
268 | ||
269 | if (use_iterator) | |
270 | cl_git_pass(diff_foreach_via_iterator( | |
271 | diff, &exp, diff_file_fn, NULL, NULL)); | |
272 | else | |
273 | cl_git_pass(git_diff_foreach(diff, &exp, diff_file_fn, NULL, NULL)); | |
274 | ||
275 | cl_assert_equal_i(3, exp.files); | |
276 | cl_assert_equal_i(0, exp.file_adds); | |
277 | cl_assert_equal_i(1, exp.file_dels); | |
278 | cl_assert_equal_i(1, exp.file_mods); | |
279 | cl_assert_equal_i(0, exp.file_ignored); | |
280 | cl_assert_equal_i(1, exp.file_untracked); | |
281 | } | |
14a513e0 RB |
282 | |
283 | git_diff_list_free(diff); | |
284 | ||
14a513e0 RB |
285 | pathspec = "*_deleted"; |
286 | ||
287 | cl_git_pass(git_diff_workdir_to_index(g_repo, &opts, &diff)); | |
14a513e0 | 288 | |
f335ecd6 RB |
289 | for (use_iterator = 0; use_iterator <= 1; use_iterator++) { |
290 | memset(&exp, 0, sizeof(exp)); | |
291 | ||
292 | if (use_iterator) | |
293 | cl_git_pass(diff_foreach_via_iterator( | |
294 | diff, &exp, diff_file_fn, NULL, NULL)); | |
295 | else | |
296 | cl_git_pass(git_diff_foreach(diff, &exp, diff_file_fn, NULL, NULL)); | |
297 | ||
298 | cl_assert_equal_i(2, exp.files); | |
299 | cl_assert_equal_i(0, exp.file_adds); | |
300 | cl_assert_equal_i(2, exp.file_dels); | |
301 | cl_assert_equal_i(0, exp.file_mods); | |
302 | cl_assert_equal_i(0, exp.file_ignored); | |
303 | cl_assert_equal_i(0, exp.file_untracked); | |
304 | } | |
14a513e0 RB |
305 | |
306 | git_diff_list_free(diff); | |
307 | } | |
308 | ||
0abd7244 RB |
309 | void test_diff_workdir__filemode_changes(void) |
310 | { | |
311 | git_config *cfg; | |
312 | git_diff_list *diff = NULL; | |
313 | diff_expects exp; | |
f335ecd6 | 314 | int use_iterator; |
0abd7244 RB |
315 | |
316 | if (!cl_is_chmod_supported()) | |
317 | return; | |
318 | ||
319 | g_repo = cl_git_sandbox_init("issue_592"); | |
320 | ||
321 | cl_git_pass(git_repository_config(&cfg, g_repo)); | |
322 | cl_git_pass(git_config_set_bool(cfg, "core.filemode", true)); | |
323 | ||
324 | /* test once with no mods */ | |
325 | ||
326 | cl_git_pass(git_diff_workdir_to_index(g_repo, NULL, &diff)); | |
327 | ||
f335ecd6 RB |
328 | for (use_iterator = 0; use_iterator <= 1; use_iterator++) { |
329 | memset(&exp, 0, sizeof(exp)); | |
0abd7244 | 330 | |
f335ecd6 RB |
331 | if (use_iterator) |
332 | cl_git_pass(diff_foreach_via_iterator( | |
333 | diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
334 | else | |
335 | cl_git_pass(git_diff_foreach( | |
336 | diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
337 | ||
338 | cl_assert_equal_i(0, exp.files); | |
339 | cl_assert_equal_i(0, exp.file_mods); | |
340 | cl_assert_equal_i(0, exp.hunks); | |
341 | } | |
0abd7244 RB |
342 | |
343 | git_diff_list_free(diff); | |
344 | ||
345 | /* chmod file and test again */ | |
346 | ||
347 | cl_assert(cl_toggle_filemode("issue_592/a.txt")); | |
348 | ||
349 | cl_git_pass(git_diff_workdir_to_index(g_repo, NULL, &diff)); | |
350 | ||
f335ecd6 RB |
351 | for (use_iterator = 0; use_iterator <= 1; use_iterator++) { |
352 | memset(&exp, 0, sizeof(exp)); | |
0abd7244 | 353 | |
f335ecd6 RB |
354 | if (use_iterator) |
355 | cl_git_pass(diff_foreach_via_iterator( | |
356 | diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
357 | else | |
358 | cl_git_pass(git_diff_foreach( | |
359 | diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
360 | ||
361 | cl_assert_equal_i(1, exp.files); | |
362 | cl_assert_equal_i(1, exp.file_mods); | |
363 | cl_assert_equal_i(0, exp.hunks); | |
364 | } | |
0abd7244 RB |
365 | |
366 | git_diff_list_free(diff); | |
367 | ||
368 | cl_assert(cl_toggle_filemode("issue_592/a.txt")); | |
369 | git_config_free(cfg); | |
370 | } | |
371 | ||
372 | void test_diff_workdir__filemode_changes_with_filemode_false(void) | |
373 | { | |
374 | git_config *cfg; | |
375 | git_diff_list *diff = NULL; | |
376 | diff_expects exp; | |
377 | ||
378 | if (!cl_is_chmod_supported()) | |
379 | return; | |
380 | ||
381 | g_repo = cl_git_sandbox_init("issue_592"); | |
382 | ||
383 | cl_git_pass(git_repository_config(&cfg, g_repo)); | |
384 | cl_git_pass(git_config_set_bool(cfg, "core.filemode", false)); | |
385 | ||
386 | /* test once with no mods */ | |
387 | ||
388 | cl_git_pass(git_diff_workdir_to_index(g_repo, NULL, &diff)); | |
389 | ||
390 | memset(&exp, 0, sizeof(exp)); | |
391 | cl_git_pass(git_diff_foreach( | |
392 | diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
393 | ||
394 | cl_assert_equal_i(0, exp.files); | |
395 | cl_assert_equal_i(0, exp.file_mods); | |
396 | cl_assert_equal_i(0, exp.hunks); | |
397 | ||
398 | git_diff_list_free(diff); | |
399 | ||
400 | /* chmod file and test again */ | |
401 | ||
402 | cl_assert(cl_toggle_filemode("issue_592/a.txt")); | |
403 | ||
404 | cl_git_pass(git_diff_workdir_to_index(g_repo, NULL, &diff)); | |
405 | ||
406 | memset(&exp, 0, sizeof(exp)); | |
407 | cl_git_pass(git_diff_foreach( | |
408 | diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
409 | ||
410 | cl_assert_equal_i(0, exp.files); | |
411 | cl_assert_equal_i(0, exp.file_mods); | |
412 | cl_assert_equal_i(0, exp.hunks); | |
413 | ||
414 | git_diff_list_free(diff); | |
415 | ||
416 | cl_assert(cl_toggle_filemode("issue_592/a.txt")); | |
417 | git_config_free(cfg); | |
418 | } | |
419 | ||
145e696b RB |
420 | void test_diff_workdir__head_index_and_workdir_all_differ(void) |
421 | { | |
422 | git_diff_options opts = {0}; | |
423 | git_diff_list *diff_i2t = NULL, *diff_w2i = NULL; | |
424 | diff_expects exp; | |
425 | char *pathspec = "staged_changes_modified_file"; | |
426 | git_tree *tree; | |
f335ecd6 | 427 | int use_iterator; |
145e696b RB |
428 | |
429 | /* For this file, | |
430 | * - head->index diff has 1 line of context, 1 line of diff | |
431 | * - index->workdir diff has 2 lines of context, 1 line of diff | |
432 | * but | |
433 | * - head->workdir diff has 1 line of context, 2 lines of diff | |
434 | * Let's make sure the right one is returned from each fn. | |
435 | */ | |
436 | ||
437 | g_repo = cl_git_sandbox_init("status"); | |
438 | ||
439 | tree = resolve_commit_oid_to_tree(g_repo, "26a125ee1bfc5df1e1b2e9441bbe63c8a7ae989f"); | |
440 | ||
441 | opts.pathspec.strings = &pathspec; | |
442 | opts.pathspec.count = 1; | |
443 | ||
444 | cl_git_pass(git_diff_index_to_tree(g_repo, &opts, tree, &diff_i2t)); | |
445 | cl_git_pass(git_diff_workdir_to_index(g_repo, &opts, &diff_w2i)); | |
446 | ||
f335ecd6 RB |
447 | for (use_iterator = 0; use_iterator <= 1; use_iterator++) { |
448 | memset(&exp, 0, sizeof(exp)); | |
449 | ||
450 | if (use_iterator) | |
451 | cl_git_pass(diff_foreach_via_iterator( | |
452 | diff_i2t, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
453 | else | |
454 | cl_git_pass(git_diff_foreach( | |
455 | diff_i2t, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
456 | ||
457 | cl_assert_equal_i(1, exp.files); | |
458 | cl_assert_equal_i(0, exp.file_adds); | |
459 | cl_assert_equal_i(0, exp.file_dels); | |
460 | cl_assert_equal_i(1, exp.file_mods); | |
461 | cl_assert_equal_i(1, exp.hunks); | |
462 | cl_assert_equal_i(2, exp.lines); | |
463 | cl_assert_equal_i(1, exp.line_ctxt); | |
464 | cl_assert_equal_i(1, exp.line_adds); | |
465 | cl_assert_equal_i(0, exp.line_dels); | |
466 | } | |
467 | ||
468 | for (use_iterator = 0; use_iterator <= 1; use_iterator++) { | |
469 | memset(&exp, 0, sizeof(exp)); | |
470 | ||
471 | if (use_iterator) | |
472 | cl_git_pass(diff_foreach_via_iterator( | |
473 | diff_w2i, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
474 | else | |
475 | cl_git_pass(git_diff_foreach( | |
476 | diff_w2i, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
477 | ||
478 | cl_assert_equal_i(1, exp.files); | |
479 | cl_assert_equal_i(0, exp.file_adds); | |
480 | cl_assert_equal_i(0, exp.file_dels); | |
481 | cl_assert_equal_i(1, exp.file_mods); | |
482 | cl_assert_equal_i(1, exp.hunks); | |
483 | cl_assert_equal_i(3, exp.lines); | |
484 | cl_assert_equal_i(2, exp.line_ctxt); | |
485 | cl_assert_equal_i(1, exp.line_adds); | |
486 | cl_assert_equal_i(0, exp.line_dels); | |
487 | } | |
145e696b RB |
488 | |
489 | cl_git_pass(git_diff_merge(diff_i2t, diff_w2i)); | |
490 | ||
f335ecd6 RB |
491 | for (use_iterator = 0; use_iterator <= 1; use_iterator++) { |
492 | memset(&exp, 0, sizeof(exp)); | |
493 | ||
494 | if (use_iterator) | |
495 | cl_git_pass(diff_foreach_via_iterator( | |
496 | diff_i2t, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
497 | else | |
498 | cl_git_pass(git_diff_foreach( | |
499 | diff_i2t, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
500 | ||
501 | cl_assert_equal_i(1, exp.files); | |
502 | cl_assert_equal_i(0, exp.file_adds); | |
503 | cl_assert_equal_i(0, exp.file_dels); | |
504 | cl_assert_equal_i(1, exp.file_mods); | |
505 | cl_assert_equal_i(1, exp.hunks); | |
506 | cl_assert_equal_i(3, exp.lines); | |
507 | cl_assert_equal_i(1, exp.line_ctxt); | |
508 | cl_assert_equal_i(2, exp.line_adds); | |
509 | cl_assert_equal_i(0, exp.line_dels); | |
510 | } | |
145e696b RB |
511 | |
512 | git_diff_list_free(diff_i2t); | |
513 | git_diff_list_free(diff_w2i); | |
cdca82c7 CMN |
514 | |
515 | git_tree_free(tree); | |
145e696b RB |
516 | } |
517 | ||
518 | void test_diff_workdir__eof_newline_changes(void) | |
519 | { | |
520 | git_diff_options opts = {0}; | |
521 | git_diff_list *diff = NULL; | |
522 | diff_expects exp; | |
523 | char *pathspec = "current_file"; | |
f335ecd6 | 524 | int use_iterator; |
145e696b RB |
525 | |
526 | g_repo = cl_git_sandbox_init("status"); | |
527 | ||
528 | opts.pathspec.strings = &pathspec; | |
529 | opts.pathspec.count = 1; | |
530 | ||
531 | cl_git_pass(git_diff_workdir_to_index(g_repo, &opts, &diff)); | |
532 | ||
f335ecd6 RB |
533 | for (use_iterator = 0; use_iterator <= 1; use_iterator++) { |
534 | memset(&exp, 0, sizeof(exp)); | |
535 | ||
536 | if (use_iterator) | |
537 | cl_git_pass(diff_foreach_via_iterator( | |
538 | diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
539 | else | |
540 | cl_git_pass(git_diff_foreach( | |
541 | diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
542 | ||
543 | cl_assert_equal_i(0, exp.files); | |
544 | cl_assert_equal_i(0, exp.file_adds); | |
545 | cl_assert_equal_i(0, exp.file_dels); | |
546 | cl_assert_equal_i(0, exp.file_mods); | |
547 | cl_assert_equal_i(0, exp.hunks); | |
548 | cl_assert_equal_i(0, exp.lines); | |
549 | cl_assert_equal_i(0, exp.line_ctxt); | |
550 | cl_assert_equal_i(0, exp.line_adds); | |
551 | cl_assert_equal_i(0, exp.line_dels); | |
552 | } | |
145e696b RB |
553 | |
554 | git_diff_list_free(diff); | |
555 | ||
556 | cl_git_append2file("status/current_file", "\n"); | |
557 | ||
558 | cl_git_pass(git_diff_workdir_to_index(g_repo, &opts, &diff)); | |
559 | ||
f335ecd6 RB |
560 | for (use_iterator = 0; use_iterator <= 1; use_iterator++) { |
561 | memset(&exp, 0, sizeof(exp)); | |
562 | ||
563 | if (use_iterator) | |
564 | cl_git_pass(diff_foreach_via_iterator( | |
565 | diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
566 | else | |
567 | cl_git_pass(git_diff_foreach( | |
568 | diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
569 | ||
570 | cl_assert_equal_i(1, exp.files); | |
571 | cl_assert_equal_i(0, exp.file_adds); | |
572 | cl_assert_equal_i(0, exp.file_dels); | |
573 | cl_assert_equal_i(1, exp.file_mods); | |
574 | cl_assert_equal_i(1, exp.hunks); | |
575 | cl_assert_equal_i(2, exp.lines); | |
576 | cl_assert_equal_i(1, exp.line_ctxt); | |
577 | cl_assert_equal_i(1, exp.line_adds); | |
578 | cl_assert_equal_i(0, exp.line_dels); | |
579 | } | |
145e696b RB |
580 | |
581 | git_diff_list_free(diff); | |
582 | ||
583 | cl_git_rewritefile("status/current_file", "current_file"); | |
584 | ||
585 | cl_git_pass(git_diff_workdir_to_index(g_repo, &opts, &diff)); | |
586 | ||
f335ecd6 RB |
587 | for (use_iterator = 0; use_iterator <= 1; use_iterator++) { |
588 | memset(&exp, 0, sizeof(exp)); | |
589 | ||
590 | if (use_iterator) | |
591 | cl_git_pass(diff_foreach_via_iterator( | |
592 | diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
593 | else | |
594 | cl_git_pass(git_diff_foreach( | |
595 | diff, &exp, diff_file_fn, diff_hunk_fn, diff_line_fn)); | |
596 | ||
597 | cl_assert_equal_i(1, exp.files); | |
598 | cl_assert_equal_i(0, exp.file_adds); | |
599 | cl_assert_equal_i(0, exp.file_dels); | |
600 | cl_assert_equal_i(1, exp.file_mods); | |
601 | cl_assert_equal_i(1, exp.hunks); | |
602 | cl_assert_equal_i(3, exp.lines); | |
603 | cl_assert_equal_i(0, exp.line_ctxt); | |
604 | cl_assert_equal_i(1, exp.line_adds); | |
605 | cl_assert_equal_i(2, exp.line_dels); | |
606 | } | |
145e696b RB |
607 | |
608 | git_diff_list_free(diff); | |
609 | } | |
610 | ||
74fa4bfa RB |
611 | /* PREPARATION OF TEST DATA |
612 | * | |
613 | * Since there is no command line equivalent of git_diff_workdir_to_tree, | |
614 | * it was a bit of a pain to confirm that I was getting the expected | |
615 | * results in the first part of this tests. Here is what I ended up | |
616 | * doing to set my expectation for the file counts and results: | |
617 | * | |
618 | * Running "git ls-tree 26a125" and "git ls-tree aa27a6" shows: | |
619 | * | |
620 | * A a0de7e0ac200c489c41c59dfa910154a70264e6e current_file | |
621 | * B 5452d32f1dd538eb0405e8a83cc185f79e25e80f file_deleted | |
622 | * C 452e4244b5d083ddf0460acf1ecc74db9dcfa11a modified_file | |
623 | * D 32504b727382542f9f089e24fddac5e78533e96c staged_changes | |
624 | * E 061d42a44cacde5726057b67558821d95db96f19 staged_changes_file_deleted | |
625 | * F 70bd9443ada07063e7fbf0b3ff5c13f7494d89c2 staged_changes_modified_file | |
626 | * G e9b9107f290627c04d097733a10055af941f6bca staged_delete_file_deleted | |
627 | * H dabc8af9bd6e9f5bbe96a176f1a24baf3d1f8916 staged_delete_modified_file | |
628 | * I 53ace0d1cc1145a5f4fe4f78a186a60263190733 subdir/current_file | |
629 | * J 1888c805345ba265b0ee9449b8877b6064592058 subdir/deleted_file | |
630 | * K a6191982709b746d5650e93c2acf34ef74e11504 subdir/modified_file | |
631 | * L e8ee89e15bbe9b20137715232387b3de5b28972e subdir.txt | |
632 | * | |
633 | * -------- | |
634 | * | |
635 | * find . ! -path ./.git/\* -a -type f | git hash-object --stdin-paths | |
636 | * | |
637 | * A a0de7e0ac200c489c41c59dfa910154a70264e6e current_file | |
638 | * M 6a79f808a9c6bc9531ac726c184bbcd9351ccf11 ignored_file | |
639 | * C 0a539630525aca2e7bc84975958f92f10a64c9b6 modified_file | |
640 | * N d4fa8600b4f37d7516bef4816ae2c64dbf029e3a new_file | |
641 | * D 55d316c9ba708999f1918e9677d01dfcae69c6b9 staged_changes | |
642 | * F 011c3440d5c596e21d836aa6d7b10eb581f68c49 staged_changes_modified_file | |
643 | * H dabc8af9bd6e9f5bbe96a176f1a24baf3d1f8916 staged_delete_modified_file | |
644 | * O 529a16e8e762d4acb7b9636ff540a00831f9155a staged_new_file | |
645 | * P 8b090c06d14ffa09c4e880088ebad33893f921d1 staged_new_file_modified_file | |
646 | * I 53ace0d1cc1145a5f4fe4f78a186a60263190733 subdir/current_file | |
647 | * K 57274b75eeb5f36fd55527806d567b2240a20c57 subdir/modified_file | |
648 | * Q 80a86a6931b91bc01c2dbf5ca55bdd24ad1ef466 subdir/new_file | |
649 | * L e8ee89e15bbe9b20137715232387b3de5b28972e subdir.txt | |
650 | * | |
651 | * -------- | |
652 | * | |
653 | * A - current_file (UNMODIFIED) -> not in results | |
654 | * B D file_deleted | |
655 | * M I ignored_file (IGNORED) | |
656 | * C M modified_file | |
657 | * N U new_file (UNTRACKED) | |
658 | * D M staged_changes | |
659 | * E D staged_changes_file_deleted | |
660 | * F M staged_changes_modified_file | |
661 | * G D staged_delete_file_deleted | |
662 | * H - staged_delete_modified_file (UNMODIFIED) -> not in results | |
663 | * O U staged_new_file | |
664 | * P U staged_new_file_modified_file | |
665 | * I - subdir/current_file (UNMODIFIED) -> not in results | |
666 | * J D subdir/deleted_file | |
667 | * K M subdir/modified_file | |
668 | * Q U subdir/new_file | |
669 | * L - subdir.txt (UNMODIFIED) -> not in results | |
670 | * | |
671 | * Expect 13 files, 0 ADD, 4 DEL, 4 MOD, 1 IGN, 4 UNTR | |
672 | */ |