tree.c 17.3 KB
Newer Older
1 2 3 4
#include "clar_libgit2.h"
#include "diff_helpers.h"

static git_repository *g_repo = NULL;
5
static git_diff_options opts;
6
static git_diff *diff;
7
static git_tree *a, *b;
8
static diff_expects expect;
9 10 11

void test_diff_tree__initialize(void)
{
12
	cl_git_pass(git_diff_options_init(&opts, GIT_DIFF_OPTIONS_VERSION));
13

14
	memset(&expect, 0, sizeof(expect));
15 16 17 18

	diff = NULL;
	a = NULL;
	b = NULL;
19 20 21 22
}

void test_diff_tree__cleanup(void)
{
23
	git_diff_free(diff);
24 25 26
	git_tree_free(a);
	git_tree_free(b);

27
	cl_git_sandbox_cleanup();
28

29 30 31 32 33 34 35 36
}

void test_diff_tree__0(void)
{
	/* grabbed a couple of commit oids from the history of the attr repo */
	const char *a_commit = "605812a";
	const char *b_commit = "370fe9ec22";
	const char *c_commit = "f5b0af1fb4f5c";
37
	git_tree *c;
38

39 40 41 42 43
	g_repo = cl_git_sandbox_init("attr");

	cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL);
	cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL);
	cl_assert((c = resolve_commit_oid_to_tree(g_repo, c_commit)) != NULL);
44 45

	opts.context_lines = 1;
46
	opts.interhunk_lines = 1;
47 48


49
	cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts));
50 51

	cl_git_pass(git_diff_foreach(
52
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expect));
53

54 55 56 57
	cl_assert_equal_i(5, expect.files);
	cl_assert_equal_i(2, expect.file_status[GIT_DELTA_ADDED]);
	cl_assert_equal_i(1, expect.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(2, expect.file_status[GIT_DELTA_MODIFIED]);
58

59
	cl_assert_equal_i(5, expect.hunks);
60

61 62 63 64
	cl_assert_equal_i(7 + 24 + 1 + 6 + 6, expect.lines);
	cl_assert_equal_i(1, expect.line_ctxt);
	cl_assert_equal_i(24 + 1 + 5 + 5, expect.line_adds);
	cl_assert_equal_i(7 + 1, expect.line_dels);
65

66
	git_diff_free(diff);
67 68
	diff = NULL;

69
	memset(&expect, 0, sizeof(expect));
70

71
	cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, c, b, &opts));
72 73

	cl_git_pass(git_diff_foreach(
74
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expect));
75

76 77 78 79
	cl_assert_equal_i(2, expect.files);
	cl_assert_equal_i(0, expect.file_status[GIT_DELTA_ADDED]);
	cl_assert_equal_i(0, expect.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(2, expect.file_status[GIT_DELTA_MODIFIED]);
80

81
	cl_assert_equal_i(2, expect.hunks);
82

83 84 85 86
	cl_assert_equal_i(8 + 15, expect.lines);
	cl_assert_equal_i(1, expect.line_ctxt);
	cl_assert_equal_i(1, expect.line_adds);
	cl_assert_equal_i(7 + 14, expect.line_dels);
87 88 89

	git_tree_free(c);
}
90

Russell Belfer committed
91
#define DIFF_OPTS(FLAGS, CTXT) \
92
	{GIT_DIFF_OPTIONS_VERSION, (FLAGS), GIT_SUBMODULE_IGNORE_UNSPECIFIED, \
93
	{NULL,0}, NULL, NULL, NULL, (CTXT), 1}
Russell Belfer committed
94

