rename.c 70.3 KB
Newer Older
1 2 3 4 5 6 7 8
#include "clar_libgit2.h"
#include "diff_helpers.h"

static git_repository *g_repo = NULL;

void test_diff_rename__initialize(void)
{
	g_repo = cl_git_sandbox_init("renames");
9 10

	cl_repo_set_bool(g_repo, "core.autocrlf", false);
11 12 13 14 15 16 17
}

void test_diff_rename__cleanup(void)
{
	cl_git_sandbox_cleanup();
}

18 19 20 21
#define INITIAL_COMMIT "31e47d8c1fa36d7f8d537b96158e3f024de0a9f2"
#define COPY_RENAME_COMMIT "2bc7f351d20b53f1c72c16c4b036e491c478c49a"
#define REWRITE_COPY_COMMIT "1c068dee5790ef1580cfc4cd670915b48d790084"
#define RENAME_MODIFICATION_COMMIT "19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13"
22 23
#define REWRITE_DELETE_COMMIT "84d8efa38af7ace2b302de0adbda16b1f1cd2e1b"
#define DELETE_RENAME_COMMIT "be053a189b0bbde545e0a3f59ce00b46ad29ce0d"
Kartikaya Gupta committed
24 25
#define BREAK_REWRITE_BASE_COMMIT "db98035f715427eef1f5e17f03e1801c05301e9e"
#define BREAK_REWRITE_COMMIT "7e7bfb88ba9bc65fd700fee1819cf1c317aafa56"
26

27 28 29 30 31 32 33 34 35 36
/*
 * Renames repo has:
 *
 * commit 31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 -
 *   serving.txt     (25 lines)
 *   sevencities.txt (50 lines)
 * commit 2bc7f351d20b53f1c72c16c4b036e491c478c49a -
 *   serving.txt     -> sixserving.txt  (rename, no change, 100% match)
 *   sevencities.txt -> sevencities.txt (no change)
 *   sevencities.txt -> songofseven.txt (copy, no change, 100% match)
37 38 39 40 41 42 43 44 45 46
 * commit 1c068dee5790ef1580cfc4cd670915b48d790084
 *   songofseven.txt -> songofseven.txt (major rewrite, <20% match - split)
 *   sixserving.txt  -> sixserving.txt  (indentation change)
 *   sixserving.txt  -> ikeepsix.txt    (copy, add title, >80% match)
 *   sevencities.txt                    (no change)
 * commit 19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13
 *   songofseven.txt -> untimely.txt    (rename, convert to crlf)
 *   ikeepsix.txt    -> ikeepsix.txt    (reorder sections in file)
 *   sixserving.txt  -> sixserving.txt  (whitespace change - not just indent)
 *   sevencities.txt -> songof7cities.txt (rename, small text changes)
47 48 49 50 51 52 53 54 55 56
 * commit 84d8efa38af7ace2b302de0adbda16b1f1cd2e1b
 *   songof7cities.txt -> songof7citie.txt (major rewrite, <20% match)
 *   ikeepsix.txt      ->                  (deleted)
 *   untimely.txt                          (no change)
 *   sixserving.txt                        (no change)
 * commit be053a189b0bbde545e0a3f59ce00b46ad29ce0d
 *   ikeepsix.txt      ->              (deleted)
 *   songof7cities.txt -> ikeepsix.txt (rename, 100% match)
 *   untimely.txt                      (no change)
 *   sixserving.txt                    (no change)
57 58 59 60
 */

void test_diff_rename__match_oid(void)
{
61 62
	const char *old_sha = INITIAL_COMMIT;
	const char *new_sha = COPY_RENAME_COMMIT;
63
	git_tree *old_tree, *new_tree;
64
	git_diff *diff;
65
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
66
	git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
67 68 69 70 71 72
	diff_expects exp;

	old_tree = resolve_commit_oid_to_tree(g_repo, old_sha);
	new_tree = resolve_commit_oid_to_tree(g_repo, new_sha);

	/* Must pass GIT_DIFF_INCLUDE_UNMODIFIED if you expect to emulate
73
	 * --find-copies-harder during rename transformion...
74 75 76 77
	 */
	diffopts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED;

	cl_git_pass(git_diff_tree_to_tree(
78
		&diff, g_repo, old_tree, new_tree, &diffopts));
79 80 81 82 83 84 85

	/* git diff --no-renames \
	 *          31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 \
	 *          2bc7f351d20b53f1c72c16c4b036e491c478c49a
	 */
	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
86
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
87 88 89 90 91 92 93 94

	cl_assert_equal_i(4, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_ADDED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);

	/* git diff 31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 \
	 *          2bc7f351d20b53f1c72c16c4b036e491c478c49a
95
	 * don't use NULL opts to avoid config `diff.renames` contamination
96
	 */
97 98
	opts.flags = GIT_DIFF_FIND_RENAMES;
	cl_git_pass(git_diff_find_similar(diff, &opts));
99 100 101

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
102
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
103 104 105 106 107 108

	cl_assert_equal_i(3, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]);

109
	git_diff_free(diff);
110 111

	cl_git_pass(git_diff_tree_to_tree(
112
		&diff, g_repo, old_tree, new_tree, &diffopts));
113 114 115 116 117

	/* git diff --find-copies-harder \
	 *          31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 \
	 *          2bc7f351d20b53f1c72c16c4b036e491c478c49a
	 */
118 119
	opts.flags = GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED;
	cl_git_pass(git_diff_find_similar(diff, &opts));
120 121 122

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
123
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
124 125

	cl_assert_equal_i(3, exp.files);
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]);

	git_diff_free(diff);

	cl_git_pass(git_diff_tree_to_tree(
		&diff, g_repo, old_tree, new_tree, &diffopts));

	/* git diff --find-copies-harder -M100 -B100 \
	 *          31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 \
	 *          2bc7f351d20b53f1c72c16c4b036e491c478c49a
	 */
	opts.flags = GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED |
		GIT_DIFF_FIND_EXACT_MATCH_ONLY;
	cl_git_pass(git_diff_find_similar(diff, &opts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
145
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
146 147

	cl_assert_equal_i(3, exp.files);
148 149 150 151
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]);

152
	git_diff_free(diff);
153 154 155 156

	git_tree_free(old_tree);
	git_tree_free(new_tree);
}
157 158 159

void test_diff_rename__checks_options_version(void)
{
160 161
	const char *old_sha = INITIAL_COMMIT;
	const char *new_sha = COPY_RENAME_COMMIT;
162
	git_tree *old_tree, *new_tree;
163
	git_diff *diff;
164 165 166 167 168 169 170 171 172 173 174 175
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
	const git_error *err;

	old_tree = resolve_commit_oid_to_tree(g_repo, old_sha);
	new_tree = resolve_commit_oid_to_tree(g_repo, new_sha);
	diffopts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED;
	cl_git_pass(git_diff_tree_to_tree(
		&diff, g_repo, old_tree, new_tree, &diffopts));

	opts.version = 0;
	cl_git_fail(git_diff_find_similar(diff, &opts));
176 177
	err = git_error_last();
	cl_assert_equal_i(GIT_ERROR_INVALID, err->klass);
178

179
	git_error_clear();
180 181
	opts.version = 1024;
	cl_git_fail(git_diff_find_similar(diff, &opts));
182 183
	err = git_error_last();
	cl_assert_equal_i(GIT_ERROR_INVALID, err->klass);
184

185
	git_diff_free(diff);
186 187 188
	git_tree_free(old_tree);
	git_tree_free(new_tree);
}
189 190 191

void test_diff_rename__not_exact_match(void)
{
192 193 194
	const char *sha0 = COPY_RENAME_COMMIT;
	const char *sha1 = REWRITE_COPY_COMMIT;
	const char *sha2 = RENAME_MODIFICATION_COMMIT;
195
	git_tree *old_tree, *new_tree;
196
	git_diff *diff;
197 198 199 200
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
	diff_expects exp;

201 202 203 204 205
	/* == Changes =====================================================
	 * songofseven.txt -> songofseven.txt (major rewrite, <20% match - split)
	 * sixserving.txt  -> sixserving.txt  (indentation change)
	 * sixserving.txt  -> ikeepsix.txt    (copy, add title, >80% match)
	 * sevencities.txt                    (no change)
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
	 */

	old_tree = resolve_commit_oid_to_tree(g_repo, sha0);
	new_tree = resolve_commit_oid_to_tree(g_repo, sha1);

	/* Must pass GIT_DIFF_INCLUDE_UNMODIFIED if you expect to emulate
	 * --find-copies-harder during rename transformion...
	 */
	diffopts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED;

	cl_git_pass(git_diff_tree_to_tree(
		&diff, g_repo, old_tree, new_tree, &diffopts));

	/* git diff --no-renames \
	 *          2bc7f351d20b53f1c72c16c4b036e491c478c49a \
	 *          1c068dee5790ef1580cfc4cd670915b48d790084
	 */
	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
225
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
226 227 228 229 230 231

	cl_assert_equal_i(4, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]);

232 233 234 235 236
	/* git diff -M 2bc7f351d20b53f1c72c16c4b036e491c478c49a \
	 *          1c068dee5790ef1580cfc4cd670915b48d790084
	 *
	 * must not pass NULL for opts because it will pick up environment
	 * values for "diff.renames" and test won't be consistent.
237
	 */
238 239
	opts.flags = GIT_DIFF_FIND_RENAMES;
	cl_git_pass(git_diff_find_similar(diff, &opts));
240 241 242

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
243
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
244 245 246 247

	cl_assert_equal_i(4, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
248
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]);
249

250
	git_diff_free(diff);
251

