Use a temporary index for git commit --interactive
authorConrad Irwin <conrad.irwin@gmail.com>
Sat, 7 May 2011 05:59:59 +0000 (22:59 -0700)
committerJunio C Hamano <gitster@pobox.com>
Sun, 8 May 2011 19:28:30 +0000 (12:28 -0700)
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>
Documentation/git-commit.txt
builtin/commit.c
index d0534b8c05c5780a7f9debc664dd21af6739a3ec..ed50271aab4920a6135b8d8870ec22f45f2e779f 100644 (file)
@@ -41,7 +41,8 @@ The content to be added can be specified in several ways:
 
 5. by using the --interactive switch with the 'commit' command to decide one
    by one which files should be part of the commit, before finalizing the
-   operation.  Currently, this is done by invoking 'git add --interactive'.
+   operation.  Currently, this is done by invoking 'git add --interactive'
+   on a temporary index.
 
 The `--dry-run` option can be used to obtain a
 summary of what is included by any of the above for the next
index 67757e999fba514b62767d3351031e9eb6ef8c10..636aea6d6259b6151670fa6259b2eb59509a80b2 100644 (file)
@@ -336,18 +336,11 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
        int fd;
        struct string_list partial;
        const char **pathspec = NULL;
+       char *old_index_env = NULL;
        int refresh_flags = REFRESH_QUIET;
 
        if (is_status)
                refresh_flags |= REFRESH_UNMERGED;
-       if (interactive) {
-               if (interactive_add(argc, argv, prefix) != 0)
-                       die(_("interactive add failed"));
-               if (read_cache_preload(NULL) < 0)
-                       die(_("index file corrupt"));
-               commit_style = COMMIT_AS_IS;
-               return get_index_file();
-       }
 
        if (*argv)
                pathspec = get_pathspec(prefix, argv);
@@ -355,6 +348,33 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
        if (read_cache_preload(pathspec) < 0)
                die(_("index file corrupt"));
 
+       if (interactive) {
+               fd = hold_locked_index(&index_lock, 1);
+
+               refresh_cache_or_die(refresh_flags);
+
+               if (write_cache(fd, active_cache, active_nr) ||
+                   close_lock_file(&index_lock))
+                       die(_("unable to create temporary index"));
+
+               old_index_env = getenv(INDEX_ENVIRONMENT);
+               setenv(INDEX_ENVIRONMENT, index_lock.filename, 1);
+
+               if (interactive_add(argc, argv, prefix) != 0)
+                       die(_("interactive add failed"));
+
+               if (old_index_env && *old_index_env)
+                       setenv(INDEX_ENVIRONMENT, old_index_env, 1);
+               else
+                       unsetenv(INDEX_ENVIRONMENT);
+
+               discard_cache();
+               read_cache_from(index_lock.filename);
+
+               commit_style = COMMIT_NORMAL;
+               return index_lock.filename;
+       }
+
        /*
         * Non partial, non as-is commit.
         *