builtin-commit: resurrect behavior for multiple -m options
[gitweb.git] / builtin-commit.c
index c8f79a88fb16b0ab142ea95790dbb9593f386d3d..ee79cf1b72e0e250c19f075791282e6c7162e730 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"),
@@ -81,6 +95,7 @@ static char *prepare_index(const char **files, const char *prefix)
 
        if (all || also) {
                add_files_to_cache(verbose, also ? prefix : NULL, files);
+               refresh_cache(REFRESH_QUIET);
                if (write_cache(fd, active_cache, active_nr) || close(fd))
                        die("unable to write new_index file");
                return lock_file.filename;
@@ -110,17 +125,19 @@ static char *prepare_index(const char **files, const char *prefix)
        fd = hold_lock_file_for_update(next_index_lock,
                                       git_path("next-index-%d", getpid()), 1);
        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");
 
        return next_index_lock->filename;
 }
 
-static int run_status(FILE *fp, const char *index_file)
+static int run_status(FILE *fp, const char *index_file, const char *prefix)
 {
        struct wt_status s;
 
        wt_status_prepare(&s);
+       s.prefix = prefix;
 
        if (amend) {
                s.amend = 1;
@@ -138,7 +155,7 @@ static int run_status(FILE *fp, const char *index_file)
 
 static const char sign_off_header[] = "Signed-off-by: ";
 
-static int prepare_log_message(const char *index_file)
+static int prepare_log_message(const char *index_file, const char *prefix)
 {
        struct stat statbuf;
        int commitable;
@@ -147,8 +164,8 @@ static int prepare_log_message(const char *index_file)
        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");
@@ -180,21 +197,32 @@ static int prepare_log_message(const char *index_file)
                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)
@@ -214,7 +242,7 @@ static int prepare_log_message(const char *index_file)
        if (only_include_assumed)
                fprintf(fp, "# %s\n", only_include_assumed);
 
-       commitable = run_status(fp, index_file);
+       commitable = run_status(fp, index_file, prefix);
 
        fclose(fp);
 
@@ -308,7 +336,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;
@@ -333,7 +361,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;
@@ -407,7 +435,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
 
        index_file = prepare_index(argv, prefix);
 
-       commitable = run_status(stdout, index_file);
+       commitable = run_status(stdout, index_file, prefix);
 
        rollback_lock_file(&lock_file);
 
@@ -501,8 +529,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
        if (!no_verify && run_hook(index_file, "pre-commit", NULL))
                exit(1);
 
-       if (!prepare_log_message(index_file) && !in_merge) {
-               run_status(stdout, index_file);
+       if (!prepare_log_message(index_file, prefix) && !in_merge) {
+               run_status(stdout, index_file, prefix);
                unlink(commit_editmsg);
                return 1;
        }