@@ -402,6 +402,12 @@ static void complete_file(char marker, struct hunk *hunk)
402
402
hunk -> splittable_into ++ ;
403
403
}
404
404
405
+ /* Empty context lines may omit the leading ' ' */
406
+ static int normalize_marker (const char * p )
407
+ {
408
+ return p [0 ] == '\n' || (p [0 ] == '\r' && p [1 ] == '\n' ) ? ' ' : p [0 ];
409
+ }
410
+
405
411
static int parse_diff (struct add_p_state * s , const struct pathspec * ps )
406
412
{
407
413
struct strvec args = STRVEC_INIT ;
@@ -487,6 +493,7 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
487
493
while (p != pend ) {
488
494
char * eol = memchr (p , '\n' , pend - p );
489
495
const char * deleted = NULL , * mode_change = NULL ;
496
+ char ch = normalize_marker (p );
490
497
491
498
if (!eol )
492
499
eol = pend ;
@@ -534,7 +541,7 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
534
541
* Start counting into how many hunks this one can be
535
542
* split
536
543
*/
537
- marker = * p ;
544
+ marker = ch ;
538
545
} else if (hunk == & file_diff -> head &&
539
546
starts_with (p , "new file" )) {
540
547
file_diff -> added = 1 ;
@@ -588,10 +595,10 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
588
595
(int )(eol - (plain -> buf + file_diff -> head .start )),
589
596
plain -> buf + file_diff -> head .start );
590
597
591
- if ((marker == '-' || marker == '+' ) && * p == ' ' )
598
+ if ((marker == '-' || marker == '+' ) && ch == ' ' )
592
599
hunk -> splittable_into ++ ;
593
- if (marker && * p != '\\' )
594
- marker = * p ;
600
+ if (marker && ch != '\\' )
601
+ marker = ch ;
595
602
596
603
p = eol == pend ? pend : eol + 1 ;
597
604
hunk -> end = p - plain -> buf ;
@@ -815,7 +822,7 @@ static int merge_hunks(struct add_p_state *s, struct file_diff *file_diff,
815
822
(int )(hunk -> end - hunk -> start ),
816
823
plain + hunk -> start );
817
824
818
- if (plain [overlap_end ] != ' ' )
825
+ if (normalize_marker ( & plain [overlap_end ]) != ' ' )
819
826
return error (_ ("expected context line "
820
827
"#%d in\n%.*s" ),
821
828
(int )(j + 1 ),
@@ -955,7 +962,7 @@ static int split_hunk(struct add_p_state *s, struct file_diff *file_diff,
955
962
context_line_count = 0 ;
956
963
957
964
while (splittable_into > 1 ) {
958
- ch = s -> plain .buf [current ];
965
+ ch = normalize_marker ( & s -> plain .buf [current ]) ;
959
966
960
967
if (!ch )
961
968
BUG ("buffer overrun while splitting hunks" );
@@ -1173,14 +1180,14 @@ static ssize_t recount_edited_hunk(struct add_p_state *s, struct hunk *hunk,
1173
1180
1174
1181
header -> old_count = header -> new_count = 0 ;
1175
1182
for (i = hunk -> start ; i < hunk -> end ; ) {
1176
- switch ( s -> plain .buf [i ]) {
1183
+ switch ( normalize_marker ( & s -> plain .buf [i ]) ) {
1177
1184
case '-' :
1178
1185
header -> old_count ++ ;
1179
1186
break ;
1180
1187
case '+' :
1181
1188
header -> new_count ++ ;
1182
1189
break ;
1183
- case ' ' : case '\r' : case '\n' :
1190
+ case ' ' :
1184
1191
header -> old_count ++ ;
1185
1192
header -> new_count ++ ;
1186
1193
break ;
0 commit comments