95 96 97 98 99 100 101
void test_diff_tree__options(void)
{
	/* grabbed a couple of commit oids from the history of the attr repo */
	const char *a_commit = "6bab5c79cd5140d0";
	const char *b_commit = "605812ab7fe421fdd";
	const char *c_commit = "f5b0af1fb4f5";
	const char *d_commit = "a97cc019851";
102
	git_tree *c, *d;
103
	diff_expects actual;
104 105 106
	int test_ab_or_cd[] = { 0, 0, 0, 0, 1, 1, 1, 1, 1 };
	git_diff_options test_options[] = {
		/* a vs b tests */
Russell Belfer committed
107 108 109 110
		DIFF_OPTS(GIT_DIFF_NORMAL, 1),
		DIFF_OPTS(GIT_DIFF_NORMAL, 3),
		DIFF_OPTS(GIT_DIFF_REVERSE, 2),
		DIFF_OPTS(GIT_DIFF_FORCE_TEXT, 2),
111
		/* c vs d tests */
Russell Belfer committed
112 113 114 115 116
		DIFF_OPTS(GIT_DIFF_NORMAL, 3),
		DIFF_OPTS(GIT_DIFF_IGNORE_WHITESPACE, 3),
		DIFF_OPTS(GIT_DIFF_IGNORE_WHITESPACE_CHANGE, 3),
		DIFF_OPTS(GIT_DIFF_IGNORE_WHITESPACE_EOL, 3),
		DIFF_OPTS(GIT_DIFF_IGNORE_WHITESPACE | GIT_DIFF_REVERSE, 1),
117
	};
Vicent Marti committed
118

119 120 121 122 123 124
	/* to generate these values:
	 * - cd to tests/resources/attr,
	 * - mv .gitted .git
	 * - git diff [options] 6bab5c79cd5140d0 605812ab7fe421fdd
	 * - mv .git .gitted
	 */
125
#define EXPECT_STATUS_ADM(ADDS,DELS,MODS) { 0, ADDS, DELS, MODS, 0, 0, 0, 0, 0 }
Vicent Marti committed
126

127 128
	diff_expects test_expects[] = {
		/* a vs b tests */
129 130 131 132
		{ 5, 0, EXPECT_STATUS_ADM(3, 0, 2), 4, 0, 0, 51, 2, 46, 3 },
		{ 5, 0, EXPECT_STATUS_ADM(3, 0, 2), 4, 0, 0, 53, 4, 46, 3 },
		{ 5, 0, EXPECT_STATUS_ADM(0, 3, 2), 4, 0, 0, 52, 3, 3, 46 },
		{ 5, 0, EXPECT_STATUS_ADM(3, 0, 2), 5, 0, 0, 54, 3, 47, 4 },
133
		/* c vs d tests */
134 135 136 137 138
		{ 1, 0, EXPECT_STATUS_ADM(0, 0, 1), 1, 0, 0, 22, 9, 10, 3 },
		{ 1, 0, EXPECT_STATUS_ADM(0, 0, 1), 1, 0, 0, 19, 12, 7, 0 },
		{ 1, 0, EXPECT_STATUS_ADM(0, 0, 1), 1, 0, 0, 20, 11, 8, 1 },
		{ 1, 0, EXPECT_STATUS_ADM(0, 0, 1), 1, 0, 0, 20, 11, 8, 1 },
		{ 1, 0, EXPECT_STATUS_ADM(0, 0, 1), 1, 0, 0, 18, 11, 0, 7 },
139 140
		{ 0 },
	};
141
	diff_expects *expected;
142
	int i, j;
143

144 145 146 147 148 149
	g_repo = cl_git_sandbox_init("attr");

	cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL);
	cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL);
	cl_assert((c = resolve_commit_oid_to_tree(g_repo, c_commit)) != NULL);
	cl_assert((d = resolve_commit_oid_to_tree(g_repo, d_commit)) != NULL);
150 151

	for (i = 0; test_expects[i].files > 0; i++) {
152
		memset(&actual, 0, sizeof(actual)); /* clear accumulator */
153 154 155
		opts = test_options[i];

		if (test_ab_or_cd[i] == 0)
156
			cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts));
157
		else
158
			cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, c, d, &opts));
159 160

		cl_git_pass(git_diff_foreach(
161
			diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &actual));
162 163

		expected = &test_expects[i];
164
		cl_assert_equal_i(actual.files,     expected->files);
165 166
		for (j = GIT_DELTA_UNMODIFIED; j <= GIT_DELTA_TYPECHANGE; ++j)
			cl_assert_equal_i(expected->file_status[j], actual.file_status[j]);
