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

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

From
GRGatla Vishweshwar Reddy <gatlavishweshwarreddy26@gmail.com>
Date
Jul 10, 2026, 07:32 UTC
Message-ID
<20260710074105.50737-1-gatlavishweshwarreddy26@gmail.com>
In-Reply-To
<xmqqmrvzfitd.fsf@gitster.g>

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 is done by spawning a child process running "git apply --recount --cached <file>", which is an unnecessary subprocess since the apply machinery is available as a native C API.

Replace the run_command() call with a direct call to apply_all_patches() using an initialized apply_state with the cached and recount options set appropriately. This avoids the overhead of forking a subprocess, keeps the operation within the same process, and makes the intent of the code clearer to the reader.

Remove the now-unused includes of "run-command.h" and "strvec.h" since no other code in this file requires them after this change.

Signed-off-by: Gatla Vishweshwar Reddy <gatlavishweshwarreddy26@gmail.com>
---
Changes in v2:
- Fixed commit message: "was done" -> "is done" (present tense)
- Added check_apply_state() call after setting state.cached = 1,
  which sets state.check_index = 1 required for index updates
In response to review:
- check_apply_state() with cached=1 correctly
  sets check_index=1, ensuring apply_all_patches() updates the index
  as intended. Verified by reading apply.c lines 172-175.
- Tested with t3700-add.sh: all 58 tests pass
 builtin/add.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
diff --git a/builtin/add.c b/builtin/add.c
index c859f66519..a7266020cd 100644
--- a/builtin/add.c
+++ b/builtin/add.c
@@ -13,7 +13,6 @@
 #include "dir.h"
 #include "gettext.h"
 #include "pathspec.h"
-#include "run-command.h"
 #include "object-file.h"
 #include "odb.h"
 #include "odb/transaction.h"
@@ -23,9 +22,9 @@
 #include "diff.h"
 #include "read-cache.h"
 #include "revision.h"
-#include "strvec.h"
 #include "submodule.h"
 #include "add-interactive.h"
+#include "apply.h"

 static const char * const builtin_add_usage[] = {
 	N_("git add [<options>] [--] <pathspec>..."),
@@ -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,17 @@ 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 (check_apply_state(&state, 0))
+		die(_("could not check apply state"));
+	if (apply_all_patches(&state, 1, apply_argv, APPLY_OPT_RECOUNT))
 		die(_("could not apply '%s'"), file);
+	clear_apply_state(&state);

 	unlink(file);
 	free(file);
--
2.54.0
Previous: Junio C HamanoNext: Junio C Hamano
Message 3 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.