6
6
#include "pathspec.h"
7
7
#include "color.h"
8
8
#include "diff.h"
9
+ #include "compat/terminal.h"
9
10
10
11
enum prompt_mode_type {
11
12
PROMPT_MODE_CHANGE = 0 , PROMPT_DELETION , PROMPT_HUNK
@@ -354,6 +355,7 @@ static int is_octal(const char *p, size_t len)
354
355
static int parse_diff (struct add_p_state * s , const struct pathspec * ps )
355
356
{
356
357
struct argv_array args = ARGV_ARRAY_INIT ;
358
+ const char * diff_algorithm = s -> s .interactive_diff_algorithm ;
357
359
struct strbuf * plain = & s -> plain , * colored = NULL ;
358
360
struct child_process cp = CHILD_PROCESS_INIT ;
359
361
char * p , * pend , * colored_p = NULL , * colored_pend = NULL , marker = '\0' ;
@@ -363,6 +365,8 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
363
365
int res ;
364
366
365
367
argv_array_pushv (& args , s -> mode -> diff );
368
+ if (diff_algorithm )
369
+ argv_array_pushf (& args , "--diff-algorithm=%s" , diff_algorithm );
366
370
if (s -> revision ) {
367
371
struct object_id oid ;
368
372
argv_array_push (& args ,
@@ -392,6 +396,7 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
392
396
393
397
if (want_color_fd (1 , -1 )) {
394
398
struct child_process colored_cp = CHILD_PROCESS_INIT ;
399
+ const char * diff_filter = s -> s .interactive_diff_filter ;
395
400
396
401
setup_child_process (& colored_cp , s , NULL );
397
402
xsnprintf ((char * )args .argv [color_arg_index ], 8 , "--color" );
@@ -401,6 +406,24 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
401
406
argv_array_clear (& args );
402
407
if (res )
403
408
return error (_ ("could not parse colored diff" ));
409
+
410
+ if (diff_filter ) {
411
+ struct child_process filter_cp = CHILD_PROCESS_INIT ;
412
+
413
+ setup_child_process (& filter_cp , s ,
414
+ diff_filter , NULL );
415
+ filter_cp .git_cmd = 0 ;
416
+ filter_cp .use_shell = 1 ;
417
+ strbuf_reset (& s -> buf );
418
+ if (pipe_command (& filter_cp ,
419
+ colored -> buf , colored -> len ,
420
+ & s -> buf , colored -> len ,
421
+ NULL , 0 ) < 0 )
422
+ return error (_ ("failed to run '%s'" ),
423
+ diff_filter );
424
+ strbuf_swap (colored , & s -> buf );
425
+ }
426
+
404
427
strbuf_complete_line (colored );
405
428
colored_p = colored -> buf ;
406
429
colored_pend = colored_p + colored -> len ;
@@ -502,6 +525,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
502
525
colored_pend - colored_p );
503
526
if (colored_eol )
504
527
colored_p = colored_eol + 1 ;
528
+ else if (p != pend )
529
+ /* colored shorter than non-colored? */
530
+ goto mismatched_output ;
505
531
else
506
532
colored_p = colored_pend ;
507
533
@@ -523,6 +549,15 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
523
549
*/
524
550
hunk -> splittable_into ++ ;
525
551
552
+ /* non-colored shorter than colored? */
553
+ if (colored_p != colored_pend ) {
554
+ mismatched_output :
555
+ error (_ ("mismatched output from interactive.diffFilter" ));
556
+ advise (_ ("Your filter must maintain a one-to-one correspondence\n"
557
+ "between its input and output lines." ));
558
+ return -1 ;
559
+ }
560
+
526
561
return 0 ;
527
562
}
528
563
@@ -1063,14 +1098,27 @@ static int run_apply_check(struct add_p_state *s,
1063
1098
return 0 ;
1064
1099
}
1065
1100
1101
+ static int read_single_character (struct add_p_state * s )
1102
+ {
1103
+ if (s -> s .use_single_key ) {
1104
+ int res = read_key_without_echo (& s -> answer );
1105
+ printf ("%s\n" , res == EOF ? "" : s -> answer .buf );
1106
+ return res ;
1107
+ }
1108
+
1109
+ if (strbuf_getline (& s -> answer , stdin ) == EOF )
1110
+ return EOF ;
1111
+ strbuf_trim_trailing_newline (& s -> answer );
1112
+ return 0 ;
1113
+ }
1114
+
1066
1115
static int prompt_yesno (struct add_p_state * s , const char * prompt )
1067
1116
{
1068
1117
for (;;) {
1069
1118
color_fprintf (stdout , s -> s .prompt_color , "%s" , _ (prompt ));
1070
1119
fflush (stdout );
1071
- if (strbuf_getline ( & s -> answer , stdin ) == EOF )
1120
+ if (read_single_character ( s ) == EOF )
1072
1121
return -1 ;
1073
- strbuf_trim_trailing_newline (& s -> answer );
1074
1122
switch (tolower (s -> answer .buf [0 ])) {
1075
1123
case 'n' : return 0 ;
1076
1124
case 'y' : return 1 ;
@@ -1306,9 +1354,8 @@ static int patch_update_file(struct add_p_state *s,
1306
1354
_ (s -> mode -> prompt_mode [prompt_mode_type ]),
1307
1355
s -> buf .buf );
1308
1356
fflush (stdout );
1309
- if (strbuf_getline ( & s -> answer , stdin ) == EOF )
1357
+ if (read_single_character ( s ) == EOF )
1310
1358
break ;
1311
- strbuf_trim_trailing_newline (& s -> answer );
1312
1359
1313
1360
if (!s -> answer .len )
1314
1361
continue ;
0 commit comments