Merge branch 'dt/cache-tree-repair'
[gitweb.git] / builtin / commit.c
index 77570c48ba02adf7aa78654c644b13478246a4e0..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"
@@ -305,7 +318,6 @@ static void refresh_cache_or_die(int refresh_flags)
 static char *prepare_index(int argc, const char **argv, const char *prefix,
                           const struct commit *current_head, int is_status)
 {
-       int fd;
        struct string_list partial;
        struct pathspec pathspec;
        int refresh_flags = REFRESH_QUIET;
@@ -321,12 +333,11 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
 
        if (interactive) {
                char *old_index_env = NULL;
-               fd = hold_locked_index(&index_lock, 1);
+               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))
+               if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
                        die(_("unable to create temporary index"));
 
                old_index_env = getenv(INDEX_ENVIRONMENT);
@@ -343,14 +354,10 @@ 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) {
-                       fd = open(index_lock.filename, O_WRONLY);
-                       if (fd >= 0)
-                               if (write_cache(fd, active_cache, active_nr) < 0)
-                                       die(_("unable to write index file"));
-                               else
-                                       close_lock_file(&index_lock);
-                       else
+                       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"));
 
@@ -371,12 +378,11 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
         * (B) on failure, rollback the real index.
         */
        if (all || (also && pathspec.nr)) {
-               fd = hold_locked_index(&index_lock, 1);
+               hold_locked_index(&index_lock, 1);
                add_files_to_cache(also ? prefix : NULL, &pathspec, 0);
                refresh_cache_or_die(refresh_flags);
                update_main_cache_tree(WRITE_TREE_SILENT);
-               if (write_cache(fd, active_cache, active_nr) ||
-                   close_lock_file(&index_lock))
+               if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
                        die(_("unable to write new_index file"));
                commit_style = COMMIT_NORMAL;
                return index_lock.filename;
@@ -392,7 +398,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
         * We still need to refresh the index here.
         */
        if (!only && !pathspec.nr) {
-               fd = hold_locked_index(&index_lock, 1);
+               hold_locked_index(&index_lock, 1);
                refresh_cache_or_die(refresh_flags);
                if (active_cache_changed
                    || !cache_tree_fully_valid(active_cache_tree)) {
@@ -400,8 +406,8 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
                        active_cache_changed = 1;
                }
                if (active_cache_changed) {
-                       if (write_cache(fd, active_cache, active_nr) ||
-                           commit_locked_index(&index_lock))
+                       if (write_locked_index(&the_index, &index_lock,
+                                              COMMIT_LOCK))
                                die(_("unable to write new_index file"));
                } else {
                        rollback_lock_file(&index_lock);
@@ -438,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);
 
@@ -447,25 +452,23 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
        if (read_cache() < 0)
                die(_("cannot read the index"));
 
-       fd = hold_locked_index(&index_lock, 1);
+       hold_locked_index(&index_lock, 1);
        add_remove_files(&partial);
        refresh_cache(REFRESH_QUIET);
        update_main_cache_tree(WRITE_TREE_SILENT);
-       if (write_cache(fd, active_cache, active_nr) ||
-           close_lock_file(&index_lock))
+       if (write_locked_index(&the_index, &index_lock, CLOSE_LOCK))
                die(_("unable to write new_index file"));
 
-       fd = hold_lock_file_for_update(&false_lock,
-                                      git_path("next-index-%"PRIuMAX,
-                                               (uintmax_t) getpid()),
-                                      LOCK_DIE_ON_ERROR);
+       hold_lock_file_for_update(&false_lock,
+                                 git_path("next-index-%"PRIuMAX,
+                                          (uintmax_t) getpid()),
+                                 LOCK_DIE_ON_ERROR);
 
        create_base_index(current_head);
        add_remove_files(&partial);
        refresh_cache(REFRESH_QUIET);
 
-       if (write_cache(fd, active_cache, active_nr) ||
-           close_lock_file(&false_lock))
+       if (write_locked_index(&the_index, &false_lock, CLOSE_LOCK))
                die(_("unable to write temporary index file"));
 
        discard_cache();
@@ -542,10 +545,29 @@ static int sane_ident_split(struct ident_split *person)
        return 1;
 }
 
+static int parse_force_date(const char *in, char *out, int len)
+{
+       if (len < 1)
+               return -1;
+       *out++ = '@';
+       len--;
+
+       if (parse_date(in, out, len) < 0) {
+               int errors = 0;
+               unsigned long t = approxidate_careful(in, &errors);
+               if (errors)
+                       return -1;
+               snprintf(out, len, "%lu", t);
+       }
+
+       return 0;
+}
+
 static void determine_author_info(struct strbuf *author_ident)
 {
        char *name, *email, *date;
        struct ident_split author;
+       char date_buf[64];
 
        name = getenv("GIT_AUTHOR_NAME");
        email = getenv("GIT_AUTHOR_EMAIL");
@@ -590,8 +612,12 @@ static void determine_author_info(struct strbuf *author_ident)
                email = xstrndup(lb + 2, rb - (lb + 2));
        }
 
-       if (force_date)
-               date = force_date;
+       if (force_date) {
+               if (parse_force_date(force_date, date_buf, sizeof(date_buf)))
+                       die(_("invalid date format: %s"), force_date);
+               date = date_buf;
+       }
+
        strbuf_addstr(author_ident, fmt_ident(name, email, date, IDENT_STRICT));
        if (!split_ident_line(&author, author_ident->buf, author_ident->len) &&
            sane_ident_split(&author)) {
@@ -601,13 +627,46 @@ static void determine_author_info(struct strbuf *author_ident)
        }
 }
 
-static char *cut_ident_timestamp_part(char *string)
+static void split_ident_or_die(struct ident_split *id, const struct strbuf *buf)
+{
+       if (split_ident_line(id, buf->buf, buf->len) ||
+           !sane_ident_split(id))
+               die(_("Malformed ident string: '%s'"), buf->buf);
+}
+
+static int author_date_is_interesting(void)
+{
+       return author_message || force_date;
+}
+
+static void adjust_comment_line_char(const struct strbuf *sb)
 {
-       char *ket = strrchr(string, '>');
-       if (!ket || ket[1] != ' ')
-               die(_("Malformed ident string: '%s'"), string);
-       *++ket = '\0';
-       return ket;
+       char candidates[] = "#;@!$%^&|:";
+       char *candidate;
+       const char *p;
+
+       comment_line_char = candidates[0];
+       if (!memchr(sb->buf, comment_line_char, sb->len))
+               return;
+
+       p = sb->buf;
+       candidate = strchr(candidates, *p);
+       if (candidate)
+               *candidate = ' ';
+       for (p = sb->buf; *p; p++) {
+               if ((p[0] == '\n' || p[0] == '\r') && p[1]) {
+                       candidate = strchr(candidates, p[1]);
+                       if (candidate)
+                               *candidate = ' ';
+               }
+       }
+
+       for (p = candidates; *p == ' '; p++)
+               ;
+       if (!*p)
+               die(_("unable to select a comment character that is not used\n"
+                     "in the current commit message"));
+       comment_line_char = *p;
 }
 
 static int prepare_to_commit(const char *index_file, const char *prefix,
@@ -667,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) {
@@ -763,6 +822,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
        if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
                die_errno(_("could not write commit template"));
 
+       if (auto_comment_line_char)
+               adjust_comment_line_char(&sb);
        strbuf_release(&sb);
 
        /* This checks if committer ident is explicitly given */
@@ -770,7 +831,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
        if (use_editor && include_status) {
                int ident_shown = 0;
                int saved_color_setting;
-               char *ai_tmp, *ci_tmp;
+               struct ident_split ci, ai;
+
                if (whence != FROM_COMMIT) {
                        if (cleanup_mode == CLEANUP_SCISSORS)
                                wt_status_add_cut_line(s->fp);
@@ -810,32 +872,39 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
                        status_printf_ln(s, GIT_COLOR_NORMAL,
                                        "%s", only_include_assumed);
 
-               ai_tmp = cut_ident_timestamp_part(author_ident->buf);
-               ci_tmp = cut_ident_timestamp_part(committer_ident.buf);
-               if (strcmp(author_ident->buf, committer_ident.buf))
+               split_ident_or_die(&ai, author_ident);
+               split_ident_or_die(&ci, &committer_ident);
+
+               if (ident_cmp(&ai, &ci))
+                       status_printf_ln(s, GIT_COLOR_NORMAL,
+                               _("%s"
+                               "Author:    %.*s <%.*s>"),
+                               ident_shown++ ? "" : "\n",
+                               (int)(ai.name_end - ai.name_begin), ai.name_begin,
+                               (int)(ai.mail_end - ai.mail_begin), ai.mail_begin);
+
+               if (author_date_is_interesting())
                        status_printf_ln(s, GIT_COLOR_NORMAL,
                                _("%s"
-                               "Author:    %s"),
+                               "Date:      %s"),
                                ident_shown++ ? "" : "\n",
-                               author_ident->buf);
+                               show_ident_date(&ai, DATE_NORMAL));
 
                if (!committer_ident_sufficiently_given())
                        status_printf_ln(s, GIT_COLOR_NORMAL,
                                _("%s"
-                               "Committer: %s"),
+                               "Committer: %.*s <%.*s>"),
                                ident_shown++ ? "" : "\n",
-                               committer_ident.buf);
+                               (int)(ci.name_end - ci.name_begin), ci.name_begin,
+                               (int)(ci.mail_end - ci.mail_begin), ci.mail_begin);
 
                if (ident_shown)
