summaryrefslogtreecommitdiff
path: root/src/diff_output.c
blob: d715f9ef448738e1bb89ca79862970f5d044537d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
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
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
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
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
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
/*
 * Copyright (C) 2012 the libgit2 contributors
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
#include "common.h"
#include "git2/diff.h"
#include "git2/attr.h"
#include "git2/blob.h"
#include "git2/oid.h"
#include "xdiff/xdiff.h"
#include <ctype.h>
#include "diff.h"
#include "map.h"
#include "fileops.h"
#include "filter.h"

/*
 * A diff_delta_context represents all of the information that goes into
 * processing the diff of an observed file change.  In the case of the
 * git_diff_foreach() call it is an emphemeral structure that is filled
 * in to execute each diff.  In the case of a git_diff_iterator, it holds
 * most of the information for the diff in progress.
 */
typedef struct {
	git_repository   *repo;
	git_diff_options *opts;
	xdemitconf_t xdiff_config;
	xpparam_t    xdiff_params;
	git_diff_delta *delta;
	uint32_t prepped  : 1;
	uint32_t loaded   : 1;
	uint32_t diffable : 1;
	uint32_t diffed   : 1;
	git_iterator_type_t old_src;
	git_iterator_type_t new_src;
	git_blob *old_blob;
	git_blob *new_blob;
	git_map   old_data;
	git_map   new_data;
	void *cb_data;
	git_diff_hunk_fn per_hunk;
	git_diff_data_fn per_line;
	int cb_error;
	git_diff_range range;
} diff_delta_context;

static int read_next_int(const char **str, int *value)
{
	const char *scan = *str;
	int v = 0, digits = 0;
	/* find next digit */
	for (scan = *str; *scan && !isdigit(*scan); scan++);
	/* parse next number */
	for (; isdigit(*scan); scan++, digits++)
		v = (v * 10) + (*scan - '0');
	*str = scan;
	*value = v;
	return (digits > 0) ? 0 : -1;
}

static int parse_hunk_header(git_diff_range *range, const char *header)
{
	/* expect something of the form "@@ -%d[,%d] +%d[,%d] @@" */
	if (*header != '@')
		return -1;
	if (read_next_int(&header, &range->old_start) < 0)
		return -1;
	if (*header == ',') {
		if (read_next_int(&header, &range->old_lines) < 0)
			return -1;
	} else
		range->old_lines = 1;
	if (read_next_int(&header, &range->new_start) < 0)
		return -1;
	if (*header == ',') {
		if (read_next_int(&header, &range->new_lines) < 0)
			return -1;
	} else
		range->new_lines = 1;
	if (range->old_start < 0 || range->new_start < 0)
		return -1;

	return 0;
}

static int format_hunk_header(char *header, size_t len, git_diff_range *range)
{
	if (range->old_lines != 1) {
		if (range->new_lines != 1)
			return snprintf(
				header, len, "@@ -%d,%d +%d,%d @@",
				range->old_start, range->old_lines,
				range->new_start, range->new_lines);
		else
			return snprintf(
				header, len, "@@ -%d,%d +%d @@",
				range->old_start, range->old_lines, range->new_start);
	} else {
		if (range->new_lines != 1)
			return snprintf(
				header, len, "@@ -%d +%d,%d @@",
				range->old_start, range->new_start, range->new_lines);
		else
			return snprintf(
				header, len, "@@ -%d +%d @@",
				range->old_start, range->new_start);
	}
}

static bool diff_delta_is_ambiguous(git_diff_delta *delta)
{
	return (git_oid_iszero(&delta->new_file.oid) &&
			(delta->new_file.flags & GIT_DIFF_FILE_VALID_OID) == 0 &&
			delta->status == GIT_DELTA_MODIFIED);
}

static bool diff_delta_should_skip(git_diff_options *opts, git_diff_delta *delta)
{
	if (delta->status == GIT_DELTA_UNMODIFIED &&
		(opts->flags & GIT_DIFF_INCLUDE_UNMODIFIED) == 0)
		return true;

	if (delta->status == GIT_DELTA_IGNORED &&
		(opts->flags & GIT_DIFF_INCLUDE_IGNORED) == 0)
		return true;

	if (delta->status == GIT_DELTA_UNTRACKED &&
		(opts->flags & GIT_DIFF_INCLUDE_UNTRACKED) == 0)
		return true;

	return false;
}

#define BINARY_DIFF_FLAGS (GIT_DIFF_FILE_BINARY|GIT_DIFF_FILE_NOT_BINARY)

static int update_file_is_binary_by_attr(
	git_repository *repo, git_diff_file *file)
{
	const char *value;

	if (!repo)
		return 0;

	if (git_attr_get(&value, repo, 0, file->path, "diff") < 0)
		return -1;

	if (GIT_ATTR_FALSE(value))
		file->flags |= GIT_DIFF_FILE_BINARY;
	else if (GIT_ATTR_TRUE(value))
		file->flags |= GIT_DIFF_FILE_NOT_BINARY;
	/* otherwise leave file->flags alone */

	return 0;
}

