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 <conrad.irwin@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Conrad Irwin committed
May 6, 2011 at 22:59 UTC
1020d08786c1413d488845f9175d01c0ea073a6c
2 files changed
+30
-9
Documentation/git-commit.txt
+2
-1
@@ -41,7 +41,8 @@ The content to be added can be specified in several ways:
41
42
5. by using the --interactive switch with the 'commit' command to decide one
43
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.
46
47
The `--dry-run` option can be used to obtain a
48
summary of what is included by any of the above for the next
builtin/commit.c
+28
-8
@@ -336,18 +336,11 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
336
int fd;
337
struct string_list partial;
338
const char **pathspec = NULL;
339
+ char *old_index_env = NULL;
340
int refresh_flags = REFRESH_QUIET;
341
342
if (is_status)
343
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
- }
344
345
if (*argv)
346
pathspec = get_pathspec(prefix, argv);
@@ -355,6 +348,33 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
348
if (read_cache_preload(pathspec) < 0)
349
die(_("index file corrupt"));
350
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
+
378
/*
379
* Non partial, non as-is commit.
380
*