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

Re: [PATCH 2/3] add-patch: add diff.context command line overrides

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
May 7, 2025, 09:51 UTC
Message-ID
<61fe7690-87af-4159-be87-cd39c09475fe@gmail.com>
In-Reply-To
<7700eb173e73bb240852dc1c7ce26f3d1f95d8ca.1746436719.git.gitgitgadget@gmail.com>
Hi Leon
On 05/05/2025 10:18, Leon Michalak via GitGitGadget wrote:
Show 15 quoted lines
> From: Leon Michalak <leonmichalak6@gmail.com>
> 
> This patch compliments `8b91eef812`, where builtins that accept
> `--patch` options now respect `diff.context` and `diff.interHunkContext`
> file configurations.
> 
> In particular, this patch helps users who don't want to set persistent
> context configurations or just want a way to override them on a one-time
> basis, by allowing the relevant builtins to accept corresponding command
> line options that override the file configurations.
> 
> This mimics `diff` which allows for both context file configuration and
> command line overrides.
> 
> Signed-off-by: Leon Michalak <leonmichalak6@gmail.com>

I'm somewhat torn about the name "--unified" as I don't think it is particularly informative - it only makes sense if one knows the diff option and we only support a single diff format. Having said that it is probably better to reuse the name from "git diff" for consistency.

Show 10 quoted lines
> +`-U<n>`::
> +`--unified=<n>`::
> +	Generate diffs with _<n>_ lines of context. Defaults to `diff.context`
> +	or 3 if the config option is unset. Implies `--interactive/--patch`.
> +
> +`--inter-hunk-context=<n>`::
> +	Show the context between diff hunks, up to the specified _<number>_
> +	of lines, thereby fusing hunks that are close to each other.
> +	Defaults to `diff.interHunkContext` or 0 if the config option
> +	is unset. Implies `--interactive/--patch`.

This documentation is repeated for each command. I think it would be better to put this in separate file that is then included where it is needed. That way if we need to update the documentation in the future we only have one copy to worry about. The syntax to include a file called diff-context-options.adoc is

include::diff-context-options.adoc[]
Show 11 quoted lines
> --- a/add-interactive.c
> +++ b/add-interactive.c
> @@ -86,6 +87,11 @@ void init_add_i_state(struct add_i_state *s, struct repository *r)
>   	repo_config_get_bool(r, "interactive.singlekey", &s->use_single_key);
>   	if (s->use_single_key)
>   		setbuf(stdin, NULL);
> +
> +	if (add_p_opt->context != -1)
> +		s->context = add_p_opt->context;
> +	if (add_p_opt->interhunkcontext != -1)
> +		s->interhunkcontext = add_p_opt->interhunkcontext;

This happens after we read the config so the command line option wins - good.

Show 8 quoted lines
> index 693f125e8e4b..653f07a917b8 100644
> --- a/add-interactive.h
> +++ b/add-interactive.h
> @@ -3,6 +3,13 @@
>   
>   #include "color.h"
>   
> +#define ADD_P_OPT_INIT { .context = -1, .interhunkcontext = -1 }

I think we normally define initializer macros after the struct they initialize so the reader can more easily check the fields are all initialized correctly