252 253
	/* git diff -M -C \
	 *          2bc7f351d20b53f1c72c16c4b036e491c478c49a \
254 255
	 *          1c068dee5790ef1580cfc4cd670915b48d790084
	 */
256 257 258
	cl_git_pass(git_diff_tree_to_tree(
		&diff, g_repo, old_tree, new_tree, &diffopts));

259 260 261 262 263
	opts.flags = GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES;
	cl_git_pass(git_diff_find_similar(diff, &opts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
264
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
265 266 267 268 269 270

	cl_assert_equal_i(4, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]);

271
	git_diff_free(diff);
272 273

	/* git diff -M -C --find-copies-harder --break-rewrites \
274 275 276
	 *          2bc7f351d20b53f1c72c16c4b036e491c478c49a \
	 *          1c068dee5790ef1580cfc4cd670915b48d790084
	 */
277 278 279
	cl_git_pass(git_diff_tree_to_tree(
		&diff, g_repo, old_tree, new_tree, &diffopts));

280
	opts.flags = GIT_DIFF_FIND_ALL;
281 282
	opts.break_rewrite_threshold = 70;

283 284 285 286
	cl_git_pass(git_diff_find_similar(diff, &opts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
287
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
288 289 290 291 292

	cl_assert_equal_i(5, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]);
293
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
294 295
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]);

296
	git_diff_free(diff);
297

298 299 300 301 302
	/* == Changes =====================================================
	 * songofseven.txt -> untimely.txt    (rename, convert to crlf)
	 * ikeepsix.txt    -> ikeepsix.txt    (reorder sections in file)
	 * sixserving.txt  -> sixserving.txt  (whitespace - not just indent)
	 * sevencities.txt -> songof7cities.txt (rename, small text changes)
303 304 305 306 307 308
	 */

	git_tree_free(old_tree);
	old_tree = new_tree;
	new_tree = resolve_commit_oid_to_tree(g_repo, sha2);

309 310 311 312 313 314 315 316 317
	cl_git_pass(git_diff_tree_to_tree(
		&diff, g_repo, old_tree, new_tree, &diffopts));

	/* git diff --no-renames \
	 *          1c068dee5790ef1580cfc4cd670915b48d790084 \
	 *          19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13
	 */
	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
318
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
319 320 321 322 323

	cl_assert_equal_i(6, exp.files);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_ADDED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]);
324
	git_diff_free(diff);
325 326 327 328 329 330 331 332 333 334 335 336 337

	/* git diff -M -C \
	 *          1c068dee5790ef1580cfc4cd670915b48d790084 \
	 *          19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13
	 */
	cl_git_pass(git_diff_tree_to_tree(
		&diff, g_repo, old_tree, new_tree, &diffopts));

	opts.flags = GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES;
	cl_git_pass(git_diff_find_similar(diff, &opts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
338
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
339 340 341 342 343

	cl_assert_equal_i(4, exp.files);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_RENAMED]);

344
	git_diff_free(diff);
345 346 347 348 349 350 351 352 353 354 355 356 357 358

	/* git diff -M -C --find-copies-harder --break-rewrites \
	 *          1c068dee5790ef1580cfc4cd670915b48d790084 \
	 *          19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13
	 * with libgit2 default similarity comparison...
	 */
	cl_git_pass(git_diff_tree_to_tree(
		&diff, g_repo, old_tree, new_tree, &diffopts));

	opts.flags = GIT_DIFF_FIND_ALL;
	cl_git_pass(git_diff_find_similar(diff, &opts));

	/* the default match algorithm is going to find the internal
	 * whitespace differences in the lines of sixserving.txt to be
359 360
	 * significant enough that this will decide to split it into an ADD
	 * and a DELETE
361 362 363 364
	 */

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
365
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
366 367 368 369 370 371 372

	cl_assert_equal_i(5, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_RENAMED]);

373
	git_diff_free(diff);
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389

	/* git diff -M -C --find-copies-harder --break-rewrites \
	 *          1c068dee5790ef1580cfc4cd670915b48d790084 \
	 *          19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13
	 * with ignore_space whitespace comparision
	 */
	cl_git_pass(git_diff_tree_to_tree(
		&diff, g_repo, old_tree, new_tree, &diffopts));

	opts.flags = GIT_DIFF_FIND_ALL | GIT_DIFF_FIND_IGNORE_WHITESPACE;
	cl_git_pass(git_diff_find_similar(diff, &opts));

	/* Ignoring whitespace, this should no longer split sixserver.txt */

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
390
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
391 392 393 394 395

	cl_assert_equal_i(4, exp.files);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_RENAMED]);

396
	git_diff_free(diff);
397 398 399 400 401

	git_tree_free(old_tree);
	git_tree_free(new_tree);
}

402
void test_diff_rename__test_small_files(void)
403 404
{
	git_index *index;
405 406 407 408 409
	git_reference *head_reference;
	git_commit *head_commit;
	git_tree *head_tree;
	git_tree *commit_tree;
	git_signature *signature;
410
	git_diff *diff;
411 412 413 414
	git_oid oid;
	const git_diff_delta *delta;
	git_diff_options diff_options = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options find_options = GIT_DIFF_FIND_OPTIONS_INIT;
415 416 417

	cl_git_pass(git_repository_index(&index, g_repo));

418 419
	cl_git_mkfile("renames/small.txt", "Hello World!\n");
	cl_git_pass(git_index_add_bypath(index, "small.txt"));
420

421
	cl_git_pass(git_repository_head(&head_reference, g_repo));
422
	cl_git_pass(git_reference_peel((git_object**)&head_commit, head_reference, GIT_OBJECT_COMMIT));
423 424 425 426 427
	cl_git_pass(git_commit_tree(&head_tree, head_commit));
	cl_git_pass(git_index_write_tree(&oid, index));
	cl_git_pass(git_tree_lookup(&commit_tree, g_repo, &oid));
	cl_git_pass(git_signature_new(&signature, "Rename", "rename@example.com", 1404157834, 0));
	cl_git_pass(git_commit_create(&oid, g_repo, "HEAD", signature, signature, NULL, "Test commit", commit_tree, 1, (const git_commit**)&head_commit));
428

429 430
	cl_git_mkfile("renames/copy.txt", "Hello World!\n");
	cl_git_rmfile("renames/small.txt");
431

432 433 434 435
	diff_options.flags = GIT_DIFF_INCLUDE_UNTRACKED;
	cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, commit_tree, &diff_options));
	find_options.flags = GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_FOR_UNTRACKED;
	cl_git_pass(git_diff_find_similar(diff, &find_options));
436

437 438 439 440 441
	cl_assert_equal_i(git_diff_num_deltas(diff), 1);
	delta = git_diff_get_delta(diff, 0);
	cl_assert_equal_i(delta->status, GIT_DELTA_RENAMED);
	cl_assert_equal_s(delta->old_file.path, "small.txt");
	cl_assert_equal_s(delta->new_file.path, "copy.txt");
442

443
	git_diff_free(diff);
444 445 446 447 448
	git_signature_free(signature);
	git_tree_free(commit_tree);
	git_tree_free(head_tree);
	git_commit_free(head_commit);
	git_reference_free(head_reference);
449 450 451
	git_index_free(index);
}

452 453
void test_diff_rename__working_directory_changes(void)
{
454
	const char *sha0 = COPY_RENAME_COMMIT;
455 456 457 458
	const char *blobsha = "66311f5cfbe7836c27510a3ba2f43e282e2c8bba";
	git_oid id;
	git_tree *tree;
	git_blob *blob;
459
	git_diff *diff;
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
	diff_expects exp;
	git_buf old_content = GIT_BUF_INIT, content = GIT_BUF_INIT;;

	tree = resolve_commit_oid_to_tree(g_repo, sha0);
	diffopts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED | GIT_DIFF_INCLUDE_UNTRACKED;

	/*
	$ git cat-file -p 2bc7f351d20b53f1c72c16c4b036e491c478c49a^{tree}

	100644 blob 66311f5cfbe7836c27510a3ba2f43e282e2c8bba	sevencities.txt
	100644 blob ad0a8e55a104ac54a8a29ed4b84b49e76837a113	sixserving.txt
	100644 blob 66311f5cfbe7836c27510a3ba2f43e282e2c8bba	songofseven.txt

	$ for f in *.txt; do
		echo `git hash-object -t blob $f` $f
	done

	eaf4a3e3bfe68585e90cada20736ace491cd100b ikeepsix.txt
	f90d4fc20ecddf21eebe6a37e9225d244339d2b5 sixserving.txt
	4210ffd5c390b21dd5483375e75288dea9ede512 songof7cities.txt
	9a69d960ae94b060f56c2a8702545e2bb1abb935 untimely.txt
	*/

	cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &diffopts));

	/* git diff --no-renames 2bc7f351d20b53f1c72c16c4b036e491c478c49a */
	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
490
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
491 492 493 494

	cl_assert_equal_i(6, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]);
495
	cl_assert_equal_i(3, exp.file_status[GIT_DELTA_UNTRACKED]);
496 497 498 499 500 501 502

	/* git diff -M 2bc7f351d20b53f1c72c16c4b036e491c478c49a */
	opts.flags = GIT_DIFF_FIND_ALL;
	cl_git_pass(git_diff_find_similar(diff, &opts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
503
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
504 505

	cl_assert_equal_i(5, exp.files);
506 507
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_RENAMED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
508 509
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_UNTRACKED]);

510
	git_diff_free(diff);
511 512 513 514 515 516

	/* rewrite files in the working directory with / without CRLF changes */

	cl_git_pass(
		git_futils_readbuffer(&old_content, "renames/songof7cities.txt"));
	cl_git_pass(
517
		git_buf_lf_to_crlf(&content, &old_content));
