patch.c 23.1 KB
Newer Older
1
#include "clar_libgit2.h"
2 3
#include "git2/sys/repository.h"

4
#include "diff_helpers.h"
5
#include "diff.h"
6 7
#include "repository.h"
#include "buf_text.h"
8 9 10 11 12 13 14 15 16 17 18 19

static git_repository *g_repo = NULL;

void test_diff_patch__initialize(void)
{
}

void test_diff_patch__cleanup(void)
{
	cl_git_sandbox_cleanup();
}

20
#define EXPECTED_HEADER "diff --git a/subdir.txt b/subdir.txt\n" \
21 22 23 24 25
	"deleted file mode 100644\n" \
	"index e8ee89e..0000000\n" \
	"--- a/subdir.txt\n" \
	"+++ /dev/null\n"

26 27
#define EXPECTED_HUNK "@@ -1,2 +0,0 @@\n"

28 29 30 31 32 33
#define UTF8_HUNK_HEADER "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\n"

#define UTF8_TRUNCATED_A_HUNK_HEADER "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\n"

#define UTF8_TRUNCATED_L_HUNK_HEADER "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E\xE6\x97\xA5\n"

34
static int check_removal_cb(
35
	const git_diff_delta *delta,
36 37
	const git_diff_hunk *hunk,
	const git_diff_line *line,
38
	void *payload)
39
{
40
	switch (line->origin) {
41
	case GIT_DIFF_LINE_FILE_HDR:
42 43
		cl_assert_equal_s(EXPECTED_HEADER, line->content);
		cl_assert(hunk == NULL);
44 45 46
		goto check_delta;

	case GIT_DIFF_LINE_HUNK_HDR:
47
		cl_assert_equal_s(EXPECTED_HUNK, line->content);
48
		goto check_hunk;
49 50 51

	case GIT_DIFF_LINE_CONTEXT:
	case GIT_DIFF_LINE_DELETION:
52 53
		if (payload != NULL)
			return *(int *)payload;
54
		goto check_hunk;
55 56 57 58 59 60

	default:
		/* unexpected code path */
		return -1;
	}

61 62 63 64 65 66
check_hunk:
	cl_assert(hunk != NULL);
	cl_assert_equal_i(1, hunk->old_start);
	cl_assert_equal_i(2, hunk->old_lines);
	cl_assert_equal_i(0, hunk->new_start);
	cl_assert_equal_i(0, hunk->new_lines);
67

68
check_delta:
69 70
	cl_assert_equal_s("subdir.txt", delta->old_file.path);
	cl_assert_equal_s("subdir.txt", delta->new_file.path);
71
	cl_assert_equal_i(GIT_DELTA_DELETED, delta->status);
72

73
	return 0;
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
}

void test_diff_patch__can_properly_display_the_removal_of_a_file(void)
{
	/*
	* $ git diff 26a125e..735b6a2
	* diff --git a/subdir.txt b/subdir.txt
	* deleted file mode 100644
	* index e8ee89e..0000000
	* --- a/subdir.txt
	* +++ /dev/null
	* @@ -1,2 +0,0 @@
	* -Is it a bird?
	* -Is it a plane?
	*/

	const char *one_sha = "26a125e";
	const char *another_sha = "735b6a2";
	git_tree *one, *another;
93
	git_diff *diff;
94

95 96
	g_repo = cl_git_sandbox_init("status");

97 98 99
	one = resolve_commit_oid_to_tree(g_repo, one_sha);
	another = resolve_commit_oid_to_tree(g_repo, another_sha);

100
	cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, one, another, NULL));
101

Russell Belfer committed
102 103
	cl_git_pass(git_diff_print(
		diff, GIT_DIFF_FORMAT_PATCH, check_removal_cb, NULL));
104

105
	git_diff_free(diff);