167 168 169 170 171
		cl_assert_equal_i(actual.hunks,     expected->hunks);
		cl_assert_equal_i(actual.lines,     expected->lines);
		cl_assert_equal_i(actual.line_ctxt, expected->line_ctxt);
		cl_assert_equal_i(actual.line_adds, expected->line_adds);
		cl_assert_equal_i(actual.line_dels, expected->line_dels);
172

173
		git_diff_free(diff);
174 175 176 177 178 179
		diff = NULL;
	}

	git_tree_free(c);
	git_tree_free(d);
}
180 181 182 183 184 185 186 187 188 189 190 191 192 193

void test_diff_tree__bare(void)
{
	const char *a_commit = "8496071c1b46c85";
	const char *b_commit = "be3563ae3f79";

	g_repo = cl_git_sandbox_init("testrepo.git");

	cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL);
	cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL);

	opts.context_lines = 1;
	opts.interhunk_lines = 1;

194
	cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts));
195 196

	cl_git_pass(git_diff_foreach(
197
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expect));
198

199 200 201 202
	cl_assert_equal_i(3, expect.files);
	cl_assert_equal_i(2, expect.file_status[GIT_DELTA_ADDED]);
	cl_assert_equal_i(0, expect.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(1, expect.file_status[GIT_DELTA_MODIFIED]);
203

204
	cl_assert_equal_i(3, expect.hunks);
205

206 207 208 209
	cl_assert_equal_i(4, expect.lines);
	cl_assert_equal_i(0, expect.line_ctxt);
	cl_assert_equal_i(3, expect.line_adds);
	cl_assert_equal_i(1, expect.line_dels);
210
}
211 212 213 214 215 216 217

void test_diff_tree__merge(void)
{
	/* grabbed a couple of commit oids from the history of the attr repo */
	const char *a_commit = "605812a";
	const char *b_commit = "370fe9ec22";
	const char *c_commit = "f5b0af1fb4f5c";
218
	git_tree *c;
219
	git_diff *diff1 = NULL, *diff2 = NULL;
220 221 222 223 224 225 226

	g_repo = cl_git_sandbox_init("attr");

	cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL);
	cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL);
	cl_assert((c = resolve_commit_oid_to_tree(g_repo, c_commit)) != NULL);

227
	cl_git_pass(git_diff_tree_to_tree(&diff1, g_repo, a, b, NULL));
228

229
	cl_git_pass(git_diff_tree_to_tree(&diff2, g_repo, c, b, NULL));
230 231 232 233 234

	git_tree_free(c);

	cl_git_pass(git_diff_merge(diff1, diff2));

235
	git_diff_free(diff2);
236 237

	cl_git_pass(git_diff_foreach(
238
		diff1, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expect));
239

240 241 242 243
	cl_assert_equal_i(6, expect.files);
	cl_assert_equal_i(2, expect.file_status[GIT_DELTA_ADDED]);
	cl_assert_equal_i(1, expect.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(3, expect.file_status[GIT_DELTA_MODIFIED]);
244

245
	cl_assert_equal_i(6, expect.hunks);
246

247 248 249 250
	cl_assert_equal_i(59, expect.lines);
	cl_assert_equal_i(1, expect.line_ctxt);
	cl_assert_equal_i(36, expect.line_adds);
	cl_assert_equal_i(22, expect.line_dels);
251

252
	git_diff_free(diff1);
253
}
254 255 256 257 258

void test_diff_tree__larger_hunks(void)
{
	const char *a_commit = "d70d245ed97ed2aa596dd1af6536e4bfdb047b69";
	const char *b_commit = "7a9e0b02e63179929fed24f0a3e0f19168114d10";
259
	size_t d, num_d, h, num_h, l, num_l;
260
	git_patch *patch;
261 262
	const git_diff_hunk *hunk;
	const git_diff_line *line;
263 264 265 266 267 268 269 270 271

	g_repo = cl_git_sandbox_init("diff");

	cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL);
	cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL);

	opts.context_lines = 1;
	opts.interhunk_lines = 0;