518 519 520 521 522 523 524 525 526 527 528
	cl_git_pass(
		git_futils_writebuffer(&content, "renames/songof7cities.txt", 0, 0));

	cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &diffopts));

	/* git diff -M 2bc7f351d20b53f1c72c16c4b036e491c478c49a */
	opts.flags = GIT_DIFF_FIND_ALL;
	cl_git_pass(git_diff_find_similar(diff, &opts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
529
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
530 531

	cl_assert_equal_i(5, exp.files);
532 533
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_RENAMED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
534 535
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_UNTRACKED]);

536
	git_diff_free(diff);
537

538 539 540 541 542
	/* try a different whitespace option */

	cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &diffopts));

	opts.flags = GIT_DIFF_FIND_ALL | GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE;
543
	opts.rename_threshold = 70;
544 545 546 547
	cl_git_pass(git_diff_find_similar(diff, &opts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
548
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
549 550 551 552 553 554

	cl_assert_equal_i(6, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(3, exp.file_status[GIT_DELTA_UNTRACKED]);

555
	git_diff_free(diff);
556 557 558 559 560 561 562 563 564 565

	/* try a different matching option */

	cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &diffopts));

	opts.flags = GIT_DIFF_FIND_ALL | GIT_DIFF_FIND_EXACT_MATCH_ONLY;
	cl_git_pass(git_diff_find_similar(diff, &opts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
566
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
567 568 569 570 571 572

	cl_assert_equal_i(6, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(3, exp.file_status[GIT_DELTA_UNTRACKED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]);

573
	git_diff_free(diff);
574 575 576 577 578 579

	/* again with exact match blob */

	cl_git_pass(git_oid_fromstr(&id, blobsha));
	cl_git_pass(git_blob_lookup(&blob, g_repo, &id));
	cl_git_pass(git_buf_set(
nulltoken committed
580
		&content, git_blob_rawcontent(blob), (size_t)git_blob_rawsize(blob)));
581 582 583 584 585 586 587 588
	cl_git_rewritefile("renames/songof7cities.txt", content.ptr);
	git_blob_free(blob);

	cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, tree, &diffopts));

	opts.flags = GIT_DIFF_FIND_ALL | GIT_DIFF_FIND_EXACT_MATCH_ONLY;
	cl_git_pass(git_diff_find_similar(diff, &opts));

589 590
	/*
	fprintf(stderr, "\n\n");
591
    diff_print_raw(stderr, diff);
592 593
	*/

594 595
	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
596
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
597 598 599

	cl_assert_equal_i(5, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
600 601
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]);
602 603
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_UNTRACKED]);

604
	git_diff_free(diff);
605 606

	git_tree_free(tree);
607 608
	git_buf_dispose(&content);
	git_buf_dispose(&old_content);
609
}
610 611 612

void test_diff_rename__patch(void)
{
613 614
	const char *sha0 = COPY_RENAME_COMMIT;
	const char *sha1 = REWRITE_COPY_COMMIT;
615
	git_tree *old_tree, *new_tree;
616
	git_diff *diff;
617 618
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
619
	git_patch *patch;
620
	const git_diff_delta *delta;
Nicolas Hake committed
621
	git_buf buf = GIT_BUF_INIT;
622
	const char *expected = "diff --git a/sixserving.txt b/ikeepsix.txt\nindex ad0a8e5..36020db 100644\n--- a/sixserving.txt\n+++ b/ikeepsix.txt\n@@ -1,3 +1,6 @@\n+I Keep Six Honest Serving-Men\n+=============================\n+\n I KEEP six honest serving-men\n  (They taught me all I knew);\n Their names are What and Why and When\n@@ -21,4 +24,4 @@ She sends'em abroad on her own affairs,\n One million Hows, two million Wheres,\n And seven million Whys!\n \n-                -- Rudyard Kipling\n+  -- Rudyard Kipling\n";
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642

	old_tree = resolve_commit_oid_to_tree(g_repo, sha0);
	new_tree = resolve_commit_oid_to_tree(g_repo, sha1);

	diffopts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED;
	cl_git_pass(git_diff_tree_to_tree(
		&diff, g_repo, old_tree, new_tree, &diffopts));

	opts.flags = GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES;
	cl_git_pass(git_diff_find_similar(diff, &opts));

	/* == Changes =====================================================
	 * sixserving.txt  -> ikeepsix.txt    (copy, add title, >80% match)
	 * sevencities.txt                    (no change)
	 * sixserving.txt  -> sixserving.txt  (indentation change)
	 * songofseven.txt -> songofseven.txt (major rewrite, <20% match - split)
	 */

	cl_assert_equal_i(4, (int)git_diff_num_deltas(diff));

Russell Belfer committed
643 644
	cl_git_pass(git_patch_from_diff(&patch, diff, 0));
	cl_assert((delta = git_patch_get_delta(patch)) != NULL);
645 646
	cl_assert_equal_i(GIT_DELTA_COPIED, (int)delta->status);

Nicolas Hake committed
647 648
	cl_git_pass(git_patch_to_buf(&buf, patch));
	cl_assert_equal_s(expected, buf.ptr);
649
	git_buf_dispose(&buf);
650

651
	git_patch_free(patch);
652

Russell Belfer committed
653
	cl_assert((delta = git_diff_get_delta(diff, 1)) != NULL);
654 655
	cl_assert_equal_i(GIT_DELTA_UNMODIFIED, (int)delta->status);

Russell Belfer committed
656
	cl_assert((delta = git_diff_get_delta(diff, 2)) != NULL);
657 658
	cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status);

Russell Belfer committed
659
	cl_assert((delta = git_diff_get_delta(diff, 3)) != NULL);
660 661
	cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status);

662
	git_diff_free(diff);
nulltoken committed
663 664
	git_tree_free(old_tree);
	git_tree_free(new_tree);
665
}
666 667 668 669 670 671

void test_diff_rename__file_exchange(void)
{
	git_buf c1 = GIT_BUF_INIT, c2 = GIT_BUF_INIT;
	git_index *index;
	git_tree *tree;
672
	git_diff *diff;
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
	diff_expects exp;

	cl_git_pass(git_futils_readbuffer(&c1, "renames/untimely.txt"));
	cl_git_pass(git_futils_readbuffer(&c2, "renames/songof7cities.txt"));
	cl_git_pass(git_futils_writebuffer(&c1, "renames/songof7cities.txt", 0, 0));
	cl_git_pass(git_futils_writebuffer(&c2, "renames/untimely.txt", 0, 0));

	cl_git_pass(
		git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}"));

	cl_git_pass(git_repository_index(&index, g_repo));
	cl_git_pass(git_index_read_tree(index, tree));
	cl_git_pass(git_index_add_bypath(index, "songof7cities.txt"));
	cl_git_pass(git_index_add_bypath(index, "untimely.txt"));

	cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
694
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
695 696 697 698 699 700 701 702
	cl_assert_equal_i(2, exp.files);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);

	opts.flags = GIT_DIFF_FIND_ALL;
	cl_git_pass(git_diff_find_similar(diff, &opts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
703
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
704 705 706
	cl_assert_equal_i(2, exp.files);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_RENAMED]);

707
	git_diff_free(diff);
708 709 710
	git_tree_free(tree);
	git_index_free(index);

711 712
	git_buf_dispose(&c1);
	git_buf_dispose(&c2);
713 714
}