106 107 108 109

	git_tree_free(another);
	git_tree_free(one);
}
110

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
void test_diff_patch__can_cancel_diff_print(void)
{
	const char *one_sha = "26a125e";
	const char *another_sha = "735b6a2";
	git_tree *one, *another;
	git_diff *diff;
	int fail_with;

	g_repo = cl_git_sandbox_init("status");

	one = resolve_commit_oid_to_tree(g_repo, one_sha);
	another = resolve_commit_oid_to_tree(g_repo, another_sha);

	cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, one, another, NULL));

	fail_with = -2323;

	cl_git_fail_with(git_diff_print(
		diff, GIT_DIFF_FORMAT_PATCH, check_removal_cb, &fail_with),
		fail_with);

	fail_with = 45;

	cl_git_fail_with(git_diff_print(
		diff, GIT_DIFF_FORMAT_PATCH, check_removal_cb, &fail_with),
		fail_with);

	git_diff_free(diff);

	git_tree_free(another);
	git_tree_free(one);
}

144 145 146 147 148
void test_diff_patch__to_string(void)
{
	const char *one_sha = "26a125e";
	const char *another_sha = "735b6a2";
	git_tree *one, *another;
149 150
	git_diff *diff;
	git_patch *patch;
Nicolas Hake committed
151
	git_buf buf = GIT_BUF_INIT;
152 153
	const char *expected = "diff --git a/subdir.txt b/subdir.txt\ndeleted file mode 100644\nindex e8ee89e..0000000\n--- a/subdir.txt\n+++ /dev/null\n@@ -1,2 +0,0 @@\n-Is it a bird?\n-Is it a plane?\n";

154 155
	g_repo = cl_git_sandbox_init("status");

156 157 158
	one = resolve_commit_oid_to_tree(g_repo, one_sha);
	another = resolve_commit_oid_to_tree(g_repo, another_sha);

159
	cl_git_pass(git_diff_tree_to_tree(&diff, g_repo, one, another, NULL));
160

161
	cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
162

Russell Belfer committed
163
	cl_git_pass(git_patch_from_diff(&patch, diff, 0));
164

Nicolas Hake committed
165
	cl_git_pass(git_patch_to_buf(&buf, patch));
166

Nicolas Hake committed
167
	cl_assert_equal_s(expected, buf.ptr);
168

169 170 171 172
	cl_assert_equal_sz(31, git_patch_size(patch, 0, 0, 0));
	cl_assert_equal_sz(31, git_patch_size(patch, 1, 0, 0));
	cl_assert_equal_sz(31 + 16, git_patch_size(patch, 1, 1, 0));
	cl_assert_equal_sz(strlen(expected), git_patch_size(patch, 1, 1, 1));
173

174
	git_buf_dispose(&buf);
175 176
	git_patch_free(patch);
	git_diff_free(diff);
177 178 179
	git_tree_free(another);
	git_tree_free(one);
}
180

