All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Johannes Schindelin <johannes.schindelin@gmx.de>,
	Johannes Schindelin <johannes.schindelin@gmx.de>
Subject: [PATCH v4 02/10] built-in add -p: support interactive.diffFilter
Date: Tue, 14 Jan 2020 18:43:45 +0000	[thread overview]
Message-ID: <413a87bd798296844ebec0c1aad579044da56194.1579027433.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.175.v4.git.1579027433.gitgitgadget@gmail.com>

From: Johannes Schindelin <johannes.schindelin@gmx.de>

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>
---
 add-interactive.c | 12 ++++++++++++
 add-interactive.h |  3 +++
 add-patch.c       | 33 +++++++++++++++++++++++++++++++++
 3 files changed, 48 insertions(+)

diff --git a/add-interactive.c b/add-interactive.c
index a5bb14f2f4..1786ea29c4 100644
--- a/add-interactive.c
+++ b/add-interactive.c
@@ -52,6 +52,17 @@ void init_add_i_state(struct add_i_state *s, struct repository *r)
 		diff_get_color(s->use_color, DIFF_FILE_OLD));
 	init_color(r, s, "new", s->file_new_color,
 		diff_get_color(s->use_color, DIFF_FILE_NEW));
+
+	FREE_AND_NULL(s->interactive_diff_filter);
+	git_config_get_string("interactive.difffilter",
+			      &s->interactive_diff_filter);
+}
+
+void clear_add_i_state(struct add_i_state *s)
+{
+	FREE_AND_NULL(s->interactive_diff_filter);
+	memset(s, 0, sizeof(*s));
+	s->use_color = -1;
 }
 
 /*
@@ -1149,6 +1160,7 @@ int run_add_i(struct repository *r, const struct pathspec *ps)
 	strbuf_release(&print_file_item_data.worktree);
 	strbuf_release(&header);
 	prefix_item_list_clear(&commands);
+	clear_add_i_state(&s);
 
 	return res;
 }
diff --git a/add-interactive.h b/add-interactive.h
index b2f23479c5..46c73867ad 100644
--- a/add-interactive.h
+++ b/add-interactive.h
@@ -15,9 +15,12 @@ struct add_i_state {
 	char context_color[COLOR_MAXLEN];
 	char file_old_color[COLOR_MAXLEN];
 	char file_new_color[COLOR_MAXLEN];
+
+	char *interactive_diff_filter;
 };
 
 void init_add_i_state(struct add_i_state *s, struct repository *r);
+void clear_add_i_state(struct add_i_state *s);
 
 struct repository;
 struct pathspec;
diff --git a/add-patch.c b/add-patch.c
index 46c6c183d5..78bde41df0 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;
 }
-- 
gitgitgadget


  parent reply	other threads:[~2020-01-14 18:44 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-21 22:41 [PATCH 0/9] built-in add -p: add support for the same config settings as the Perl version Johannes Schindelin via GitGitGadget
2019-12-21 22:41 ` [PATCH 1/9] built-in add -p: support interactive.diffFilter Johannes Schindelin via GitGitGadget
2019-12-21 22:41 ` [PATCH 2/9] built-in add -p: handle diff.algorithm Johannes Schindelin via GitGitGadget
2019-12-21 22:41 ` [PATCH 3/9] terminal: make the code of disable_echo() reusable Johannes Schindelin via GitGitGadget
2019-12-21 22:41 ` [PATCH 4/9] terminal: accommodate Git for Windows' default terminal Johannes Schindelin via GitGitGadget
2019-12-21 22:41 ` [PATCH 5/9] terminal: add a new function to read a single keystroke Johannes Schindelin via GitGitGadget
2019-12-21 22:41 ` [PATCH 6/9] built-in add -p: respect the `interactive.singlekey` config setting Johannes Schindelin via GitGitGadget
2019-12-21 22:41 ` [PATCH 7/9] built-in add -p: handle Escape sequences in interactive.singlekey mode Johannes Schindelin via GitGitGadget
2019-12-21 22:41 ` [PATCH 8/9] built-in add -p: handle Escape sequences more efficiently Johannes Schindelin via GitGitGadget
2019-12-21 22:42 ` [PATCH 9/9] ci: include the built-in `git add -i` in the `linux-gcc` job Johannes Schindelin via GitGitGadget
2019-12-21 22:53   ` SZEDER Gábor
2019-12-25 11:56     ` Johannes Schindelin
2019-12-22  0:11   ` Junio C Hamano
2019-12-25 11:57     ` Johannes Schindelin
2019-12-24 18:23 ` [PATCH 0/9] built-in add -p: add support for the same config settings as the Perl version Junio C Hamano
2019-12-24 18:39   ` Junio C Hamano
2019-12-25  8:46     ` Simon Ruderich
2019-12-25 12:09     ` Johannes Schindelin
2019-12-25 12:02   ` Johannes Schindelin
2019-12-25 11:56 ` [PATCH v2 " Johannes Schindelin via GitGitGadget
2019-12-25 11:56   ` [PATCH v2 1/9] built-in add -p: support interactive.diffFilter Johannes Schindelin via GitGitGadget
2020-01-07 22:57     ` SZEDER Gábor
2020-01-13  6:47       ` Johannes Schindelin
2019-12-25 11:56   ` [PATCH v2 2/9] built-in add -p: handle diff.algorithm Johannes Schindelin via GitGitGadget
2019-12-25 11:56   ` [PATCH v2 3/9] terminal: make the code of disable_echo() reusable Johannes Schindelin via GitGitGadget
2019-12-25 11:56   ` [PATCH v2 4/9] terminal: accommodate Git for Windows' default terminal Johannes Schindelin via GitGitGadget
2019-12-25 11:56   ` [PATCH v2 5/9] terminal: add a new function to read a single keystroke Johannes Schindelin via GitGitGadget
2019-12-25 11:56   ` [PATCH v2 6/9] built-in add -p: respect the `interactive.singlekey` config setting Johannes Schindelin via GitGitGadget
2019-12-25 11:56   ` [PATCH v2 7/9] built-in add -p: handle Escape sequences in interactive.singlekey mode Johannes Schindelin via GitGitGadget
2019-12-25 11:56   ` [PATCH v2 8/9] built-in add -p: handle Escape sequences more efficiently Johannes Schindelin via GitGitGadget
2019-12-25 11:57   ` [PATCH v2 9/9] ci: include the built-in `git add -i` in the `linux-gcc` job Johannes Schindelin via GitGitGadget
2019-12-26 20:48     ` Derrick Stolee
2020-01-01 22:10       ` Johannes Schindelin
2019-12-26 20:45   ` [PATCH v2 0/9] built-in add -p: add support for the same config settings as the Perl version Junio C Hamano
2020-01-13  8:29   ` [PATCH v3 00/10] " Johannes Schindelin via GitGitGadget
2020-01-13  8:29     ` [PATCH v3 01/10] built-in add -i/-p: treat SIGPIPE as EOF Johannes Schindelin via GitGitGadget
2020-01-13 17:04       ` SZEDER Gábor
2020-01-13 18:33         ` Jeff King
2020-01-15 18:32           ` Junio C Hamano
2020-01-15 19:03             ` Jeff King
2020-01-14 12:47         ` Johannes Schindelin
2020-01-17 14:32         ` SZEDER Gábor
2020-01-17 18:58           ` Jeff King
2020-01-13  8:29     ` [PATCH v3 02/10] built-in add -p: support interactive.diffFilter Johannes Schindelin via GitGitGadget
2020-01-13  8:29     ` [PATCH v3 03/10] built-in add -p: handle diff.algorithm Johannes Schindelin via GitGitGadget
2020-01-13  8:29     ` [PATCH v3 04/10] terminal: make the code of disable_echo() reusable Johannes Schindelin via GitGitGadget
2020-01-13  8:29     ` [PATCH v3 05/10] terminal: accommodate Git for Windows' default terminal Johannes Schindelin via GitGitGadget
2020-01-13  8:29     ` [PATCH v3 06/10] terminal: add a new function to read a single keystroke Johannes Schindelin via GitGitGadget
2020-01-13  8:29     ` [PATCH v3 07/10] built-in add -p: respect the `interactive.singlekey` config setting Johannes Schindelin via GitGitGadget
2020-01-13  8:29     ` [PATCH v3 08/10] built-in add -p: handle Escape sequences in interactive.singlekey mode Johannes Schindelin via GitGitGadget
2020-01-13  8:29     ` [PATCH v3 09/10] built-in add -p: handle Escape sequences more efficiently Johannes Schindelin via GitGitGadget
2020-01-13  8:29     ` [PATCH v3 10/10] ci: include the built-in `git add -i` in the `linux-gcc` job Johannes Schindelin via GitGitGadget
2020-01-14 18:43     ` [PATCH v4 00/10] built-in add -p: add support for the same config settings as the Perl version Johannes Schindelin via GitGitGadget
2020-01-14 18:43       ` [PATCH v4 01/10] t3701: adjust difffilter test Johannes Schindelin via GitGitGadget
2020-01-14 18:43       ` Johannes Schindelin via GitGitGadget [this message]
2020-01-14 18:43       ` [PATCH v4 03/10] built-in add -p: handle diff.algorithm Johannes Schindelin via GitGitGadget
2020-01-14 18:43       ` [PATCH v4 04/10] terminal: make the code of disable_echo() reusable Johannes Schindelin via GitGitGadget
2020-01-14 18:43       ` [PATCH v4 05/10] terminal: accommodate Git for Windows' default terminal Johannes Schindelin via GitGitGadget
2020-01-14 18:43       ` [PATCH v4 06/10] terminal: add a new function to read a single keystroke Johannes Schindelin via GitGitGadget
2020-01-14 18:43       ` [PATCH v4 07/10] built-in add -p: respect the `interactive.singlekey` config setting Johannes Schindelin via GitGitGadget
2020-01-14 18:43       ` [PATCH v4 08/10] built-in add -p: handle Escape sequences in interactive.singlekey mode Johannes Schindelin via GitGitGadget
2020-01-14 18:43       ` [PATCH v4 09/10] built-in add -p: handle Escape sequences more efficiently Johannes Schindelin via GitGitGadget
2020-01-14 18:43       ` [PATCH v4 10/10] ci: include the built-in `git add -i` in the `linux-gcc` job Johannes Schindelin via GitGitGadget

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=413a87bd798296844ebec0c1aad579044da56194.1579027433.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=johannes.schindelin@gmx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.