1 #include "clar_libgit2.h"
2 #include "diff_helpers.h"
4 static git_repository
*g_repo
= NULL
;
6 void test_diff_notify__initialize(void)
10 void test_diff_notify__cleanup(void)
12 cl_git_sandbox_cleanup();
15 static int assert_called_notifications(
16 const git_diff
*diff_so_far
,
17 const git_diff_delta
*delta_to_add
,
18 const char *matched_pathspec
,
22 notify_expected
*exp
= (notify_expected
*)payload
;
25 GIT_UNUSED(diff_so_far
);
27 for (e
= exp
; e
->path
!= NULL
; e
++) {
28 if (strcmp(e
->path
, delta_to_add
->new_file
.path
))
31 cl_assert_equal_s(e
->matched_pathspec
, matched_pathspec
);
41 static void test_notify(
42 char **searched_pathspecs
,
44 notify_expected
*expected_matched_pathspecs
,
45 int expected_diffed_files_count
)
47 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
48 git_diff
*diff
= NULL
;
51 g_repo
= cl_git_sandbox_init("status");
53 opts
.flags
|= GIT_DIFF_INCLUDE_IGNORED
| GIT_DIFF_INCLUDE_UNTRACKED
;
54 opts
.notify_cb
= assert_called_notifications
;
55 opts
.pathspec
.strings
= searched_pathspecs
;
56 opts
.pathspec
.count
= pathspecs_count
;
58 opts
.payload
= expected_matched_pathspecs
;
59 memset(&exp
, 0, sizeof(exp
));
61 cl_git_pass(git_diff_index_to_workdir(&diff
, g_repo
, NULL
, &opts
));
62 cl_git_pass(git_diff_foreach(diff
, diff_file_cb
, NULL
, NULL
, NULL
, &exp
));
64 cl_assert_equal_i(expected_diffed_files_count
, exp
.files
);
69 void test_diff_notify__notify_single_pathspec(void)
71 char *searched_pathspecs
[] = {
74 notify_expected expected_matched_pathspecs
[] = {
75 { "file_deleted", "*_deleted" },
76 { "staged_changes_file_deleted", "*_deleted" },
80 test_notify(searched_pathspecs
, 1, expected_matched_pathspecs
, 2);
83 void test_diff_notify__notify_multiple_pathspec(void)
85 char *searched_pathspecs
[] = {
86 "staged_changes_cant_find_me",
87 "subdir/modified_cant_find_me",
91 notify_expected expected_matched_pathspecs
[] = {
92 { "staged_changes_file_deleted", "staged*" },
93 { "staged_changes_modified_file", "staged*" },
94 { "staged_delete_modified_file", "staged*" },
95 { "staged_new_file_deleted_file", "staged*" },
96 { "staged_new_file_modified_file", "staged*" },
97 { "subdir/deleted_file", "subdir/*" },
98 { "subdir/modified_file", "subdir/*" },
99 { "subdir/new_file", "subdir/*" },
103 test_notify(searched_pathspecs
, 4, expected_matched_pathspecs
, 8);
106 void test_diff_notify__notify_catchall_with_empty_pathspecs(void)
108 char *searched_pathspecs
[] = {
112 notify_expected expected_matched_pathspecs
[] = {
113 { "file_deleted", NULL
},
114 { "ignored_file", NULL
},
115 { "modified_file", NULL
},
116 { "new_file", NULL
},
117 { "\xe8\xbf\x99", NULL
},
118 { "staged_changes_file_deleted", NULL
},
119 { "staged_changes_modified_file", NULL
},
120 { "staged_delete_modified_file", NULL
},
121 { "staged_new_file_deleted_file", NULL
},
122 { "staged_new_file_modified_file", NULL
},
123 { "subdir/deleted_file", NULL
},
124 { "subdir/modified_file", NULL
},
125 { "subdir/new_file", NULL
},
129 test_notify(searched_pathspecs
, 1, expected_matched_pathspecs
, 13);
132 void test_diff_notify__notify_catchall(void)
134 char *searched_pathspecs
[] = {
137 notify_expected expected_matched_pathspecs
[] = {
138 { "file_deleted", "*" },
139 { "ignored_file", "*" },
140 { "modified_file", "*" },
142 { "\xe8\xbf\x99", "*" },
143 { "staged_changes_file_deleted", "*" },
144 { "staged_changes_modified_file", "*" },
145 { "staged_delete_modified_file", "*" },
146 { "staged_new_file_deleted_file", "*" },
147 { "staged_new_file_modified_file", "*" },
148 { "subdir/deleted_file", "*" },
149 { "subdir/modified_file", "*" },
150 { "subdir/new_file", "*" },
154 test_notify(searched_pathspecs
, 1, expected_matched_pathspecs
, 13);
157 static int abort_diff(
158 const git_diff
*diff_so_far
,
159 const git_diff_delta
*delta_to_add
,
160 const char *matched_pathspec
,
163 GIT_UNUSED(diff_so_far
);
164 GIT_UNUSED(delta_to_add
);
165 GIT_UNUSED(matched_pathspec
);
171 void test_diff_notify__notify_cb_can_abort_diff(void)
173 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
174 git_diff
*diff
= NULL
;
175 char *pathspec
= NULL
;
177 g_repo
= cl_git_sandbox_init("status");
179 opts
.flags
|= GIT_DIFF_INCLUDE_IGNORED
| GIT_DIFF_INCLUDE_UNTRACKED
;
180 opts
.notify_cb
= abort_diff
;
181 opts
.pathspec
.strings
= &pathspec
;
182 opts
.pathspec
.count
= 1;
184 pathspec
= "file_deleted";
186 git_diff_index_to_workdir(&diff
, g_repo
, NULL
, &opts
), -42);
188 pathspec
= "staged_changes_modified_file";
190 git_diff_index_to_workdir(&diff
, g_repo
, NULL
, &opts
), -42);
193 static int filter_all(
194 const git_diff
*diff_so_far
,
195 const git_diff_delta
*delta_to_add
,
196 const char *matched_pathspec
,
199 GIT_UNUSED(diff_so_far
);
200 GIT_UNUSED(delta_to_add
);
201 GIT_UNUSED(matched_pathspec
);
207 void test_diff_notify__notify_cb_can_be_used_as_filtering_function(void)
209 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
210 git_diff
*diff
= NULL
;
211 char *pathspec
= NULL
;
214 g_repo
= cl_git_sandbox_init("status");
216 opts
.flags
|= GIT_DIFF_INCLUDE_IGNORED
| GIT_DIFF_INCLUDE_UNTRACKED
;
217 opts
.notify_cb
= filter_all
;
218 opts
.pathspec
.strings
= &pathspec
;
219 opts
.pathspec
.count
= 1;
221 pathspec
= "*_deleted";
222 memset(&exp
, 0, sizeof(exp
));
224 cl_git_pass(git_diff_index_to_workdir(&diff
, g_repo
, NULL
, &opts
));
225 cl_git_pass(git_diff_foreach(diff
, diff_file_cb
, NULL
, NULL
, NULL
, &exp
));
227 cl_assert_equal_i(0, exp
.files
);
232 static int progress_abort_diff(
233 const git_diff
*diff_so_far
,
234 const char *old_path
,
235 const char *new_path
,
238 GIT_UNUSED(diff_so_far
);
239 GIT_UNUSED(old_path
);
240 GIT_UNUSED(new_path
);
246 void test_diff_notify__progress_cb_can_abort_diff(void)
248 git_diff_options opts
= GIT_DIFF_OPTIONS_INIT
;
249 git_diff
*diff
= NULL
;
251 g_repo
= cl_git_sandbox_init("status");
253 opts
.flags
|= GIT_DIFF_INCLUDE_IGNORED
| GIT_DIFF_INCLUDE_UNTRACKED
;
254 opts
.progress_cb
= progress_abort_diff
;
257 git_diff_index_to_workdir(&diff
, g_repo
, NULL
, &opts
), -42);