static void update_delta_is_binary(git_diff_delta *delta)
{
	if ((delta->old_file.flags & GIT_DIFF_FILE_BINARY) != 0 ||
		(delta->new_file.flags & GIT_DIFF_FILE_BINARY) != 0)
		delta->binary = 1;
	else if ((delta->old_file.flags & GIT_DIFF_FILE_NOT_BINARY) != 0 ||
			 (delta->new_file.flags & GIT_DIFF_FILE_NOT_BINARY) != 0)
		delta->binary = 0;
	/* otherwise leave delta->binary value untouched */
}

static int diff_delta_is_binary_by_attr(diff_delta_context *ctxt)
{
	int error = 0, mirror_new;
	git_diff_delta *delta = ctxt->delta;

	delta->binary = -1;

	/* make sure files are conceivably mmap-able */
	if ((git_off_t)((size_t)delta->old_file.size) != delta->old_file.size ||
		(git_off_t)((size_t)delta->new_file.size) != delta->new_file.size)
	{
		delta->old_file.flags |= GIT_DIFF_FILE_BINARY;
		delta->new_file.flags |= GIT_DIFF_FILE_BINARY;
		delta->binary = 1;
		return 0;
	}

	/* check if user is forcing us to text diff these files */
	if (ctxt->opts->flags & GIT_DIFF_FORCE_TEXT) {
		delta->old_file.flags |= GIT_DIFF_FILE_NOT_BINARY;
		delta->new_file.flags |= GIT_DIFF_FILE_NOT_BINARY;
		delta->binary = 0;
		return 0;
	}

	/* check diff attribute +, -, or 0 */
	if (update_file_is_binary_by_attr(ctxt->repo, &delta->old_file) < 0)
		return -1;

	mirror_new = (delta->new_file.path == delta->old_file.path ||
				  strcmp(delta->new_file.path, delta->old_file.path) == 0);
	if (mirror_new)
		delta->new_file.flags |= (delta->old_file.flags & BINARY_DIFF_FLAGS);
	else
		error = update_file_is_binary_by_attr(ctxt->repo, &delta->new_file);

	update_delta_is_binary(delta);

	return error;
}

static int diff_delta_is_binary_by_content(diff_delta_context *ctxt)
{
	git_diff_delta *delta = ctxt->delta;
	git_buf search;

	if ((delta->old_file.flags & BINARY_DIFF_FLAGS) == 0) {
		search.ptr  = ctxt->old_data.data;
		search.size = min(ctxt->old_data.len, 4000);

		if (git_buf_is_binary(&search))
			delta->old_file.flags |= GIT_DIFF_FILE_BINARY;
		else
			delta->old_file.flags |= GIT_DIFF_FILE_NOT_BINARY;
	}

	if ((delta->new_file.flags & BINARY_DIFF_FLAGS) == 0) {
		search.ptr  = ctxt->new_data.data;
		search.size = min(ctxt->new_data.len, 4000);

		if (git_buf_is_binary(&search))
			delta->new_file.flags |= GIT_DIFF_FILE_BINARY;
		else
			delta->new_file.flags |= GIT_DIFF_FILE_NOT_BINARY;
	}

	update_delta_is_binary(delta);

	/* TODO: if value != NULL, implement diff drivers */

	return 0;
}

static void setup_xdiff_options(
	git_diff_options *opts, xdemitconf_t *cfg, xpparam_t *param)
{
	memset(cfg, 0, sizeof(xdemitconf_t));
	memset(param, 0, sizeof(xpparam_t));

	cfg->ctxlen =
		(!opts || !opts->context_lines) ? 3 : opts->context_lines;
	cfg->interhunkctxlen =
		(!opts) ? 0 : opts->interhunk_lines;

	if (!opts)
		return;

	if (opts->flags & GIT_DIFF_IGNORE_WHITESPACE)
		param->flags |= XDF_WHITESPACE_FLAGS;
	if (opts->flags & GIT_DIFF_IGNORE_WHITESPACE_CHANGE)
		param->flags |= XDF_IGNORE_WHITESPACE_CHANGE;
	if (opts->flags & GIT_DIFF_IGNORE_WHITESPACE_EOL)
		param->flags |= XDF_IGNORE_WHITESPACE_AT_EOL;
}

static int get_blob_content(
	git_repository *repo,
	const git_oid *oid,
	git_map *map,
	git_blob **blob)
{
	if (git_oid_iszero(oid))
		return 0;

	if (git_blob_lookup(blob, repo, oid) < 0)
		return -1;

	map->data = (void *)git_blob_rawcontent(*blob);
	map->len  = git_blob_rawsize(*blob);
	return 0;
}

static int get_workdir_content(
	git_repository *repo,
	git_diff_file *file,
	git_map *map)
{
	int error = 0;
	git_buf path = GIT_BUF_INIT;

	if (git_buf_joinpath(&path, git_repository_workdir(repo), file->path) < 0)
		return -1;

	if (S_ISLNK(file->mode)) {
		ssize_t alloc_len, read_len;

		file->flags |= GIT_DIFF_FILE_FREE_DATA;
		file->flags |= GIT_DIFF_FILE_BINARY;

		/* link path on disk could be UTF-16, so prepare a buffer that is
		 * big enough to handle some UTF-8 data expansion
		 */
		alloc_len = (ssize_t)(file->size * 2) + 1;

		map->data = git__malloc(alloc_len);
		GITERR_CHECK_ALLOC(map->data);

		read_len = p_readlink(path.ptr, map->data, (int)alloc_len);
		if (read_len < 0) {
			giterr_set(GITERR_OS, "Failed to read symlink '%s'", file->path);
			error = -1;
		} else
			map->len = read_len;
	}
	else {
		error = git_futils_mmap_ro_file(map, path.ptr);
		file->flags |= GIT_DIFF_FILE_UNMAP_DATA;
	}
	git_buf_free(&path);
	return error;
}

