Code

test-lib: unset GIT_NOTES_REF to stop it from influencing tests
[git.git] / builtin-commit.c
index 073fe90ba1e04c3845fe49521db39b28339acb43..55676fd874466c70445e81c69cf397cd01380aaf 100644 (file)
@@ -36,7 +36,20 @@ static const char * const builtin_status_usage[] = {
        NULL
 };
 
+static const char implicit_ident_advice[] =
+"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"
+"\n"
+"    git config --global user.name Your Name\n"
+"    git config --global user.email you@example.com\n"
+"\n"
+"If the identity used for this commit is wrong, you can fix it with:\n"
+"\n"
+"    git commit --amend --author='Your Name <you@example.com>'\n";
+
 static unsigned char head_sha1[20];
+
 static char *use_message_buffer;
 static const char commit_editmsg[] = "COMMIT_EDITMSG";
 static struct lock_file index_lock; /* real index */
@@ -68,7 +81,7 @@ static enum {
 } cleanup_mode;
 static char *cleanup_arg;
 
-static int use_editor = 1, initial_commit, in_merge;
+static int use_editor = 1, initial_commit, in_merge, include_status = 1;
 static const char *only_include_assumed;
 static struct strbuf message;
 
@@ -107,6 +120,7 @@ static struct option builtin_commit_options[] = {
        OPT_FILENAME('t', "template", &template_file, "use specified template file"),
        OPT_BOOLEAN('e', "edit", &edit_flag, "force edit of commit"),
        OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"),
+       OPT_BOOLEAN(0, "status", &include_status, "include status in commit message template"),
        /* end commit message options */
 
        OPT_GROUP("Commit contents options"),
@@ -183,11 +197,15 @@ static int list_paths(struct string_list *list, const char *with_tree,
 
        for (i = 0; i < active_nr; i++) {
                struct cache_entry *ce = active_cache[i];
+               struct string_list_item *item;
+
                if (ce->ce_flags & CE_UPDATE)
                        continue;
                if (!match_pathspec(pattern, ce->name, ce_namelen(ce), 0, m))
                        continue;
-               string_list_insert(ce->name, list);
+               item = string_list_insert(ce->name, list);
+               if (ce_skip_worktree(ce))
+                       item->util = item; /* better a valid pointer than a fake one */
        }
 
        return report_path_error(m, pattern, prefix ? strlen(prefix) : 0);
@@ -200,6 +218,10 @@ static void add_remove_files(struct string_list *list)
                struct stat st;
                struct string_list_item *p = &(list->items[i]);
 
+               /* p->util is skip-worktree */
+               if (p->util)
+                       continue;
+
                if (!lstat(p->string, &st)) {
                        if (add_to_cache(p->string, &st, 0))
                                die("updating files failed");
@@ -236,6 +258,16 @@ static void create_base_index(void)
                exit(128); /* We've already reported the error, finish dying */
 }
 
+static void refresh_cache_or_die(int refresh_flags)
+{
+       /*
+        * refresh_flags contains REFRESH_QUIET, so the only errors
+        * are for unmerged entries.
+        */
+       if (refresh_cache(refresh_flags | REFRESH_IN_PORCELAIN))
+               die_resolve_conflict("commit");
+}
+
 static char *prepare_index(int argc, const char **argv, const char *prefix, int is_status)
 {
        int fd;
@@ -275,7 +307,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
        if (all || (also && pathspec && *pathspec)) {
                int fd = hold_locked_index(&index_lock, 1);
                add_files_to_cache(also ? prefix : NULL, pathspec, 0);
-               refresh_cache(refresh_flags);
+               refresh_cache_or_die(refresh_flags);
                if (write_cache(fd, active_cache, active_nr) ||
                    close_lock_file(&index_lock))
                        die("unable to write new_index file");
@@ -294,7 +326,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
         */
        if (!pathspec || !*pathspec) {
                fd = hold_locked_index(&index_lock, 1);
-               refresh_cache(refresh_flags);
+               refresh_cache_or_die(refresh_flags);
                if (write_cache(fd, active_cache, active_nr) ||
                    commit_locked_index(&index_lock))
                        die("unable to write new_index file");
@@ -582,7 +614,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 
        /* This checks if committer ident is explicitly given */
        git_committer_info(0);
-       if (use_editor) {
+       if (use_editor && include_status) {
                char *author_ident;
                const char *committer_ident;
 
@@ -624,7 +656,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
                                author_ident);
                free(author_ident);
 
-               if (!user_ident_explicitly_given)
+               if (!user_ident_sufficiently_given())
                        fprintf(fp,
                                "%s"
                                "# Committer: %s\n",
@@ -1015,7 +1047,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
                s.pathspec = get_pathspec(prefix, argv);
 
        read_cache();
-       refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
+       refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, s.pathspec, NULL, NULL);
        s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;
        s.in_merge = in_merge;
        wt_status_collect(&s);
@@ -1046,9 +1078,12 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
 {
        struct rev_info rev;
        struct commit *commit;
-       static const char *format = "format:%h] %s";
+       struct strbuf format = STRBUF_INIT;
        unsigned char junk_sha1[20];
        const char *head = resolve_ref("HEAD", junk_sha1, 0, NULL);
+       struct pretty_print_context pctx = {0};
+       struct strbuf author_ident = STRBUF_INIT;
+       struct strbuf committer_ident = STRBUF_INIT;
 
        commit = lookup_commit(sha1);
        if (!commit)
@@ -1056,6 +1091,25 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
        if (!commit || parse_commit(commit))
                die("could not parse newly created commit");
 
+       strbuf_addstr(&format, "format:%h] %s");
+
+       format_commit_message(commit, "%an <%ae>", &author_ident, &pctx);
+       format_commit_message(commit, "%cn <%ce>", &committer_ident, &pctx);
+       if (strbuf_cmp(&author_ident, &committer_ident)) {
+               strbuf_addstr(&format, "\n Author: ");
+               strbuf_addbuf_percentquote(&format, &author_ident);
+       }
+       if (!user_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_release(&author_ident);
+       strbuf_release(&committer_ident);
+
        init_revisions(&rev, prefix);
        setup_revisions(0, NULL, &rev, NULL);
 
@@ -1066,7 +1120,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
 
        rev.verbose_header = 1;
        rev.show_root_diff = 1;
-       get_commit_format(format, &rev);
+       get_commit_format(format.buf, &rev);
        rev.always_show_header = 0;
        rev.diffopt.detect_rename = 1;
        rev.diffopt.rename_limit = 100;
@@ -1085,10 +1139,11 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
                struct pretty_print_context ctx = {0};
                struct strbuf buf = STRBUF_INIT;
                ctx.date_mode = DATE_NORMAL;
-               format_commit_message(commit, format + 7, &buf, &ctx);
+               format_commit_message(commit, format.buf + 7, &buf, &ctx);
                printf("%s\n", buf.buf);
                strbuf_release(&buf);
        }
+       strbuf_release(&format);
 }
 
 static int git_commit_config(const char *k, const char *v, void *cb)
@@ -1097,6 +1152,10 @@ static int git_commit_config(const char *k, const char *v, void *cb)
 
        if (!strcmp(k, "commit.template"))
                return git_config_pathname(&template_file, k, v);
+       if (!strcmp(k, "commit.status")) {
+               include_status = git_config_bool(k, v);
+               return 0;
+       }
 
        return git_status_config(k, v, s);
 }
@@ -1242,7 +1301,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
                     "new_index file. Check that disk is not full or quota is\n"
                     "not exceeded, and then \"git reset HEAD\" to recover.");
 
-       rerere();
+       rerere(0);
        run_hook(get_index_file(), "post-commit", NULL);
        if (!quiet)
                print_summary(prefix, commit_sha1);