272
	cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts));
273

274 275
	num_d = git_diff_num_deltas(diff);
	for (d = 0; d < num_d; ++d) {
Russell Belfer committed
276 277
		cl_git_pass(git_patch_from_diff(&patch, diff, d));
		cl_assert(patch);
278

279
		num_h = git_patch_num_hunks(patch);
280
		for (h = 0; h < num_h; h++) {
281
			cl_git_pass(git_patch_get_hunk(&hunk, &num_l, patch, h));
282

283
			for (l = 0; l < num_l; ++l) {
284
				cl_git_pass(git_patch_get_line_in_hunk(&line, patch, h, l));
285 286
				cl_assert(line);
			}
287

288
			cl_git_fail(git_patch_get_line_in_hunk(&line, patch, h, num_l));
289 290
		}

291
		cl_git_fail(git_patch_get_hunk(&hunk, &num_l, patch, num_h));
292

293
		git_patch_free(patch);
294 295
	}

Russell Belfer committed
296
	cl_git_fail(git_patch_from_diff(&patch, diff, num_d));
297

Russell Belfer committed
298
	cl_assert_equal_i(2, (int)num_d);
299
}
300 301 302 303 304 305 306 307 308 309 310 311 312 313

void test_diff_tree__checks_options_version(void)
{
	const char *a_commit = "8496071c1b46c85";
	const char *b_commit = "be3563ae3f79";
	const git_error *err;

	g_repo = cl_git_sandbox_init("testrepo.git");

	cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL);
	cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL);

	opts.version = 0;
	cl_git_fail(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts));
314 315
	err = git_error_last();
	cl_assert_equal_i(GIT_ERROR_INVALID, err->klass);
316

317
	git_error_clear();
318 319
	opts.version = 1024;
	cl_git_fail(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts));
320
	err = git_error_last();
321
}
322 323 324 325 326 327 328 329 330 331 332 333 334

void process_tree_to_tree_diffing(
	const char *old_commit,
	const char *new_commit)
{
	g_repo = cl_git_sandbox_init("unsymlinked.git");

	cl_assert((a = resolve_commit_oid_to_tree(g_repo, old_commit)) != NULL);
	cl_assert((b = resolve_commit_oid_to_tree(g_repo, new_commit)) != NULL);

	cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts));

	cl_git_pass(git_diff_foreach(
335
		diff, diff_file_cb, NULL, NULL, NULL, &expect));
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
}