181 182 183 184 185
void test_diff_patch__config_options(void)
{
	const char *one_sha = "26a125e"; /* current HEAD */
	git_tree *one;
	git_config *cfg;
186 187
	git_diff *diff;
	git_patch *patch;
Nicolas Hake committed
188
	git_buf buf = GIT_BUF_INIT;
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
	char *onefile = "staged_changes_modified_file";
	const char *expected1 = "diff --git c/staged_changes_modified_file i/staged_changes_modified_file\nindex 70bd944..906ee77 100644\n--- c/staged_changes_modified_file\n+++ i/staged_changes_modified_file\n@@ -1 +1,2 @@\n staged_changes_modified_file\n+staged_changes_modified_file\n";
	const char *expected2 = "diff --git i/staged_changes_modified_file w/staged_changes_modified_file\nindex 906ee77..011c344 100644\n--- i/staged_changes_modified_file\n+++ w/staged_changes_modified_file\n@@ -1,2 +1,3 @@\n staged_changes_modified_file\n staged_changes_modified_file\n+staged_changes_modified_file\n";
	const char *expected3 = "diff --git staged_changes_modified_file staged_changes_modified_file\nindex 906ee77..011c344 100644\n--- staged_changes_modified_file\n+++ staged_changes_modified_file\n@@ -1,2 +1,3 @@\n staged_changes_modified_file\n staged_changes_modified_file\n+staged_changes_modified_file\n";
	const char *expected4 = "diff --git staged_changes_modified_file staged_changes_modified_file\nindex 70bd9443ada0..906ee7711f4f 100644\n--- staged_changes_modified_file\n+++ staged_changes_modified_file\n@@ -1 +1,2 @@\n staged_changes_modified_file\n+staged_changes_modified_file\n";

	g_repo = cl_git_sandbox_init("status");
	cl_git_pass(git_repository_config(&cfg, g_repo));
	one = resolve_commit_oid_to_tree(g_repo, one_sha);
	opts.pathspec.count = 1;
	opts.pathspec.strings = &onefile;


	cl_git_pass(git_config_set_string(cfg, "diff.mnemonicprefix", "true"));

	cl_git_pass(git_diff_tree_to_index(&diff, g_repo, one, NULL, &opts));

	cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
Russell Belfer committed
208
	cl_git_pass(git_patch_from_diff(&patch, diff, 0));
Nicolas Hake committed
209 210
	cl_git_pass(git_patch_to_buf(&buf, patch));
	cl_assert_equal_s(expected1, buf.ptr);
211

Nicolas Hake committed
212
	git_buf_clear(&buf);
213 214
	git_patch_free(patch);
	git_diff_free(diff);
215 216 217 218

	cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));

	cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
Russell Belfer committed
219
	cl_git_pass(git_patch_from_diff(&patch, diff, 0));
Nicolas Hake committed
220 221
	cl_git_pass(git_patch_to_buf(&buf, patch));
	cl_assert_equal_s(expected2, buf.ptr);
222

Nicolas Hake committed
223
	git_buf_clear(&buf);
224 225
	git_patch_free(patch);
	git_diff_free(diff);
226 227 228 229 230 231 232


	cl_git_pass(git_config_set_string(cfg, "diff.noprefix", "true"));

	cl_git_pass(git_diff_index_to_workdir(&diff, g_repo, NULL, &opts));

	cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
Russell Belfer committed
233
	cl_git_pass(git_patch_from_diff(&patch, diff, 0));
Nicolas Hake committed
234 235
	cl_git_pass(git_patch_to_buf(&buf, patch));
	cl_assert_equal_s(expected3, buf.ptr);
236

Nicolas Hake committed
237
	git_buf_clear(&buf);
238 239
	git_patch_free(patch);
	git_diff_free(diff);
240 241 242 243 244 245 246


	cl_git_pass(git_config_set_int32(cfg, "core.abbrev", 12));

	cl_git_pass(git_diff_tree_to_index(&diff, g_repo, one, NULL, &opts));

	cl_assert_equal_i(1, (int)git_diff_num_deltas(diff));
Russell Belfer committed
247
	cl_git_pass(git_patch_from_diff(&patch, diff, 0));
Nicolas Hake committed
248 249
	cl_git_pass(git_patch_to_buf(&buf, patch));
	cl_assert_equal_s(expected4, buf.ptr);
250

Nicolas Hake committed
251
	git_buf_clear(&buf);
252 253
	git_patch_free(patch);
	git_diff_free(diff);
254

255
	git_buf_dispose(&buf);
256 257 258 259
	git_tree_free(one);
	git_config_free(cfg);
}