715 716 717 718 719
void test_diff_rename__file_exchange_three(void)
{
	git_buf c1 = GIT_BUF_INIT, c2 = GIT_BUF_INIT, c3 = GIT_BUF_INIT;
	git_index *index;
	git_tree *tree;
720
	git_diff *diff;
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
	diff_expects exp;

	cl_git_pass(git_futils_readbuffer(&c1, "renames/untimely.txt"));
	cl_git_pass(git_futils_readbuffer(&c2, "renames/songof7cities.txt"));
	cl_git_pass(git_futils_readbuffer(&c3, "renames/ikeepsix.txt"));

	cl_git_pass(git_futils_writebuffer(&c1, "renames/ikeepsix.txt", 0, 0));
	cl_git_pass(git_futils_writebuffer(&c2, "renames/untimely.txt", 0, 0));
	cl_git_pass(git_futils_writebuffer(&c3, "renames/songof7cities.txt", 0, 0));

	cl_git_pass(
		git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}"));

	cl_git_pass(git_repository_index(&index, g_repo));
	cl_git_pass(git_index_read_tree(index, tree));
	cl_git_pass(git_index_add_bypath(index, "songof7cities.txt"));
	cl_git_pass(git_index_add_bypath(index, "untimely.txt"));
	cl_git_pass(git_index_add_bypath(index, "ikeepsix.txt"));

	cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
746
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
747 748 749 750 751 752 753 754
	cl_assert_equal_i(3, exp.files);
	cl_assert_equal_i(3, exp.file_status[GIT_DELTA_MODIFIED]);

	opts.flags = GIT_DIFF_FIND_ALL;
	cl_git_pass(git_diff_find_similar(diff, &opts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
755
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
756 757 758
	cl_assert_equal_i(3, exp.files);
	cl_assert_equal_i(3, exp.file_status[GIT_DELTA_RENAMED]);

759
	git_diff_free(diff);
760 761 762
	git_tree_free(tree);
	git_index_free(index);

763 764 765
	git_buf_dispose(&c1);
	git_buf_dispose(&c2);
	git_buf_dispose(&c3);
766 767
}

768 769 770 771 772
void test_diff_rename__file_partial_exchange(void)
{
	git_buf c1 = GIT_BUF_INIT, c2 = GIT_BUF_INIT;
	git_index *index;
	git_tree *tree;
773
	git_diff *diff;
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
	diff_expects exp;
	int i;

	cl_git_pass(git_futils_readbuffer(&c1, "renames/untimely.txt"));
	cl_git_pass(git_futils_writebuffer(&c1, "renames/songof7cities.txt", 0, 0));
	for (i = 0; i < 100; ++i)
		cl_git_pass(git_buf_puts(&c2, "this is not the content you are looking for\n"));
	cl_git_pass(git_futils_writebuffer(&c2, "renames/untimely.txt", 0, 0));

	cl_git_pass(
		git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}"));

	cl_git_pass(git_repository_index(&index, g_repo));
	cl_git_pass(git_index_read_tree(index, tree));
	cl_git_pass(git_index_add_bypath(index, "songof7cities.txt"));
	cl_git_pass(git_index_add_bypath(index, "untimely.txt"));

	cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
797
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
798 799 800 801 802 803 804 805
	cl_assert_equal_i(2, exp.files);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);

	opts.flags = GIT_DIFF_FIND_ALL;
	cl_git_pass(git_diff_find_similar(diff, &opts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
806
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
807 808 809 810 811
	cl_assert_equal_i(3, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);

812
	git_diff_free(diff);
813 814 815
	git_tree_free(tree);
	git_index_free(index);

816 817
	git_buf_dispose(&c1);
	git_buf_dispose(&c2);
818 819
}

820
void test_diff_rename__rename_and_copy_from_same_source(void)
821 822 823 824
{
	git_buf c1 = GIT_BUF_INIT, c2 = GIT_BUF_INIT;
	git_index *index;
	git_tree *tree;
825
	git_diff *diff;
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
	diff_expects exp;

	/* put the first 2/3 of file into one new place
	 * and the second 2/3 of file into another new place
	 */
	cl_git_pass(git_futils_readbuffer(&c1, "renames/songof7cities.txt"));
	cl_git_pass(git_buf_set(&c2, c1.ptr, c1.size));
	git_buf_truncate(&c1, c1.size * 2 / 3);
	git_buf_consume(&c2, ((char *)c2.ptr) + (c2.size / 3));
	cl_git_pass(git_futils_writebuffer(&c1, "renames/song_a.txt", 0, 0));
	cl_git_pass(git_futils_writebuffer(&c2, "renames/song_b.txt", 0, 0));

	cl_git_pass(
		git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}"));

	cl_git_pass(git_repository_index(&index, g_repo));
	cl_git_pass(git_index_read_tree(index, tree));
	cl_git_pass(git_index_add_bypath(index, "song_a.txt"));
	cl_git_pass(git_index_add_bypath(index, "song_b.txt"));

	diffopts.flags = GIT_DIFF_INCLUDE_UNMODIFIED;

	cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
854
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
855 856 857 858 859 860 861 862 863
	cl_assert_equal_i(6, exp.files);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_ADDED]);
	cl_assert_equal_i(4, exp.file_status[GIT_DELTA_UNMODIFIED]);

	opts.flags = GIT_DIFF_FIND_ALL;
	cl_git_pass(git_diff_find_similar(diff, &opts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
864
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
865 866 867 868
	cl_assert_equal_i(6, exp.files);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_COPIED]);
	cl_assert_equal_i(4, exp.file_status[GIT_DELTA_UNMODIFIED]);

869
	git_diff_free(diff);
870 871 872
	git_tree_free(tree);
	git_index_free(index);

873 874
	git_buf_dispose(&c1);
	git_buf_dispose(&c2);
875
}
876 877 878 879 880 881

void test_diff_rename__from_deleted_to_split(void)
{
	git_buf c1 = GIT_BUF_INIT;
	git_index *index;
	git_tree *tree;
882
	git_diff *diff;
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
	diff_expects exp;

	/* old file is missing, new file is actually old file renamed */

	cl_git_pass(git_futils_readbuffer(&c1, "renames/songof7cities.txt"));
	cl_git_pass(git_futils_writebuffer(&c1, "renames/untimely.txt", 0, 0));

	cl_git_pass(
		git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}"));

	cl_git_pass(git_repository_index(&index, g_repo));
	cl_git_pass(git_index_read_tree(index, tree));
	cl_git_pass(git_index_remove_bypath(index, "songof7cities.txt"));
	cl_git_pass(git_index_add_bypath(index, "untimely.txt"));

	diffopts.flags = GIT_DIFF_INCLUDE_UNMODIFIED;

	cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
906
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
907 908 909 910 911 912 913 914 915 916
	cl_assert_equal_i(4, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_UNMODIFIED]);

	opts.flags = GIT_DIFF_FIND_ALL;
	cl_git_pass(git_diff_find_similar(diff, &opts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
917
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
918 919 920 921 922
	cl_assert_equal_i(4, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_UNMODIFIED]);

923
	git_diff_free(diff);
924 925 926
	git_tree_free(tree);
	git_index_free(index);

927
	git_buf_dispose(&c1);
928
}
929 930 931 932

struct rename_expected
{
	size_t len;
933

934
	unsigned int *status;
935 936 937 938 939 940 941 942 943 944
	const char **sources;
	const char **targets;

	size_t idx;
};

int test_names_expected(const git_diff_delta *delta, float progress, void *p)
{
	struct rename_expected *expected = p;

yorah committed
945 946
	GIT_UNUSED(progress);

947 948
	cl_assert(expected->idx < expected->len);

949
	cl_assert_equal_i(delta->status, expected->status[expected->idx]);
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965

	cl_assert(git__strcmp(expected->sources[expected->idx],
		delta->old_file.path) == 0);
	cl_assert(git__strcmp(expected->targets[expected->idx],
		delta->new_file.path) == 0);

	expected->idx++;

	return 0;
}

void test_diff_rename__rejected_match_can_match_others(void)
{
	git_reference *head, *selfsimilar;
	git_index *index;
	git_tree *tree;
966
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
967
	git_diff *diff;
968 969 970
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT;
	git_buf one = GIT_BUF_INIT, two = GIT_BUF_INIT;
971
	unsigned int status[] = { GIT_DELTA_RENAMED, GIT_DELTA_RENAMED };
972 973
	const char *sources[] = { "Class1.cs", "Class2.cs" };
	const char *targets[] = { "ClassA.cs", "ClassB.cs" };
974
	struct rename_expected expect = { 2, status, sources, targets };
975 976 977
	char *ptr;

	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
978
	findopts.flags = GIT_DIFF_FIND_RENAMES;
979 980 981

	cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
	cl_git_pass(git_reference_symbolic_set_target(
982
		&selfsimilar, head, "refs/heads/renames_similar", NULL));
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
	cl_git_pass(git_checkout_head(g_repo, &opts));
	cl_git_pass(git_repository_index(&index, g_repo));

	cl_git_pass(git_futils_readbuffer(&one, "renames/Class1.cs"));
	cl_git_pass(git_futils_readbuffer(&two, "renames/Class2.cs"));

	cl_git_pass(p_unlink("renames/Class1.cs"));
	cl_git_pass(p_unlink("renames/Class2.cs"));

	cl_git_pass(git_index_remove_bypath(index, "Class1.cs"));
	cl_git_pass(git_index_remove_bypath(index, "Class2.cs"));

	cl_assert(ptr = strstr(one.ptr, "Class1"));
	ptr[5] = 'A';

	cl_assert(ptr = strstr(two.ptr, "Class2"));
	ptr[5] = 'B';

	cl_git_pass(
		git_futils_writebuffer(&one, "renames/ClassA.cs", O_RDWR|O_CREAT, 0777));
	cl_git_pass(
		git_futils_writebuffer(&two, "renames/ClassB.cs", O_RDWR|O_CREAT, 0777));

	cl_git_pass(git_index_add_bypath(index, "ClassA.cs"));
	cl_git_pass(git_index_add_bypath(index, "ClassB.cs"));

	cl_git_pass(git_index_write(index));

	cl_git_pass(
		git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}"));

	cl_git_pass(
		git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts));
1016

1017 1018
	cl_git_pass(git_diff_find_similar(diff, &findopts));

1019 1020
	cl_git_pass(git_diff_foreach(
		diff, test_names_expected, NULL, NULL, NULL, &expect));
1021

1022
	git_diff_free(diff);
1023 1024 1025 1026
	git_tree_free(tree);
	git_index_free(index);
	git_reference_free(head);
	git_reference_free(selfsimilar);
1027 1028
	git_buf_dispose(&one);
	git_buf_dispose(&two);
1029
}
1030

1031 1032 1033 1034 1035 1036
static void write_similarity_file_two(const char *filename, size_t b_lines)
{
	git_buf contents = GIT_BUF_INIT;
	size_t i;

	for (i = 0; i < b_lines; i++)
1037
		git_buf_printf(&contents, "%02d - bbbbb\r\n", (int)(i+1));
1038 1039

	for (i = b_lines; i < 50; i++)
1040
		git_buf_printf(&contents, "%02d - aaaaa%s", (int)(i+1), (i == 49 ? "" : "\r\n"));
1041 1042 1043 1044

	cl_git_pass(
		git_futils_writebuffer(&contents, filename, O_RDWR|O_CREAT, 0777));

1045
	git_buf_dispose(&contents);
1046 1047 1048 1049 1050 1051 1052
}