void test_diff_tree__symlink_blob_mode_changed_to_regular_file(void)
{
	/*
	* $ git diff  7fccd7..806999
	* diff --git a/include/Nu/Nu.h b/include/Nu/Nu.h
	* deleted file mode 120000
	* index 19bf568..0000000
	* --- a/include/Nu/Nu.h
	* +++ /dev/null
	* @@ -1 +0,0 @@
	* -../../objc/Nu.h
	* \ No newline at end of file
	* diff --git a/include/Nu/Nu.h b/include/Nu/Nu.h
	* new file mode 100644
	* index 0000000..f9e6561
	* --- /dev/null
	* +++ b/include/Nu/Nu.h
	* @@ -0,0 +1 @@
	* +awesome content
	* diff --git a/objc/Nu.h b/objc/Nu.h
	* deleted file mode 100644
	* index f9e6561..0000000
	* --- a/objc/Nu.h
	* +++ /dev/null
	* @@ -1 +0,0 @@
	* -awesome content
	*/

	process_tree_to_tree_diffing("7fccd7", "806999");

368 369 370 371 372
	cl_assert_equal_i(3, expect.files);
	cl_assert_equal_i(2, expect.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(0, expect.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(1, expect.file_status[GIT_DELTA_ADDED]);
	cl_assert_equal_i(0, expect.file_status[GIT_DELTA_TYPECHANGE]);
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
}

void test_diff_tree__symlink_blob_mode_changed_to_regular_file_as_typechange(void)
{
	/*
	 * $ git diff  7fccd7..a8595c
	 * diff --git a/include/Nu/Nu.h b/include/Nu/Nu.h
	 * deleted file mode 120000
	 * index 19bf568..0000000
	 * --- a/include/Nu/Nu.h
	 * +++ /dev/null
	 * @@ -1 +0,0 @@
	 * -../../objc/Nu.h
	 * \ No newline at end of file
	 * diff --git a/include/Nu/Nu.h b/include/Nu/Nu.h
	 * new file mode 100755
	 * index 0000000..f9e6561
	 * --- /dev/null
	 * +++ b/include/Nu/Nu.h
	 * @@ -0,0 +1 @@
	 * +awesome content
	 * diff --git a/objc/Nu.h b/objc/Nu.h
	 * deleted file mode 100644
	 * index f9e6561..0000000
	 * --- a/objc/Nu.h
	 * +++ /dev/null
	 * @@ -1 +0,0 @@
	 * -awesome content
	*/

	opts.flags = GIT_DIFF_INCLUDE_TYPECHANGE;
	process_tree_to_tree_diffing("7fccd7", "a8595c");

406 407 408 409 410
	cl_assert_equal_i(2, expect.files);
	cl_assert_equal_i(1, expect.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(0, expect.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(0, expect.file_status[GIT_DELTA_ADDED]);
	cl_assert_equal_i(1, expect.file_status[GIT_DELTA_TYPECHANGE]);
411 412 413 414 415 416 417 418 419 420 421 422 423
}

void test_diff_tree__regular_blob_mode_changed_to_executable_file(void)
{
	/*
	 * $ git diff 806999..a8595c
	 * diff --git a/include/Nu/Nu.h b/include/Nu/Nu.h
	 * old mode 100644
	 * new mode 100755
	 */

	process_tree_to_tree_diffing("806999", "a8595c");

424 425 426 427 428
	cl_assert_equal_i(1, expect.files);
	cl_assert_equal_i(0, expect.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(1, expect.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(0, expect.file_status[GIT_DELTA_ADDED]);
	cl_assert_equal_i(0, expect.file_status[GIT_DELTA_TYPECHANGE]);
429
}
430 431 432

void test_diff_tree__issue_1397(void)
{
Russell Belfer committed
433 434
	/* this test shows that it is not needed */

435 436
	g_repo = cl_git_sandbox_init("issue_1397");

Russell Belfer committed
437
	cl_repo_set_bool(g_repo, "core.autocrlf", true);
438 439 440 441 442 443

	cl_assert((a = resolve_commit_oid_to_tree(g_repo, "8a7ef04")) != NULL);
	cl_assert((b = resolve_commit_oid_to_tree(g_repo, "7f483a7")) != NULL);

	cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, &opts));

444 445
	cl_git_pass(git_diff_foreach(diff,
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expect));
446 447 448 449 450 451 452

	cl_assert_equal_i(1, expect.files);
	cl_assert_equal_i(0, expect.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(1, expect.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(0, expect.file_status[GIT_DELTA_ADDED]);
	cl_assert_equal_i(0, expect.file_status[GIT_DELTA_TYPECHANGE]);
}
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474

static void set_config_int(git_repository *repo, const char *name, int value)
{
	git_config *cfg;

	cl_git_pass(git_repository_config(&cfg, repo));
	cl_git_pass(git_config_set_int32(cfg, name, value));
	git_config_free(cfg);
}

void test_diff_tree__diff_configs(void)
{
	const char *a_commit = "d70d245e";
	const char *b_commit = "7a9e0b02";

	g_repo = cl_git_sandbox_init("diff");

	cl_assert((a = resolve_commit_oid_to_tree(g_repo, a_commit)) != NULL);
	cl_assert((b = resolve_commit_oid_to_tree(g_repo, b_commit)) != NULL);

	cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, NULL));

475
	cl_git_pass(git_diff_foreach(diff,
476
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expect));
477 478 479 480 481 482 483 484 485

	cl_assert_equal_i(2, expect.files);
	cl_assert_equal_i(2, expect.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(6, expect.hunks);
	cl_assert_equal_i(55, expect.lines);
	cl_assert_equal_i(33, expect.line_ctxt);
	cl_assert_equal_i(7, expect.line_adds);
	cl_assert_equal_i(15, expect.line_dels);

486
	git_diff_free(diff);
487 488 489 490 491 492 493 494
	diff = NULL;

	set_config_int(g_repo, "diff.context", 1);

	memset(&expect, 0, sizeof(expect));

	cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, NULL));

495 496
	cl_git_pass(git_diff_foreach(diff,
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expect));
497 498 499 500 501 502 503 504 505

	cl_assert_equal_i(2, expect.files);
	cl_assert_equal_i(2, expect.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(7, expect.hunks);
	cl_assert_equal_i(34, expect.lines);
	cl_assert_equal_i(12, expect.line_ctxt);
	cl_assert_equal_i(7, expect.line_adds);
	cl_assert_equal_i(15, expect.line_dels);

506
	git_diff_free(diff);
507 508 509 510 511 512 513 514 515
	diff = NULL;

	set_config_int(g_repo, "diff.context", 0);
	set_config_int(g_repo, "diff.noprefix", 1);

	memset(&expect, 0, sizeof(expect));

	cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, NULL));

516 517
	cl_git_pass(git_diff_foreach(diff,
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expect));
518 519 520 521 522 523 524 525 526

	cl_assert_equal_i(2, expect.files);
	cl_assert_equal_i(2, expect.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(7, expect.hunks);
	cl_assert_equal_i(22, expect.lines);
	cl_assert_equal_i(0, expect.line_ctxt);
	cl_assert_equal_i(7, expect.line_adds);
	cl_assert_equal_i(15, expect.line_dels);
}
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575

void test_diff_tree__diff_tree_with_empty_dir_entry_succeeds(void)
{
	const char *content = "This is a blob\n";
	const git_diff_delta *delta;
	git_oid empty_tree, invalid_tree, blob;
	git_buf patch = GIT_BUF_INIT;
	git_treebuilder *builder;

	g_repo = cl_git_sandbox_init("empty_standard_repo");

	cl_git_pass(git_blob_create_from_buffer(&blob, g_repo, content, strlen(content)));
	cl_git_pass(git_treebuilder_new(&builder, g_repo, NULL));
	cl_git_pass(git_treebuilder_write(&empty_tree, builder));
	cl_git_pass(git_treebuilder_insert(NULL, builder, "empty_tree", &empty_tree, GIT_FILEMODE_TREE));
	cl_git_pass(git_treebuilder_insert(NULL, builder, "blob", &blob, GIT_FILEMODE_BLOB));
	cl_git_pass(git_treebuilder_write(&invalid_tree, builder));

	cl_git_pass(git_tree_lookup(&a, g_repo, &empty_tree));
	cl_git_pass(git_tree_lookup(&b, g_repo, &invalid_tree));
	cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, a, b, NULL));

	cl_git_pass(git_diff_foreach(diff,
		diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &expect));
	cl_assert_equal_i(1, expect.files);
	cl_assert_equal_i(0, expect.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(1, expect.hunks);
	cl_assert_equal_i(1, expect.lines);
	cl_assert_equal_i(0, expect.line_ctxt);
	cl_assert_equal_i(1, expect.line_adds);
	cl_assert_equal_i(0, expect.line_dels);

	cl_git_pass(git_diff_to_buf(&patch, diff, GIT_DIFF_FORMAT_PATCH));
	cl_assert_equal_s(patch.ptr,
		"diff --git a/blob b/blob\n"
		"new file mode 100644\n"
		"index 0000000..bbf2e80\n"
		"--- /dev/null\n"
		"+++ b/blob\n"
		"@@ -0,0 +1 @@\n"
		"+This is a blob\n");

	cl_assert_equal_i(git_diff_num_deltas(diff), 1);
	delta = git_diff_get_delta(diff, 0);
	cl_assert_equal_s(delta->new_file.path, "blob");

	git_treebuilder_free(builder);
	git_buf_dispose(&patch);
}