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

Re: [PATCH] builtin/add.c: replace run_command() with direct apply_all_patches() call

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 10, 2026, 06:41 UTC
Message-ID
<xmqqmrvzfitd.fsf@gitster.g>
In-Reply-To
<20260709192619.46791-1-gatlavishweshwarreddy26@gmail.com>
Gatla Vishweshwar Reddy <gatlavishweshwarreddy26@gmail.com> writes:
> When the user runs "git add -e", the diff of the working tree changes
> is written to a temporary file, opened in an editor, and then applied
> back to the index. The application step was done by spawning a child

"was" -> "is"; in the first part of the log message that gives an observation, we describe the status quo in the present tense.

Show 28 quoted lines
> process running "git apply --recount --cached <file>", which is an
> unnecessary subprocess since the apply machinery is available as a
> native C API.
> @@ -187,7 +186,6 @@ static int edit_patch(struct repository *repo,
>  		      const char *prefix)
>  {
>  	char *file = repo_git_path(repo, "ADD_EDIT.patch");
> -	struct child_process child = CHILD_PROCESS_INIT;
>  	struct rev_info rev;
>  	int out;
>  	struct stat st;
> @@ -217,11 +215,15 @@ static int edit_patch(struct repository *repo,
>  	if (!st.st_size)
>  		die(_("empty patch. aborted"));
>  
> -	child.git_cmd = 1;
> -	strvec_pushl(&child.args, "apply", "--recount", "--cached", file,
> -		     NULL);
> -	if (run_command(&child))
> +	struct apply_state state;
> +	const char *apply_argv[] = { file, NULL };
> +
> +	if (init_apply_state(&state, repo, prefix))
> +		die(_("could not initialize apply state"));
> +	state.cached = 1;
> +	if (apply_all_patches(&state, 1, apply_argv, APPLY_OPT_RECOUNT))
>  		die(_("could not apply '%s'"), file);
> +	clear_apply_state(&state);

Compared to existing callers of the apply_all_patches() API function, this implementation curiously lacks a prior call to check_apply_state().

Has this been tested, and do we have sufficient test coverage for it?

Calling check_apply_state() should flip state->check_index on, given that state.cached is set to 1 above. If I remember correctly, having this bit enabled is required for apply_patch() to toggle the .update_index member, which in turn allows apply_all_patches() to update the index with the patch results. Please double-check this logic, since it has been a while since I looked at these specific code paths.

If my assumption holds, this patch might inadvertently stop writing the result to the index, even though the original intent of replacing 'apply --cached' was clearly to update it.

Thanks.
>  
>  	unlink(file);
>  	free(file);
Previous: Gatla Vishweshwar ReddyNext: Gatla Vishweshwar Reddy
Message 2 of 9 in “builtin/add.c: replace run_command() with direct apply_all_patches() call”
  1. builtin/add.c: replace run_command() with direct apply_all_patches() callGatla Vishweshwar Reddy, Jul 9, 2026
  2. Junio C HamanoJul 10, 2026
  3. builtin/add.c: replace run_command() with direct apply_all_patches() callGatla Vishweshwar Reddy, Jul 10, 2026
  4. Junio C HamanoJul 10, 2026
  5. builtin/add.c: replace run_command() with direct apply_all_patches() callGatla Vishweshwar Reddy, Jul 10, 2026
  6. Junio C HamanoJul 11, 2026
  7. builtin/add.c: replace run_command() with direct apply_all_patches() callGatla Vishweshwar Reddy, Jul 11, 2026
  8. Junio C HamanoJul 29, 2026
  9. Junio C HamanoAug 26, 2026

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.