void test_diff_rename__rejected_match_can_match_others_two(void)
{
	git_reference *head, *selfsimilar;
	git_index *index;
	git_tree *tree;
1053
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1054
	git_diff *diff;
1055 1056
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT;
1057
	unsigned int status[] = { GIT_DELTA_RENAMED, GIT_DELTA_RENAMED };
1058 1059
	const char *sources[] = { "a.txt", "b.txt" };
	const char *targets[] = { "c.txt", "d.txt" };
1060
	struct rename_expected expect = { 2, status, sources, targets };
1061 1062

	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
1063
	findopts.flags = GIT_DIFF_FIND_RENAMES;
1064 1065 1066

	cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
	cl_git_pass(git_reference_symbolic_set_target(
1067
		&selfsimilar, head, "refs/heads/renames_similar_two", NULL));
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
	cl_git_pass(git_checkout_head(g_repo, &opts));
	cl_git_pass(git_repository_index(&index, g_repo));

	cl_git_pass(p_unlink("renames/a.txt"));
	cl_git_pass(p_unlink("renames/b.txt"));

	cl_git_pass(git_index_remove_bypath(index, "a.txt"));
	cl_git_pass(git_index_remove_bypath(index, "b.txt"));

	write_similarity_file_two("renames/c.txt", 7);
	write_similarity_file_two("renames/d.txt", 8);

	cl_git_pass(git_index_add_bypath(index, "c.txt"));
	cl_git_pass(git_index_add_bypath(index, "d.txt"));

	cl_git_pass(git_index_write(index));

	cl_git_pass(
		git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}"));

	cl_git_pass(
		git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts));
1090

1091 1092
	cl_git_pass(git_diff_find_similar(diff, &findopts));

1093 1094
	cl_git_pass(git_diff_foreach(
		diff, test_names_expected, NULL, NULL, NULL, &expect));
1095 1096
	cl_assert(expect.idx > 0);

1097
	git_diff_free(diff);
1098 1099 1100 1101 1102 1103
	git_tree_free(tree);
	git_index_free(index);
	git_reference_free(head);
	git_reference_free(selfsimilar);
}

1104 1105 1106 1107 1108
void test_diff_rename__rejected_match_can_match_others_three(void)
{
	git_reference *head, *selfsimilar;
	git_index *index;
	git_tree *tree;
1109
	git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
1110
	git_diff *diff;
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT;

	/* Both cannot be renames from a.txt */
	unsigned int status[] = { GIT_DELTA_ADDED, GIT_DELTA_RENAMED };
	const char *sources[] = { "0001.txt", "a.txt" };
	const char *targets[] = { "0001.txt", "0002.txt" };
	struct rename_expected expect = { 2, status, sources, targets };

	opts.checkout_strategy = GIT_CHECKOUT_FORCE;
1121
	findopts.flags = GIT_DIFF_FIND_RENAMES;
1122 1123 1124

	cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD"));
	cl_git_pass(git_reference_symbolic_set_target(
1125
		&selfsimilar, head, "refs/heads/renames_similar_two", NULL));
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
	cl_git_pass(git_checkout_head(g_repo, &opts));
	cl_git_pass(git_repository_index(&index, g_repo));

	cl_git_pass(p_unlink("renames/a.txt"));

	cl_git_pass(git_index_remove_bypath(index, "a.txt"));

	write_similarity_file_two("renames/0001.txt", 7);
	write_similarity_file_two("renames/0002.txt", 0);

	cl_git_pass(git_index_add_bypath(index, "0001.txt"));
	cl_git_pass(git_index_add_bypath(index, "0002.txt"));

	cl_git_pass(git_index_write(index));

	cl_git_pass(
		git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}"));

	cl_git_pass(
		git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts));

	cl_git_pass(git_diff_find_similar(diff, &findopts));

1149 1150
	cl_git_pass(git_diff_foreach(
		diff, test_names_expected, NULL, NULL, NULL, &expect));
1151 1152 1153

	cl_assert(expect.idx == expect.len);

1154
	git_diff_free(diff);
1155 1156 1157 1158 1159 1160
	git_tree_free(tree);
	git_index_free(index);
	git_reference_free(head);
	git_reference_free(selfsimilar);
}

1161 1162 1163 1164
void test_diff_rename__can_rename_from_rewrite(void)
{
	git_index *index;
	git_tree *tree;
1165
	git_diff *diff;
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT;

	unsigned int status[] = { GIT_DELTA_RENAMED, GIT_DELTA_RENAMED };
	const char *sources[] = { "ikeepsix.txt", "songof7cities.txt" };
	const char *targets[] = { "songof7cities.txt", "this-is-a-rename.txt" };
	struct rename_expected expect = { 2, status, sources, targets };

	cl_git_pass(git_repository_index(&index, g_repo));

	cl_git_pass(p_rename("renames/songof7cities.txt", "renames/this-is-a-rename.txt"));
	cl_git_pass(p_rename("renames/ikeepsix.txt", "renames/songof7cities.txt"));

	cl_git_pass(git_index_remove_bypath(index, "ikeepsix.txt"));

	cl_git_pass(git_index_add_bypath(index, "songof7cities.txt"));
	cl_git_pass(git_index_add_bypath(index, "this-is-a-rename.txt"));

	cl_git_pass(git_index_write(index));

	cl_git_pass(
		git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}"));

	cl_git_pass(
		git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts));
1191

1192 1193 1194 1195 1196 1197
	findopts.flags |= GIT_DIFF_FIND_AND_BREAK_REWRITES |
		GIT_DIFF_FIND_REWRITES |
		GIT_DIFF_FIND_RENAMES_FROM_REWRITES;

	cl_git_pass(git_diff_find_similar(diff, &findopts));

1198 1199
	cl_git_pass(git_diff_foreach(
		diff, test_names_expected, NULL, NULL, NULL, &expect));
1200 1201 1202

	cl_assert(expect.idx == expect.len);

1203
	git_diff_free(diff);
1204 1205 1206 1207
	git_tree_free(tree);
	git_index_free(index);
}

1208 1209 1210 1211
void test_diff_rename__case_changes_are_split(void)
{
	git_index *index;
	git_tree *tree;
1212
	git_diff *diff = NULL;
1213
	diff_expects exp;
1214
	git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230

	cl_git_pass(git_repository_index(&index, g_repo));

	cl_git_pass(
		git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}"));

	cl_git_pass(p_rename("renames/ikeepsix.txt", "renames/IKEEPSIX.txt"));

	cl_git_pass(git_index_remove_bypath(index, "ikeepsix.txt"));
	cl_git_pass(git_index_add_bypath(index, "IKEEPSIX.txt"));
	cl_git_pass(git_index_write(index));

	cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, NULL));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
1231
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
1232 1233 1234 1235
	cl_assert_equal_i(2, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]);

1236 1237 1238 1239 1240
	opts.flags = GIT_DIFF_FIND_ALL;
	cl_git_pass(git_diff_find_similar(diff, &opts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
1241
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
1242 1243 1244
	cl_assert_equal_i(1, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]);

1245
	git_diff_free(diff);
1246
	git_index_free(index);
1247
	git_tree_free(tree);
1248 1249
}

1250 1251 1252 1253
void test_diff_rename__unmodified_can_be_renamed(void)
{
	git_index *index;
	git_tree *tree;
1254
	git_diff *diff = NULL;
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
	diff_expects exp;
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;

	cl_git_pass(git_repository_index(&index, g_repo));
	cl_git_pass(
		git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}"));

	cl_git_pass(p_rename("renames/ikeepsix.txt", "renames/ikeepsix2.txt"));

	cl_git_pass(git_index_remove_bypath(index, "ikeepsix.txt"));
	cl_git_pass(git_index_add_bypath(index, "ikeepsix2.txt"));
	cl_git_pass(git_index_write(index));

	cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
1273
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
1274 1275 1276 1277 1278 1279 1280 1281 1282
	cl_assert_equal_i(2, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]);

	opts.flags = GIT_DIFF_FIND_ALL;
	cl_git_pass(git_diff_find_similar(diff, &opts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
1283
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
1284 1285 1286 1287 1288
	cl_assert_equal_i(1, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]);

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
1289
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
1290 1291 1292
	cl_assert_equal_i(1, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]);

1293
	git_diff_free(diff);
1294 1295
	git_index_free(index);
	git_tree_free(tree);
1296
}
1297 1298 1299 1300

void test_diff_rename__rewrite_on_single_file(void)
{
	git_index *index;
1301
	git_diff *diff = NULL;
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
	diff_expects exp;
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT;

	diffopts.flags = GIT_DIFF_INCLUDE_UNTRACKED;

	findopts.flags = GIT_DIFF_FIND_FOR_UNTRACKED |
		GIT_DIFF_FIND_AND_BREAK_REWRITES |
		GIT_DIFF_FIND_RENAMES_FROM_REWRITES;

	cl_git_pass(git_repository_index(&index, g_repo));

	cl_git_rewritefile("renames/ikeepsix.txt",
		"This is enough content for the file to be rewritten.\n" \
		"This is enough content for the file to be rewritten.\n" \
		"This is enough content for the file to be rewritten.\n" \
		"This is enough content for the file to be rewritten.\n" \
		"This is enough content for the file to be rewritten.\n" \
		"This is enough content for the file to be rewritten.\n" \
		"This is enough content for the file to be rewritten.\n" \
		"This is enough content for the file to be rewritten.\n" \
		"This is enough content for the file to be rewritten.\n" \
		"This is enough content for the file to be rewritten.\n" \
		"This is enough content for the file to be rewritten.\n" \
		"This is enough content for the file to be rewritten.\n" \
		"This is enough content for the file to be rewritten.\n" \
		"This is enough content for the file to be rewritten.\n" \
		"This is enough content for the file to be rewritten.\n" \
		"This is enough content for the file to be rewritten.\n");

	cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, &diffopts));
	cl_git_pass(git_diff_find_similar(diff, &findopts));

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

	cl_git_pass(git_diff_foreach(
1338
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
1339 1340 1341 1342
	cl_assert_equal_i(2, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNTRACKED]);

1343
	git_diff_free(diff);
1344 1345
	git_index_free(index);
}
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372

