@@ -398,6 +398,7 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
398
398
399
399
if (want_color_fd (1 , -1 )) {
400
400
struct child_process colored_cp = CHILD_PROCESS_INIT ;
401
+ const char * diff_filter = s -> s .interactive_diff_filter ;
401
402
402
403
setup_child_process (s , & colored_cp , NULL );
403
404
xsnprintf ((char * )args .argv [color_arg_index ], 8 , "--color" );
@@ -407,6 +408,24 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
407
408
argv_array_clear (& args );
408
409
if (res )
409
410
return error (_ ("could not parse colored diff" ));
411
+
412
+ if (diff_filter ) {
413
+ struct child_process filter_cp = CHILD_PROCESS_INIT ;
414
+
415
+ setup_child_process (s , & filter_cp ,
416
+ diff_filter , NULL );
417
+ filter_cp .git_cmd = 0 ;
418
+ filter_cp .use_shell = 1 ;
419
+ strbuf_reset (& s -> buf );
420
+ if (pipe_command (& filter_cp ,
421
+ colored -> buf , colored -> len ,
422
+ & s -> buf , colored -> len ,
423
+ NULL , 0 ) < 0 )
424
+ return error (_ ("failed to run '%s'" ),
425
+ diff_filter );
426
+ strbuf_swap (colored , & s -> buf );
427
+ }
428
+
410
429
strbuf_complete_line (colored );
411
430
colored_p = colored -> buf ;
412
431
colored_pend = colored_p + colored -> len ;
@@ -531,6 +550,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
531
550
colored_pend - colored_p );
532
551
if (colored_eol )
533
552
colored_p = colored_eol + 1 ;
553
+ else if (p != pend )
554
+ /* colored shorter than non-colored? */
555
+ goto mismatched_output ;
534
556
else
535
557
colored_p = colored_pend ;
536
558
@@ -555,6 +577,15 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
555
577
*/
556
578
hunk -> splittable_into ++ ;
557
579
580
+ /* non-colored shorter than colored? */
581
+ if (colored_p != colored_pend ) {
582
+ mismatched_output :
583
+ error (_ ("mismatched output from interactive.diffFilter" ));
584
+ advise (_ ("Your filter must maintain a one-to-one correspondence\n"
585
+ "between its input and output lines." ));
586
+ return -1 ;
587
+ }
588
+
558
589
return 0 ;
559
590
}
560
591
@@ -1612,6 +1643,7 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
1612
1643
parse_diff (& s , ps ) < 0 ) {
1613
1644
strbuf_release (& s .plain );
1614
1645
strbuf_release (& s .colored );
1646
+ clear_add_i_state (& s .s );
1615
1647
return -1 ;
1616
1648
}
1617
1649
@@ -1630,5 +1662,6 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
1630
1662
strbuf_release (& s .buf );
1631
1663
strbuf_release (& s .plain );
1632
1664
strbuf_release (& s .colored );
1665
+ clear_add_i_state (& s .s );
1633
1666
return 0 ;
1634
1667
}
0 commit comments