static void release_content(git_diff_file *file, git_map *map, git_blob *blob)
{
	if (blob != NULL)
		git_blob_free(blob);

	if (file->flags & GIT_DIFF_FILE_FREE_DATA) {
		git__free(map->data);
		map->data = "";
		map->len  = 0;
		file->flags &= ~GIT_DIFF_FILE_FREE_DATA;
	}
	else if (file->flags & GIT_DIFF_FILE_UNMAP_DATA) {
		git_futils_mmap_free(map);
		map->data = "";
		map->len  = 0;
		file->flags &= ~GIT_DIFF_FILE_UNMAP_DATA;
	}
}

static void diff_delta_init_context(
	diff_delta_context *ctxt,
	git_repository   *repo,
	git_diff_options *opts,
	git_iterator_type_t old_src,
	git_iterator_type_t new_src)
{
	memset(ctxt, 0, sizeof(diff_delta_context));

	ctxt->repo = repo;
	ctxt->opts = opts;
	ctxt->old_src = old_src;
	ctxt->new_src = new_src;

	setup_xdiff_options(opts, &ctxt->xdiff_config, &ctxt->xdiff_params);
}

static void diff_delta_init_context_from_diff_list(
	diff_delta_context *ctxt,
	git_diff_list *diff)
{
	diff_delta_init_context(
		ctxt, diff->repo, &diff->opts, diff->old_src, diff->new_src);
}

static void diff_delta_unload(diff_delta_context *ctxt)
{
	ctxt->diffed = 0;

	if (ctxt->loaded) {
		release_content(&ctxt->delta->old_file, &ctxt->old_data, ctxt->old_blob);
		release_content(&ctxt->delta->new_file, &ctxt->new_data, ctxt->new_blob);
		ctxt->loaded = 0;
	}

	ctxt->delta = NULL;
	ctxt->prepped = 0;
}

static int diff_delta_prep(diff_delta_context *ctxt)
{
	int error;

	if (ctxt->prepped || !ctxt->delta)
		return 0;

	error = diff_delta_is_binary_by_attr(ctxt);

	ctxt->prepped = !error;

	return error;
}

static int diff_delta_load(diff_delta_context *ctxt)
{
	int error = 0;
	git_diff_delta *delta = ctxt->delta;

	if (ctxt->loaded || !ctxt->delta)
		return 0;

	if (!ctxt->prepped && (error = diff_delta_prep(ctxt)) < 0)
		goto cleanup;

	ctxt->old_data.data = "";
	ctxt->old_data.len  = 0;
	ctxt->old_blob      = NULL;

	if (!error && delta->binary != 1 &&
		(delta->status == GIT_DELTA_DELETED ||
		 delta->status == GIT_DELTA_MODIFIED))
	{
		if (ctxt->old_src == GIT_ITERATOR_WORKDIR)
			error = get_workdir_content(
				ctxt->repo, &delta->old_file, &ctxt->old_data);
		else {
			error = get_blob_content(
				ctxt->repo, &delta->old_file.oid,
				&ctxt->old_data, &ctxt->old_blob);

			if (ctxt->new_src == GIT_ITERATOR_WORKDIR) {
				/* TODO: convert crlf of blob content */
			}
		}
	}

	ctxt->new_data.data = "";
	ctxt->new_data.len  = 0;
	ctxt->new_blob      = NULL;

	if (!error && delta->binary != 1 &&
		(delta->status == GIT_DELTA_ADDED ||
		 delta->status == GIT_DELTA_MODIFIED))
	{
		if (ctxt->new_src == GIT_ITERATOR_WORKDIR)
			error = get_workdir_content(
				ctxt->repo, &delta->new_file, &ctxt->new_data);
		else {
			error = get_blob_content(
				ctxt->repo, &delta->new_file.oid,
				&ctxt->new_data, &ctxt->new_blob);

			if (ctxt->old_src == GIT_ITERATOR_WORKDIR) {
				/* TODO: convert crlf of blob content */
			}
		}

		if (!error && !(delta->new_file.flags & GIT_DIFF_FILE_VALID_OID)) {
			error = git_odb_hash(
				&delta->new_file.oid, ctxt->new_data.data,
				ctxt->new_data.len, GIT_OBJ_BLOB);
			if (error < 0)
				goto cleanup;

			delta->new_file.flags |= GIT_DIFF_FILE_VALID_OID;

			/* since we did not have the definitive oid, we may have
			 * incorrect status and need to skip this item.
			 */
			if (delta->old_file.mode == delta->new_file.mode &&
				!git_oid_cmp(&delta->old_file.oid, &delta->new_file.oid))
			{
				delta->status = GIT_DELTA_UNMODIFIED;

				if ((ctxt->opts->flags & GIT_DIFF_INCLUDE_UNMODIFIED) == 0)
					goto cleanup;
			}
		}
	}

	/* if we have not already decided whether file is binary,
	 * check the first 4K for nul bytes to decide...
	 */
	if (!error && delta->binary == -1)
		error = diff_delta_is_binary_by_content(ctxt);

cleanup:
	ctxt->loaded = !error;

	/* flag if we would want to diff the contents of these files */
	if (ctxt->loaded)
		ctxt->diffable =
			(delta->binary != 1 &&
			 delta->status != GIT_DELTA_UNMODIFIED &&
			 (ctxt->old_data.len || ctxt->new_data.len) &&
			 git_oid_cmp(&delta->old_file.oid, &delta->new_file.oid));

	return error;
}

