Commit 3b5f795446601868d52d09ebac70ae3b7aee157a

Russell Belfer 2013-10-21T13:42:42

Create git_diff_line and extend git_diff_hunk Instead of having functions with so very many parameters to pass hunk and line data, this takes the existing git_diff_hunk struct and extends it with more hunk data, plus adds a git_diff_line. Those structs are used to pass back hunk and line data instead of the old APIs that took tons of parameters. Some work that was previously only being done for git_diff_patch creation (scanning the diff content for exact line counts) is now done for all callbacks, but the performance difference should not be noticable.

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
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
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
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
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
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
diff --git a/examples/diff.c b/examples/diff.c
index 2542f42..694621f 100644
--- a/examples/diff.c
+++ b/examples/diff.c
@@ -45,25 +45,23 @@ char *colors[] = {
 
 static int printer(
 	const git_diff_delta *delta,
-	const git_diff_hunk *range,
-	char usage,
-	const char *line,
-	size_t line_len,
+	const git_diff_hunk *hunk,
+	const git_diff_line *line,
 	void *data)
 {
 	int *last_color = data, color = 0;
 
-	(void)delta; (void)range; (void)line_len;
+	(void)delta; (void)hunk;
 
 	if (*last_color >= 0) {
-		switch (usage) {
-		case GIT_DIFF_LINE_ADDITION: color = 3; break;
-		case GIT_DIFF_LINE_DELETION: color = 2; break;
+		switch (line->origin) {
+		case GIT_DIFF_LINE_ADDITION:  color = 3; break;
+		case GIT_DIFF_LINE_DELETION:  color = 2; break;
 		case GIT_DIFF_LINE_ADD_EOFNL: color = 3; break;
 		case GIT_DIFF_LINE_DEL_EOFNL: color = 2; break;
-		case GIT_DIFF_LINE_FILE_HDR: color = 1; break;
-		case GIT_DIFF_LINE_HUNK_HDR: color = 4; break;
-		default: color = 0;
+		case GIT_DIFF_LINE_FILE_HDR:  color = 1; break;
+		case GIT_DIFF_LINE_HUNK_HDR:  color = 4; break;
+		default: break;
 		}
 		if (color != *last_color) {
 			if (*last_color == 1 || color == 1)
@@ -73,7 +71,13 @@ static int printer(
 		}
 	}
 
-	fputs(line, stdout);
+	if (line->origin == GIT_DIFF_LINE_CONTEXT ||
+		line->origin == GIT_DIFF_LINE_ADDITION ||
+		line->origin == GIT_DIFF_LINE_DELETION)
+		fputc(line->origin, stdout);
+
+	fwrite(line->content, 1, line->content_len, stdout);
+
 	return 0;
 }
 
diff --git a/examples/log.c b/examples/log.c
index 30de16a..4c2df07 100644
--- a/examples/log.c
+++ b/examples/log.c
@@ -184,14 +184,18 @@ static void print_commit(git_commit *commit)
 
 static int print_diff(
 	const git_diff_delta *delta,
-	const git_diff_hunk *range,
-	char usage,
-	const char *line,
-	size_t line_len,
+	const git_diff_hunk *hunk,
+	const git_diff_line *line,
 	void *data)
 {
-	(void)delta; (void)range; (void)usage; (void)line_len; (void)data;
-	fputs(line, stdout);
+	(void)delta; (void)hunk; (void)data;
+
+	if (line->origin == GIT_DIFF_LINE_CONTEXT ||
+		line->origin == GIT_DIFF_LINE_ADDITION ||
+		line->origin == GIT_DIFF_LINE_DELETION)
+		fputc(line->origin, stdout);
+
+	fwrite(line->content, 1, line->content_len, stdout);
 	return 0;
 }
 
diff --git a/include/git2/diff.h b/include/git2/diff.h
index f1572cb..ed9f713 100644
--- a/include/git2/diff.h
+++ b/include/git2/diff.h
@@ -385,10 +385,12 @@ typedef int (*git_diff_file_cb)(
  */
 typedef struct git_diff_hunk git_diff_hunk;
 struct git_diff_hunk {
-	int old_start; /** Starting line number in old_file */
-	int old_lines; /** Number of lines in old_file */
-	int new_start; /** Starting line number in new_file */
-	int new_lines; /** Number of lines in new_file */
+	int    old_start;     /** Starting line number in old_file */
+	int    old_lines;     /** Number of lines in old_file */
+	int    new_start;     /** Starting line number in new_file */
+	int    new_lines;     /** Number of lines in new_file */
+	size_t header_len;    /** Number of bytes in header text */
+	char   header[128];   /** Header text, NUL-byte terminated */
 };
 
 /**
@@ -397,8 +399,6 @@ struct git_diff_hunk {
 typedef int (*git_diff_hunk_cb)(
 	const git_diff_delta *delta,
 	const git_diff_hunk *hunk,
-	const char *header,
-	size_t header_len,
 	void *payload);
 
 /**
@@ -429,6 +429,19 @@ typedef enum {
 } git_diff_line_t;
 
 /**
+ * Structure describing a line (or data span) of a diff.
+ */
+typedef struct git_diff_line git_diff_line;
+struct git_diff_line {
+	char   origin;       /** A git_diff_line_t value */
+	int    old_lineno;   /** Line number in old file or -1 for added line */
+	int    new_lineno;   /** Line number in new file or -1 for deleted line */
+	int    num_lines;    /** Number of newline characters in content */
+	size_t content_len;  /** Number of bytes of data */
+	const char *content; /** Pointer to diff text, not NUL-byte terminated */
+};
+
+/**
  * When iterating over a diff, callback that will be made per text diff
  * line. In this context, the provided range will be NULL.
  *
@@ -438,10 +451,8 @@ typedef enum {
  */
 typedef int (*git_diff_line_cb)(
 	const git_diff_delta *delta, /** delta that contains this data */
-	const git_diff_hunk *hunk,   /** range of lines containing this data */
-	char line_origin,            /** git_diff_t value from above */
-	const char *content,         /** diff data - not NUL terminated */
-	size_t content_len,          /** number of bytes of diff data */
+	const git_diff_hunk *hunk,   /** hunk containing this data */
+	const git_diff_line *line,   /** line data */
 	void *payload);              /** user reference data */
 
 /**
diff --git a/include/git2/patch.h b/include/git2/patch.h
index 9836245..6a6ad92 100644
--- a/include/git2/patch.h
+++ b/include/git2/patch.h
@@ -150,9 +150,6 @@ GIT_EXTERN(int) git_patch_line_stats(
  * as NULL if you don't care about that particular piece of information.
  *
  * @param out Output pointer to git_diff_hunk of hunk
- * @param header Output pointer to header string for hunk.  Unlike the
- *               content pointer for each line, this will be NUL-terminated
- * @param header_len Output value of characters in header string
  * @param lines_in_hunk Output count of total lines in this hunk
  * @param patch Input pointer to patch object
  * @param hunk_idx Input index of hunk to get information about
@@ -160,8 +157,6 @@ GIT_EXTERN(int) git_patch_line_stats(
  */
 GIT_EXTERN(int) git_patch_get_hunk(
 	const git_diff_hunk **out,
-	const char **header,
-	size_t *header_len,
 	size_t *lines_in_hunk,
 	git_patch *patch,
 	size_t hunk_idx);
@@ -185,22 +180,14 @@ GIT_EXTERN(int) git_patch_num_lines_in_hunk(
  * index larger than the number of hunks or a line index larger than
  * the number of lines in the hunk, this will return -1.
  *
- * @param line_origin A GIT_DIFF_LINE constant from above
- * @param content Pointer to content of diff line, not NUL-terminated
- * @param content_len Number of characters in content
- * @param old_lineno Line number in old file or -1 if line is added
- * @param new_lineno Line number in new file or -1 if line is deleted
+ * @param out The git_diff_line data for this line
  * @param patch The patch to look in
  * @param hunk_idx The index of the hunk
  * @param line_of_hunk The index of the line in the hunk
  * @return 0 on success, <0 on failure
  */
 GIT_EXTERN(int) git_patch_get_line_in_hunk(
-	char *line_origin,
-	const char **content,
-	size_t *content_len,
-	int *old_lineno,
-	int *new_lineno,
+	const git_diff_line **out,
 	git_patch *patch,
 	size_t hunk_idx,
 	size_t line_of_hunk);
diff --git a/src/diff_patch.c b/src/diff_patch.c
index 9513682..cc49d68 100644
--- a/src/diff_patch.c
+++ b/src/diff_patch.c
@@ -12,23 +12,10 @@
 #include "diff_xdiff.h"
 #include "fileops.h"
 
-/* cached information about a single span in a diff */
-typedef struct diff_patch_line diff_patch_line;
-struct diff_patch_line {
-	const char *ptr;
-	size_t len;
-	size_t lines;
-	size_t oldno;
-	size_t newno;
-	char origin;
-};
-
 /* cached information about a hunk in a diff */
 typedef struct diff_patch_hunk diff_patch_hunk;
 struct diff_patch_hunk {
 	git_diff_hunk hunk;
-	char   header[128];
-	size_t header_len;
 	size_t line_start;
 	size_t line_count;
 };
@@ -42,8 +29,7 @@ struct git_patch {
 	git_diff_file_content nfile;
 	uint32_t flags;
 	git_array_t(diff_patch_hunk) hunks;
-	git_array_t(diff_patch_line) lines;
-	size_t oldno, newno;
+	git_array_t(git_diff_line)   lines;
 	size_t content_size, context_size, header_size;
 	git_pool flattened;
 };
@@ -57,7 +43,8 @@ enum {
 	GIT_DIFF_PATCH_FLATTENED   = (1 << 5),
 };
 
-static void diff_output_init(git_diff_output*, const git_diff_options*,
+static void diff_output_init(
+	git_diff_output*, const git_diff_options*,
 	git_diff_file_cb, git_diff_hunk_cb, git_diff_line_cb, void*);
 
 static void diff_output_to_patch(git_diff_output *, git_patch *);
@@ -81,7 +68,7 @@ static void diff_patch_init_common(git_patch *patch)
 	diff_patch_update_binary(patch);
 
 	if ((patch->delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
-		patch->flags |= GIT_DIFF_PATCH_LOADED; /* set LOADED but not DIFFABLE */
+		patch->flags |= GIT_DIFF_PATCH_LOADED; /* LOADED but not DIFFABLE */
 
 	patch->flags |= GIT_DIFF_PATCH_INITIALIZED;
 
@@ -687,7 +674,7 @@ int git_patch_line_stats(
 	memset(totals, 0, sizeof(totals));
 
 	for (idx = 0; idx < git_array_size(patch->lines); ++idx) {
-		diff_patch_line *line = git_array_get(patch->lines, idx);
+		git_diff_line *line = git_array_get(patch->lines, idx);
 		if (!line)
 			continue;
 
@@ -721,8 +708,6 @@ static int diff_error_outofrange(const char *thing)
 
 int git_patch_get_hunk(
 	const git_diff_hunk **out,
-	const char **header,
-	size_t *header_len,
 	size_t *lines_in_hunk,
 	git_patch *patch,
 	size_t hunk_idx)
@@ -734,15 +719,11 @@ int git_patch_get_hunk(
 
 	if (!hunk) {
 		if (out) *out = NULL;
-		if (header) *header = NULL;
-		if (header_len) *header_len = 0;
 		if (lines_in_hunk) *lines_in_hunk = 0;
 		return diff_error_outofrange("hunk");
 	}
 
 	if (out) *out = &hunk->hunk;
-	if (header) *header = hunk->header;
-	if (header_len) *header_len = hunk->header_len;
 	if (lines_in_hunk) *lines_in_hunk = hunk->line_count;
 	return 0;
 }
@@ -758,49 +739,30 @@ int git_patch_num_lines_in_hunk(git_patch *patch, size_t hunk_idx)
 }
 
 int git_patch_get_line_in_hunk(
-	char *line_origin,
-	const char **content,
-	size_t *content_len,
-	int *old_lineno,
-	int *new_lineno,
+	const git_diff_line **out,
 	git_patch *patch,
 	size_t hunk_idx,
 	size_t line_of_hunk)
 {
 	diff_patch_hunk *hunk;
-	diff_patch_line *line;
-	const char *thing;
+	git_diff_line *line;
 
 	assert(patch);
 
 	if (!(hunk = git_array_get(patch->hunks, hunk_idx))) {
-		thing = "hunk";
-		goto notfound;
+		if (out) *out = NULL;
+		return diff_error_outofrange("hunk");
 	}
 
 	if (line_of_hunk >= hunk->line_count ||
 		!(line = git_array_get(
 			patch->lines, hunk->line_start + line_of_hunk))) {
-		thing = "line";
-		goto notfound;
+		if (out) *out = NULL;
+		return diff_error_outofrange("line");
 	}
 
-	if (line_origin) *line_origin = line->origin;
-	if (content) *content = line->ptr;
-	if (content_len) *content_len = line->len;
-	if (old_lineno) *old_lineno = (int)line->oldno;
-	if (new_lineno) *new_lineno = (int)line->newno;
-
+	if (out) *out = line;
 	return 0;
-
-notfound:
-	if (line_origin) *line_origin = GIT_DIFF_LINE_CONTEXT;
-	if (content) *content = NULL;
-	if (content_len) *content_len = 0;
-	if (old_lineno) *old_lineno = -1;
-	if (new_lineno) *new_lineno = -1;
-
-	return diff_error_outofrange(thing);
 }
 
 size_t git_patch_size(
@@ -880,18 +842,16 @@ int git_patch__invoke_callbacks(
 	for (i = 0; !error && i < git_array_size(patch->hunks); ++i) {
 		diff_patch_hunk *h = git_array_get(patch->hunks, i);
 
-		error = hunk_cb(
-			patch->delta, &h->hunk, h->header, h->header_len, payload);
+		error = hunk_cb(patch->delta, &h->hunk, payload);
 
 		if (!line_cb)
 			continue;
 
 		for (j = 0; !error && j < h->line_count; ++j) {
-			diff_patch_line *l =
+			git_diff_line *l =
 				git_array_get(patch->lines, h->line_start + j);
 
-			error = line_cb(
-				patch->delta, &h->hunk, l->origin, l->ptr, l->len, payload);
+			error = line_cb(patch->delta, &h->hunk, l, payload);
 		}
 	}
 
@@ -911,8 +871,6 @@ static int diff_patch_file_cb(
 static int diff_patch_hunk_cb(
 	const git_diff_delta *delta,
 	const git_diff_hunk *hunk_,
-	const char *header,
-	size_t header_len,
 	void *payload)
 {
 	git_patch *patch = payload;
@@ -925,34 +883,23 @@ static int diff_patch_hunk_cb(
 
 	memcpy(&hunk->hunk, hunk_, sizeof(hunk->hunk));
 
-	assert(header_len + 1 < sizeof(hunk->header));
-	memcpy(&hunk->header, header, header_len);
-	hunk->header[header_len] = '\0';
-	hunk->header_len = header_len;
-
-	patch->header_size += header_len;
+	patch->header_size += hunk_->header_len;
 
 	hunk->line_start = git_array_size(patch->lines);
 	hunk->line_count = 0;
 
-	patch->oldno = hunk_->old_start;
-	patch->newno = hunk_->new_start;
-
 	return 0;
 }
 
 static int diff_patch_line_cb(
 	const git_diff_delta *delta,
 	const git_diff_hunk *hunk_,
-	char line_origin,
-	const char *content,
-	size_t content_len,
+	const git_diff_line *line_,
 	void *payload)
 {
 	git_patch *patch = payload;
 	diff_patch_hunk *hunk;
-	diff_patch_line *line;
-	const char *content_end = content + content_len;
+	git_diff_line   *line;
 
 	GIT_UNUSED(delta);
 	GIT_UNUSED(hunk_);
@@ -963,48 +910,20 @@ static int diff_patch_line_cb(
 	line = git_array_alloc(patch->lines);
 	GITERR_CHECK_ALLOC(line);
 
-	line->ptr = content;
-	line->len = content_len;
-	line->origin = line_origin;
+	memcpy(line, line_, sizeof(*line));
 
 	/* do some bookkeeping so we can provide old/new line numbers */
 
-	line->lines = 0;
-	while (content < content_end)
-		if (*content++ == '\n')
-			++line->lines;
+	patch->content_size += line->content_len;
 
-	patch->content_size += content_len;
-
-	switch (line_origin) {
-	case GIT_DIFF_LINE_ADDITION:
-		patch->content_size += 1;
-	case GIT_DIFF_LINE_DEL_EOFNL:
-		line->oldno = -1;
-		line->newno = patch->newno;
-		patch->newno += line->lines;
-		break;
-	case GIT_DIFF_LINE_DELETION:
+	if (line->origin == GIT_DIFF_LINE_ADDITION ||
+		line->origin == GIT_DIFF_LINE_DELETION)
 		patch->content_size += 1;
-	case GIT_DIFF_LINE_ADD_EOFNL:
-		line->oldno = patch->oldno;
-		line->newno = -1;
-		patch->oldno += line->lines;
-		break;
-	case GIT_DIFF_LINE_CONTEXT:
+	else if (line->origin == GIT_DIFF_LINE_CONTEXT) {
 		patch->content_size += 1;
-		patch->context_size += 1;
-	case GIT_DIFF_LINE_CONTEXT_EOFNL:
-		patch->context_size += content_len;
-		line->oldno = patch->oldno;
-		line->newno = patch->newno;
-		patch->oldno += line->lines;
-		patch->newno += line->lines;
-		break;
-	default:
-		assert(false);
-		break;
-	}
+		patch->context_size += line->content_len + 1;
+	} else if (line->origin == GIT_DIFF_LINE_CONTEXT_EOFNL)
+		patch->context_size += line->content_len;
 
 	hunk->line_count++;
 
diff --git a/src/diff_print.c b/src/diff_print.c
index a6423d9..b04b115 100644
--- a/src/diff_print.c
+++ b/src/diff_print.c
@@ -17,6 +17,7 @@ typedef struct {
 	git_buf *buf;
 	uint32_t flags;
 	int oid_strlen;
+	git_diff_line line;
 } diff_print_info;
 
 static int diff_print_info_init(
@@ -51,6 +52,11 @@ static int diff_print_info_init(
 	else if (pi->oid_strlen > GIT_OID_HEXSZ + 1)
 		pi->oid_strlen = GIT_OID_HEXSZ + 1;
 
+	memset(&pi->line, 0, sizeof(pi->line));
+	pi->line.old_lineno = -1;
+	pi->line.new_lineno = -1;
+	pi->line.num_lines  = 1;
+
 	return 0;
 }
 
@@ -107,8 +113,11 @@ static int diff_print_one_name_only(
 		git_buf_putc(out, '\n'))
 		return -1;
 
-	if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_FILE_HDR,
-			git_buf_cstr(out), git_buf_len(out), pi->payload))
+	pi->line.origin      = GIT_DIFF_LINE_FILE_HDR;
+	pi->line.content     = git_buf_cstr(out);
+	pi->line.content_len = git_buf_len(out);
+
+	if (pi->print_cb(delta, NULL, &pi->line, pi->payload))
 		return callback_error();
 
 	return 0;
@@ -149,8 +158,11 @@ static int diff_print_one_name_status(
 	if (git_buf_oom(out))
 		return -1;
 
-	if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_FILE_HDR,
-			git_buf_cstr(out), git_buf_len(out), pi->payload))
+	pi->line.origin      = GIT_DIFF_LINE_FILE_HDR;
+	pi->line.content     = git_buf_cstr(out);
+	pi->line.content_len = git_buf_len(out);
+
+	if (pi->print_cb(delta, NULL, &pi->line, pi->payload))
 		return callback_error();
 
 	return 0;
@@ -192,8 +204,11 @@ static int diff_print_one_raw(
 	if (git_buf_oom(out))
 		return -1;
 
-	if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_FILE_HDR,
-			git_buf_cstr(out), git_buf_len(out), pi->payload))
+	pi->line.origin      = GIT_DIFF_LINE_FILE_HDR;
+	pi->line.content     = git_buf_cstr(out);
+	pi->line.content_len = git_buf_len(out);
+
+	if (pi->print_cb(delta, NULL, &pi->line, pi->payload))
 		return callback_error();
 
 	return 0;
@@ -302,8 +317,11 @@ static int diff_print_patch_file(
 			pi->buf, delta, oldpfx, newpfx, pi->oid_strlen) < 0)
 		return -1;
 
-	if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_FILE_HDR,
-			git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload))
+	pi->line.origin      = GIT_DIFF_LINE_FILE_HDR;
+	pi->line.content     = git_buf_cstr(pi->buf);
+	pi->line.content_len = git_buf_len(pi->buf);
+
+	if (pi->print_cb(delta, NULL, &pi->line, pi->payload))
 		return callback_error();
 
 	if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0)
@@ -316,8 +334,12 @@ static int diff_print_patch_file(
 			"Binary files %s%s and %s%s differ\n") < 0)
 		return -1;
 
-	if (pi->print_cb(delta, NULL, GIT_DIFF_LINE_BINARY,
-			git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload))
+	pi->line.origin      = GIT_DIFF_LINE_BINARY;
+	pi->line.content     = git_buf_cstr(pi->buf);
+	pi->line.content_len = git_buf_len(pi->buf);
+	pi->line.num_lines   = 1;
+
+	if (pi->print_cb(delta, NULL, &pi->line, pi->payload))
 		return callback_error();
 
 	return 0;
@@ -325,9 +347,7 @@ static int diff_print_patch_file(
 
 static int diff_print_patch_hunk(
 	const git_diff_delta *d,
-	const git_diff_hunk *r,
-	const char *header,
-	size_t header_len,
+	const git_diff_hunk *h,
 	void *data)
 {
 	diff_print_info *pi = data;
@@ -335,12 +355,11 @@ static int diff_print_patch_hunk(
 	if (S_ISDIR(d->new_file.mode))
 		return 0;
 
-	git_buf_clear(pi->buf);
-	if (git_buf_put(pi->buf, header, header_len) < 0)
-		return -1;
+	pi->line.origin      = GIT_DIFF_LINE_HUNK_HDR;
+	pi->line.content     = h->header;
+	pi->line.content_len = h->header_len;
 
-	if (pi->print_cb(d, r, GIT_DIFF_LINE_HUNK_HDR,
-			git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload))
+	if (pi->print_cb(d, h, &pi->line, pi->payload))
 		return callback_error();
 
 	return 0;
@@ -348,10 +367,8 @@ static int diff_print_patch_hunk(
 
 static int diff_print_patch_line(
 	const git_diff_delta *delta,
-	const git_diff_hunk *range,
-	char line_origin, /* GIT_DIFF_LINE value from above */
-	const char *content,
-	size_t content_len,
+	const git_diff_hunk *hunk,
+	const git_diff_line *line,
 	void *data)
 {
 	diff_print_info *pi = data;
@@ -359,21 +376,7 @@ static int diff_print_patch_line(
 	if (S_ISDIR(delta->new_file.mode))
 		return 0;
 
-	git_buf_clear(pi->buf);
-	git_buf_grow(pi->buf, content_len + 2);
-
-	if (line_origin == GIT_DIFF_LINE_ADDITION ||
-		line_origin == GIT_DIFF_LINE_DELETION ||
-		line_origin == GIT_DIFF_LINE_CONTEXT)
-		git_buf_putc(pi->buf, line_origin);
-
-	git_buf_put(pi->buf, content, content_len);
-
-	if (git_buf_oom(pi->buf))
-		return -1;
-
-	if (pi->print_cb(delta, range, line_origin,
-			git_buf_cstr(pi->buf), git_buf_len(pi->buf), pi->payload))
+	if (pi->print_cb(delta, hunk, line, pi->payload))
 		return callback_error();
 
 	return 0;
@@ -452,15 +455,19 @@ int git_patch_print(
 
 static int diff_print_to_buffer_cb(
 	const git_diff_delta *delta,
-	const git_diff_hunk *range,
-	char line_origin,
-	const char *content,
-	size_t content_len,
+	const git_diff_hunk *hunk,
+	const git_diff_line *line,
 	void *payload)
 {
 	git_buf *output = payload;
-	GIT_UNUSED(delta); GIT_UNUSED(range); GIT_UNUSED(line_origin);
-	return git_buf_put(output, content, content_len);
+	GIT_UNUSED(delta); GIT_UNUSED(hunk);
+
+	if (line->origin == GIT_DIFF_LINE_ADDITION ||
+		line->origin == GIT_DIFF_LINE_DELETION ||
+		line->origin == GIT_DIFF_LINE_CONTEXT)
+		git_buf_putc(output, line->origin);
+
+	return git_buf_put(output, line->content, line->content_len);
 }
 
 /* print a git_patch to a string buffer */
diff --git a/src/diff_xdiff.c b/src/diff_xdiff.c
index 9720397..a90f8d5 100644
--- a/src/diff_xdiff.c
+++ b/src/diff_xdiff.c
@@ -53,36 +53,94 @@ typedef struct {
 	git_xdiff_output *xo;
 	git_patch *patch;
 	git_diff_hunk hunk;
+	size_t old_lineno, new_lineno;
 } git_xdiff_info;
 
+static int diff_update_lines(
+	git_xdiff_info *info,
+	git_diff_line *line,
+	const char *content,
+	size_t content_len)
+{
+	const char *scan = content, *scan_end = content + content_len;
+
+	for (line->num_lines = 0; scan < scan_end; ++scan)
+		if (*scan == '\n')
+			++line->num_lines;
+
+	line->content     = content;
+	line->content_len = content_len;
+
+	/* expect " "/"-"/"+", then data */
+	switch (line->origin) {
+	case GIT_DIFF_LINE_ADDITION:
+	case GIT_DIFF_LINE_DEL_EOFNL:
+		line->old_lineno = -1;
+		line->new_lineno = info->new_lineno;
+		info->new_lineno += line->num_lines;
+		break;
+	case GIT_DIFF_LINE_DELETION:
+	case GIT_DIFF_LINE_ADD_EOFNL:
+		line->old_lineno = info->old_lineno;
+		line->new_lineno = -1;
+		info->old_lineno += line->num_lines;
+		break;
+	case GIT_DIFF_LINE_CONTEXT:
+	case GIT_DIFF_LINE_CONTEXT_EOFNL:
+		line->old_lineno = info->old_lineno;
+		line->new_lineno = info->new_lineno;
+		info->old_lineno += line->num_lines;
+		info->new_lineno += line->num_lines;
+		break;
+	default:
+		giterr_set(GITERR_INVALID, "Unknown diff line origin %02x",
+			(unsigned int)line->origin);
+		return -1;
+	}
+
+	return 0;
+}
+
 static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len)
 {
 	git_xdiff_info *info = priv;
 	git_patch *patch = info->patch;
 	const git_diff_delta *delta = git_patch_get_delta(patch);
 	git_diff_output *output = &info->xo->output;
+	git_diff_line line;
 
 	if (len == 1) {
 		output->error = git_xdiff_parse_hunk(&info->hunk, bufs[0].ptr);
 		if (output->error < 0)
 			return output->error;
 
+		info->hunk.header_len = bufs[0].size;
+		if (info->hunk.header_len >= sizeof(info->hunk.header))
+			info->hunk.header_len = sizeof(info->hunk.header) - 1;
+		memcpy(info->hunk.header, bufs[0].ptr, info->hunk.header_len);
+		info->hunk.header[info->hunk.header_len] = '\0';
+
 		if (output->hunk_cb != NULL &&
-			output->hunk_cb(delta, &info->hunk,
-				bufs[0].ptr, bufs[0].size, output->payload))
+			output->hunk_cb(delta, &info->hunk, output->payload))
 			output->error = GIT_EUSER;
+
+		info->old_lineno = info->hunk.old_start;
+		info->new_lineno = info->hunk.new_start;
 	}
 
 	if (len == 2 || len == 3) {
 		/* expect " "/"-"/"+", then data */
-		char origin =
+		line.origin =
 			(*bufs[0].ptr == '+') ? GIT_DIFF_LINE_ADDITION :
 			(*bufs[0].ptr == '-') ? GIT_DIFF_LINE_DELETION :
 			GIT_DIFF_LINE_CONTEXT;
 
-		if (output->data_cb != NULL &&
-			output->data_cb(delta, &info->hunk,
-				origin, bufs[1].ptr, bufs[1].size, output->payload))
+		output->error = diff_update_lines(
+			info, &line, bufs[1].ptr, bufs[1].size);
+
+		if (!output->error &&
+			output->data_cb != NULL &&
+			output->data_cb(delta, &info->hunk, &line, output->payload))
 			output->error = GIT_EUSER;
 	}
 
@@ -92,14 +150,17 @@ static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len)
 		 * If we have a '-' and a third buf, then we have removed a line
 		 * with out a newline but added a blank line, so ADD_EOFNL.
 		 */
-		char origin =
+		line.origin =
 			(*bufs[0].ptr == '+') ? GIT_DIFF_LINE_DEL_EOFNL :
 			(*bufs[0].ptr == '-') ? GIT_DIFF_LINE_ADD_EOFNL :
 			GIT_DIFF_LINE_CONTEXT_EOFNL;
 
-		if (output->data_cb != NULL &&
-			output->data_cb(delta, &info->hunk,
-				origin, bufs[2].ptr, bufs[2].size, output->payload))
+		output->error = diff_update_lines(
+			info, &line, bufs[2].ptr, bufs[2].size);
+
+		if (!output->error &&
+			output->data_cb != NULL &&
+			output->data_cb(delta, &info->hunk, &line, output->payload))
 			output->error = GIT_EUSER;
 	}
 
diff --git a/tests-clar/diff/blob.c b/tests-clar/diff/blob.c
index 898c037..b51bc0f 100644
--- a/tests-clar/diff/blob.c
+++ b/tests-clar/diff/blob.c
@@ -319,8 +319,8 @@ void test_diff_blob__can_compare_against_null_blobs_with_patch(void)
 	git_blob *e = NULL;
 	git_patch *p;
 	const git_diff_delta *delta;
-	int line;
-	char origin;
+	const git_diff_line *line;
+	int l, max_l;
 
 	cl_git_pass(git_patch_from_blobs(&p, d, NULL, e, NULL, &opts));
 
@@ -337,10 +337,10 @@ void test_diff_blob__can_compare_against_null_blobs_with_patch(void)
 	cl_assert_equal_i(1, (int)git_patch_num_hunks(p));
 	cl_assert_equal_i(14, git_patch_num_lines_in_hunk(p, 0));
 
-	for (line = 0; line < git_patch_num_lines_in_hunk(p, 0); ++line) {
-		cl_git_pass(git_patch_get_line_in_hunk(
-			&origin, NULL, NULL, NULL, NULL, p, 0, line));
-		cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)origin);
+	max_l = git_patch_num_lines_in_hunk(p, 0);
+	for (l = 0; l < max_l; ++l) {
+		cl_git_pass(git_patch_get_line_in_hunk(&line, p, 0, l));
+		cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)line->origin);
 	}
 
 	git_patch_free(p);
@@ -362,10 +362,10 @@ void test_diff_blob__can_compare_against_null_blobs_with_patch(void)
 	cl_assert_equal_i(1, (int)git_patch_num_hunks(p));
 	cl_assert_equal_i(14, git_patch_num_lines_in_hunk(p, 0));
 
-	for (line = 0; line < git_patch_num_lines_in_hunk(p, 0); ++line) {
-		cl_git_pass(git_patch_get_line_in_hunk(
-			&origin, NULL, NULL, NULL, NULL, p, 0, line));
-		cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)origin);
+	max_l = git_patch_num_lines_in_hunk(p, 0);
+	for (l = 0; l < max_l; ++l) {
+		cl_git_pass(git_patch_get_line_in_hunk(&line, p, 0, l));
+		cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)line->origin);
 	}
 
 	git_patch_free(p);
diff --git a/tests-clar/diff/diff_helpers.c b/tests-clar/diff/diff_helpers.c
index cf76886..466d0ef 100644
--- a/tests-clar/diff/diff_helpers.c
+++ b/tests-clar/diff/diff_helpers.c
@@ -95,41 +95,37 @@ int diff_print_file_cb(
 
 int diff_hunk_cb(
 	const git_diff_delta *delta,
-	const git_diff_hunk *range,
-	const char *header,
-	size_t header_len,
+	const git_diff_hunk *hunk,
 	void *payload)
 {
 	diff_expects *e = payload;
+	const char *scan = hunk->header, *scan_end = scan + hunk->header_len;
 
 	GIT_UNUSED(delta);
 
 	/* confirm no NUL bytes in header text */
-	while (header_len--) cl_assert('\0' != *header++);
+	while (scan < scan_end)
+		cl_assert('\0' != *scan++);
 
 	e->hunks++;
-	e->hunk_old_lines += range->old_lines;
-	e->hunk_new_lines += range->new_lines;
+	e->hunk_old_lines += hunk->old_lines;
+	e->hunk_new_lines += hunk->new_lines;
 	return 0;
 }
 
 int diff_line_cb(
 	const git_diff_delta *delta,
-	const git_diff_hunk *range,
-	char line_origin,
-	const char *content,
-	size_t content_len,
+	const git_diff_hunk *hunk,
+	const git_diff_line *line,
 	void *payload)
 {
 	diff_expects *e = payload;
 
 	GIT_UNUSED(delta);
-	GIT_UNUSED(range);
-	GIT_UNUSED(content);
-	GIT_UNUSED(content_len);
+	GIT_UNUSED(hunk);
 
 	e->lines++;
-	switch (line_origin) {
+	switch (line->origin) {
 	case GIT_DIFF_LINE_CONTEXT:
 	case GIT_DIFF_LINE_CONTEXT_EOFNL: /* techically not a line */
 		e->line_ctxt++;
@@ -186,30 +182,23 @@ int diff_foreach_via_iterator(
 		num_h = git_patch_num_hunks(patch);
 
 		for (h = 0; h < num_h; h++) {
-			const git_diff_hunk *range;
-			const char *hdr;
-			size_t hdr_len, l, num_l;
+			const git_diff_hunk *hunk;
+			size_t l, num_l;
 
-			cl_git_pass(git_patch_get_hunk(
-				&range, &hdr, &hdr_len, &num_l, patch, h));
+			cl_git_pass(git_patch_get_hunk(&hunk, &num_l, patch, h));
 
-			if (hunk_cb && hunk_cb(delta, range, hdr, hdr_len, data) != 0) {
+			if (hunk_cb && hunk_cb(delta, hunk, data) != 0) {
 				git_patch_free(patch);
 				goto abort;
 			}
 
 			for (l = 0; l < num_l; ++l) {
-				char origin;
-				const char *line;
-				size_t line_len;
-				int old_lineno, new_lineno;
+				const git_diff_line *line;
 
-				cl_git_pass(git_patch_get_line_in_hunk(
-					&origin, &line, &line_len, &old_lineno, &new_lineno,
-					patch, h, l));
+				cl_git_pass(git_patch_get_line_in_hunk(&line, patch, h, l));
 
 				if (line_cb &&
-					line_cb(delta, range, origin, line, line_len, data) != 0) {
+					line_cb(delta, hunk, line, data) != 0) {
 					git_patch_free(patch);
 					goto abort;
 				}
@@ -228,18 +217,15 @@ abort:
 
 static int diff_print_cb(
 	const git_diff_delta *delta,
-	const git_diff_hunk *range,
-	char line_origin, /**< GIT_DIFF_LINE_... value from above */
-	const char *content,
-	size_t content_len,
+	const git_diff_hunk *hunk,
+	const git_diff_line *line,
 	void *payload)
 {
 	GIT_UNUSED(payload);
 	GIT_UNUSED(delta);
-	GIT_UNUSED(range);
-	GIT_UNUSED(line_origin);
-	GIT_UNUSED(content_len);
-	fputs(content, (FILE *)payload);
+	GIT_UNUSED(hunk);
+	fprintf((FILE *)payload, "%c%.*s",
+		line->origin, (int)line->content_len, line->content);
 	return 0;
 }
 
diff --git a/tests-clar/diff/diff_helpers.h b/tests-clar/diff/diff_helpers.h
index e3ad61f..bf21f4b 100644
--- a/tests-clar/diff/diff_helpers.h
+++ b/tests-clar/diff/diff_helpers.h
@@ -44,17 +44,13 @@ extern int diff_print_file_cb(
 
 extern int diff_hunk_cb(
 	const git_diff_delta *delta,
-	const git_diff_hunk *range,
-	const char *header,
-	size_t header_len,
+	const git_diff_hunk *hunk,
 	void *cb_data);
 
 extern int diff_line_cb(
 	const git_diff_delta *delta,
-	const git_diff_hunk *range,
-	char line_origin,
-	const char *content,
-	size_t content_len,
+	const git_diff_hunk *hunk,
+	const git_diff_line *line,
 	void *cb_data);
 
 extern int diff_foreach_via_iterator(
diff --git a/tests-clar/diff/diffiter.c b/tests-clar/diff/diffiter.c
index 1a1e5df..f886e1b 100644
--- a/tests-clar/diff/diffiter.c
+++ b/tests-clar/diff/diffiter.c
@@ -101,15 +101,10 @@ void test_diff_diffiter__iterate_files_and_hunks(void)
 		num_h = git_patch_num_hunks(patch);
 
 		for (h = 0; h < num_h; h++) {
-			const git_diff_hunk *range;
-			const char *header;
-			size_t header_len, num_l;
-
-			cl_git_pass(git_patch_get_hunk(
-				&range, &header, &header_len, &num_l, patch, h));
+			const git_diff_hunk *hunk;
 
-			cl_assert(range);
-			cl_assert(header);
+			cl_git_pass(git_patch_get_hunk(&hunk, NULL, patch, h));
+			cl_assert(hunk);
 
 			hunk_count++;
 		}
@@ -229,22 +224,17 @@ void test_diff_diffiter__iterate_all(void)
 		num_h = git_patch_num_hunks(patch);
 		for (h = 0; h < num_h; h++) {
 			const git_diff_hunk *range;
-			const char *header;
-			size_t header_len, l, num_l;
+			size_t l, num_l;
 
-			cl_git_pass(git_patch_get_hunk(
-				&range, &header, &header_len, &num_l, patch, h));
-			cl_assert(range && header);
+			cl_git_pass(git_patch_get_hunk(&range, &num_l, patch, h));
+			cl_assert(range);
 			exp.hunks++;
 
 			for (l = 0; l < num_l; ++l) {
-				char origin;
-				const char *content;
-				size_t content_len;
+				const git_diff_line *line;
 
-				cl_git_pass(git_patch_get_line_in_hunk(
-					&origin, &content, &content_len, NULL, NULL, patch, h, l));
-				cl_assert(content);
+				cl_git_pass(git_patch_get_line_in_hunk(&line, patch, h, l));
+				cl_assert(line && line->content);
 				exp.lines++;
 			}
 		}
diff --git a/tests-clar/diff/patch.c b/tests-clar/diff/patch.c
index 9c01c3b..366e5da 100644
--- a/tests-clar/diff/patch.c
+++ b/tests-clar/diff/patch.c
@@ -26,40 +26,37 @@ void test_diff_patch__cleanup(void)
 
 static int check_removal_cb(
 	const git_diff_delta *delta,
-	const git_diff_hunk *range,
-	char line_origin,
-	const char *formatted_output,
-	size_t output_len,
+	const git_diff_hunk *hunk,
+	const git_diff_line *line,
 	void *payload)
 {
 	GIT_UNUSED(payload);
-	GIT_UNUSED(output_len);
 
-	switch (line_origin) {
+	switch (line->origin) {
 	case GIT_DIFF_LINE_FILE_HDR:
-		cl_assert_equal_s(EXPECTED_HEADER, formatted_output);
-		cl_assert(range == NULL);
+		cl_assert_equal_s(EXPECTED_HEADER, line->content);
+		cl_assert(hunk == NULL);
 		goto check_delta;
 
 	case GIT_DIFF_LINE_HUNK_HDR:
-		cl_assert_equal_s(EXPECTED_HUNK, formatted_output);
+		cl_assert_equal_s(EXPECTED_HUNK, line->content);
 		/* Fall through */
 
 	case GIT_DIFF_LINE_CONTEXT:
 	case GIT_DIFF_LINE_DELETION:
-		goto check_range;
+		goto check_hunk;
 
 	default:
 		/* unexpected code path */
 		return -1;
 	}
 
-check_range:
-	cl_assert(range != NULL);
-	cl_assert_equal_i(1, range->old_start);
-	cl_assert_equal_i(2, range->old_lines);
-	cl_assert_equal_i(0, range->new_start);
-	cl_assert_equal_i(0, range->new_lines);
+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);
 
 check_delta:
 	cl_assert_equal_s("subdir.txt", delta->old_file.path);
@@ -227,11 +224,9 @@ void test_diff_patch__hunks_have_correct_line_numbers(void)
 	git_diff *diff;
 	git_patch *patch;
 	const git_diff_delta *delta;
-	const git_diff_hunk *range;
-	const char *hdr, *text;
-	size_t hdrlen, hunklen, textlen;
-	char origin;
-	int oldno, newno;
+	const git_diff_hunk *hunk;
+	const git_diff_line *line;
+	size_t hunklen;
 	git_buf old_content = GIT_BUF_INIT, actual = GIT_BUF_INIT;
 	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";
 
@@ -263,78 +258,71 @@ void test_diff_patch__hunks_have_correct_line_numbers(void)
 	/* check hunk 0 */
 
 	cl_git_pass(
-		git_patch_get_hunk(&range, &hdr, &hdrlen, &hunklen, patch, 0));
+		git_patch_get_hunk(&hunk, &hunklen, patch, 0));
 
 	cl_assert_equal_i(18, (int)hunklen);
 
-	cl_assert_equal_i(6, (int)range->old_start);
-	cl_assert_equal_i(15, (int)range->old_lines);
-	cl_assert_equal_i(6, (int)range->new_start);
-	cl_assert_equal_i(9, (int)range->new_lines);
+	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);
 
 	cl_assert_equal_i(18, (int)git_patch_num_lines_in_hunk(patch, 0));
 
-	cl_git_pass(git_patch_get_line_in_hunk(
-		&origin, &text, &textlen, &oldno, &newno, patch, 0, 0));
-	cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)origin);
-	cl_git_pass(git_buf_set(&actual, text, textlen));
+	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));
 	cl_assert_equal_s("Ivory their outposts were--the guardrooms of them gilded,\n", actual.ptr);
-	cl_assert_equal_i(6, oldno);
-	cl_assert_equal_i(6, newno);
+	cl_assert_equal_i(6, line->old_lineno);
+	cl_assert_equal_i(6, line->new_lineno);
 
-	cl_git_pass(git_patch_get_line_in_hunk(
-		&origin, &text, &textlen, &oldno, &newno, patch, 0, 3));
-	cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)origin);
-	cl_git_pass(git_buf_set(&actual, text, textlen));
+	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));
 	cl_assert_equal_s("All the world went softly when it walked before my Cities--\n", actual.ptr);
-	cl_assert_equal_i(9, oldno);
-	cl_assert_equal_i(-1, newno);
+	cl_assert_equal_i(9, line->old_lineno);
+	cl_assert_equal_i(-1, line->new_lineno);
 
-	cl_git_pass(git_patch_get_line_in_hunk(
-		&origin, &text, &textlen, &oldno, &newno, patch, 0, 12));
-	cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)origin);
-	cl_git_pass(git_buf_set(&actual, text, textlen));
+	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));
 	cl_assert_equal_s("This is some new text;\n", actual.ptr);
-	cl_assert_equal_i(-1, oldno);
-	cl_assert_equal_i(9, newno);
+	cl_assert_equal_i(-1, line->old_lineno);
+	cl_assert_equal_i(9, line->new_lineno);
 
 	/* check hunk 1 */
 
-	cl_git_pass(
-		git_patch_get_hunk(&range, &hdr, &hdrlen, &hunklen, patch, 1));
+	cl_git_pass(git_patch_get_hunk(&hunk, &hunklen, patch, 1));
 
 	cl_assert_equal_i(18, (int)hunklen);
 
-	cl_assert_equal_i(31, (int)range->old_start);
-	cl_assert_equal_i(15, (int)range->old_lines);
-	cl_assert_equal_i(25, (int)range->new_start);
-	cl_assert_equal_i(9, (int)range->new_lines);
+	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);
 
 	cl_assert_equal_i(18, (int)git_patch_num_lines_in_hunk(patch, 1));
 
-	cl_git_pass(git_patch_get_line_in_hunk(
-		&origin, &text, &textlen, &oldno, &newno, patch, 1, 0));
-	cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)origin);
-	cl_git_pass(git_buf_set(&actual, text, textlen));
+	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));
 	cl_assert_equal_s("My rulers and their treasure and their unborn populations,\n", actual.ptr);
