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
@@ -309,6 +310,7 @@ static int is_octal(const char *p, size_t len)
309
310
static int parse_diff (struct add_p_state * s , const struct pathspec * ps )
310
311
{
311
312
struct argv_array args = ARGV_ARRAY_INIT ;
313
+ const char * diff_algorithm = s -> s .interactive_diff_algorithm ;
312
314
struct strbuf * plain = & s -> plain , * colored = NULL ;
313
315
struct child_process cp = CHILD_PROCESS_INIT ;
314
316
char * p , * pend , * colored_p = NULL , * colored_pend = NULL , marker = '\0' ;
@@ -318,6 +320,8 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
318
320
int res ;
319
321
320
322
argv_array_pushv (& args , s -> mode -> diff );
323
+ if (diff_algorithm )
324
+ argv_array_pushf (& args , "--diff-algorithm=%s" , diff_algorithm );
321
325
if (s -> revision ) {
322
326
struct object_id oid ;
323
327
argv_array_push (& args ,
@@ -347,6 +351,7 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
347
351
348
352
if (want_color_fd (1 , -1 )) {
349
353
struct child_process colored_cp = CHILD_PROCESS_INIT ;
354
+ const char * diff_filter = s -> s .interactive_diff_filter ;
350
355
351
356
setup_child_process (& colored_cp , s , NULL );
352
357
xsnprintf ((char * )args .argv [color_arg_index ], 8 , "--color" );
@@ -356,6 +361,24 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
356
361
argv_array_clear (& args );
357
362
if (res )
358
363
return error (_ ("could not parse colored diff" ));
364
+
365
+ if (diff_filter ) {
366
+ struct child_process filter_cp = CHILD_PROCESS_INIT ;
367
+
368
+ setup_child_process (& filter_cp , s ,
369
+ diff_filter , NULL );
370
+ filter_cp .git_cmd = 0 ;
371
+ filter_cp .use_shell = 1 ;
372
+ strbuf_reset (& s -> buf );
373
+ if (pipe_command (& filter_cp ,
374
+ colored -> buf , colored -> len ,
375
+ & s -> buf , colored -> len ,
376
+ NULL , 0 ) < 0 )
377
+ return error (_ ("failed to run '%s'" ),
378
+ diff_filter );
379
+ strbuf_swap (colored , & s -> buf );
380
+ }
381
+
359
382
strbuf_complete_line (colored );
360
383
colored_p = colored -> buf ;
361
384
colored_pend = colored_p + colored -> len ;
@@ -457,6 +480,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
457
480
colored_pend - colored_p );
458
481
if (colored_eol )
459
482
colored_p = colored_eol + 1 ;
483
+ else if (p != pend )
484
+ /* colored shorter than non-colored? */
485
+ goto mismatched_output ;
460
486
else
461
487
colored_p = colored_pend ;
462
488
@@ -478,6 +504,15 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
478
504
*/
479
505
hunk -> splittable_into ++ ;
480
506
507
+ /* non-colored shorter than colored? */
508
+ if (colored_p != colored_pend ) {
509
+ mismatched_output :
510
+ error (_ ("mismatched output from interactive.diffFilter" ));
511
+ advise (_ ("Your filter must maintain a one-to-one correspondence\n"
512
+ "between its input and output lines." ));
513
+ return -1 ;
514
+ }
515
+
481
516
return 0 ;
482
517
}
483
518
@@ -1012,14 +1047,27 @@ static int run_apply_check(struct add_p_state *s,
1012
1047
return 0 ;
1013
1048
}
1014
1049
1050
+ static int read_single_character (struct add_p_state * s )
1051
+ {
1052
+ if (s -> s .use_single_key ) {
1053
+ int res = read_key_without_echo (& s -> answer );
1054
+ printf ("%s\n" , res == EOF ? "" : s -> answer .buf );
1055
+ return res ;
1056
+ }
1057
+
1058
+ if (strbuf_getline (& s -> answer , stdin ) == EOF )
1059
+ return EOF ;
1060
+ strbuf_trim_trailing_newline (& s -> answer );
1061
+ return 0 ;
1062
+ }
1063
+
1015
1064
static int prompt_yesno (struct add_p_state * s , const char * prompt )
1016
1065
{
1017
1066
for (;;) {
1018
1067
color_fprintf (stdout , s -> s .prompt_color , "%s" , _ (prompt ));
1019
1068
fflush (stdout );
1020
- if (strbuf_getline ( & s -> answer , stdin ) == EOF )
1069
+ if (read_single_character ( s ) == EOF )
1021
1070
return -1 ;
1022
- strbuf_trim_trailing_newline (& s -> answer );
1023
1071
switch (tolower (s -> answer .buf [0 ])) {
1024
1072
case 'n' : return 0 ;
1025
1073
case 'y' : return 1 ;
@@ -1255,9 +1303,8 @@ static int patch_update_file(struct add_p_state *s,
1255
1303
_ (s -> mode -> prompt_mode [prompt_mode_type ]),
1256
1304
s -> buf .buf );
1257
1305
fflush (stdout );
1258
- if (strbuf_getline ( & s -> answer , stdin ) == EOF )
1306
+ if (read_single_character ( s ) == EOF )
1259
1307
break ;
1260
- strbuf_trim_trailing_newline (& s -> answer );
1261
1308
1262
1309
if (!s -> answer .len )
1263
1310
continue ;
0 commit comments