static int diff_delta_cb(void *priv, mmbuffer_t *bufs, int len)
{
	diff_delta_context *ctxt = priv;

	if (len == 1) {
		if ((ctxt->cb_error = parse_hunk_header(&ctxt->range, bufs[0].ptr)) < 0)
			return ctxt->cb_error;

		if (ctxt->per_hunk != NULL &&
			ctxt->per_hunk(ctxt->cb_data, ctxt->delta, &ctxt->range,
				bufs[0].ptr, bufs[0].size))
			ctxt->cb_error = GIT_EUSER;
	}

	if (len == 2 || len == 3) {
		/* expect " "/"-"/"+", then data */
		char origin =
			(*bufs[0].ptr == '+') ? GIT_DIFF_LINE_ADDITION :
			(*bufs[0].ptr == '-') ? GIT_DIFF_LINE_DELETION :
			GIT_DIFF_LINE_CONTEXT;

		if (ctxt->per_line != NULL &&
			ctxt->per_line(ctxt->cb_data, ctxt->delta, &ctxt->range, origin,
				bufs[1].ptr, bufs[1].size))
			ctxt->cb_error = GIT_EUSER;
	}

	if (len == 3 && !ctxt->cb_error) {
		/* This should only happen if we are adding a line that does not
		 * have a newline at the end and the old code did.  In that case,
		 * we have a ADD with a DEL_EOFNL as a pair.
		 */
		char origin =
			(*bufs[0].ptr == '+') ? GIT_DIFF_LINE_DEL_EOFNL :
			(*bufs[0].ptr == '-') ? GIT_DIFF_LINE_ADD_EOFNL :
			GIT_DIFF_LINE_CONTEXT;

		if (ctxt->per_line != NULL &&
			ctxt->per_line(ctxt->cb_data, ctxt->delta, &ctxt->range, origin,
				bufs[2].ptr, bufs[2].size))
			ctxt->cb_error = GIT_EUSER;
	}

	return ctxt->cb_error;
}

static int diff_delta_exec(
	diff_delta_context *ctxt,
	void *cb_data,
	git_diff_hunk_fn per_hunk,
	git_diff_data_fn per_line)
{
	int error = 0;
	xdemitcb_t xdiff_callback;
	mmfile_t old_xdiff_data, new_xdiff_data;

	if (ctxt->diffed || !ctxt->delta)
		return 0;

	if (!ctxt->loaded && (error = diff_delta_load(ctxt)) < 0)
		goto cleanup;

	if (!ctxt->diffable)
		return 0;

	ctxt->cb_data  = cb_data;
	ctxt->per_hunk = per_hunk;
	ctxt->per_line = per_line;
	ctxt->cb_error = 0;

	memset(&xdiff_callback, 0, sizeof(xdiff_callback));
	xdiff_callback.outf = diff_delta_cb;
	xdiff_callback.priv = ctxt;

	old_xdiff_data.ptr  = ctxt->old_data.data;
	old_xdiff_data.size = ctxt->old_data.len;
	new_xdiff_data.ptr  = ctxt->new_data.data;
	new_xdiff_data.size = ctxt->new_data.len;

	xdl_diff(&old_xdiff_data, &new_xdiff_data,
		&ctxt->xdiff_params, &ctxt->xdiff_config, &xdiff_callback);

	error = ctxt->cb_error;

cleanup:
	ctxt->diffed = !error;

	return error;
}

int git_diff_foreach(
	git_diff_list *diff,
	void *data,
	git_diff_file_fn file_cb,
	git_diff_hunk_fn hunk_cb,
	git_diff_data_fn line_cb)
{
	int error = 0;
	diff_delta_context ctxt;
	size_t idx;

	diff_delta_init_context_from_diff_list(&ctxt, diff);

	git_vector_foreach(&diff->deltas, idx, ctxt.delta) {
		if (diff_delta_is_ambiguous(ctxt.delta))
			if ((error = diff_delta_load(&ctxt)) < 0)
				goto cleanup;

		if (diff_delta_should_skip(ctxt.opts, ctxt.delta))
			continue;

		if ((error = diff_delta_load(&ctxt)) < 0)
			goto cleanup;

		if (file_cb != NULL &&
			file_cb(data, ctxt.delta, (float)idx / diff->deltas.length) != 0)
		{
			error = GIT_EUSER;
			goto cleanup;
		}

		error = diff_delta_exec(&ctxt, data, hunk_cb, line_cb);

cleanup:
		diff_delta_unload(&ctxt);

		if (error < 0)
			break;
	}

	if (error == GIT_EUSER)
		giterr_clear();

	return error;
}

