summaryrefslogtreecommitdiff
path: root/add-patch.c
diff options
context:
space:
mode:
authorJohannes Schindelin <johannes.schindelin@gmx.de>2020-01-14 18:43:45 (GMT)
committerJunio C Hamano <gitster@pobox.com>2020-01-15 20:06:16 (GMT)
commit180f48df69d8e7a1a413d7c11907ecf975f09cf7 (patch)
treefc919d689fdeaac5aaac15fa0a8470c6e5e6eaa4 /add-patch.c
parent1e4ffc765db69c1c2061452c122caf17c7fb25f3 (diff)
downloadgit-180f48df69d8e7a1a413d7c11907ecf975f09cf7.zip
git-180f48df69d8e7a1a413d7c11907ecf975f09cf7.tar.gz
git-180f48df69d8e7a1a413d7c11907ecf975f09cf7.tar.bz2
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 <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'add-patch.c')
-rw-r--r--add-patch.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/add-patch.c b/add-patch.c
index 46c6c18..78bde41 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -398,6 +398,7 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
if (want_color_fd(1, -1)) {
struct child_process colored_cp = CHILD_PROCESS_INIT;
+ const char *diff_filter = s->s.interactive_diff_filter;
setup_child_process(s, &colored_cp, NULL);
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)
argv_array_clear(&args);
if (res)
return error(_("could not parse colored diff"));
+
+ if (diff_filter) {
+ struct child_process filter_cp = CHILD_PROCESS_INIT;
+
+ setup_child_process(s, &filter_cp,
+ diff_filter, NULL);
+ filter_cp.git_cmd = 0;
+ filter_cp.use_shell = 1;
+ strbuf_reset(&s->buf);
+ if (pipe_command(&filter_cp,
+ colored->buf, colored->len,
+ &s->buf, colored->len,
+ NULL, 0) < 0)
+ return error(_("failed to run '%s'"),
+ diff_filter);
+ strbuf_swap(colored, &s->buf);
+ }
+
strbuf_complete_line(colored);
colored_p = colored->buf;
colored_pend = colored_p + colored->len;
@@ -531,6 +550,9 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
colored_pend - colored_p);
if (colored_eol)
colored_p = colored_eol + 1;
+ else if (p != pend)
+ /* colored shorter than non-colored? */
+ goto mismatched_output;
else
colored_p = colored_pend;
@@ -555,6 +577,15 @@ static int parse_diff(struct add_p_state *s, const struct pathspec *ps)
*/
hunk->splittable_into++;
+ /* non-colored shorter than colored? */
+ if (colored_p != colored_pend) {
+mismatched_output:
+ error(_("mismatched output from interactive.diffFilter"));
+ advise(_("Your filter must maintain a one-to-one correspondence\n"
+ "between its input and output lines."));
+ return -1;
+ }
+
return 0;
}
@@ -1612,6 +1643,7 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
parse_diff(&s, ps) < 0) {
strbuf_release(&s.plain);
strbuf_release(&s.colored);
+ clear_add_i_state(&s.s);
return -1;
}
@@ -1630,5 +1662,6 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
strbuf_release(&s.buf);
strbuf_release(&s.plain);
strbuf_release(&s.colored);
+ clear_add_i_state(&s.s);
return 0;
}