Skip to content

Commit d858409

Browse files
committed
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 it has to, because the index was already locked, 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()` (and `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 <[email protected]>
1 parent 125dfcf commit d858409

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

builtin/commit.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,21 +344,25 @@ static const char *prepare_index(int argc, const char **argv, const char *prefix
344344
die(_("index file corrupt"));
345345

346346
if (interactive) {
347-
char *old_index_env = NULL;
347+
char *old_index_env = NULL, *old_repo_index_file;
348348
hold_locked_index(&index_lock, LOCK_DIE_ON_ERROR);
349349

350350
refresh_cache_or_die(refresh_flags);
351351

352352
if (write_locked_index(&the_index, &index_lock, 0))
353353
die(_("unable to create temporary index"));
354354

355+
old_repo_index_file = the_repository->index_file;
356+
the_repository->index_file =
357+
(char *)get_lock_file_path(&index_lock);
355358
old_index_env = xstrdup_or_null(getenv(INDEX_ENVIRONMENT));
356-
setenv(INDEX_ENVIRONMENT, get_lock_file_path(&index_lock), 1);
359+
setenv(INDEX_ENVIRONMENT, the_repository->index_file, 1);
357360

358361
if (interactive_add(argc, argv, prefix,
359362
patch_interactive ? "" : NULL) != 0)
360363
die(_("interactive add failed"));
361364

365+
the_repository->index_file = old_repo_index_file;
362366
if (old_index_env && *old_index_env)
363367
setenv(INDEX_ENVIRONMENT, old_index_env, 1);
364368
else

0 commit comments

Comments
 (0)