typedef struct {
	git_diff_list *diff;
	git_diff_data_fn print_cb;
	void *cb_data;
	git_buf *buf;
} diff_print_info;

static char pick_suffix(int mode)
{
	if (S_ISDIR(mode))
		return '/';
	else if (mode & 0100) //-V536
		/* in git, modes are very regular, so we must have 0100755 mode */
		return '*';
	else
		return ' ';
}

static int print_compact(void *data, git_diff_delta *delta, float progress)
{
	diff_print_info *pi = data;
	char code, old_suffix, new_suffix;

	GIT_UNUSED(progress);

	switch (delta->status) {
	case GIT_DELTA_ADDED: code = 'A'; break;
	case GIT_DELTA_DELETED: code = 'D'; break;
	case GIT_DELTA_MODIFIED: code = 'M'; break;
	case GIT_DELTA_RENAMED: code = 'R'; break;
	case GIT_DELTA_COPIED: code = 'C'; break;
	case GIT_DELTA_IGNORED: code = 'I'; break;
	case GIT_DELTA_UNTRACKED: code = '?'; break;
	default: code = 0;
	}

	if (!code)
		return 0;

	old_suffix = pick_suffix(delta->old_file.mode);
	new_suffix = pick_suffix(delta->new_file.mode);

	git_buf_clear(pi->buf);

	if (delta->old_file.path != delta->new_file.path &&
		strcmp(delta->old_file.path,delta->new_file.path) != 0)
		git_buf_printf(pi->buf, "%c\t%s%c -> %s%c\n", code,
			delta->old_file.path, old_suffix, delta->new_file.path, new_suffix);
	else if (delta->old_file.mode != delta->new_file.mode &&
		delta->old_file.mode != 0 && delta->new_file.mode != 0)
		git_buf_printf(pi->buf, "%c\t%s%c (%o -> %o)\n", code,
			delta->old_file.path, new_suffix, delta->old_file.mode, delta->new_file.mode);
	else if (old_suffix != ' ')
		git_buf_printf(pi->buf, "%c\t%s%c\n", code, delta->old_file.path, old_suffix);
	else
		git_buf_printf(pi->buf, "%c\t%s\n", code, delta->old_file.path);

	if (git_buf_oom(pi->buf))
		return -1;

	if (pi->print_cb(pi->cb_data, delta, NULL, GIT_DIFF_LINE_FILE_HDR,
			git_buf_cstr(pi->buf), git_buf_len(pi->buf)))
	{
		giterr_clear();
		return GIT_EUSER;
	}

	return 0;
}

int git_diff_print_compact(
	git_diff_list *diff,
	void *cb_data,
	git_diff_data_fn print_cb)
{
	int error;
	git_buf buf = GIT_BUF_INIT;
	diff_print_info pi;

	pi.diff     = diff;
	pi.print_cb = print_cb;
	pi.cb_data  = cb_data;
	pi.buf      = &buf;

	error = git_diff_foreach(diff, &pi, print_compact, NULL, NULL);

	git_buf_free(&buf);

	return error;
}


static int print_oid_range(diff_print_info *pi, git_diff_delta *delta)
{
	char start_oid[8], end_oid[8];

	/* TODO: Determine a good actual OID range to print */
	git_oid_tostr(start_oid, sizeof(start_oid), &delta->old_file.oid);
	git_oid_tostr(end_oid, sizeof(end_oid), &delta->new_file.oid);

	/* TODO: Match git diff more closely */
	if (delta->old_file.mode == delta->new_file.mode) {
		git_buf_printf(pi->buf, "index %s..%s %o\n",
			start_oid, end_oid, delta->old_file.mode);
	} else {
		if (delta->old_file.mode == 0) {
			git_buf_printf(pi->buf, "new file mode %o\n", delta->new_file.mode);
		} else if (delta->new_file.mode == 0) {
			git_buf_printf(pi->buf, "deleted file mode %o\n", delta->old_file.mode);
		} else {
			git_buf_printf(pi->buf, "old mode %o\n", delta->old_file.mode);
			git_buf_printf(pi->buf, "new mode %o\n", delta->new_file.mode);
		}
		git_buf_printf(pi->buf, "index %s..%s\n", start_oid, end_oid);
	}

	if (git_buf_oom(pi->buf))
		return -1;

	return 0;
}

