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

[PATCH v2 7/7] commit --interactive: make it work with the built-in `add -i`

From
Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
Date
Dec 21, 2019, 21:57 UTC
Message-ID
<ecb22159d8cf1aab83bafdf7c0cb10a2e2a6a826.1576965436.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.174.v2.git.1576965436.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

The built-in `git add -i` machinery obviously has its `the_repository` structure initialized at the point where `cmd_commit()` calls it, and therefore does not look at the environment variable `GIT_INDEX_FILE`.

But when being called from `commit --interactive`, it has to, because the index was already locked in that case, and we want to ask the interactive add machinery to work on the `index.lock` file instead of the `index` file.

Technically, we could teach `run_add_i()`, or for that matter `run_add_p()`, to look specifically at that environment variable, but the entire idea of passing in a parameter of type `struct repository *` is to allow working on multiple repositories (and their index files) independently.

So let's instead override the `index_file` field of that structure temporarily.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 builtin/commit.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/builtin/commit.c b/builtin/commit.c
index e588bc6ad3..32ffc7beee 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -347,7 +347,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
 		die(_("index file corrupt"));
 
 	if (interactive) {
-		char *old_index_env = NULL;
+		char *old_index_env = NULL, *old_repo_index_file;
 		hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
 
 		refresh_cache_or_die(refresh_flags);
@@ -355,12 +355,16 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
 		if (write_locked_index(&the_index, &index_lock, 0))
 			die(_("unable to create temporary index"));
 
+		old_repo_index_file = the_repository->index_file;
+		the_repository->index_file =
+			(char *)get_lock_file_path(&index_lock);
 		old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
-		setenv(INDEX_ENVIRONMENT, get_lock_file_path(&index_lock), 1);
+		setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
 
 		if (interactive_add(argc, argv, prefix, patch_interactive) != 0)
 			die(_("interactive add failed"));
 
+		the_repository->index_file = old_repo_index_file;
 		if (old_index_env && *old_index_env)
 			setenv(INDEX_ENVIRONMENT, old_index_env, 1);
 		else
-- 
gitgitgadget
Previous: Johannes Schindelin via GitGitGadgetNext: Johannes Schindelin via GitGitGadget
Message 20 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.