void test_diff_rename__can_find_copy_to_split(void)
{
	git_buf c1 = GIT_BUF_INIT;
	git_index *index;
	git_tree *tree;
	git_diff *diff;
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
	diff_expects exp;

	cl_git_pass(git_futils_readbuffer(&c1, "renames/songof7cities.txt"));
	cl_git_pass(git_futils_writebuffer(&c1, "renames/untimely.txt", 0, 0));

	cl_git_pass(
		git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}"));

	cl_git_pass(git_repository_index(&index, g_repo));
	cl_git_pass(git_index_read_tree(index, tree));
	cl_git_pass(git_index_add_bypath(index, "untimely.txt"));

	diffopts.flags = GIT_DIFF_INCLUDE_UNMODIFIED;

	cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
1373
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
1374 1375 1376 1377 1378 1379 1380 1381 1382
	cl_assert_equal_i(4, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(3, exp.file_status[GIT_DELTA_UNMODIFIED]);

	opts.flags = GIT_DIFF_FIND_ALL;
	cl_git_pass(git_diff_find_similar(diff, &opts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
1383
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
1384 1385 1386 1387 1388 1389 1390 1391 1392
	cl_assert_equal_i(5, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]);
	cl_assert_equal_i(3, exp.file_status[GIT_DELTA_UNMODIFIED]);

	git_diff_free(diff);
	git_tree_free(tree);
	git_index_free(index);

1393
	git_buf_dispose(&c1);
1394
}
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421

void test_diff_rename__can_delete_unmodified_deltas(void)
{
	git_buf c1 = GIT_BUF_INIT;
	git_index *index;
	git_tree *tree;
	git_diff *diff;
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
	diff_expects exp;

	cl_git_pass(git_futils_readbuffer(&c1, "renames/songof7cities.txt"));
	cl_git_pass(git_futils_writebuffer(&c1, "renames/untimely.txt", 0, 0));

	cl_git_pass(
		git_revparse_single((git_object **)&tree, g_repo, "HEAD^{tree}"));

	cl_git_pass(git_repository_index(&index, g_repo));
	cl_git_pass(git_index_read_tree(index, tree));
	cl_git_pass(git_index_add_bypath(index, "untimely.txt"));

	diffopts.flags = GIT_DIFF_INCLUDE_UNMODIFIED;

	cl_git_pass(git_diff_tree_to_index(&diff, g_repo, tree, index, &diffopts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
1422
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
1423 1424 1425 1426
	cl_assert_equal_i(4, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(3, exp.file_status[GIT_DELTA_UNMODIFIED]);

1427
	opts.flags = GIT_DIFF_FIND_ALL | GIT_DIFF_FIND_REMOVE_UNMODIFIED;
1428 1429 1430 1431
	cl_git_pass(git_diff_find_similar(diff, &opts));

	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_foreach(
1432
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
1433 1434 1435 1436 1437 1438 1439 1440
	cl_assert_equal_i(2, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]);

	git_diff_free(diff);
	git_tree_free(tree);
	git_index_free(index);

1441
	git_buf_dispose(&c1);
1442
}
1443 1444 1445

void test_diff_rename__matches_config_behavior(void)
{
1446 1447 1448
	const char *sha0 = INITIAL_COMMIT;
	const char *sha1 = COPY_RENAME_COMMIT;
	const char *sha2 = REWRITE_COPY_COMMIT;
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471

	git_tree *tree0, *tree1, *tree2;
	git_config *cfg;
	git_diff *diff;
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
	diff_expects exp;

	opts.flags = GIT_DIFF_FIND_BY_CONFIG;
	tree0 = resolve_commit_oid_to_tree(g_repo, sha0);
	tree1 = resolve_commit_oid_to_tree(g_repo, sha1);
	tree2 = resolve_commit_oid_to_tree(g_repo, sha2);

	diffopts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED;
	cl_git_pass(git_repository_config(&cfg, g_repo));

	/* diff.renames = false; no rename detection should happen */
	cl_git_pass(git_config_set_bool(cfg, "diff.renames", false));
	cl_git_pass(git_diff_tree_to_tree(
				&diff, g_repo, tree0, tree1, &diffopts));
	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_find_similar(diff, &opts));
	cl_git_pass(git_diff_foreach(diff,
1472
				diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
	cl_assert_equal_i(4, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_ADDED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
	git_diff_free(diff);

	/* diff.renames = true; should act like -M */
	cl_git_pass(git_config_set_bool(cfg, "diff.renames", true));
	cl_git_pass(git_diff_tree_to_tree(
				&diff, g_repo, tree0, tree1, &diffopts));
	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_find_similar(diff, &opts));
	cl_git_pass(git_diff_foreach(diff,
1486
				diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
	cl_assert_equal_i(3, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]);
	git_diff_free(diff);

	/* diff.renames = copies; should act like -M -C */
	cl_git_pass(git_config_set_string(cfg, "diff.renames", "copies"));
	cl_git_pass(git_diff_tree_to_tree(
				&diff, g_repo, tree1, tree2, &diffopts));
	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_find_similar(diff, &opts));
	cl_git_pass(git_diff_foreach(diff,
1500
				diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
	cl_assert_equal_i(4, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]);
	git_diff_free(diff);

	/* NULL find options is the same as GIT_DIFF_FIND_BY_CONFIG */
	cl_git_pass(git_diff_tree_to_tree(
				&diff, g_repo, tree1, tree2, &diffopts));
	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_find_similar(diff, NULL));
	cl_git_pass(git_diff_foreach(diff,
1513
				diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
	cl_assert_equal_i(4, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]);
	git_diff_free(diff);

	/* Cleanup */
	git_tree_free(tree0);
	git_tree_free(tree1);
	git_tree_free(tree2);
	git_config_free(cfg);
}

void test_diff_rename__can_override_thresholds_when_obeying_config(void)
{
1529 1530
	const char *sha1 = COPY_RENAME_COMMIT;
	const char *sha2 = REWRITE_COPY_COMMIT;
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555

	git_tree *tree1, *tree2;
	git_config *cfg;
	git_diff *diff;
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
	diff_expects exp;

	tree1 = resolve_commit_oid_to_tree(g_repo, sha1);
	tree2 = resolve_commit_oid_to_tree(g_repo, sha2);

	diffopts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED;
	opts.flags = GIT_DIFF_FIND_BY_CONFIG;

	cl_git_pass(git_repository_config(&cfg, g_repo));
	cl_git_pass(git_config_set_string(cfg, "diff.renames", "copies"));
	git_config_free(cfg);

	/* copy threshold = 96%, should see creation of ikeepsix.txt */
	opts.copy_threshold = 96;
	cl_git_pass(git_diff_tree_to_tree(
				&diff, g_repo, tree1, tree2, &diffopts));
	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_find_similar(diff, &opts));
	cl_git_pass(git_diff_foreach(diff,
1556
				diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
	cl_assert_equal_i(4, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]);
	git_diff_free(diff);

	/* copy threshold = 20%, should see sixserving.txt => ikeepsix.txt */
	opts.copy_threshold = 20;
	cl_git_pass(git_diff_tree_to_tree(
				&diff, g_repo, tree1, tree2, &diffopts));
	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_find_similar(diff, &opts));
	cl_git_pass(git_diff_foreach(diff,
1570
				diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
	cl_assert_equal_i(4, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]);
	git_diff_free(diff);

	/* Cleanup */
	git_tree_free(tree1);
	git_tree_free(tree2);
}
1581 1582 1583

void test_diff_rename__by_config_doesnt_mess_with_whitespace_settings(void)
{
1584 1585
	const char *sha1 = REWRITE_COPY_COMMIT;
	const char *sha2 = RENAME_MODIFICATION_COMMIT;
1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610

	git_tree *tree1, *tree2;
	git_config *cfg;
	git_diff *diff;
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
	diff_expects exp;

	tree1 = resolve_commit_oid_to_tree(g_repo, sha1);
	tree2 = resolve_commit_oid_to_tree(g_repo, sha2);

	diffopts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED;
	opts.flags = GIT_DIFF_FIND_BY_CONFIG;

	cl_git_pass(git_repository_config(&cfg, g_repo));
	cl_git_pass(git_config_set_string(cfg, "diff.renames", "copies"));
	git_config_free(cfg);

	/* Don't ignore whitespace; this should find a change in sixserving.txt */
	opts.flags |= 0 | GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE;
	cl_git_pass(git_diff_tree_to_tree(
				&diff, g_repo, tree1, tree2, &diffopts));
	memset(&exp, 0, sizeof(exp));
	cl_git_pass(git_diff_find_similar(diff, &opts));
	cl_git_pass(git_diff_foreach(diff,
1611
				diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
	cl_assert_equal_i(5, exp.files);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]);
	git_diff_free(diff);

	/* Cleanup */
	git_tree_free(tree1);
	git_tree_free(tree2);
}
1623

1624
static void expect_files_renamed(const char *one, const char *two, uint32_t whitespace_flags)
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
{
	git_index *index;
	git_diff *diff = NULL;
	diff_expects exp;
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT;

	diffopts.flags = GIT_DIFF_INCLUDE_UNTRACKED;
	findopts.flags = GIT_DIFF_FIND_FOR_UNTRACKED |
		GIT_DIFF_FIND_AND_BREAK_REWRITES |
1635 1636
		GIT_DIFF_FIND_RENAMES_FROM_REWRITES |
		whitespace_flags;
1637 1638 1639

	cl_git_pass(git_repository_index(&index, g_repo));

1640
	cl_git_rewritefile("renames/ikeepsix.txt", one);
1641 1642 1643
	cl_git_pass(git_index_add_bypath(index, "ikeepsix.txt"));

	cl_git_rmfile("renames/ikeepsix.txt");
1644 1645 1646 1647 1648 1649 1650 1651
	cl_git_rewritefile("renames/ikeepsix2.txt", two);

	cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, &diffopts));
	cl_git_pass(git_diff_find_similar(diff, &findopts));

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

	cl_git_pass(git_diff_foreach(
1652
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
	cl_assert_equal_i(1, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_RENAMED]);

	git_diff_free(diff);
	git_index_free(index);
}

/* test some variations on empty and blank files */
void test_diff_rename__empty_files_renamed(void)
{
	/* empty files are identical when ignoring whitespace or not */
	expect_files_renamed("", "", GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE);
	expect_files_renamed("", "",  GIT_DIFF_FIND_IGNORE_WHITESPACE);
}

/* test that blank files are similar when ignoring whitespace */
void test_diff_rename__blank_files_renamed_when_ignoring_whitespace(void)
{
	expect_files_renamed("", "\n\n",  GIT_DIFF_FIND_IGNORE_WHITESPACE);
	expect_files_renamed("", "\r\n\r\n",  GIT_DIFF_FIND_IGNORE_WHITESPACE);
	expect_files_renamed("\r\n\r\n", "\n\n\n",  GIT_DIFF_FIND_IGNORE_WHITESPACE);

	expect_files_renamed("    ", "\n\n",  GIT_DIFF_FIND_IGNORE_WHITESPACE);
	expect_files_renamed("   \n   \n", "\n\n",  GIT_DIFF_FIND_IGNORE_WHITESPACE);
}

/* blank files are not similar when whitespace is not ignored */
static void expect_files_not_renamed(const char *one, const char *two, uint32_t whitespace_flags)
{
	git_index *index;
	git_diff *diff = NULL;
	diff_expects exp;
	git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT;

	diffopts.flags = GIT_DIFF_INCLUDE_UNTRACKED;

	findopts.flags = GIT_DIFF_FIND_FOR_UNTRACKED |
		whitespace_flags;

	cl_git_pass(git_repository_index(&index, g_repo));

	cl_git_rewritefile("renames/ikeepsix.txt", one);
	cl_git_pass(git_index_add_bypath(index, "ikeepsix.txt"));

	cl_git_rmfile("renames/ikeepsix.txt");
	cl_git_rewritefile("renames/ikeepsix2.txt", two);
1700 1701 1702 1703 1704 1705 1706

	cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, index, &diffopts));
	cl_git_pass(git_diff_find_similar(diff, &findopts));

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

	cl_git_pass(git_diff_foreach(
1707
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
1708 1709 1710 1711 1712 1713 1714
	cl_assert_equal_i(2, exp.files);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNTRACKED]);

	git_diff_free(diff);
	git_index_free(index);
}
1715 1716 1717 1718 1719 1720 1721 1722

/* test that blank files are similar when ignoring renames */
void test_diff_rename__blank_files_not_renamed_when_not_ignoring_whitespace(void)
{
	expect_files_not_renamed("", "\r\n\r\n\r\n",  GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE);
	expect_files_not_renamed("", "\n\n\n\n",  GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE);
	expect_files_not_renamed("\n\n\n\n", "\r\n\r\n\r\n",  GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE);
}
1723 1724 1725 1726 1727 1728 1729 1730

/* test that 100% renames and copies emit the correct patch file
 * git diff --find-copies-harder -M100 -B100 \
 *          31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 \
 *          2bc7f351d20b53f1c72c16c4b036e491c478c49a
 */
void test_diff_rename__identical(void)
{
1731 1732
	const char *old_sha = INITIAL_COMMIT;
	const char *new_sha = COPY_RENAME_COMMIT;
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
	git_tree *old_tree, *new_tree;
    git_diff *diff;
	git_diff_options diff_opts = GIT_DIFF_OPTIONS_INIT;
	git_diff_find_options find_opts = GIT_DIFF_FIND_OPTIONS_INIT;
	git_buf diff_buf = GIT_BUF_INIT;
	const char *expected =
		"diff --git a/serving.txt b/sixserving.txt\n"
		"similarity index 100%\n"
		"rename from serving.txt\n"
		"rename to sixserving.txt\n"
		"diff --git a/sevencities.txt b/songofseven.txt\n"
		"similarity index 100%\n"
		"copy from sevencities.txt\n"
		"copy to songofseven.txt\n";

	old_tree = resolve_commit_oid_to_tree(g_repo, old_sha);
	new_tree = resolve_commit_oid_to_tree(g_repo, new_sha);

	diff_opts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED;
	find_opts.flags = GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED |
		GIT_DIFF_FIND_EXACT_MATCH_ONLY;

	cl_git_pass(git_diff_tree_to_tree(&diff,
		g_repo, old_tree, new_tree, &diff_opts));
	cl_git_pass(git_diff_find_similar(diff, &find_opts));

	cl_git_pass(git_diff_to_buf(&diff_buf, diff, GIT_DIFF_FORMAT_PATCH));

	cl_assert_equal_s(expected, diff_buf.ptr);

1763
	git_buf_dispose(&diff_buf);
1764 1765 1766 1767 1768
	git_diff_free(diff);
	git_tree_free(old_tree);
	git_tree_free(new_tree);
}

1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913
void test_diff_rename__rewrite_and_delete(void)
{
	const char *old_sha = RENAME_MODIFICATION_COMMIT;
	const char *new_sha = REWRITE_DELETE_COMMIT;
	git_tree *old_tree, *new_tree;
	git_diff *diff;
	git_diff_find_options find_opts = GIT_DIFF_FIND_OPTIONS_INIT;
	git_buf diff_buf = GIT_BUF_INIT;
	const char *expected =
		"diff --git a/ikeepsix.txt b/ikeepsix.txt\n"
		"deleted file mode 100644\n"
		"index eaf4a3e..0000000\n"
		"--- a/ikeepsix.txt\n"
		"+++ /dev/null\n"
		"@@ -1,27 +0,0 @@\n"
		"-I Keep Six Honest Serving-Men\n"
		"-=============================\n"
		"-\n"
		"-She sends'em abroad on her own affairs,\n"
		"- From the second she opens her eyes—\n"
		"-One million Hows, two million Wheres,\n"
		"-And seven million Whys!\n"
		"-\n"
		"-I let them rest from nine till five,\n"
		"- For I am busy then,\n"
		"-As well as breakfast, lunch, and tea,\n"
		"- For they are hungry men.\n"
		"-But different folk have different views;\n"
		"-I know a person small—\n"
		"-She keeps ten million serving-men,\n"
		"-Who get no rest at all!\n"
		"-\n"
		"-  -- Rudyard Kipling\n"
		"-\n"
		"-I KEEP six honest serving-men\n"
		"- (They taught me all I knew);\n"
		"-Their names are What and Why and When\n"
		"- And How and Where and Who.\n"
		"-I send them over land and sea,\n"
		"- I send them east and west;\n"
		"-But after they have worked for me,\n"
		"- I give them all a rest.\n"
		"diff --git a/songof7cities.txt b/songof7cities.txt\n"
		"index 4210ffd..95ceb12 100644\n"
		"--- a/songof7cities.txt\n"
		"+++ b/songof7cities.txt\n"
		"@@ -1,45 +1,45 @@\n"
		"-The Song of Seven Cities\n"
		"+THE SONG OF SEVEN CITIES\n"
		" ------------------------\n"
		" \n"
		"-I WAS Lord of Cities very sumptuously builded.\n"
		"-Seven roaring Cities paid me tribute from afar.\n"
		"-Ivory their outposts were--the guardrooms of them gilded,\n"
		"-And garrisoned with Amazons invincible in war.\n"
		"-\n"
		"-All the world went softly when it walked before my Cities--\n"
		"-Neither King nor Army vexed my peoples at their toil,\n"
		"-Never horse nor chariot irked or overbore my Cities,\n"
		"-Never Mob nor Ruler questioned whence they drew their spoil.\n"
		"-\n"
		"-Banded, mailed and arrogant from sunrise unto sunset;\n"
		"-Singing while they sacked it, they possessed the land at large.\n"
		"-Yet when men would rob them, they resisted, they made onset\n"
		"-And pierced the smoke of battle with a thousand-sabred charge.\n"
		"-\n"
		"-So they warred and trafficked only yesterday, my Cities.\n"
		"-To-day there is no mark or mound of where my Cities stood.\n"
		"-For the River rose at midnight and it washed away my Cities.\n"
		"-They are evened with Atlantis and the towns before the Flood.\n"
		"-\n"
		"-Rain on rain-gorged channels raised the water-levels round them,\n"
		"-Freshet backed on freshet swelled and swept their world from sight,\n"
		"-Till the emboldened floods linked arms and, flashing forward, drowned them--\n"
		"-Drowned my Seven Cities and their peoples in one night!\n"
		"-\n"
		"-Low among the alders lie their derelict foundations,\n"
		"-The beams wherein they trusted and the plinths whereon they built--\n"
		"-My rulers and their treasure and their unborn populations,\n"
		"-Dead, destroyed, aborted, and defiled with mud and silt!\n"
		"-\n"
		"-The Daughters of the Palace whom they cherished in my Cities,\n"
		"-My silver-tongued Princesses, and the promise of their May--\n"
		"-Their bridegrooms of the June-tide--all have perished in my Cities,\n"
		"-With the harsh envenomed virgins that can neither love nor play.\n"
		"-\n"
		"-I was Lord of Cities--I will build anew my Cities,\n"
		"-Seven, set on rocks, above the wrath of any flood.\n"
		"-Nor will I rest from search till I have filled anew my Cities\n"
		"-With peoples undefeated of the dark, enduring blood.\n"
		"+I WAS LORD OF CITIES VERY SUMPTUOUSLY BUILDED.\n"
		"+SEVEN ROARING CITIES PAID ME TRIBUTE FROM AFAR.\n"
		"+IVORY THEIR OUTPOSTS WERE--THE GUARDROOMS OF THEM GILDED,\n"
		"+AND GARRISONED WITH AMAZONS INVINCIBLE IN WAR.\n"
		"+\n"
		"+ALL THE WORLD WENT SOFTLY WHEN IT WALKED BEFORE MY CITIES--\n"
		"+NEITHER KING NOR ARMY VEXED MY PEOPLES AT THEIR TOIL,\n"
		"+NEVER HORSE NOR CHARIOT IRKED OR OVERBORE MY CITIES,\n"
		"+NEVER MOB NOR RULER QUESTIONED WHENCE THEY DREW THEIR SPOIL.\n"
		"+\n"
		"+BANDED, MAILED AND ARROGANT FROM SUNRISE UNTO SUNSET;\n"
		"+SINGING WHILE THEY SACKED IT, THEY POSSESSED THE LAND AT LARGE.\n"
		"+YET WHEN MEN WOULD ROB THEM, THEY RESISTED, THEY MADE ONSET\n"
		"+AND PIERCED THE SMOKE OF BATTLE WITH A THOUSAND-SABRED CHARGE.\n"
		"+\n"
		"+SO THEY WARRED AND TRAFFICKED ONLY YESTERDAY, MY CITIES.\n"
		"+TO-DAY THERE IS NO MARK OR MOUND OF WHERE MY CITIES STOOD.\n"
		"+FOR THE RIVER ROSE AT MIDNIGHT AND IT WASHED AWAY MY CITIES.\n"
		"+THEY ARE EVENED WITH ATLANTIS AND THE TOWNS BEFORE THE FLOOD.\n"
		"+\n"
		"+RAIN ON RAIN-GORGED CHANNELS RAISED THE WATER-LEVELS ROUND THEM,\n"
		"+FRESHET BACKED ON FRESHET SWELLED AND SWEPT THEIR WORLD FROM SIGHT,\n"
		"+TILL THE EMBOLDENED FLOODS LINKED ARMS AND, FLASHING FORWARD, DROWNED THEM--\n"
		"+DROWNED MY SEVEN CITIES AND THEIR PEOPLES IN ONE NIGHT!\n"
		"+\n"
		"+LOW AMONG THE ALDERS LIE THEIR DERELICT FOUNDATIONS,\n"
		"+THE BEAMS WHEREIN THEY TRUSTED AND THE PLINTHS WHEREON THEY BUILT--\n"
		"+MY RULERS AND THEIR TREASURE AND THEIR UNBORN POPULATIONS,\n"
		"+DEAD, DESTROYED, ABORTED, AND DEFILED WITH MUD AND SILT!\n"
		"+\n"
		"+THE DAUGHTERS OF THE PALACE WHOM THEY CHERISHED IN MY CITIES,\n"
		"+MY SILVER-TONGUED PRINCESSES, AND THE PROMISE OF THEIR MAY--\n"
		"+THEIR BRIDEGROOMS OF THE JUNE-TIDE--ALL HAVE PERISHED IN MY CITIES,\n"
		"+WITH THE HARSH ENVENOMED VIRGINS THAT CAN NEITHER LOVE NOR PLAY.\n"
		"+\n"
		"+I WAS LORD OF CITIES--I WILL BUILD ANEW MY CITIES,\n"
		"+SEVEN, SET ON ROCKS, ABOVE THE WRATH OF ANY FLOOD.\n"
		"+NOR WILL I REST FROM SEARCH TILL I HAVE FILLED ANEW MY CITIES\n"
		"+WITH PEOPLES UNDEFEATED OF THE DARK, ENDURING BLOOD.\n"
		" \n"
		" To the sound of trumpets shall their seed restore my Cities\n"
		" Wealthy and well-weaponed, that once more may I behold\n";

	old_tree = resolve_commit_oid_to_tree(g_repo, old_sha);
	new_tree = resolve_commit_oid_to_tree(g_repo, new_sha);

	find_opts.flags = GIT_DIFF_FIND_RENAMES_FROM_REWRITES;

	cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, old_tree, new_tree, NULL));
	cl_git_pass(git_diff_find_similar(diff, &find_opts));

	cl_git_pass(git_diff_to_buf(&diff_buf, diff, GIT_DIFF_FORMAT_PATCH));

	cl_assert_equal_s(expected, diff_buf.ptr);

1914
	git_buf_dispose(&diff_buf);
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976
	git_diff_free(diff);
	git_tree_free(old_tree);
	git_tree_free(new_tree);
}

void test_diff_rename__delete_and_rename(void)
{
	const char *old_sha = RENAME_MODIFICATION_COMMIT;
	const char *new_sha = DELETE_RENAME_COMMIT;
	git_tree *old_tree, *new_tree;
	git_diff *diff;
	git_diff_find_options find_opts = GIT_DIFF_FIND_OPTIONS_INIT;
	git_buf diff_buf = GIT_BUF_INIT;
	const char *expected =
		"diff --git a/sixserving.txt b/sixserving.txt\n"
		"deleted file mode 100644\n"
		"index f90d4fc..0000000\n"
		"--- a/sixserving.txt\n"
		"+++ /dev/null\n"
		"@@ -1,25 +0,0 @@\n"
		"-I  KEEP  six  honest  serving-men\n"
		"-  (They  taught  me  all  I  knew);\n"
		"-Their  names  are  What  and  Why  and  When\n"
		"-  And  How  and  Where  and  Who.\n"
		"-I  send  them  over  land  and  sea,\n"
		"-  I  send  them  east  and  west;\n"
		"-But  after  they  have  worked  for  me,\n"
		"-  I  give  them  all  a  rest.\n"
		"-\n"
		"-I  let  them  rest  from  nine  till  five,\n"
		"-  For  I  am  busy  then,\n"
		"-As  well  as  breakfast,  lunch,  and  tea,\n"
		"-  For  they  are  hungry  men.\n"
		"-But  different  folk  have  different  views;\n"
		"-I  know  a  person  small—\n"
		"-She  keeps  ten  million  serving-men,\n"
		"-Who  get  no  rest  at  all!\n"
		"-\n"
		"-She  sends'em  abroad  on  her  own  affairs,\n"
		"-  From  the  second  she  opens  her  eyes—\n"
		"-One  million  Hows,  two  million  Wheres,\n"
		"-And  seven  million  Whys!\n"
		"-\n"
		"-    --  Rudyard  Kipling\n"
		"-\n"
		"diff --git a/songof7cities.txt b/sixserving.txt\n"
		"similarity index 100%\n"
		"rename from songof7cities.txt\n"
		"rename to sixserving.txt\n";

	old_tree = resolve_commit_oid_to_tree(g_repo, old_sha);
	new_tree = resolve_commit_oid_to_tree(g_repo, new_sha);

	find_opts.flags = GIT_DIFF_FIND_RENAMES_FROM_REWRITES;

	cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, old_tree, new_tree, NULL));
	cl_git_pass(git_diff_find_similar(diff, &find_opts));

	cl_git_pass(git_diff_to_buf(&diff_buf, diff, GIT_DIFF_FORMAT_PATCH));

	cl_assert_equal_s(expected, diff_buf.ptr);

1977
	git_buf_dispose(&diff_buf);
1978 1979 1980 1981
	git_diff_free(diff);
	git_tree_free(old_tree);
	git_tree_free(new_tree);
}
Kartikaya Gupta committed
1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034

