1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
#include "clar_libgit2.h"
#include "diff_helpers.h"
void test_diff_diffiter__initialize(void)
{
}
void test_diff_diffiter__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_diff_diffiter__create(void)
{
git_repository *repo = cl_git_sandbox_init("attr");
git_diff_list *diff;
size_t d, num_d;
cl_git_pass(git_diff_workdir_to_index(repo, NULL, &diff));
num_d = git_diff_num_deltas(diff);
for (d = 0; d < num_d; ++d) {
const git_diff_delta *delta;
cl_git_pass(git_diff_get_patch(NULL, &delta, diff, d));
}
git_diff_list_free(diff);
}
void test_diff_diffiter__iterate_files(void)
{
git_repository *repo = cl_git_sandbox_init("attr");
git_diff_list *diff;
size_t d, num_d;
int count = 0;
cl_git_pass(git_diff_workdir_to_index(repo, NULL, &diff));
num_d = git_diff_num_deltas(diff);
cl_assert_equal_i(6, (int)num_d);
for (d = 0; d < num_d; ++d) {
const git_diff_delta *delta;
cl_git_pass(git_diff_get_patch(NULL, &delta, diff, d));
cl_assert(delta != NULL);
count++;
}
cl_assert_equal_i(6, count);
git_diff_list_free(diff);
}
void test_diff_diffiter__iterate_files_2(void)
{
git_repository *repo = cl_git_sandbox_init("status");
git_diff_list *diff;
size_t d, num_d;
int count = 0;
cl_git_pass(git_diff_workdir_to_index(repo, NULL, &diff));
num_d = git_diff_num_deltas(diff);
cl_assert_equal_i(8, (int)num_d);
for (d = 0; d < num_d; ++d) {
const git_diff_delta *delta;
cl_git_pass(git_diff_get_patch(NULL, &delta, diff, d));
cl_assert(delta != NULL);
count++;
}
cl_assert_equal_i(8, count);
git_diff_list_free(diff);
}
void test_diff_diffiter__iterate_files_and_hunks(void)
{
git_repository *repo = cl_git_sandbox_init("status");
git_diff_options opts = {0};
git_diff_list *diff = NULL;
size_t d, num_d;
int file_count = 0, hunk_count = 0;
opts.context_lines = 3;
opts.interhunk_lines = 1;
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
cl_git_pass(git_diff_workdir_to_index(repo, &opts, &diff));
num_d = git_diff_num_deltas(diff);
for (d = 0; d < num_d; ++d) {
git_diff_patch *patch;
const git_diff_delta *delta;
size_t h, num_h;
cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d));
cl_assert(delta);
cl_assert(patch);
file_count++;
num_h = git_diff_patch_num_hunks(patch);
for (h = 0; h < num_h; h++) {
const git_diff_range *range;
const char *header;
size_t header_len, num_l;
cl_git_pass(git_diff_patch_get_hunk(
&range, &header, &header_len, &num_l, patch, h));
cl_assert(range);
cl_assert(header);
hunk_count++;
}
git_diff_patch_free(patch);
}
cl_assert_equal_i(13, file_count);
cl_assert_equal_i(8, hunk_count);
git_diff_list_free(diff);
}
void test_diff_diffiter__max_size_threshold(void)
{
git_repository *repo = cl_git_sandbox_init("status");
git_diff_options opts = {0};
git_diff_list *diff = NULL;
int file_count = 0, binary_count = 0, hunk_count = 0;
size_t d, num_d;
opts.context_lines = 3;
opts.interhunk_lines = 1;
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
cl_git_pass(git_diff_workdir_to_index(repo, &opts, &diff));
num_d = git_diff_num_deltas(diff);
for (d = 0; d < num_d; ++d) {
git_diff_patch *patch;
const git_diff_delta *delta;
cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d));
cl_assert(delta);
cl_assert(patch);
file_count++;
hunk_count += (int)git_diff_patch_num_hunks(patch);
assert(delta->binary == 0 || delta->binary == 1);
binary_count += delta->binary;
git_diff_patch_free(patch);
}
cl_assert_equal_i(13, file_count);
cl_assert_equal_i(0, binary_count);
cl_assert_equal_i(8, hunk_count);
git_diff_list_free(diff);
/* try again with low file size threshold */
file_count = binary_count = hunk_count = 0;
opts.context_lines = 3;
opts.interhunk_lines = 1;
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
opts.max_size = 50; /* treat anything over 50 bytes as binary! */
cl_git_pass(git_diff_workdir_to_index(repo, &opts, &diff));
num_d = git_diff_num_deltas(diff);
for (d = 0; d < num_d; ++d) {
git_diff_patch *patch;
const git_diff_delta *delta;
cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d));
file_count++;
hunk_count += (int)git_diff_patch_num_hunks(patch);
assert(delta->binary == 0 || delta->binary == 1);
binary_count += delta->binary;
git_diff_patch_free(patch);
}
cl_assert_equal_i(13, file_count);
/* Three files are over the 50 byte threshold:
* - staged_changes_file_deleted
* - staged_changes_modified_file
* - staged_new_file_modified_file
*/
cl_assert_equal_i(3, binary_count);
cl_assert_equal_i(5, hunk_count);
git_diff_list_free(diff);
}
void test_diff_diffiter__iterate_all(void)
{
git_repository *repo = cl_git_sandbox_init("status");
git_diff_options opts = {0};
git_diff_list *diff = NULL;
diff_expects exp = {0};
size_t d, num_d;
opts.context_lines = 3;
opts.interhunk_lines = 1;
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
cl_git_pass(git_diff_workdir_to_index(repo, &opts, &diff));
num_d = git_diff_num_deltas(diff);
for (d = 0; d < num_d; ++d) {
git_diff_patch *patch;
const git_diff_delta *delta;
size_t h, num_h;
cl_git_pass(git_diff_get_patch(&patch, &delta, diff, d));
cl_assert(patch && delta);
exp.files++;
num_h = git_diff_patch_num_hunks(patch);
for (h = 0; h < num_h; h++) {
const git_diff_range *range;
const char *header;
size_t header_len, l, num_l;
cl_git_pass(git_diff_patch_get_hunk(
&range, &header, &header_len, &num_l, patch, h));
cl_assert(range && header);
exp.hunks++;
for (l = 0; l < num_l; ++l) {
char origin;
const char *content;
size_t content_len;
cl_git_pass(git_diff_patch_get_line_in_hunk(
&origin, &content, &content_len, NULL, NULL, patch, h, l));
cl_assert(content);
exp.lines++;
}
}
git_diff_patch_free(patch);
}
cl_assert_equal_i(13, exp.files);
cl_assert_equal_i(8, exp.hunks);
cl_assert_equal_i(14, exp.lines);
git_diff_list_free(diff);
}
static void iterate_over_patch(git_diff_patch *patch, diff_expects *exp)
{
size_t h, num_h = git_diff_patch_num_hunks(patch), num_l;
exp->files++;
exp->hunks += (int)num_h;
/* let's iterate in reverse, just because we can! */
for (h = 1, num_l = 0; h <= num_h; ++h)
num_l += git_diff_patch_num_lines_in_hunk(patch, num_h - h);
exp->lines += (int)num_l;
}
#define PATCH_CACHE 5
void test_diff_diffiter__iterate_randomly_while_saving_state(void)
{
git_repository *repo = cl_git_sandbox_init("status");
git_diff_options opts = {0};
git_diff_list *diff = NULL;
diff_expects exp = {0};
git_diff_patch *patches[PATCH_CACHE];
size_t p, d, num_d;
memset(patches, 0, sizeof(patches));
opts.context_lines = 3;
opts.interhunk_lines = 1;
opts.flags |= GIT_DIFF_INCLUDE_IGNORED | GIT_DIFF_INCLUDE_UNTRACKED;
cl_git_pass(git_diff_workdir_to_index(repo, &opts, &diff));
num_d = git_diff_num_deltas(diff);
/* To make sure that references counts work for diff and patch objects,
* this generates patches and randomly caches them. Only when the patch
* is removed from the cache are hunks and lines counted. At the end,
* there are still patches in the cache, so free the diff and try to
* process remaining patches after the diff is freed.
*/
srand(121212);
p = rand() % PATCH_CACHE;
for (d = 0; d < num_d; ++d) {
/* take old patch */
git_diff_patch *patch = patches[p];
patches[p] = NULL;
/* cache new patch */
cl_git_pass(git_diff_get_patch(&patches[p], NULL, diff, d));
cl_assert(patches[p] != NULL);
/* process old patch if non-NULL */
if (patch != NULL) {
iterate_over_patch(patch, &exp);
git_diff_patch_free(patch);
}
p = rand() % PATCH_CACHE;
}
/* free diff list now - refcounts should keep things safe */
git_diff_list_free(diff);
/* process remaining unprocessed patches */
for (p = 0; p < PATCH_CACHE; p++) {
git_diff_patch *patch = patches[p];
if (patch != NULL) {
iterate_over_patch(patch, &exp);
git_diff_patch_free(patch);
}
}
/* hopefully it all still added up right */
cl_assert_equal_i(13, exp.files);
cl_assert_equal_i(8, exp.hunks);
cl_assert_equal_i(14, exp.lines);
}