-	cl_assert_equal_i(31, oldno);
-	cl_assert_equal_i(25, newno);
+	cl_assert_equal_i(31, line->old_lineno);
+	cl_assert_equal_i(25, line->new_lineno);
 
-	cl_git_pass(git_patch_get_line_in_hunk(
-		&origin, &text, &textlen, &oldno, &newno, patch, 1, 3));
-	cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)origin);
-	cl_git_pass(git_buf_set(&actual, text, textlen));
+	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));
 	cl_assert_equal_s("The Daughters of the Palace whom they cherished in my Cities,\n", actual.ptr);
-	cl_assert_equal_i(34, oldno);
-	cl_assert_equal_i(-1, newno);
+	cl_assert_equal_i(34, line->old_lineno);
+	cl_assert_equal_i(-1, line->new_lineno);
 
-	cl_git_pass(git_patch_get_line_in_hunk(
-		&origin, &text, &textlen, &oldno, &newno, patch, 1, 12));
-	cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)origin);
-	cl_git_pass(git_buf_set(&actual, text, textlen));
+	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));
 	cl_assert_equal_s("Another replacement;\n", actual.ptr);
-	cl_assert_equal_i(-1, oldno);
-	cl_assert_equal_i(28, newno);
+	cl_assert_equal_i(-1, line->old_lineno);
+	cl_assert_equal_i(28, line->new_lineno);
 
 	git_patch_free(patch);
 	git_diff_free(diff);