Show 13 quoted lines
> +struct add_p_opt {
> +	int context;
> +	int interhunkcontext;
> +};
> +
>   struct add_i_state {
>   	struct repository *r;
>   	int use_color;
> @@ -18,14 +25,17 @@ struct add_i_state {
>   
>   	int use_single_key;
>   	char *interactive_diff_filter, *interactive_diff_algorithm;
> +	int context, interhunkcontext;
Should this be in the previous patch? It is a good idea to run
     git rebase --keep-base --exec 'make && cd t && prove -j8 
tests-that-you-think-might-fail :: --root=/dev/shm'

before submitting a patch series so that you can be sure the patches all compile and the tests pass. Running the whole test suite can be pretty slow so just run the tests that are relevant to your changes.

Show 7 quoted lines
> @@ -169,9 +170,10 @@ int interactive_add(struct repository *repo,
>   		       prefix, argv);
>   
>   	if (patch)
> -		ret = !!run_add_p(repo, ADD_P_ADD, NULL, &pathspec);
> +		ret = !!run_add_p(repo, ADD_P_ADD, add_p_opt, NULL,
> +				  &pathspec);
This line could be unwraped and still fit within 80 columns I think.
Show 12 quoted lines
>   	else
> -		ret = !!run_add_i(repo, &pathspec);
> +		ret = !!run_add_i(repo, &pathspec, add_p_opt);
>   
>   	clear_pathspec(&pathspec);
>   	return ret;
> @@ -253,6 +255,12 @@ static struct option builtin_add_options[] = {
>   	OPT_GROUP(""),
>   	OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")),
>   	OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")),
> +	OPT_INTEGER_F('U', "unified", &add_p_opt.context,
> +		      N_("generate diffs with <n> lines context, implies --interactive/--patch"),

It cannot imply both --interactive and --patch as they are two different operations. I think it should imply --patch because that option is supported by all the commands we're adding -U to.

> +		      PARSE_OPT_NONEG),
> +	OPT_INTEGER_F(0, "inter-hunk-context", &add_p_opt.interhunkcontext,
> +		      N_("show context between diff hunks up to the specified number of lines, implies --interactive/--patch"),
> +		      PARSE_OPT_NONEG),

As these two options are duplicated in many commands we should define a preprocessor macro for them in parse-options.h. If you look at the end of that file there are a bunch of similar definitions.

Show 14 quoted lines
>   	OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")),
>   	OPT__FORCE(&ignored_too, N_("allow adding otherwise ignored files"), 0),
>   	OPT_BOOL('u', "update", &take_worktree_changes, N_("update tracked files")),
> @@ -398,7 +406,12 @@ int cmd_add(int argc,
>   			die(_("options '%s' and '%s' cannot be used together"), "--dry-run", "--interactive/--patch");
>   		if (pathspec_from_file)
>   			die(_("options '%s' and '%s' cannot be used together"), "--pathspec-from-file", "--interactive/--patch");
> -		exit(interactive_add(repo, argv + 1, prefix, patch_interactive));
> +		exit(interactive_add(repo, argv + 1, prefix, patch_interactive, &add_p_opt));
> +	} else {
> +		if (add_p_opt.context != -1)
> +			die(_("the option '%s' requires '%s'"), "--unified", "--interactive/--patch");
> +		if (add_p_opt.interhunkcontext != -1)
> +			die(_("the option '%s' requires '%s'"), "--inter-hunk-context", "--interactive/--patch");

I don't understand this - I thought the help for --unified said it implied --patch but here it is erroring out if --patch is not given.

Somewhere we should check that the values given for the context and interhunk context are non-negative. git_diff_ui_config() errors out if the context is negative and we should do the same in add-patch.c otherwise we'll get some wierd error about "git diff-index" failing. We should also think about -U0 as "git apply" rejects hunks without any context unless by default and I dont think "add -p" passes the option to enable that and I'm not sure it should.

Show 5 quoted lines
> +test_expect_success 'The -U option overrides diff.context for "add"' '
> +	git config diff.context 8 &&
> +	git add -U4 -p >output &&
> +	! grep "^ firstline" output
> +'

It is great that you've added tests and I do think we should test all the commands here as unlike the previous patch there isn't a common code path. My other comments about the tests in the previous patch apply here though I think.

We should have a test to check negative context values and possibly zero are rejected.

Thanks
Phillip
Previous: Kristoffer HaugsbakkNext: Junio C Hamano
Message 17 of 77 in “Better support for customising context lines in --patch commands”
  1. 0/3 Better support for customising context lines in --patch commandsLeon Michalak via GitGitGadget, May 5, 2025
  2. 1/3 add-patch: respect diff.context configurationLeon Michalak via GitGitGadget, May 5, 2025
  3. Eric SunshineMay 5, 2025
  4. Phillip WoodMay 6, 2025
  5. Junio C HamanoMay 6, 2025
  6. 3/3 add-interactive: add new "context" subcommandLeon Michalak via GitGitGadget, May 5, 2025
  7. Eric SunshineMay 6, 2025
  8. Leon MichalakMay 6, 2025
  9. Christian CouderMay 6, 2025
  10. Leon MichalakMay 6, 2025
  11. Junio C HamanoMay 6, 2025
  12. Leon MichalakMay 6, 2025
  13. Phillip WoodMay 7, 2025
  14. Junio C HamanoMay 7, 2025
  15. 2/3 add-patch: add diff.context command line overridesLeon Michalak via GitGitGadget, May 5, 2025
  16. Kristoffer HaugsbakkMay 5, 2025
  17. Phillip WoodMay 7, 2025
  18. Junio C HamanoMay 7, 2025
  19. Leon MichalakMay 7, 2025
  20. Junio C HamanoMay 7, 2025
  21. 0/4 Better support for customising context lines in --patch commandsLeon Michalak via GitGitGadget, May 10, 2025
  22. 1/4 test: refactor to use "test_grep"Leon Michalak via GitGitGadget, May 10, 2025
  23. Junio C HamanoMay 12, 2025
  24. Leon MichalakMay 12, 2025
  25. 2/4 test: refactor to use "test_config"Leon Michalak via GitGitGadget, May 10, 2025
  26. 3/4 add-patch: respect diff.context configurationLeon Michalak via GitGitGadget, May 10, 2025
  27. Phillip WoodMay 13, 2025
  28. Junio C HamanoMay 13, 2025
  29. Phillip WoodMay 14, 2025
  30. Junio C HamanoMay 15, 2025
  31. 4/4 add-patch: add diff.context command line overridesLeon Michalak via GitGitGadget, May 10, 2025
  32. Junio C HamanoMay 12, 2025
  33. Leon MichalakMay 12, 2025
  34. Phillip WoodMay 13, 2025
  35. Phillip WoodMay 13, 2025
  36. Leon MichalakMay 13, 2025
  37. phillip.wood123@gmail.comMay 14, 2025
  38. 0/4 Better support for customising context lines in --patch commandsLeon Michalak via GitGitGadget, Jun 28, 2025
  39. 1/4 test: use "test_grep"Leon Michalak via GitGitGadget, Jun 28, 2025
  40. Junio C HamanoJun 30, 2025
  41. 2/4 test: use "test_config"Leon Michalak via GitGitGadget, Jun 28, 2025
  42. Junio C HamanoJun 30, 2025
  43. 4/4 add-patch: add diff.context command line overridesLeon Michalak via GitGitGadget, Jun 28, 2025
  44. Junio C HamanoJun 30, 2025
  45. Phillip WoodJul 1, 2025
  46. Junio C HamanoJul 1, 2025
  47. Phillip WoodJul 2, 2025
  48. Junio C HamanoJul 2, 2025
  49. Phillip WoodJul 1, 2025
  50. 3/4 add-patch: respect diff.context configurationLeon Michalak via GitGitGadget, Jun 28, 2025
  51. Junio C HamanoJun 30, 2025
  52. Phillip WoodJul 1, 2025
  53. Junio C HamanoJun 30, 2025
  54. Junio C HamanoJul 9, 2025
  55. Leon MichalakJul 9, 2025
  56. Junio C HamanoJul 9, 2025
  57. 0/4 Better support for customising context lines in --patch commandsLeon Michalak via GitGitGadget, Jul 19, 2025
  58. 1/4 t: use test_grep in t3701 and t4055Leon Michalak via GitGitGadget, Jul 19, 2025
  59. 2/4 t: use test_config in t4055Leon Michalak via GitGitGadget, Jul 19, 2025
  60. 3/4 add-patch: respect diff.context configurationLeon Michalak via GitGitGadget, Jul 19, 2025
  61. 4/4 add-patch: add diff.context command line overridesLeon Michalak via GitGitGadget, Jul 19, 2025
  62. Phillip WoodJul 22, 2025
  63. Leon MichalakJul 22, 2025
  64. Leon MichalakJul 22, 2025
  65. Phillip WoodJul 23, 2025
  66. Junio C HamanoJul 21, 2025
  67. Phillip WoodJul 22, 2025
  68. Junio C HamanoJul 22, 2025
  69. 0/4 Better support for customising context lines in --patch commandsLeon Michalak via GitGitGadget, Jul 29, 2025
  70. 2/4 t: use test_config in t4055Leon Michalak via GitGitGadget, Jul 29, 2025
  71. 3/4 add-patch: respect diff.context configurationLeon Michalak via GitGitGadget, Jul 29, 2025
  72. 1/4 t: use test_grep in t3701 and t4055Leon Michalak via GitGitGadget, Jul 29, 2025
  73. 4/4 add-patch: add diff.context command line overridesLeon Michalak via GitGitGadget, Jul 29, 2025
  74. Phillip WoodJul 29, 2025
  75. Junio C HamanoJul 29, 2025
  76. Leon MichalakJul 29, 2025
  77. Leon MichalakMay 5, 2025

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.