Code

Make 'unpack_trees()' have a separate source and destination index
[git.git] / builtin-commit.c
index e8cb32059091ce0fdc693096617f08e93203e0e8..660a3458f7f4ef24dfa4fd5bdf902174da1eefb4 100644 (file)
@@ -7,6 +7,7 @@
 
 #include "cache.h"
 #include "cache-tree.h"
+#include "color.h"
 #include "dir.h"
 #include "builtin.h"
 #include "diff.h"
@@ -160,7 +161,7 @@ static int list_paths(struct path_list *list, const char *with_tree,
 
        for (i = 0; i < active_nr; i++) {
                struct cache_entry *ce = active_cache[i];
-               if (ce->ce_flags & htons(CE_UPDATE))
+               if (ce->ce_flags & CE_UPDATE)
                        continue;
                if (!pathspec_match(pattern, m, ce->name, 0))
                        continue;
@@ -197,6 +198,8 @@ static void create_base_index(void)
        opts.head_idx = 1;
        opts.index_only = 1;
        opts.merge = 1;
+       opts.src_index = &the_index;
+       opts.dst_index = &the_index;
 
        opts.fn = oneway_merge;
        tree = parse_tree_indirect(head_sha1);
@@ -204,7 +207,8 @@ static void create_base_index(void)
                die("failed to unpack HEAD tree object");
        parse_tree(tree);
        init_tree_desc(&t, tree->buffer, tree->size);
-       unpack_trees(1, &t, &opts);
+       if (unpack_trees(1, &t, &opts))
+               exit(128); /* We've already reported the error, finish dying */
 }
 
 static char *prepare_index(int argc, const char **argv, const char *prefix)
@@ -317,6 +321,10 @@ static char *prepare_index(int argc, const char **argv, const char *prefix)
        if (write_cache(fd, active_cache, active_nr) ||
            close_lock_file(&false_lock))
                die("unable to write temporary index file");
+
+       discard_cache();
+       read_cache_from(false_lock.filename);
+
        return false_lock.filename;
 }
 
@@ -394,6 +402,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
        struct strbuf sb;
        char *buffer;
        FILE *fp;
+       const char *hook_arg1 = NULL;
+       const char *hook_arg2 = NULL;
 
        if (!no_verify && run_hook(index_file, "pre-commit", NULL))
                return 0;
@@ -401,32 +411,47 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
        strbuf_init(&sb, 0);
        if (message.len) {
                strbuf_addbuf(&sb, &message);
+               hook_arg1 = "message";
        } else if (logfile && !strcmp(logfile, "-")) {
                if (isatty(0))
                        fprintf(stderr, "(reading log message from standard input)\n");
                if (strbuf_read(&sb, 0, 0) < 0)
                        die("could not read log from standard input");
+               hook_arg1 = "message";
        } else if (logfile) {
                if (strbuf_read_file(&sb, logfile, 0) < 0)
                        die("could not read log file '%s': %s",
                            logfile, strerror(errno));
+               hook_arg1 = "message";
        } else if (use_message) {
                buffer = strstr(use_message_buffer, "\n\n");
                if (!buffer || buffer[2] == '\0')
                        die("commit has empty message");
                strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
+               hook_arg1 = "commit";
+               hook_arg2 = use_message;
        } else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
                if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
                        die("could not read MERGE_MSG: %s", strerror(errno));
+               hook_arg1 = "merge";
        } else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
                if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
                        die("could not read SQUASH_MSG: %s", strerror(errno));
+               hook_arg1 = "squash";
        } else if (template_file && !stat(template_file, &statbuf)) {
                if (strbuf_read_file(&sb, template_file, 0) < 0)
                        die("could not read %s: %s",
                            template_file, strerror(errno));
+               hook_arg1 = "template";
        }
 
+       /*
+        * This final case does not modify the template message,
+        * it just sets the argument to the prepare-commit-msg hook.
+        */
+       else if (in_merge)
+               hook_arg1 = "merge";
+
        fp = fopen(git_path(commit_editmsg), "w");
        if (fp == NULL)
                die("could not open %s", git_path(commit_editmsg));
@@ -534,6 +559,10 @@ static int prepare_to_commit(const char *index_file, const char *prefix)
                return 0;
        }
 
+       if (run_hook(index_file, "prepare-commit-msg",
+                    git_path(commit_editmsg), hook_arg1, hook_arg2, NULL))
+               return 0;
+
        if (use_editor) {
                char index[PATH_MAX];
                const char *env[2] = { index, NULL };
@@ -746,6 +775,9 @@ int cmd_status(int argc, const char **argv, const char *prefix)
 
        git_config(git_status_config);
 
+       if (wt_status_use_color == -1)
+               wt_status_use_color = git_use_color_default;
+
        argc = parse_and_validate_options(argc, argv, builtin_status_usage);
 
        index_file = prepare_index(argc, argv, prefix);
@@ -798,6 +830,8 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
 int git_commit_config(const char *k, const char *v)
 {
        if (!strcmp(k, "commit.template")) {
+               if (!v)
+                       return config_error_nonbool(v);
                template_file = xstrdup(v);
                return 0;
        }
@@ -945,6 +979,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 
        unlink(git_path("MERGE_HEAD"));
        unlink(git_path("MERGE_MSG"));
+       unlink(git_path("SQUASH_MSG"));
 
        if (commit_index_files())
                die ("Repository has been updated, but unable to write\n"