260 261
void test_diff_patch__hunks_have_correct_line_numbers(void)
{
262
	git_config *cfg;
263
	git_tree *head;
264
	git_diff_options opt = GIT_DIFF_OPTIONS_INIT;
265 266
	git_diff *diff;
	git_patch *patch;
267
	const git_diff_delta *delta;
268 269 270
	const git_diff_hunk *hunk;
	const git_diff_line *line;
	size_t hunklen;
271
	git_buf old_content = GIT_BUF_INIT, actual = GIT_BUF_INIT;
272
	const char *new_content = "The Song of Seven Cities\n------------------------\n\nI WAS Lord of Cities very sumptuously builded.\nSeven roaring Cities paid me tribute from afar.\nIvory their outposts were--the guardrooms of them gilded,\nAnd garrisoned with Amazons invincible in war.\n\nThis is some new text;\nNot as good as the old text;\nBut here it is.\n\nSo they warred and trafficked only yesterday, my Cities.\nTo-day there is no mark or mound of where my Cities stood.\nFor the River rose at midnight and it washed away my Cities.\nThey are evened with Atlantis and the towns before the Flood.\n\nRain on rain-gorged channels raised the water-levels round them,\nFreshet backed on freshet swelled and swept their world from sight,\nTill the emboldened floods linked arms and, flashing forward, drowned them--\nDrowned my Seven Cities and their peoples in one night!\n\nLow among the alders lie their derelict foundations,\nThe beams wherein they trusted and the plinths whereon they built--\nMy rulers and their treasure and their unborn populations,\nDead, destroyed, aborted, and defiled with mud and silt!\n\nAnother replacement;\nBreaking up the poem;\nGenerating some hunks.\n\nTo the sound of trumpets shall their seed restore my Cities\nWealthy and well-weaponed, that once more may I behold\nAll the world go softly when it walks before my Cities,\nAnd the horses and the chariots fleeing from them as of old!\n\n  -- Rudyard Kipling\n";
273 274 275

	g_repo = cl_git_sandbox_init("renames");

276 277
	cl_git_pass(git_config_new(&cfg));
	git_repository_set_config(g_repo, cfg);
278 279
	git_config_free(cfg);

280
	git_repository_reinit_filesystem(g_repo, false);
281

282 283 284
	cl_git_pass(
		git_futils_readbuffer(&old_content, "renames/songof7cities.txt"));

285
	cl_git_rewritefile("renames/songof7cities.txt", new_content);
286 287 288

	cl_git_pass(git_repository_head_tree(&head, g_repo));

289
	cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, head, &opt));
290 291 292

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

Russell Belfer committed
293 294
	cl_git_pass(git_patch_from_diff(&patch, diff, 0));
	cl_assert((delta = git_patch_get_delta(patch)) != NULL);
295 296

	cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status);
297
	cl_assert_equal_i(2, (int)git_patch_num_hunks(patch));
298 299 300 301

	/* check hunk 0 */

	cl_git_pass(
302
		git_patch_get_hunk(&hunk, &hunklen, patch, 0));
303 304 305

	cl_assert_equal_i(18, (int)hunklen);

306 307 308 309
	cl_assert_equal_i(6, (int)hunk->old_start);
	cl_assert_equal_i(15, (int)hunk->old_lines);
	cl_assert_equal_i(6, (int)hunk->new_start);
	cl_assert_equal_i(9, (int)hunk->new_lines);
310

311
	cl_assert_equal_i(18, (int)git_patch_num_lines_in_hunk(patch, 0));
312

313 314 315
	cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 0));
	cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)line->origin);
	cl_git_pass(git_buf_set(&actual, line->content, line->content_len));
316
	cl_assert_equal_s("Ivory their outposts were--the guardrooms of them gilded,\n", actual.ptr);
317 318
	cl_assert_equal_i(6, line->old_lineno);
	cl_assert_equal_i(6, line->new_lineno);
319
	cl_assert_equal_i(-1, line->content_offset);
320

321 322 323
	cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 3));
	cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)line->origin);
	cl_git_pass(git_buf_set(&actual, line->content, line->content_len));