@@ -356,57 +344,51 @@ void test_diff_patch__hunks_have_correct_line_numbers(void)
 
 	/* check hunk 0 */
 
-	cl_git_pass(
-		git_patch_get_hunk(&range, &hdr, &hdrlen, &hunklen, patch, 0));
+	cl_git_pass(git_patch_get_hunk(&hunk, &hunklen, patch, 0));
 
 	cl_assert_equal_i(6, (int)hunklen);
 
-	cl_assert_equal_i(46, (int)range->old_start);
-	cl_assert_equal_i(4, (int)range->old_lines);
-	cl_assert_equal_i(46, (int)range->new_start);
-	cl_assert_equal_i(4, (int)range->new_lines);
+	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);
 
 	cl_assert_equal_i(6, (int)git_patch_num_lines_in_hunk(patch, 0));
 
-	cl_git_pass(git_patch_get_line_in_hunk(
-		&origin, &text, &textlen, &oldno, &newno, patch, 0, 1));
-	cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)origin);
-	cl_git_pass(git_buf_set(&actual, text, textlen));
+	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));
 	cl_assert_equal_s("And the horses and the chariots fleeing from them as of old!\n", actual.ptr);
-	cl_assert_equal_i(47, oldno);
-	cl_assert_equal_i(47, newno);
+	cl_assert_equal_i(47, line->old_lineno);
+	cl_assert_equal_i(47, line->new_lineno);
 
