Skip to content

Commit 1020d08

Browse files
ConradIrwingitster
authored andcommitted
Use a temporary index for git commit --interactive
Change the behaviour of git commit --interactive so that when you abort the commit (by leaving the commit message empty) the index remains unchanged. Hitherto an aborted commit --interactive has added the selected hunks to the index regardless of whether the commit succeeded or not. Signed-off-by: Conrad Irwin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6086ff6 commit 1020d08

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

Documentation/git-commit.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ The content to be added can be specified in several ways:
4141

4242
5. by using the --interactive switch with the 'commit' command to decide one
4343
by one which files should be part of the commit, before finalizing the
44-
operation. Currently, this is done by invoking 'git add --interactive'.
44+
operation. Currently, this is done by invoking 'git add --interactive'
45+
on a temporary index.
4546

4647
The `--dry-run` option can be used to obtain a
4748
summary of what is included by any of the above for the next

builtin/commit.c

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,25 +336,45 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
336336
int fd;
337337
struct string_list partial;
338338
const char **pathspec = NULL;
339+
char *old_index_env = NULL;
339340
int refresh_flags = REFRESH_QUIET;
340341

341342
if (is_status)
342343
refresh_flags |= REFRESH_UNMERGED;
343-
if (interactive) {
344-
if (interactive_add(argc, argv, prefix) != 0)
345-
die(_("interactive add failed"));
346-
if (read_cache_preload(NULL) < 0)
347-
die(_("index file corrupt"));
348-
commit_style = COMMIT_AS_IS;
349-
return get_index_file();
350-
}
351344

352345
if (*argv)
353346
pathspec = get_pathspec(prefix, argv);
354347

355348
if (read_cache_preload(pathspec) < 0)
356349
die(_("index file corrupt"));
357350

351+
if (interactive) {
352+
fd = hold_locked_index(&index_lock, 1);
353+
354+
refresh_cache_or_die(refresh_flags);
355+
356+
if (write_cache(fd, active_cache, active_nr) ||
357+
close_lock_file(&index_lock))
358+
die(_("unable to create temporary index"));
359+
360+
old_index_env = getenv(INDEX_ENVIRONMENT);
361+
setenv(INDEX_ENVIRONMENT, index_lock.filename, 1);
362+
363+
if (interactive_add(argc, argv, prefix) != 0)
364+
die(_("interactive add failed"));
365+
366+
if (old_index_env && *old_index_env)
367+
setenv(INDEX_ENVIRONMENT, old_index_env, 1);
368+
else
369+
unsetenv(INDEX_ENVIRONMENT);
370+
371+
discard_cache();
372+
read_cache_from(index_lock.filename);
373+
374+
commit_style = COMMIT_NORMAL;
375+
return index_lock.filename;
376+
}
377+
358378
/*
359379
* Non partial, non as-is commit.
360380
*

0 commit comments

Comments
 (0)