git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2 1/9] built-in add -p: support interactive.diffFilter

From
SZEDER Gábor <szeder.dev@gmail.com>
Date
Jan 7, 2020, 22:57 UTC
Message-ID
<20200107225749.GD32750@szeder.dev>
In-Reply-To
<f45ff08bd0a0a2e2aba9ae929b6e5ecb3bdd4e07.1577275020.git.gitgitgadget@gmail.com>
On Wed, Dec 25, 2019 at 11:56:52AM +0000, Johannes Schindelin via GitGitGadget wrote:
> 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.

So this patch makes the test 'detect bogus diffFilter output' in 't3701-add-interactive.sh' succeed with the builtin interactive add, but I stumbled upon a test failure caused by SIGPIPE in an experimental Travis CI s390x build:

  expecting success of 3701.49 'detect bogus diffFilter output': 
          git reset --hard &&
  
          echo content >test &&
          test_config interactive.diffFilter "echo too-short" &&
          printf y >y &&
          test_must_fail force_color git add -p <y
  
  + git reset --hard
  HEAD is now at 6ee5ee5 test
  + echo content
  + test_config interactive.diffFilter echo too-short
  + printf y
  + test_must_fail force_color git add -p
  test_must_fail: died by signal 13: force_color git add -p
  error: last command exited with $?=1
Turns out it's a general issue, and
  GIT_TEST_ADD_I_USE_BUILTIN=1 ./t3701-add-interactive.sh -r 39,49 --stress

fails within 10 seconds on my Linux box, whereas the scripted 'add -p' managed to survive a couple hundred repetitions.

