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

Re: [PATCH 1/7] built-in add -p: prepare for patch modes other than "stage"

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 17, 2019, 19:37 UTC
Message-ID
<xmqq36dihhfj.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<8f6139f94debb9b3bfb9c9e8b8e18e3c2fb46a06.1576579264.git.gitgitgadget@gmail.com>

"Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com> writes:

> +
> +enum add_p_mode {
> +	ADD_P_STAGE,
> +};
Nice to see a trailing comma here ;-)
> +int run_add_p(struct repository *r, enum add_p_mode mode,
> +	      const char *revision, const struct pathspec *ps);

This makes readers wonder if "const struct object_id *" is more appropriate; "const char *revision" that holds human-readable name is better when the internal machinery uses it for reporting, so that may be what is going on here (so is the new field in add_p_state structure).

Show 15 quoted lines
>  #endif
> diff --git a/add-patch.c b/add-patch.c
> index 2c46fe5b33..8a691f07da 100644
> --- a/add-patch.c
> +++ b/add-patch.c
> @@ -11,10 +11,33 @@ enum prompt_mode_type {
>  	PROMPT_MODE_CHANGE = 0, PROMPT_DELETION, PROMPT_HUNK
>  };
>  
> -static const char *prompt_mode[] = {
> -	N_("Stage mode change [y,n,a,q,d%s,?]? "),
> -	N_("Stage deletion [y,n,a,q,d%s,?]? "),
> -	N_("Stage this hunk [y,n,a,q,d%s,?]? ")
> +struct patch_mode {
> +	const char *diff[4], *apply[4], *apply_check[4];

Hardcoded "4" and not-quite descriptive names puzzle readers at the first glance. Let's read on to see if they need any further improvement.

> +	unsigned is_reverse:1, apply_for_checkout:1;
> +	const char *prompt_mode[PROMPT_HUNK + 1];

This relies on the enum value assignment (or listing) order to ensure that PROMPT_HUNK always comes at the end. Perhaps that deserves a comment before "enum prompt_mode_type", e.g.

	+/* Keep PROMPT_HUNK at the end */
	 enum prompt_mode_type {
	 	PROMPT_MODE_CHANGE = 0, ...
	 };
Show 5 quoted lines
> +	const char *edit_hunk_hint, *help_patch_text;
> +};
> +
> +static struct patch_mode patch_mode_stage = {
> +	.diff = { "diff-files", NULL },
Nice to see designated initializers used ;-)

Mental note: the "diff" field is (probably) for "the command line to be used to generate the patch"

> +	.apply = { "--cached", NULL },
> +	.apply_check = { "--cached", NULL },

Mental note: these "apply" and "apply_check" fields are (probably) not for the command line; unlike the "diff" field, these only have arguments.

Mental note: if the three field names become confusing, perhaps we can clarify them by either (1) calling diff as diff_cmd[], or (2) calling the other as apply_args[] and apply_check_args[], or (3) rename both.

> +	.is_reverse = 0,

Wouldn't it be sufficient to apply the default initialization, just like it is done for apply_for_checkout bitfield?

Show 6 quoted lines
> @@ -1310,6 +1345,9 @@ int run_add_p(struct repository *r, const struct pathspec *ps)
>  
>  	init_add_i_state(&s.s, r);
>  
> +	s.mode = &patch_mode_stage;
> +	s.revision = revision;

The phrase "mode_stage" may become problematic, as other modes that will be introduced, like "reset", "checkout" all will stage different contents to the index. The only mode the machinery knows at this point in the series is how "add" stages contents to the index, so "patch_mode_add" might turn out to be a better choice of the phrase as we read the series along. We'll see.

> +		if (!strcmp(patch_mode, "--patch"))
> +			mode = ADD_P_STAGE;
The same comment applies to this enum token.
Thanks.
Previous: Johannes Schindelin via GitGitGadgetNext: Johannes Schindelin
Message 4 of 23 in “stash/reset/checkout -p: optionally use the add --patch backend written in pure C”
  1. 0/7 stash/reset/checkout -p: optionally use the add --patch backend written in pure CJohannes Schindelin via GitGitGadget, Dec 17, 2019
  2. 3/7 legacy stash -p: respect the add.interactive.usebuiltin settingJohannes Schindelin via GitGitGadget, Dec 17, 2019
  3. 1/7 built-in add -p: prepare for patch modes other than "stage"Johannes Schindelin via GitGitGadget, Dec 17, 2019
  4. Junio C HamanoDec 17, 2019
  5. Johannes SchindelinDec 21, 2019
  6. 2/7 built-in add -p: implement the "stash" and "reset" patch modesJohannes Schindelin via GitGitGadget, Dec 17, 2019
  7. Junio C HamanoDec 17, 2019
  8. Johannes SchindelinDec 21, 2019
  9. 4/7 built-in stash: use the built-in `git add -p` if so configuredJohannes Schindelin via GitGitGadget, Dec 17, 2019
  10. Junio C HamanoDec 17, 2019
  11. Johannes SchindelinDec 21, 2019
  12. 5/7 built-in add -p: implement the "checkout" patch modesJohannes Schindelin via GitGitGadget, Dec 17, 2019
  13. 7/7 commit --interactive: make it work with the built-in `add -i`Johannes Schindelin via GitGitGadget, Dec 17, 2019
  14. 6/7 built-in add -p: implement the "worktree" patch modesJohannes Schindelin via GitGitGadget, Dec 17, 2019
  15. Junio C HamanoDec 17, 2019
  16. 0/7 stash/reset/checkout -p: optionally use the add --patch backend written in pure CJohannes Schindelin via GitGitGadget, Dec 21, 2019
  17. 1/7 built-in add -p: prepare for patch modes other than "stage"Johannes Schindelin via GitGitGadget, Dec 21, 2019
  18. 3/7 legacy stash -p: respect the add.interactive.usebuiltin settingJohannes Schindelin via GitGitGadget, Dec 21, 2019
  19. 2/7 built-in add -p: implement the "stash" and "reset" patch modesJohannes Schindelin via GitGitGadget, Dec 21, 2019
  20. 7/7 commit --interactive: make it work with the built-in `add -i`Johannes Schindelin via GitGitGadget, Dec 21, 2019
  21. 5/7 built-in add -p: implement the "checkout" patch modesJohannes Schindelin via GitGitGadget, Dec 21, 2019
  22. 4/7 built-in stash: use the built-in `git add -p` if so configuredJohannes Schindelin via GitGitGadget, Dec 21, 2019
  23. 6/7 built-in add -p: implement the "worktree" patch modesJohannes Schindelin via GitGitGadget, Dec 21, 2019

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.