@matheusbsbs / matheus-bagliano / commits / c480eeb574
Fork 0

commit --interactive: make it work with the built-in `add -i`

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>

Johannes Schindelin committed Dec 21, 2019 at 21:57 UTC c480eeb574e649a19f27dc09a994e45f9b2c2622
1 file changed +6 -2
builtin/commit.c
+6 -2
@@ -347,7 +347,7 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
347 die(_("index file corrupt"));
348
349 if (interactive) {
350 - char *old_index_env = NULL;
350 + char *old_index_env = NULL, *old_repo_index_file;
351 hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
352
353 refresh_cache_or_die(refresh_flags);
@@ -355,12 +355,16 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
355 if (write_locked_index(&the_index, &index_lock, 0))
356 die(_("unable to create temporary index"));
357
358 + old_repo_index_file = the_repository->index_file;
359 + the_repository->index_file =
360 + (char *)get_lock_file_path(&index_lock);
361 old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
359 - setenv(INDEX_ENVIRONMENT, get_lock_file_path(&index_lock), 1);
362 + setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
363
364 if (interactive_add(argc, argv, prefix, patch_interactive) != 0)
365 die(_("interactive add failed"));
366
367 + the_repository->index_file = old_repo_index_file;
368 if (old_index_env && *old_index_env)
369 setenv(INDEX_ENVIRONMENT, old_index_env, 1);
370 else