static int print_patch_file(void *data, git_diff_delta *delta, float progress)
{
	diff_print_info *pi = data;
	const char *oldpfx = pi->diff->opts.old_prefix;
	const char *oldpath = delta->old_file.path;
	const char *newpfx = pi->diff->opts.new_prefix;
	const char *newpath = delta->new_file.path;

	GIT_UNUSED(progress);

	if (!oldpfx)
		oldpfx = DIFF_OLD_PREFIX_DEFAULT;

	if (!newpfx)
		newpfx = DIFF_NEW_PREFIX_DEFAULT;

	git_buf_clear(pi->buf);
	git_buf_printf(pi->buf, "diff --git %s%s %s%s\n", oldpfx, delta->old_file.path, newpfx, delta->new_file.path);

	if (print_oid_range(pi, delta) < 0)
		return -1;

	if (git_oid_iszero(&delta->old_file.oid)) {
		oldpfx = "";
		oldpath = "/dev/null";
	}
	if (git_oid_iszero(&delta->new_file.oid)) {
		newpfx = "";
		newpath = "/dev/null";
	}

	if (delta->binary != 1) {
		git_buf_printf(pi->buf, "--- %s%s\n", oldpfx, oldpath);
		git_buf_printf(pi->buf, "+++ %s%s\n", newpfx, newpath);
	}

	if (git_buf_oom(pi->buf))
		return -1;

    if (pi->print_cb(pi->cb_data, delta, NULL, GIT_DIFF_LINE_FILE_HDR, git_buf_cstr(pi->buf), git_buf_len(pi->buf)))
	{
		giterr_clear();
		return GIT_EUSER;
	}

    if (delta->binary != 1)
        return 0;

	git_buf_clear(pi->buf);
	git_buf_printf(
		pi->buf, "Binary files %s%s and %s%s differ\n",
		oldpfx, oldpath, newpfx, newpath);
	if (git_buf_oom(pi->buf))
		return -1;

	if (pi->print_cb(pi->cb_data, delta, NULL, GIT_DIFF_LINE_BINARY,
			git_buf_cstr(pi->buf), git_buf_len(pi->buf)))
	{
		giterr_clear();
		return GIT_EUSER;
	}

	return 0;
}

static int print_patch_hunk(
	void *data,
	git_diff_delta *d,
	git_diff_range *r,
	const char *header,
	size_t header_len)
{
	diff_print_info *pi = data;

	git_buf_clear(pi->buf);
	if (git_buf_printf(pi->buf, "%.*s", (int)header_len, header) < 0)
		return -1;

	if (pi->print_cb(pi->cb_data, d, r, GIT_DIFF_LINE_HUNK_HDR,
			git_buf_cstr(pi->buf), git_buf_len(pi->buf)))
	{
		giterr_clear();
		return GIT_EUSER;
	}

	return 0;
}

static int print_patch_line(
	void *data,
	git_diff_delta *delta,
	git_diff_range *range,
	char line_origin, /* GIT_DIFF_LINE value from above */
	const char *content,
	size_t content_len)
{
	diff_print_info *pi = data;

	git_buf_clear(pi->buf);

	if (line_origin == GIT_DIFF_LINE_ADDITION ||
		line_origin == GIT_DIFF_LINE_DELETION ||
		line_origin == GIT_DIFF_LINE_CONTEXT)
		git_buf_printf(pi->buf, "%c%.*s", line_origin, (int)content_len, content);
	else if (content_len > 0)
		git_buf_printf(pi->buf, "%.*s", (int)content_len, content);

	if (git_buf_oom(pi->buf))
		return -1;

	if (pi->print_cb(pi->cb_data, delta, range, line_origin,
			git_buf_cstr(pi->buf), git_buf_len(pi->buf)))
	{
		giterr_clear();
		return GIT_EUSER;
	}

	return 0;
}

int git_diff_print_patch(
	git_diff_list *diff,
	void *cb_data,
	git_diff_data_fn print_cb)
{
	int error;
	git_buf buf = GIT_BUF_INIT;
	diff_print_info pi;

	pi.diff     = diff;
	pi.print_cb = print_cb;
	pi.cb_data  = cb_data;
	pi.buf      = &buf;

	error = git_diff_foreach(
		diff, &pi, print_patch_file, print_patch_hunk, print_patch_line);

	git_buf_free(&buf);

	return error;
}

int git_diff_entrycount(git_diff_list *diff, int delta_t)
{
	int count = 0;
	unsigned int i;
	git_diff_delta *delta;

	assert(diff);

	git_vector_foreach(&diff->deltas, i, delta) {
		if (diff_delta_should_skip(&diff->opts, delta))
			continue;

		if (delta_t < 0 || delta->status == (git_delta_t)delta_t)
			count++;
	}

	/* It is possible that this has overcounted the number of diffs because
	 * there may be entries that are marked as MODIFIED due to differences
	 * in stat() output that will turn out to be the same once we calculate
	 * the actual SHA of the data on disk.
	 */

	return count;
}

static void set_data_from_blob(
	git_blob *blob, git_map *map, git_diff_file *file)
{
	if (blob) {
		map->data = (char *)git_blob_rawcontent(blob);
		file->size = map->len = git_blob_rawsize(blob);
		git_oid_cpy(&file->oid, git_object_id((const git_object *)blob));
	} else {
		map->data = "";
		file->size = map->len = 0;
	}
}

