]> git.ipfire.org Git - thirdparty/git.git/commitdiff
commit --interactive: make it work with the built-in `add -i`
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Sat, 21 Dec 2019 21:57:16 +0000 (21:57 +0000)
committerJunio C Hamano <gitster@pobox.com>
Sun, 22 Dec 2019 00:06:22 +0000 (16:06 -0800)
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/commit.c

index e588bc6ad3c66c46ba904c14e39b325055209734..32ffc7beee19082ba1ed126a35e9b3a1123f7671 100644 (file)
@@ -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