324
	cl_assert_equal_s("All the world went softly when it walked before my Cities--\n", actual.ptr);
325 326
	cl_assert_equal_i(9, line->old_lineno);
	cl_assert_equal_i(-1, line->new_lineno);
327
	cl_assert_equal_i(252, line->content_offset);
328

329 330 331
	cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 12));
	cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)line->origin);
	cl_git_pass(git_buf_set(&actual, line->content, line->content_len));
332
	cl_assert_equal_s("This is some new text;\n", actual.ptr);
333 334
	cl_assert_equal_i(-1, line->old_lineno);
	cl_assert_equal_i(9, line->new_lineno);
335
	cl_assert_equal_i(252, line->content_offset);
336 337 338

	/* check hunk 1 */

339
	cl_git_pass(git_patch_get_hunk(&hunk, &hunklen, patch, 1));
340 341 342

	cl_assert_equal_i(18, (int)hunklen);

343 344 345 346
	cl_assert_equal_i(31, (int)hunk->old_start);
	cl_assert_equal_i(15, (int)hunk->old_lines);
	cl_assert_equal_i(25, (int)hunk->new_start);
	cl_assert_equal_i(9, (int)hunk->new_lines);
347

348
	cl_assert_equal_i(18, (int)git_patch_num_lines_in_hunk(patch, 1));
349

350 351 352
	cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 1, 0));
	cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)line->origin);
	cl_git_pass(git_buf_set(&actual, line->content, line->content_len));
353
	cl_assert_equal_s("My rulers and their treasure and their unborn populations,\n", actual.ptr);
354 355
	cl_assert_equal_i(31, line->old_lineno);
	cl_assert_equal_i(25, line->new_lineno);
356
	cl_assert_equal_i(-1, line->content_offset);
357

358 359 360
	cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 1, 3));
	cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)line->origin);
	cl_git_pass(git_buf_set(&actual, line->content, line->content_len));
361
	cl_assert_equal_s("The Daughters of the Palace whom they cherished in my Cities,\n", actual.ptr);
362 363
	cl_assert_equal_i(34, line->old_lineno);
	cl_assert_equal_i(-1, line->new_lineno);
364
	cl_assert_equal_i(1468, line->content_offset);
365

366 367 368
	cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 1, 12));
	cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)line->origin);
	cl_git_pass(git_buf_set(&actual, line->content, line->content_len));
369
	cl_assert_equal_s("Another replacement;\n", actual.ptr);
370 371
	cl_assert_equal_i(-1, line->old_lineno);
	cl_assert_equal_i(28, line->new_lineno);
372
	cl_assert_equal_i(1066, line->content_offset);
373

374 375
	git_patch_free(patch);
	git_diff_free(diff);
376 377 378 379 380 381 382 383 384 385

	/* Let's check line numbers when there is no newline */

	git_buf_rtrim(&old_content);
	cl_git_rewritefile("renames/songof7cities.txt", old_content.ptr);

	cl_git_pass(git_diff_tree_to_workdir(&diff, g_repo, head, &opt));

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

Russell Belfer committed
386 387
	cl_git_pass(git_patch_from_diff(&patch, diff, 0));
	cl_assert((delta = git_patch_get_delta(patch)) != NULL);
388 389

	cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status);
390
	cl_assert_equal_i(1, (int)git_patch_num_hunks(patch));
391 392 393

	/* check hunk 0 */

394
	cl_git_pass(git_patch_get_hunk(&hunk, &hunklen, patch, 0));
395 396 397

	cl_assert_equal_i(6, (int)hunklen);

398 399 400 401
	cl_assert_equal_i(46, (int)hunk->old_start);
	cl_assert_equal_i(4, (int)hunk->old_lines);
	cl_assert_equal_i(46, (int)hunk->new_start);
	cl_assert_equal_i(4, (int)hunk->new_lines);
402

403
	cl_assert_equal_i(6, (int)git_patch_num_lines_in_hunk(patch, 0));