int git_diff_blobs(
	git_blob *old_blob,
	git_blob *new_blob,
	git_diff_options *options,
	void *cb_data,
	git_diff_file_fn file_cb,
	git_diff_hunk_fn hunk_cb,
	git_diff_data_fn line_cb)
{
	int error;
	diff_delta_context ctxt;
	git_diff_delta delta;
	git_blob *new, *old;

	new = new_blob;
	old = old_blob;

	if (options && (options->flags & GIT_DIFF_REVERSE)) {
		git_blob *swap = old;
		old = new;
		new = swap;
	}

	diff_delta_init_context(
		&ctxt, NULL, options, GIT_ITERATOR_TREE, GIT_ITERATOR_TREE);

	/* populate a "fake" delta record */

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

	set_data_from_blob(old, &ctxt.old_data, &delta.old_file);
	set_data_from_blob(new, &ctxt.new_data, &delta.new_file);

	delta.status = new ?
		(old ? GIT_DELTA_MODIFIED : GIT_DELTA_ADDED) :
		(old ? GIT_DELTA_DELETED : GIT_DELTA_UNTRACKED);

	if (git_oid_cmp(&delta.new_file.oid, &delta.old_file.oid) == 0)
		delta.status = GIT_DELTA_UNMODIFIED;

	ctxt.delta = &delta;

	if ((error = diff_delta_prep(&ctxt)) < 0)
		goto cleanup;

	if (delta.binary == -1 &&
		(error = diff_delta_is_binary_by_content(&ctxt)) < 0)
		goto cleanup;

	ctxt.loaded = 1;
	ctxt.diffable = (delta.binary != 1 && delta.status != GIT_DELTA_UNMODIFIED);

	/* do diffs */

	if (file_cb != NULL && file_cb(cb_data, &delta, 1)) {
		error = GIT_EUSER;
		goto cleanup;
	}

	error = diff_delta_exec(&ctxt, cb_data, hunk_cb, line_cb);

cleanup:
	if (error == GIT_EUSER)
		giterr_clear();

	diff_delta_unload(&ctxt);

	return error;
}

typedef struct diffiter_line diffiter_line;
struct diffiter_line {
	diffiter_line *next;
	char origin;
	const char *ptr;
	size_t len;
};

typedef struct diffiter_hunk diffiter_hunk;
struct diffiter_hunk {
	diffiter_hunk *next;
	git_diff_range range;
	diffiter_line *line_head;
	size_t line_count;
};

struct git_diff_iterator {
	git_diff_list *diff;
	diff_delta_context ctxt;
	size_t file_index;
	size_t next_index;
	size_t file_count;
	git_pool hunks;
	size_t   hunk_count;
	diffiter_hunk *hunk_head;
	diffiter_hunk *hunk_curr;
	char hunk_header[128];
	git_pool lines;
	diffiter_line *line_curr;
};

typedef struct {
	git_diff_iterator *iter;
	diffiter_hunk *last_hunk;
	diffiter_line *last_line;
} diffiter_cb_info;

static int diffiter_hunk_cb(
	void *cb_data,
	git_diff_delta *delta,
	git_diff_range *range,
	const char *header,
	size_t header_len)
{
	diffiter_cb_info *info = cb_data;
	git_diff_iterator *iter = info->iter;
	diffiter_hunk *hunk;

	GIT_UNUSED(delta);
	GIT_UNUSED(header);
	GIT_UNUSED(header_len);

	if ((hunk = git_pool_mallocz(&iter->hunks, 1)) == NULL) {
		iter->ctxt.cb_error = -1;
		return -1;
	}

	if (info->last_hunk)
		info->last_hunk->next = hunk;
	info->last_hunk = hunk;

	memcpy(&hunk->range, range, sizeof(hunk->range));

	iter->hunk_count++;

	if (iter->hunk_head == NULL)
		iter->hunk_curr = iter->hunk_head = hunk;

	return 0;
}

static int diffiter_line_cb(
	void *cb_data,
	git_diff_delta *delta,
	git_diff_range *range,
	char line_origin,
	const char *content,
	size_t content_len)
{
	diffiter_cb_info *info = cb_data;
	git_diff_iterator *iter = info->iter;
	diffiter_line *line;

	GIT_UNUSED(delta);
	GIT_UNUSED(range);

	if ((line = git_pool_mallocz(&iter->lines, 1)) == NULL) {
		iter->ctxt.cb_error = -1;
		return -1;
	}

	if (info->last_line)
		info->last_line->next = line;
	info->last_line = line;

	line->origin = line_origin;
	line->ptr = content;
	line->len = content_len;

	info->last_hunk->line_count++;

	if (info->last_hunk->line_head == NULL)
		info->last_hunk->line_head = line;

	return 0;
}

static int diffiter_do_diff_file(git_diff_iterator *iter)
{
	int error;
	diffiter_cb_info info;

	if (iter->ctxt.diffed || !iter->ctxt.delta)
		return 0;

	memset(&info, 0, sizeof(info));
	info.iter = iter;

	error = diff_delta_exec(
		&iter->ctxt, &info, diffiter_hunk_cb, diffiter_line_cb);

	if (error == GIT_EUSER)
		error = iter->ctxt.cb_error;

	return error;
}

static void diffiter_do_unload_file(git_diff_iterator *iter)
{
	if (iter->ctxt.loaded) {
		diff_delta_unload(&iter->ctxt);

		git_pool_clear(&iter->lines);
		git_pool_clear(&iter->hunks);
	}

	iter->ctxt.delta = NULL;
	iter->hunk_head = NULL;
	iter->hunk_count = 0;
}