-	cl_git_pass(git_patch_get_line_in_hunk(
-		&origin, &text, &textlen, &oldno, &newno, patch, 0, 2));
-	cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)origin);
-	cl_git_pass(git_buf_set(&actual, text, textlen));
+	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));
 	cl_assert_equal_s("\n", actual.ptr);
-	cl_assert_equal_i(48, oldno);
-	cl_assert_equal_i(48, newno);
+	cl_assert_equal_i(48, line->old_lineno);
+	cl_assert_equal_i(48, line->new_lineno);
 
-	cl_git_pass(git_patch_get_line_in_hunk(
-		&origin, &text, &textlen, &oldno, &newno, patch, 0, 3));
-	cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)origin);
-	cl_git_pass(git_buf_set(&actual, text, textlen));
+	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));
 	cl_assert_equal_s("  -- Rudyard Kipling\n", actual.ptr);
-	cl_assert_equal_i(49, oldno);
-	cl_assert_equal_i(-1, newno);
+	cl_assert_equal_i(49, line->old_lineno);
+	cl_assert_equal_i(-1, line->new_lineno);
 
-	cl_git_pass(git_patch_get_line_in_hunk(
-		&origin, &text, &textlen, &oldno, &newno, patch, 0, 4));
-	cl_assert_equal_i(GIT_DIFF_LINE_ADDITION, (int)origin);
-	cl_git_pass(git_buf_set(&actual, text, textlen));
+	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));
 	cl_assert_equal_s("  -- Rudyard Kipling", actual.ptr);