/*
 * The break_rewrite branch contains a testcase reduced from
 * a real-world scenario, rather than being "constructed" like
 * the above tests seem to be. There are two commits layered
 * on top of the repo's initial commit; the base commit which
 * clears out the files from the initial commit and installs
 * four files. And then there's the modification commit which
 * mutates the files in such a way as to trigger the bug in
 * libgit2.
 * commit db98035f715427eef1f5e17f03e1801c05301e9e
 *   serving.txt     (deleted)
 *   sevencities.txt (deleted)
 *   AAA             (313 lines)
 *   BBB             (314 lines)
 *   CCC             (704 lines)
 *   DDD             (314 lines, identical to BBB)
 * commit 7e7bfb88ba9bc65fd700fee1819cf1c317aafa56
 *   This deletes CCC and makes slight modifications
 *   to AAA, BBB, and DDD. The find_best_matches loop
 *   for git_diff_find_similar computes the following:
 *   CCC    moved to    AAA     (similarity 91)
 *   CCC    copied to   AAA     (similarity 91)
 *   DDD    moved to    BBB     (similarity 52)
 *   CCC    copied to   BBB     (similarity 90)
 *   BBB    moved to    DDD     (similarity 52)
 *   CCC    copied to   DDD     (similarity 90)
 * The code to rewrite the diffs by resolving these
 * copies/renames would resolve the BBB <-> DDD moves
 * but then still leave BBB as a rename target for
 * the deleted file CCC. Since the split flag on BBB
 * was cleared, this would trigger an error.
 */
void test_diff_rename__break_rewrite(void)
{
	const char *old_sha = BREAK_REWRITE_BASE_COMMIT;
	const char *new_sha = BREAK_REWRITE_COMMIT;
	git_tree *old_tree, *new_tree;
	git_diff *diff;
	git_diff_find_options find_opts = GIT_DIFF_FIND_OPTIONS_INIT;

	old_tree = resolve_commit_oid_to_tree(g_repo, old_sha);
	new_tree = resolve_commit_oid_to_tree(g_repo, new_sha);

	find_opts.flags = GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES | GIT_DIFF_BREAK_REWRITES | GIT_DIFF_BREAK_REWRITES_FOR_RENAMES_ONLY;

	cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, old_tree, new_tree, NULL));
	cl_git_pass(git_diff_find_similar(diff, &find_opts));

	git_diff_free(diff);
	git_tree_free(old_tree);
	git_tree_free(new_tree);
}