int git_diff_iterator_new(
	git_diff_iterator **iterator_ptr,
	git_diff_list *diff)
{
	size_t i;
	git_diff_delta *delta;
	git_diff_iterator *iter;

	assert(diff && iterator_ptr);

	*iterator_ptr = NULL;

	iter = git__malloc(sizeof(git_diff_iterator));
	GITERR_CHECK_ALLOC(iter);

	memset(iter, 0, sizeof(*iter));

	iter->diff = diff;
	GIT_REFCOUNT_INC(iter->diff);

	diff_delta_init_context_from_diff_list(&iter->ctxt, diff);

	if (git_pool_init(&iter->hunks, sizeof(diffiter_hunk), 0) < 0 ||
		git_pool_init(&iter->lines, sizeof(diffiter_line), 0) < 0)
		goto fail;

	git_vector_foreach(&diff->deltas, i, delta) {
		if (diff_delta_should_skip(iter->ctxt.opts, delta))
			continue;
		iter->file_count++;
	}

	*iterator_ptr = iter;

	return 0;

fail:
	git_diff_iterator_free(iter);

	return -1;
}

void git_diff_iterator_free(git_diff_iterator *iter)
{
	diffiter_do_unload_file(iter);
	git_diff_list_free(iter->diff); /* decrement ref count */
	git__free(iter);
}

int git_diff_iterator_num_files(git_diff_iterator *iter)
{
	return (int)iter->file_count;
}

int git_diff_iterator_num_hunks_in_file(git_diff_iterator *iter)
{
	int error = diffiter_do_diff_file(iter);
	return (error != 0) ? error : (int)iter->hunk_count;
}

int git_diff_iterator_num_lines_in_hunk(git_diff_iterator *iter)
{
	int error = diffiter_do_diff_file(iter);
	if (!error && iter->hunk_curr)
		error = iter->hunk_curr->line_count;
	return error;
}

int git_diff_iterator_next_file(
	git_diff_delta **delta_ptr,
	git_diff_iterator *iter)
{
	int error = 0;

	assert(iter);

	iter->file_index = iter->next_index;

	diffiter_do_unload_file(iter);

	while (!error) {
		iter->ctxt.delta = git_vector_get(&iter->diff->deltas, iter->file_index);
		if (!iter->ctxt.delta) {
			error = GIT_ITEROVER;
			break;
		}

		if (diff_delta_is_ambiguous(iter->ctxt.delta) &&
			(error = diff_delta_load(&iter->ctxt)) < 0)
			break;

		if (!diff_delta_should_skip(iter->ctxt.opts, iter->ctxt.delta))
			break;

		iter->file_index++;
	}

	if (!error) {
		iter->next_index = iter->file_index + 1;

		error = diff_delta_prep(&iter->ctxt);
	}

	if (iter->ctxt.delta == NULL) {
		iter->hunk_curr = NULL;
		iter->line_curr = NULL;
	}

	if (delta_ptr != NULL)
		*delta_ptr = !error ? iter->ctxt.delta : NULL;

	return error;
}

int git_diff_iterator_next_hunk(
	git_diff_range **range_ptr,
	const char **header,
	size_t *header_len,
	git_diff_iterator *iter)
{
	int error = diffiter_do_diff_file(iter);
	git_diff_range *range;

	if (error)
		return error;

	if (iter->hunk_curr == NULL) {
		if (range_ptr) *range_ptr = NULL;
		if (header) *header = NULL;
		if (header_len) *header_len = 0;
		iter->line_curr = NULL;
		return GIT_ITEROVER;
	}

	range = &iter->hunk_curr->range;

	if (range_ptr)
		*range_ptr = range;

	if (header) {
		int out = format_hunk_header(
			iter->hunk_header, sizeof(iter->hunk_header), range);

		/* TODO: append function name to header */

		*(iter->hunk_header + out++) = '\n';

		*header = iter->hunk_header;

		if (header_len)
			*header_len = (size_t)out;
	}

	iter->line_curr = iter->hunk_curr->line_head;
	iter->hunk_curr = iter->hunk_curr->next;

	return error;
}

int git_diff_iterator_next_line(
	char *line_origin, /**< GIT_DIFF_LINE_... value from above */
	const char **content_ptr,
	size_t *content_len,
	git_diff_iterator *iter)
{
	int error = diffiter_do_diff_file(iter);

	if (error)
		return error;

	/* if the user has not called next_hunk yet, call it implicitly (OK?) */
	if (iter->hunk_curr == iter->hunk_head) {
		error = git_diff_iterator_next_hunk(NULL, NULL, NULL, iter);
		if (error)
			return error;
	}

	if (iter->line_curr == NULL) {
		if (line_origin) *line_origin = GIT_DIFF_LINE_CONTEXT;
		if (content_ptr) *content_ptr = NULL;
		if (content_len) *content_len = 0;
		return GIT_ITEROVER;
	}

	if (line_origin)
		*line_origin = iter->line_curr->origin;
	if (content_ptr)
		*content_ptr = iter->line_curr->ptr;
	if (content_len)
		*content_len = iter->line_curr->len;

	iter->line_curr = iter->line_curr->next;

	return error;
}