Show 135 quoted lines
> 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
> 
Previous: Johannes Schindelin via GitGitGadgetNext: Johannes Schindelin
Message 22 of 63 in “built-in add -p: add support for the same config settings as the Perl version”
  1. 0/9 built-in add -p: add support for the same config settings as the Perl versionJohannes Schindelin via GitGitGadget, Dec 21, 2019
  2. 1/9 built-in add -p: support interactive.diffFilterJohannes Schindelin via GitGitGadget, Dec 21, 2019
  3. 2/9 built-in add -p: handle diff.algorithmJohannes Schindelin via GitGitGadget, Dec 21, 2019
  4. 6/9 built-in add -p: respect the `interactive.singlekey` config settingJohannes Schindelin via GitGitGadget, Dec 21, 2019
  5. 8/9 built-in add -p: handle Escape sequences more efficientlyJohannes Schindelin via GitGitGadget, Dec 21, 2019
  6. 4/9 terminal: accommodate Git for Windows' default terminalJohannes Schindelin via GitGitGadget, Dec 21, 2019
  7. 3/9 terminal: make the code of disable_echo() reusableJohannes Schindelin via GitGitGadget, Dec 21, 2019
  8. 7/9 built-in add -p: handle Escape sequences in interactive.singlekey modeJohannes Schindelin via GitGitGadget, Dec 21, 2019
  9. 5/9 terminal: add a new function to read a single keystrokeJohannes Schindelin via GitGitGadget, Dec 21, 2019
  10. 9/9 ci: include the built-in `git add -i` in the `linux-gcc` jobJohannes Schindelin via GitGitGadget, Dec 21, 2019
  11. SZEDER GáborDec 21, 2019
  12. Johannes SchindelinDec 25, 2019
  13. Junio C HamanoDec 22, 2019
  14. Johannes SchindelinDec 25, 2019
  15. Junio C HamanoDec 24, 2019
  16. Junio C HamanoDec 24, 2019
  17. Simon RuderichDec 25, 2019
  18. Johannes SchindelinDec 25, 2019
  19. Johannes SchindelinDec 25, 2019
  20. 0/9 built-in add -p: add support for the same config settings as the Perl versionJohannes Schindelin via GitGitGadget, Dec 25, 2019
  21. 1/9 built-in add -p: support interactive.diffFilterJohannes Schindelin via GitGitGadget, Dec 25, 2019
  22. SZEDER GáborJan 7, 2020
  23. Johannes SchindelinJan 13, 2020
  24. 2/9 built-in add -p: handle diff.algorithmJohannes Schindelin via GitGitGadget, Dec 25, 2019
  25. 3/9 terminal: make the code of disable_echo() reusableJohannes Schindelin via GitGitGadget, Dec 25, 2019
  26. 4/9 terminal: accommodate Git for Windows' default terminalJohannes Schindelin via GitGitGadget, Dec 25, 2019
  27. 6/9 built-in add -p: respect the `interactive.singlekey` config settingJohannes Schindelin via GitGitGadget, Dec 25, 2019
  28. 8/9 built-in add -p: handle Escape sequences more efficientlyJohannes Schindelin via GitGitGadget, Dec 25, 2019
  29. 9/9 ci: include the built-in `git add -i` in the `linux-gcc` jobJohannes Schindelin via GitGitGadget, Dec 25, 2019
  30. Derrick StoleeDec 26, 2019
  31. Johannes SchindelinJan 1, 2020
  32. 7/9 built-in add -p: handle Escape sequences in interactive.singlekey modeJohannes Schindelin via GitGitGadget, Dec 25, 2019
  33. 5/9 terminal: add a new function to read a single keystrokeJohannes Schindelin via GitGitGadget, Dec 25, 2019
  34. Junio C HamanoDec 26, 2019
  35. 00/10 built-in add -p: add support for the same config settings as the Perl versionJohannes Schindelin via GitGitGadget, Jan 13, 2020
  36. 01/10 built-in add -i/-p: treat SIGPIPE as EOFJohannes Schindelin via GitGitGadget, Jan 13, 2020
  37. SZEDER GáborJan 13, 2020
  38. Jeff KingJan 13, 2020
  39. Junio C HamanoJan 15, 2020
  40. Jeff KingJan 15, 2020
  41. Johannes SchindelinJan 14, 2020
  42. SZEDER GáborJan 17, 2020
  43. Jeff KingJan 17, 2020
  44. 02/10 built-in add -p: support interactive.diffFilterJohannes Schindelin via GitGitGadget, Jan 13, 2020
  45. 03/10 built-in add -p: handle diff.algorithmJohannes Schindelin via GitGitGadget, Jan 13, 2020
  46. 05/10 terminal: accommodate Git for Windows' default terminalJohannes Schindelin via GitGitGadget, Jan 13, 2020
  47. 04/10 terminal: make the code of disable_echo() reusableJohannes Schindelin via GitGitGadget, Jan 13, 2020
  48. 06/10 terminal: add a new function to read a single keystrokeJohannes Schindelin via GitGitGadget, Jan 13, 2020
  49. 07/10 built-in add -p: respect the `interactive.singlekey` config settingJohannes Schindelin via GitGitGadget, Jan 13, 2020
  50. 10/10 ci: include the built-in `git add -i` in the `linux-gcc` jobJohannes Schindelin via GitGitGadget, Jan 13, 2020
  51. 09/10 built-in add -p: handle Escape sequences more efficientlyJohannes Schindelin via GitGitGadget, Jan 13, 2020
  52. 08/10 built-in add -p: handle Escape sequences in interactive.singlekey modeJohannes Schindelin via GitGitGadget, Jan 13, 2020
  53. 00/10 built-in add -p: add support for the same config settings as the Perl versionJohannes Schindelin via GitGitGadget, Jan 14, 2020
  54. 01/10 t3701: adjust difffilter testJohannes Schindelin via GitGitGadget, Jan 14, 2020
  55. 02/10 built-in add -p: support interactive.diffFilterJohannes Schindelin via GitGitGadget, Jan 14, 2020
  56. 05/10 terminal: accommodate Git for Windows' default terminalJohannes Schindelin via GitGitGadget, Jan 14, 2020
  57. 08/10 built-in add -p: handle Escape sequences in interactive.singlekey modeJohannes Schindelin via GitGitGadget, Jan 14, 2020
  58. 10/10 ci: include the built-in `git add -i` in the `linux-gcc` jobJohannes Schindelin via GitGitGadget, Jan 14, 2020
  59. 07/10 built-in add -p: respect the `interactive.singlekey` config settingJohannes Schindelin via GitGitGadget, Jan 14, 2020
  60. 03/10 built-in add -p: handle diff.algorithmJohannes Schindelin via GitGitGadget, Jan 14, 2020
  61. 06/10 terminal: add a new function to read a single keystrokeJohannes Schindelin via GitGitGadget, Jan 14, 2020
  62. 04/10 terminal: make the code of disable_echo() reusableJohannes Schindelin via GitGitGadget, Jan 14, 2020
  63. 09/10 built-in add -p: handle Escape sequences more efficientlyJohannes Schindelin via GitGitGadget, Jan 14, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.