-	cl_assert_equal_i(-1, oldno);
-	cl_assert_equal_i(49, newno);
+	cl_assert_equal_i(-1, line->old_lineno);
+	cl_assert_equal_i(49, line->new_lineno);
 
-	cl_git_pass(git_patch_get_line_in_hunk(
-		&origin, &text, &textlen, &oldno, &newno, patch, 0, 5));
-	cl_assert_equal_i(GIT_DIFF_LINE_DEL_EOFNL, (int)origin);
-	cl_git_pass(git_buf_set(&actual, text, textlen));
+	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));
 	cl_assert_equal_s("\n\\ No newline at end of file\n", actual.ptr);
-	cl_assert_equal_i(-1, oldno);
-	cl_assert_equal_i(49, newno);
+	cl_assert_equal_i(-1, line->old_lineno);
+	cl_assert_equal_i(49, line->new_lineno);
 
 	git_patch_free(patch);
 	git_diff_free(diff);
@@ -465,31 +447,34 @@ static void check_single_patch_stats(
 	/* walk lines in hunk with basic sanity checks */
 	for (; hunks > 0; --hunks) {
 		size_t i, max_i;
-		int lastoldno = -1, oldno, lastnewno = -1, newno;
-		char origin;
+		const git_diff_line *line;
+		int last_new_lineno = -1, last_old_lineno = -1;
 
 		max_i = git_patch_num_lines_in_hunk(patch, hunks - 1);
 
 		for (i = 0; i < max_i; ++i) {
 			int expected = 1;
 
-			cl_git_pass(git_patch_get_line_in_hunk(
-				&origin, NULL, NULL, &oldno, &newno, patch, hunks - 1, i));
+			cl_git_pass(
+				git_patch_get_line_in_hunk(&line, patch, hunks - 1, i));
 
-			if (origin == GIT_DIFF_LINE_ADD_EOFNL ||
-				origin == GIT_DIFF_LINE_DEL_EOFNL ||
-				origin == GIT_DIFF_LINE_CONTEXT_EOFNL)
+			if (line->origin == GIT_DIFF_LINE_ADD_EOFNL ||
+				line->origin == GIT_DIFF_LINE_DEL_EOFNL ||
+				line->origin == GIT_DIFF_LINE_CONTEXT_EOFNL)
 				expected = 0;
 
-			if (oldno >= 0) {
-				if (lastoldno >= 0)
-					cl_assert_equal_i(expected, oldno - lastoldno);
-				lastoldno = oldno;
+			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;
 			}
-			if (newno >= 0) {
-				if (lastnewno >= 0)
-					cl_assert_equal_i(expected, newno - lastnewno);
-				lastnewno = newno;
+
+			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;
 			}
 		}
 	}
diff --git a/tests-clar/diff/tree.c b/tests-clar/diff/tree.c
index 8231d36..ca2daf5 100644
--- a/tests-clar/diff/tree.c
+++ b/tests-clar/diff/tree.c
@@ -257,11 +257,10 @@ void test_diff_tree__larger_hunks(void)
 {
 	const char *a_commit = "d70d245ed97ed2aa596dd1af6536e4bfdb047b69";
 	const char *b_commit = "7a9e0b02e63179929fed24f0a3e0f19168114d10";
-	size_t d, num_d, h, num_h, l, num_l, header_len, line_len;
+	size_t d, num_d, h, num_h, l, num_l;
 	git_patch *patch;
-	const git_diff_hunk *range;
-	const char *header, *line;
-	char origin;
+	const git_diff_hunk *hunk;
+	const git_diff_line *line;
 
 	g_repo = cl_git_sandbox_init("diff");
 
@@ -280,21 +279,17 @@ void test_diff_tree__larger_hunks(void)
 
 		num_h = git_patch_num_hunks(patch);
 		for (h = 0; h < num_h; h++) {
-			cl_git_pass(git_patch_get_hunk(
-				&range, &header, &header_len, &num_l, patch, h));
+			cl_git_pass(git_patch_get_hunk(&hunk, &num_l, patch, h));
 
 			for (l = 0; l < num_l; ++l) {
-				cl_git_pass(git_patch_get_line_in_hunk(
-					&origin, &line, &line_len, NULL, NULL, patch, h, l));
+				cl_git_pass(git_patch_get_line_in_hunk(&line, patch, h, l));
 				cl_assert(line);
 			}
 
-			cl_git_fail(git_patch_get_line_in_hunk(
-				&origin, &line, &line_len, NULL, NULL, patch, h, num_l));
+			cl_git_fail(git_patch_get_line_in_hunk(&line, patch, h, num_l));
 		}
 
-		cl_git_fail(git_patch_get_hunk(
-			&range, &header, &header_len, &num_l, patch, num_h));
+		cl_git_fail(git_patch_get_hunk(&hunk, &num_l, patch, num_h));
 
 		git_patch_free(patch);
 	}
diff --git a/tests-clar/diff/workdir.c b/tests-clar/diff/workdir.c
index ee63a70..474891c 100644
--- a/tests-clar/diff/workdir.c
+++ b/tests-clar/diff/workdir.c
@@ -673,7 +673,7 @@ void test_diff_workdir__larger_hunks(void)
 	const char *b_commit = "7a9e0b02e63179929fed24f0a3e0f19168114d10";
 	git_tree *a, *b;
 	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
-	size_t i, d, num_d, h, num_h, l, num_l, header_len, line_len;
+	size_t i, d, num_d, h, num_h, l, num_l;
 
 	g_repo = cl_git_sandbox_init("diff");
 
@@ -686,9 +686,8 @@ void test_diff_workdir__larger_hunks(void)
 	for (i = 0; i <= 2; ++i) {
 		git_diff *diff = NULL;
 		git_patch *patch;
-		const git_diff_hunk *range;
-		const char *header, *line;
-		char origin;
+		const git_diff_hunk *hunk;
+		const git_diff_line *line;
 
 		/* okay, this is a bit silly, but oh well */
 		switch (i) {
@@ -712,23 +711,21 @@ void test_diff_workdir__larger_hunks(void)
 
 			num_h = git_patch_num_hunks(patch);
 			for (h = 0; h < num_h; h++) {
-				cl_git_pass(git_patch_get_hunk(
-					&range, &header, &header_len, &num_l, patch, h));
+				cl_git_pass(git_patch_get_hunk(&hunk, &num_l, patch, h));
 
 				for (l = 0; l < num_l; ++l) {
-					cl_git_pass(git_patch_get_line_in_hunk(
-						&origin, &line, &line_len, NULL, NULL, patch, h, l));
+					cl_git_pass(
+						git_patch_get_line_in_hunk(&line, patch, h, l));
 					cl_assert(line);
 				}
 
 				/* confirm fail after the last item */
-				cl_git_fail(git_patch_get_line_in_hunk(
-					&origin, &line, &line_len, NULL, NULL, patch, h, num_l));
+				cl_git_fail(
+					git_patch_get_line_in_hunk(&line, patch, h, num_l));
 			}
 
 			/* confirm fail after the last item */
-			cl_git_fail(git_patch_get_hunk(
-				&range, &header, &header_len, &num_l, patch, num_h));
+			cl_git_fail(git_patch_get_hunk(&hunk, &num_l, patch, num_h));
 
 			git_patch_free(patch);
 		}