-                       status_printf_ln(s, GIT_COLOR_NORMAL, "");
+                       status_printf_ln(s, GIT_COLOR_NORMAL, "%s", "");
 
                saved_color_setting = s->use_color;
                s->use_color = 0;
                commitable = run_status(s->fp, index_file, prefix, 1, s);
                s->use_color = saved_color_setting;
-
-               *ai_tmp = ' ';
-               *ci_tmp = ' ';
        } else {
                unsigned char sha1[20];
                const char *parent = "HEAD";
@@ -970,7 +1039,7 @@ static int message_is_empty(struct strbuf *sb)
 static int template_untouched(struct strbuf *sb)
 {
        struct strbuf tmpl = STRBUF_INIT;
-       char *start;
+       const char *start;
 
        if (cleanup_mode == CLEANUP_NONE && sb->len)
                return 0;
@@ -979,8 +1048,7 @@ static int template_untouched(struct strbuf *sb)
                return 0;
 
        stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
-       start = (char *)skip_prefix(sb->buf, tmpl.buf);
-       if (!start)
+       if (!skip_prefix(sb->buf, tmpl.buf, &start))
                start = sb->buf;
        strbuf_release(&tmpl);
        return rest_is_empty(sb, start - sb->buf);
@@ -1005,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};
@@ -1359,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)
 {
@@ -1385,12 +1472,19 @@ static void print_summary(const char *prefix, const unsigned char *sha1,
                strbuf_addstr(&format, "\n Author: ");
                strbuf_addbuf_percentquote(&format, &author_ident);
        }
+       if (author_date_is_interesting()) {
+               struct strbuf date = STRBUF_INIT;
+               format_commit_message(commit, "%ad", &date, &pctx);
+               strbuf_addstr(&format, "\n Date: ");
+               strbuf_addbuf_percentquote(&format, &date);
+               strbuf_release(&date);
+       }
        if (!committer_ident_sufficiently_given()) {
                strbuf_addstr(&format, "\n Committer: ");
                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);
@@ -1458,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;
@@ -1470,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;
@@ -1570,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);
@@ -1688,20 +1782,14 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
                append_merge_tag_headers(parents, &tail);
        }
 
-       if (commit_tree_extended(&sb, active_cache_tree->sha1, parents, sha1,
-                                author_ident.buf, sign_commit, extra)) {
+       if (commit_tree_extended(sb.buf, sb.len, active_cache_tree->sha1,
+                        parents, sha1, author_ident.buf, sign_commit, extra)) {
                rollback_index_files();
                die(_("failed to write commit object"));
        }
        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);
-
        nl = strchr(sb.buf, '\n');
        if (nl)
                strbuf_setlen(&sb, nl + 1 - sb.buf);
@@ -1710,14 +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 (!ref_lock) {
-               rollback_index_files();
-               die(_("cannot lock HEAD ref"));
-       }
-       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"));
@@ -1746,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;
 }