Merge branch 'dt/cache-tree-repair'
[gitweb.git] / builtin / commit.c
index 72eb3beb36cd06f6e41d80d5761a953a1c3a1d56..41f481bd030ba96f8883e64517eda95931344545 100644 (file)
@@ -42,7 +42,20 @@ static const char * const builtin_status_usage[] = {
        NULL
 };
 
-static const char implicit_ident_advice[] =
+static const char implicit_ident_advice_noconfig[] =
+N_("Your name and email address were configured automatically based\n"
+"on your username and hostname. Please check that they are accurate.\n"
+"You can suppress this message by setting them explicitly. Run the\n"
+"following command and follow the instructions in your editor to edit\n"
+"your configuration file:\n"
+"\n"
+"    git config --global --edit\n"
+"\n"
+"After doing this, you may fix the identity used for this commit with:\n"
+"\n"
+"    git commit --amend --reset-author\n");
+
+static const char implicit_ident_advice_config[] =
 N_("Your name and email address were configured automatically based\n"
 "on your username and hostname. Please check that they are accurate.\n"
 "You can suppress this message by setting them explicitly:\n"
@@ -340,6 +353,13 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
 
                discard_cache();
                read_cache_from(index_lock.filename);
+               if (update_main_cache_tree(WRITE_TREE_SILENT) == 0) {
+                       if (reopen_lock_file(&index_lock) < 0)
+                               die(_("unable to write index file"));
+                       if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
+                               die(_("unable to update temporary index"));
+               } else
+                       warning(_("Failed to update main cache tree"));
 
                commit_style = COMMIT_NORMAL;
                return index_lock.filename;
@@ -380,8 +400,12 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
        if (!only && !pathspec.nr) {
                hold_locked_index(&index_lock, 1);
                refresh_cache_or_die(refresh_flags);
-               if (active_cache_changed) {
+               if (active_cache_changed
+                   || !cache_tree_fully_valid(active_cache_tree)) {
                        update_main_cache_tree(WRITE_TREE_SILENT);
+                       active_cache_changed = 1;
+               }
+               if (active_cache_changed) {
                        if (write_locked_index(&the_index, &index_lock,
                                               COMMIT_LOCK))
                                die(_("unable to write new_index file"));
@@ -420,8 +444,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
                        die(_("cannot do a partial commit during a cherry-pick."));
        }
 
-       memset(&partial, 0, sizeof(partial));
-       partial.strdup_strings = 1;
+       string_list_init(&partial, 1);
        if (list_paths(&partial, !current_head ? NULL : "HEAD", prefix, &pathspec))
                exit(1);
 
@@ -432,6 +455,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
        hold_locked_index(&index_lock, 1);
        add_remove_files(&partial);
        refresh_cache(REFRESH_QUIET);
+       update_main_cache_tree(WRITE_TREE_SILENT);
        if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
                die(_("unable to write new_index file"));
 
@@ -702,7 +726,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
                char *buffer;
                buffer = strstr(use_message_buffer, "\n\n");
                if (buffer)
-                       strbuf_add(&sb, buffer + 2, strlen(buffer + 2));
+                       strbuf_addstr(&sb, buffer + 2);
                hook_arg1 = "commit";
                hook_arg2 = use_message;
        } else if (fixup_message) {
@@ -1049,7 +1073,8 @@ static const char *find_author_by_nickname(const char *name)
        revs.mailmap = &mailmap;
        read_mailmap(revs.mailmap, NULL);
 
-       prepare_revision_walk(&revs);
+       if (prepare_revision_walk(&revs))
+               die(_("revision walk setup failed"));
        commit = get_revision(&revs);
        if (commit) {
                struct pretty_print_context ctx = {0};
@@ -1403,6 +1428,24 @@ int cmd_status(int argc, const char **argv, const char *prefix)
        return 0;
 }
 
+static const char *implicit_ident_advice(void)
+{
+       char *user_config = NULL;
+       char *xdg_config = NULL;
+       int config_exists;
+
+       home_config_paths(&user_config, &xdg_config, "config");
+       config_exists = file_exists(user_config) || file_exists(xdg_config);
+       free(user_config);
+       free(xdg_config);
+
+       if (config_exists)
+               return _(implicit_ident_advice_config);
+       else
+               return _(implicit_ident_advice_noconfig);
+
+}
+
 static void print_summary(const char *prefix, const unsigned char *sha1,
                          int initial_commit)
 {
@@ -1441,7 +1484,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1,
                strbuf_addbuf_percentquote(&format, &committer_ident);
                if (advice_implicit_identity) {
                        strbuf_addch(&format, '\n');
-                       strbuf_addstr(&format, _(implicit_ident_advice));
+                       strbuf_addstr(&format, implicit_ident_advice());
                }
        }
        strbuf_release(&author_ident);
@@ -1509,7 +1552,7 @@ static int run_rewrite_hook(const unsigned char *oldsha1,
 {
        /* oldsha1 SP newsha1 LF NUL */
        static char buf[2*40 + 3];
-       struct child_process proc;
+       struct child_process proc = CHILD_PROCESS_INIT;
        const char *argv[3];
        int code;
        size_t n;
@@ -1521,7 +1564,6 @@ static int run_rewrite_hook(const unsigned char *oldsha1,
        argv[1] = "amend";
        argv[2] = NULL;
 
-       memset(&proc, 0, sizeof(proc));
        proc.argv = argv;
        proc.in = -1;
        proc.stdout_to_stderr = 1;
@@ -1621,11 +1663,12 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
        const char *index_file, *reflog_msg;
        char *nl;
        unsigned char sha1[20];
-       struct ref_lock *ref_lock;
        struct commit_list *parents = NULL, **pptr = &parents;
        struct stat statbuf;
        struct commit *current_head = NULL;
        struct commit_extra_header *extra = NULL;
+       struct ref_transaction *transaction;
+       struct strbuf err = STRBUF_INIT;
 
        if (argc == 2 && !strcmp(argv[1], "-h"))
                usage_with_options(builtin_commit_usage, builtin_commit_options);
@@ -1747,16 +1790,6 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
        strbuf_release(&author_ident);
        free_commit_extra_headers(extra);
 
-       ref_lock = lock_any_ref_for_update("HEAD",
-                                          !current_head
-                                          ? NULL
-                                          : current_head->object.sha1,
-                                          0, NULL);
-       if (!ref_lock) {
-               rollback_index_files();
-               die(_("cannot lock HEAD ref"));
-       }
-
        nl = strchr(sb.buf, '\n');
        if (nl)
                strbuf_setlen(&sb, nl + 1 - sb.buf);
@@ -1765,10 +1798,17 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
        strbuf_insert(&sb, 0, reflog_msg, strlen(reflog_msg));
        strbuf_insert(&sb, strlen(reflog_msg), ": ", 2);
 
-       if (write_ref_sha1(ref_lock, sha1, sb.buf) < 0) {
+       transaction = ref_transaction_begin(&err);
+       if (!transaction ||
+           ref_transaction_update(transaction, "HEAD", sha1,
+                                  current_head
+                                  ? current_head->object.sha1 : NULL,
+                                  0, !!current_head, &err) ||
+           ref_transaction_commit(transaction, sb.buf, &err)) {
                rollback_index_files();
-               die(_("cannot update HEAD ref"));
+               die("%s", err.buf);
        }
+       ref_transaction_free(transaction);
 
        unlink(git_path("CHERRY_PICK_HEAD"));
        unlink(git_path("REVERT_HEAD"));
@@ -1797,5 +1837,6 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
        if (!quiet)
                print_summary(prefix, sha1, !current_head);
 
+       strbuf_release(&err);
        return 0;
 }