git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Phillip Wood <phillip.wood123@gmail.com>
To: Leon Michalak via GitGitGadget <gitgitgadget@gmail.com>,
	git@vger.kernel.org
Cc: Leon Michalak <leonmichalak6@gmail.com>
Subject: Re: [PATCH 2/3] add-patch: add diff.context command line overrides
Date: Wed, 7 May 2025 10:51:13 +0100	[thread overview]
Message-ID: <61fe7690-87af-4159-be87-cd39c09475fe@gmail.com> (raw)
In-Reply-To: <7700eb173e73bb240852dc1c7ce26f3d1f95d8ca.1746436719.git.gitgitgadget@gmail.com>

Hi Leon

On 05/05/2025 10:18, Leon Michalak via GitGitGadget wrote:
> 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.

> +`-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[]

> --- 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.

> 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

> +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.

> @@ -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.

>   	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.

>   	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.

> +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


  parent reply	other threads:[~2025-05-07  9:51 UTC|newest]

Thread overview: 77+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-05  9:18 [PATCH 0/3] Better support for customising context lines in --patch commands Leon Michalak via GitGitGadget
2025-05-05  9:18 ` [PATCH 1/3] add-patch: respect diff.context configuration Leon Michalak via GitGitGadget
2025-05-05 20:29   ` Eric Sunshine
2025-05-06  8:55   ` Phillip Wood
2025-05-06 17:29   ` Junio C Hamano
2025-05-05  9:18 ` [PATCH 2/3] add-patch: add diff.context command line overrides Leon Michalak via GitGitGadget
2025-05-05  9:49   ` Kristoffer Haugsbakk
     [not found]     ` <CAP9jKjHj7WP91aKA9SE94zYj+naBGLUs99mF3G4BhTGcGjFDUQ@mail.gmail.com>
2025-05-05 10:11       ` Leon Michalak
2025-05-07  9:51   ` Phillip Wood [this message]
2025-05-07 18:07     ` Junio C Hamano
2025-05-07 18:28       ` Leon Michalak
2025-05-07 20:25         ` Junio C Hamano
2025-05-05  9:18 ` [PATCH 3/3] add-interactive: add new "context" subcommand Leon Michalak via GitGitGadget
2025-05-06  0:02   ` Eric Sunshine
2025-05-06  7:20     ` Leon Michalak
2025-05-06  8:28       ` Christian Couder
2025-05-06 17:07         ` Leon Michalak
2025-05-06 16:37     ` Junio C Hamano
2025-05-06 17:25       ` Leon Michalak
2025-05-07 13:30       ` Phillip Wood
2025-05-07 19:10         ` Junio C Hamano
2025-05-10 13:46 ` [PATCH v2 0/4] Better support for customising context lines in --patch commands Leon Michalak via GitGitGadget
2025-05-10 13:46   ` [PATCH v2 1/4] test: refactor to use "test_grep" Leon Michalak via GitGitGadget
2025-05-12 13:42     ` Junio C Hamano
2025-05-12 16:58       ` Leon Michalak
2025-05-10 13:46   ` [PATCH v2 2/4] test: refactor to use "test_config" Leon Michalak via GitGitGadget
2025-05-10 13:46   ` [PATCH v2 3/4] add-patch: respect diff.context configuration Leon Michalak via GitGitGadget
2025-05-13 13:52     ` Phillip Wood
2025-05-13 15:47       ` Junio C Hamano
2025-05-14 15:13         ` Phillip Wood
2025-05-15 12:58           ` Junio C Hamano
2025-05-10 13:46   ` [PATCH v2 4/4] add-patch: add diff.context command line overrides Leon Michalak via GitGitGadget
2025-05-12 16:45     ` Junio C Hamano
2025-05-12 17:03       ` Leon Michalak
2025-05-13 13:52     ` Phillip Wood
2025-05-13 14:39       ` Phillip Wood
2025-05-13 15:05         ` Leon Michalak
2025-05-14 15:13           ` phillip.wood123
2025-06-28 16:34   ` [PATCH v3 0/4] Better support for customising context lines in --patch commands Leon Michalak via GitGitGadget
2025-06-28 16:34     ` [PATCH v3 1/4] test: use "test_grep" Leon Michalak via GitGitGadget
2025-06-30 16:23       ` Junio C Hamano
2025-06-28 16:34     ` [PATCH v3 2/4] test: use "test_config" Leon Michalak via GitGitGadget
2025-06-30 16:35       ` Junio C Hamano
2025-06-28 16:34     ` [PATCH v3 3/4] add-patch: respect diff.context configuration Leon Michalak via GitGitGadget
2025-06-30 16:55       ` Junio C Hamano
2025-07-01 10:00       ` Phillip Wood
2025-06-28 16:34     ` [PATCH v3 4/4] add-patch: add diff.context command line overrides Leon Michalak via GitGitGadget
2025-06-30 17:03       ` Junio C Hamano
2025-07-01  9:59         ` Phillip Wood
2025-07-01 15:54           ` Junio C Hamano
2025-07-02 14:07             ` Phillip Wood
2025-07-02 18:28               ` Junio C Hamano
2025-07-01  9:59       ` Phillip Wood
2025-06-30 16:16     ` [PATCH v3 0/4] Better support for customising context lines in --patch commands Junio C Hamano
2025-07-09  0:09     ` Junio C Hamano
2025-07-09  7:57       ` Leon Michalak
2025-07-09 15:32         ` Junio C Hamano
2025-07-19 12:28     ` [PATCH v4 " Leon Michalak via GitGitGadget
2025-07-19 12:28       ` [PATCH v4 1/4] t: use test_grep in t3701 and t4055 Leon Michalak via GitGitGadget
2025-07-19 12:28       ` [PATCH v4 2/4] t: use test_config in t4055 Leon Michalak via GitGitGadget
2025-07-19 12:28       ` [PATCH v4 3/4] add-patch: respect diff.context configuration Leon Michalak via GitGitGadget
2025-07-19 12:28       ` [PATCH v4 4/4] add-patch: add diff.context command line overrides Leon Michalak via GitGitGadget
2025-07-22 16:01         ` Phillip Wood
2025-07-22 18:02           ` Leon Michalak
2025-07-22 18:05             ` Leon Michalak
2025-07-23  9:41             ` Phillip Wood
2025-07-21 16:50       ` [PATCH v4 0/4] Better support for customising context lines in --patch commands Junio C Hamano
2025-07-22 16:05         ` Phillip Wood
2025-07-22 17:20           ` Junio C Hamano
2025-07-29  7:01       ` [PATCH v5 " Leon Michalak via GitGitGadget
2025-07-29  7:01         ` [PATCH v5 1/4] t: use test_grep in t3701 and t4055 Leon Michalak via GitGitGadget
2025-07-29  7:01         ` [PATCH v5 2/4] t: use test_config in t4055 Leon Michalak via GitGitGadget
2025-07-29  7:01         ` [PATCH v5 3/4] add-patch: respect diff.context configuration Leon Michalak via GitGitGadget
2025-07-29  7:01         ` [PATCH v5 4/4] add-patch: add diff.context command line overrides Leon Michalak via GitGitGadget
2025-07-29 15:21         ` [PATCH v5 0/4] Better support for customising context lines in --patch commands Phillip Wood
2025-07-29 15:55           ` Junio C Hamano
2025-07-29 16:18             ` Leon Michalak

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=61fe7690-87af-4159-be87-cd39c09475fe@gmail.com \
    --to=phillip.wood123@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=leonmichalak6@gmail.com \
    --cc=phillip.wood@dunelm.org.uk \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).