Skip to content

Commit 180f48d

Browse files
dschogitster
authored andcommitted
built-in add -p: support interactive.diffFilter
The Perl version supports post-processing the colored diff (that is generated in addition to the uncolored diff, intended to offer a prettier user experience) by a command configured via that config setting, and now the built-in version does that, too. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1e4ffc7 commit 180f48d

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

add-interactive.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ void init_add_i_state(struct add_i_state *s, struct repository *r)
5252
diff_get_color(s->use_color, DIFF_FILE_OLD));
5353
init_color(r, s, "new", s->file_new_color,
5454
diff_get_color(s->use_color, DIFF_FILE_NEW));
55+
56+
FREE_AND_NULL(s->interactive_diff_filter);
57+
git_config_get_string("interactive.difffilter",
58+
&s->interactive_diff_filter);
59+
}
60+
61+
void clear_add_i_state(struct add_i_state *s)
62+
{
63+
FREE_AND_NULL(s->interactive_diff_filter);
64+
memset(s, 0, sizeof(*s));
65+
s->use_color = -1;
5566
}
5667

5768
/*
@@ -1149,6 +1160,7 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
11491160
strbuf_release(&print_file_item_data.worktree);
11501161
strbuf_release(&header);
11511162
prefix_item_list_clear(&commands);
1163+
clear_add_i_state(&s);
11521164

11531165
return res;
11541166
}

add-interactive.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ struct add_i_state {
1515
char context_color[COLOR_MAXLEN];
1616
char file_old_color[COLOR_MAXLEN];
1717
char file_new_color[COLOR_MAXLEN];
18+
19+
char *interactive_diff_filter;
1820
};
1921

2022
void init_add_i_state(struct add_i_state *s, struct repository *r);
23+
void clear_add_i_state(struct add_i_state *s);
2124

2225
struct repository;
2326
struct pathspec;

add-patch.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
398398

399399
if (want_color_fd(1, -1)) {
400400
struct child_process colored_cp = CHILD_PROCESS_INIT;
401+
const char *diff_filter = s->s.interactive_diff_filter;
401402

402403
setup_child_process(s, &colored_cp, NULL);
403404
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)
407408
argv_array_clear(&args);
408409
if (res)
409410
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+
410429
strbuf_complete_line(colored);
411430
colored_p = colored->buf;
412431
colored_pend = colored_p + colored->len;
@@ -531,6 +550,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
531550
colored_pend - colored_p);
532551
if (colored_eol)
533552
colored_p = colored_eol + 1;
553+
else if (p != pend)
554+
/* colored shorter than non-colored? */
555+
goto mismatched_output;
534556
else
535557
colored_p = colored_pend;
536558

@@ -555,6 +577,15 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
555577
*/
556578
hunk->splittable_into++;
557579

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+
558589
return 0;
559590
}
560591

@@ -1612,6 +1643,7 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
16121643
parse_diff(&s, ps) < 0) {
16131644
strbuf_release(&s.plain);
16141645
strbuf_release(&s.colored);
1646+
clear_add_i_state(&s.s);
16151647
return -1;
16161648
}
16171649

@@ -1630,5 +1662,6 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
16301662
strbuf_release(&s.buf);
16311663
strbuf_release(&s.plain);
16321664
strbuf_release(&s.colored);
1665+
clear_add_i_state(&s.s);
16331666
return 0;
16341667
}

0 commit comments

Comments
 (0)