404

405 406 407
	cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 1));
	cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)line->origin);
	cl_git_pass(git_buf_set(&actual, line->content, line->content_len));
408
	cl_assert_equal_s("And the horses and the chariots fleeing from them as of old!\n", actual.ptr);
409 410
	cl_assert_equal_i(47, line->old_lineno);
	cl_assert_equal_i(47, line->new_lineno);
411

412 413 414
	cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 2));
	cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)line->origin);
	cl_git_pass(git_buf_set(&actual, line->content, line->content_len));
415
	cl_assert_equal_s("\n", actual.ptr);
416 417
	cl_assert_equal_i(48, line->old_lineno);
	cl_assert_equal_i(48, line->new_lineno);
418

419 420 421
	cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 3));
	cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)line->origin);
	cl_git_pass(git_buf_set(&actual, line->content, line->content_len));
422
	cl_assert_equal_s("  -- Rudyard Kipling\n", actual.ptr);
423 424
	cl_assert_equal_i(49, line->old_lineno);
	cl_assert_equal_i(-1, line->new_lineno);
425

426 427 428
	cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 4));
	cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)line->origin);
	cl_git_pass(git_buf_set(&actual, line->content, line->content_len));
429
	cl_assert_equal_s("  -- Rudyard Kipling", actual.ptr);
430 431
	cl_assert_equal_i(-1, line->old_lineno);
	cl_assert_equal_i(49, line->new_lineno);
432

433 434 435
	cl_git_pass(git_patch_get_line_in_hunk(&line, patch, 0, 5));
	cl_assert_equal_i(GIT_DIFF_LINE_DEL_EOFNL, (int)line->origin);
	cl_git_pass(git_buf_set(&actual, line->content, line->content_len));
436
	cl_assert_equal_s("\n\\ No newline at end of file\n", actual.ptr);
437 438
	cl_assert_equal_i(-1, line->old_lineno);
	cl_assert_equal_i(49, line->new_lineno);
439

440 441
	git_patch_free(patch);
	git_diff_free(diff);
442

443 444
	git_buf_dispose(&actual);
	git_buf_dispose(&old_content);
445 446
	git_tree_free(head);
}
447 448

static void check_single_patch_stats(
449
	git_repository *repo, size_t hunks,
450
	size_t adds, size_t dels, size_t ctxt, size_t *sizes,
451
	const char *expected)
