Fix add_files_to_cache() to take pathspec, not user specified list of files
[gitweb.git] / builtin-commit.c
index 400ee93a936342ad379b110b41bab1334f65eb85..cd2f5cad1d3f1e985fcd2dfbc8537008d3cfbcbb 100644 (file)
@@ -30,13 +30,27 @@ static char *use_message_buffer;
 static const char commit_editmsg[] = "COMMIT_EDITMSG";
 static struct lock_file lock_file;
 
-static char *logfile, *force_author, *message, *template_file;
+static char *logfile, *force_author, *template_file;
 static char *edit_message, *use_message;
 static int all, edit_flag, also, interactive, only, amend, signoff;
 static int quiet, verbose, untracked_files, no_verify;
 
 static int no_edit, initial_commit, in_merge;
 const char *only_include_assumed;
+struct strbuf message;
+
+static int opt_parse_m(const struct option *opt, const char *arg, int unset)
+{
+       struct strbuf *buf = opt->value;
+       if (unset)
+               strbuf_setlen(buf, 0);
+       else {
+               strbuf_addstr(buf, arg);
+               strbuf_addch(buf, '\n');
+               strbuf_addch(buf, '\n');
+       }
+       return 0;
+}
 
 static struct option builtin_commit_options[] = {
        OPT__QUIET(&quiet),
@@ -45,7 +59,7 @@ static struct option builtin_commit_options[] = {
 
        OPT_STRING('F', "file", &logfile, "FILE", "read log from file"),
        OPT_STRING(0, "author", &force_author, "AUTHOR", "override author for commit"),
-       OPT_STRING('m', "message", &message, "MESSAGE", "specify commit message"),
+       OPT_CALLBACK('m', "message", &message, "MESSAGE", "specify commit message", opt_parse_m),
        OPT_STRING('c', "reedit-message", &edit_message, "COMMIT", "reuse and edit message from specified commit "),
        OPT_STRING('C', "reuse-message", &use_message, "COMMIT", "reuse message from specified commit"),
        OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by: header"),
@@ -95,6 +109,7 @@ static char *prepare_index(const char **files, const char *prefix)
 
        /* update the user index file */
        add_files_to_cache(verbose, prefix, files);
+       refresh_cache(REFRESH_QUIET);
        if (write_cache(fd, active_cache, active_nr) || close(fd))
                die("unable to write new_index file");
 
@@ -144,14 +159,14 @@ static const char sign_off_header[] = "Signed-off-by: ";
 static int prepare_log_message(const char *index_file, const char *prefix)
 {
        struct stat statbuf;
-       int commitable;
+       int commitable, saved_color_setting;
        struct strbuf sb;
        char *buffer;
        FILE *fp;
 
        strbuf_init(&sb, 0);
-       if (message) {
-               strbuf_add(&sb, message, strlen(message));
+       if (message.len) {
+               strbuf_addbuf(&sb, &message);
        } else if (logfile && !strcmp(logfile, "-")) {
                if (isatty(0))
                        fprintf(stderr, "(reading log message from standard input)\n");
@@ -183,21 +198,32 @@ static int prepare_log_message(const char *index_file, const char *prefix)
                die("could not open %s\n", git_path(commit_editmsg));
 
        stripspace(&sb, 0);
-       if (fwrite(sb.buf, 1, sb.len, fp) < sb.len)
-               die("could not write commit template: %s\n",
-                   strerror(errno));
 
        if (signoff) {
-               const char *info, *bol;
-
-               info = git_committer_info(1);
-               strbuf_addch(&sb, '\0');
-               bol = strrchr(sb.buf + sb.len - 1, '\n');
-               if (!bol || prefixcmp(bol, sign_off_header))
-                       fprintf(fp, "\n");
-               fprintf(fp, "%s%s\n", sign_off_header, git_committer_info(1));
+               struct strbuf sob;
+               int i;
+
+               strbuf_init(&sob, 0);
+               strbuf_addstr(&sob, sign_off_header);
+               strbuf_addstr(&sob, fmt_ident(getenv("GIT_COMMITTER_NAME"),
+                                             getenv("GIT_COMMITTER_EMAIL"),
+                                             "", 1));
+               strbuf_addch(&sob, '\n');
+
+               for (i = sb.len - 1; i > 0 && sb.buf[i - 1] != '\n'; i--)
+                       ; /* do nothing */
+               if (prefixcmp(sb.buf + i, sob.buf)) {
+                       if (prefixcmp(sb.buf + i, sign_off_header))
+                               strbuf_addch(&sb, '\n');
+                       strbuf_addbuf(&sb, &sob);
+               }
+               strbuf_release(&sob);
        }
 
+       if (fwrite(sb.buf, 1, sb.len, fp) < sb.len)
+               die("could not write commit template: %s\n",
+                   strerror(errno));
+
        strbuf_release(&sb);
 
        if (in_merge && !no_edit)
@@ -217,7 +243,10 @@ static int prepare_log_message(const char *index_file, const char *prefix)
        if (only_include_assumed)
                fprintf(fp, "# %s\n", only_include_assumed);
 
+       saved_color_setting = wt_status_use_color;
+       wt_status_use_color = 0;
        commitable = run_status(fp, index_file, prefix);
+       wt_status_use_color = saved_color_setting;
 
        fclose(fp);
 
@@ -311,7 +340,7 @@ static int parse_and_validate_options(int argc, const char *argv[])
        argc = parse_options(argc, argv, builtin_commit_options,
                             builtin_commit_usage, 0);
 
-       if (logfile || message || use_message)
+       if (logfile || message.len || use_message)
                no_edit = 1;
        if (edit_flag)
                no_edit = 0;
@@ -336,7 +365,7 @@ static int parse_and_validate_options(int argc, const char *argv[])
                f++;
        if (f > 1)
                die("Only one of -c/-C/-F can be used.");
-       if (message && f > 0)
+       if (message.len && f > 0)
                die("Option -m cannot be combined with -c/-C/-F.");
        if (edit_message)
                use_message = edit_message;
@@ -469,6 +498,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
        printf("Created %scommit ", initial_commit ? "initial " : "");
 
        log_tree_commit(&rev, commit);
+       printf("\n");
 }
 
 int git_commit_config(const char *k, const char *v)
@@ -488,7 +518,7 @@ static const char commit_utf8_warn[] =
 
 int cmd_commit(int argc, const char **argv, const char *prefix)
 {
-       int header_len, parent_count = 0;
+       int header_len;
        struct strbuf sb;
        const char *index_file, *reflog_msg;
        char *nl;
@@ -524,7 +554,6 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
        /* Determine parents */
        if (initial_commit) {
                reflog_msg = "commit (initial)";
-               parent_count = 0;
        } else if (amend) {
                struct commit_list *c;
                struct commit *commit;
@@ -565,12 +594,11 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 
        /* Get the commit message and validate it */
        header_len = sb.len;
-       if (!no_edit) {
-               fprintf(stderr, "launching editor, log %s\n", logfile);
+       if (!no_edit)
                launch_editor(git_path(commit_editmsg), &sb);
-       else if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0)
+       else if (strbuf_read_file(&sb, git_path(commit_editmsg), 0) < 0)
                die("could not read commit message\n");
-       if (run_hook(index_file, "commit-msg", commit_editmsg))
+       if (run_hook(index_file, "commit-msg", git_path(commit_editmsg)))
                exit(1);
        stripspace(&sb, 1);
        if (sb.len < header_len ||