452
{
453 454
	git_diff *diff;
	git_patch *patch;
455
	const git_diff_delta *delta;
456
	size_t actual_ctxt, actual_adds, actual_dels;
457 458 459 460 461

	cl_git_pass(git_diff_index_to_workdir(&diff, repo, NULL, NULL));

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

Russell Belfer committed
462 463
	cl_git_pass(git_patch_from_diff(&patch, diff, 0));
	cl_assert((delta = git_patch_get_delta(patch)) != NULL);
464 465
	cl_assert_equal_i(GIT_DELTA_MODIFIED, (int)delta->status);

466
	cl_assert_equal_i((int)hunks, (int)git_patch_num_hunks(patch));
467

468
	cl_git_pass( git_patch_line_stats(
469
		&actual_ctxt, &actual_adds, &actual_dels, patch) );
470

471
	cl_assert_equal_sz(ctxt, actual_ctxt);
472 473
	cl_assert_equal_sz(adds, actual_adds);
	cl_assert_equal_sz(dels, actual_dels);
474

475
	if (expected != NULL) {
Nicolas Hake committed
476 477 478
		git_buf buf = GIT_BUF_INIT;
		cl_git_pass(git_patch_to_buf(&buf, patch));
		cl_assert_equal_s(expected, buf.ptr);
479
		git_buf_dispose(&buf);
480

481
		cl_assert_equal_sz(
482
			strlen(expected), git_patch_size(patch, 1, 1, 1));
483 484 485 486
	}

	if (sizes) {
		if (sizes[0])
487
			cl_assert_equal_sz(sizes[0], git_patch_size(patch, 0, 0, 0));
488
		if (sizes[1])
489
			cl_assert_equal_sz(sizes[1], git_patch_size(patch, 1, 0, 0));
490
		if (sizes[2])
491
			cl_assert_equal_sz(sizes[2], git_patch_size(patch, 1, 1, 0));
492
	}
493

494 495 496
	/* walk lines in hunk with basic sanity checks */
	for (; hunks > 0; --hunks) {
		size_t i, max_i;
497 498
		const git_diff_line *line;
		int last_new_lineno = -1, last_old_lineno = -1;
499

500
		max_i = git_patch_num_lines_in_hunk(patch, hunks - 1);
501 502 503 504

		for (i = 0; i < max_i; ++i) {
			int expected = 1;

505 506
			cl_git_pass(
				git_patch_get_line_in_hunk(&line, patch, hunks - 1, i));
507

508 509 510
			if (line->origin == GIT_DIFF_LINE_ADD_EOFNL ||
				line->origin == GIT_DIFF_LINE_DEL_EOFNL ||
				line->origin == GIT_DIFF_LINE_CONTEXT_EOFNL)
511 512
				expected = 0;

513 514 515 516 517
			if (line->old_lineno >= 0) {
				if (last_old_lineno >= 0)
					cl_assert_equal_i(
						expected, line->old_lineno - last_old_lineno);
				last_old_lineno = line->old_lineno;
518
			}
519 520 521 522 523 524

			if (line->new_lineno >= 0) {
				if (last_new_lineno >= 0)
					cl_assert_equal_i(
						expected, line->new_lineno - last_new_lineno);
				last_new_lineno = line->new_lineno;
525 526 527 528
			}
		}
	}

529 530
	git_patch_free(patch);
	git_diff_free(diff);
531 532 533 534
}

void test_diff_patch__line_counts_with_eofnl(void)
{
535
	git_config *cfg;
536 537 538
	git_buf content = GIT_BUF_INIT;
	const char *end;
	git_index *index;
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
	const char *expected =
		/* below is pasted output of 'git diff' with fn context removed */
		"diff --git a/songof7cities.txt b/songof7cities.txt\n"
		"index 378a7d9..3d0154e 100644\n"
		"--- a/songof7cities.txt\n"
		"+++ b/songof7cities.txt\n"
		"@@ -42,7 +42,7 @@ 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"
		"-All the world go softly when it walks before my Cities,\n"
		"+#All the world go softly when it walks before my Cities,\n"
		" And the horses and the chariots fleeing from them as of old!\n"
		" \n"
		"   -- Rudyard Kipling\n"
		"\\ No newline at end of file\n";
	size_t expected_sizes[3] = { 115, 119 + 115 + 114, 119 + 115 + 114 + 71 };
556 557 558

	g_repo = cl_git_sandbox_init("renames");

559 560
	cl_git_pass(git_config_new(&cfg));
	git_repository_set_config(g_repo, cfg);
561 562
	git_config_free(cfg);

563
	git_repository_reinit_filesystem(g_repo, false);
564

565
	cl_git_pass(git_futils_readbuffer(&content, "renames/songof7cities.txt"));
566 567 568 569 570

	/* remove first line */

	end = git_buf_cstr(&content) + git_buf_find(&content, '\n') + 1;
	git_buf_consume(&content, end);
571
	cl_git_rewritefile("renames/songof7cities.txt", content.ptr);
572

573
	check_single_patch_stats(g_repo, 1, 0, 1, 3, NULL, NULL);
574 575 576 577

	/* remove trailing whitespace */

	git_buf_rtrim(&content);
578
	cl_git_rewritefile("renames/songof7cities.txt", content.ptr);
579

580
	check_single_patch_stats(g_repo, 2, 1, 2, 6, NULL, NULL);
581 582 583 584

	/* add trailing whitespace */

	cl_git_pass(git_repository_index(&index, g_repo));
585
	cl_git_pass(git_index_add_bypath(index, "songof7cities.txt"));
586 587 588 589
	cl_git_pass(git_index_write(index));
	git_index_free(index);

	cl_git_pass(git_buf_putc(&content, '\n'));
590
	cl_git_rewritefile("renames/songof7cities.txt", content.ptr);
591

592
	check_single_patch_stats(g_repo, 1, 1, 1, 3, NULL, NULL);
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613

	/* no trailing whitespace as context line */

	{
		/* walk back a couple lines, make space and insert char */
		char *scan = content.ptr + content.size;
		int i;

		for (i = 0; i < 5; ++i) {
			for (--scan; scan > content.ptr && *scan != '\n'; --scan)
				/* seek to prev \n */;
		}
		cl_assert(scan > content.ptr);

		/* overwrite trailing \n with right-shifted content */
		memmove(scan + 1, scan, content.size - (scan - content.ptr) - 1);
		/* insert '#' char into space we created */
		scan[1] = '#';
	}
	cl_git_rewritefile("renames/songof7cities.txt", content.ptr);

614
	check_single_patch_stats(
615
		g_repo, 1, 1, 1, 6, expected_sizes, expected);
616

617
	git_buf_dispose(&content);
618
}
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694

void test_diff_patch__can_strip_bad_utf8(void)
{
	const char *a = "A " UTF8_HUNK_HEADER
		"  B\n"
		"  C\n"
		"  D\n"
		"  E\n"
		"  F\n"
		"  G\n"
		"  H\n"
		"  I\n"
		"  J\n"
		"  K\n"
		"L  " UTF8_HUNK_HEADER
		"  M\n"
		"  N\n"
		"  O\n"
		"  P\n"
		"  Q\n"
		"  R\n"
		"  S\n"
		"  T\n"
		"  U\n"
		"  V\n";

	const char *b = "A " UTF8_HUNK_HEADER
		"  B\n"
		"  C\n"
		"  D\n"
		"  E modified\n"
		"  F\n"
		"  G\n"
		"  H\n"
		"  I\n"
		"  J\n"
		"  K\n"
		"L  " UTF8_HUNK_HEADER
		"  M\n"
		"  N\n"
		"  O\n"
		"  P modified\n"
		"  Q\n"
		"  R\n"
		"  S\n"
		"  T\n"
		"  U\n"
		"  V\n";

	const char *expected = "diff --git a/file b/file\n"
		"index d0647c4..7827ce5 100644\n"
		"--- a/file\n"
		"+++ b/file\n"
		"@@ -2,7 +2,7 @@ A " UTF8_TRUNCATED_A_HUNK_HEADER
		"   B\n"
		"   C\n"
		"   D\n"
		"-  E\n"
		"+  E modified\n"
		"   F\n"
		"   G\n"
		"   H\n"
		"@@ -13,7 +13,7 @@ L  " UTF8_TRUNCATED_L_HUNK_HEADER
		"   M\n"
		"   N\n"
		"   O\n"
		"-  P\n"
		"+  P modified\n"
		"   Q\n"
		"   R\n"
		"   S\n";

	git_diff_options opts;
	git_patch *patch;
	git_buf buf = GIT_BUF_INIT;

695
	cl_git_pass(git_diff_options_init(&opts, GIT_DIFF_OPTIONS_VERSION));
696 697 698 699 700 701 702

	cl_git_pass(git_patch_from_buffers(&patch, a, strlen(a), NULL, b, strlen(b), NULL, &opts));
	cl_git_pass(git_patch_to_buf(&buf, patch));

	cl_assert_equal_s(expected, buf.ptr);

	git_patch_free(patch);
703
	git_buf_dispose(&buf);
704
}