Code

Merge branch 'jk/1.7.0-status'
[git.git] / builtin-commit.c
index f49b598cbdee2c48571b84567c8692d61443224f..6822aa07ca80ee648fd331a79f54915cd338f8d2 100644 (file)
@@ -36,7 +36,7 @@ static const char * const builtin_status_usage[] = {
        NULL
 };
 
-static unsigned char head_sha1[20], merge_head_sha1[20];
+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 */
@@ -52,7 +52,7 @@ static const char *template_file;
 static char *edit_message, *use_message;
 static char *author_name, *author_email, *author_date;
 static int all, edit_flag, also, interactive, only, amend, signoff;
-static int quiet, verbose, no_verify, allow_empty, dry_run;
+static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
 static char *untracked_files_arg;
 /*
  * The default commit message cleanup mode will remove the lines
@@ -79,8 +79,6 @@ static enum {
        STATUS_FORMAT_PORCELAIN,
 } status_format = STATUS_FORMAT_LONG;
 
-static void short_print(struct wt_status *s, int null_termination);
-
 static int opt_parse_m(const struct option *opt, const char *arg, int unset)
 {
        struct strbuf *buf = opt->value;
@@ -101,8 +99,9 @@ static struct option builtin_commit_options[] = {
        OPT_FILENAME('F', "file", &logfile, "read log from file"),
        OPT_STRING(0, "author", &force_author, "AUTHOR", "override author for commit"),
        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', "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(0, "reset-author", &renew_authorship, "the commit is authored by me now (used with -C-c/--amend)"),
        OPT_BOOLEAN('s', "signoff", &signoff, "add Signed-off-by:"),
        OPT_FILENAME('t', "template", &template_file, "use specified template file"),
        OPT_BOOLEAN('e', "edit", &edit_flag, "force edit of commit"),
@@ -321,7 +320,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
         */
        commit_style = COMMIT_PARTIAL;
 
-       if (file_exists(git_path("MERGE_HEAD")))
+       if (in_merge)
                die("cannot do a partial commit during a merge.");
 
        memset(&partial, 0, sizeof(partial));
@@ -381,10 +380,10 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
 
        switch (status_format) {
        case STATUS_FORMAT_SHORT:
-               short_print(s, null_termination);
+               wt_shortstatus_print(s, null_termination);
                break;
        case STATUS_FORMAT_PORCELAIN:
-               short_print(s, null_termination);
+               wt_porcelain_print(s, null_termination);
                break;
        case STATUS_FORMAT_LONG:
                wt_status_print(s);
@@ -412,7 +411,7 @@ static void determine_author_info(void)
        email = getenv("GIT_AUTHOR_EMAIL");
        date = getenv("GIT_AUTHOR_DATE");
 
-       if (use_message) {
+       if (use_message && !renew_authorship) {
                const char *a, *lb, *rb, *eol;
 
                a = strstr(use_message_buffer, "\nauthor ");
@@ -445,6 +444,47 @@ static void determine_author_info(void)
        author_date = date;
 }
 
+static int ends_rfc2822_footer(struct strbuf *sb)
+{
+       int ch;
+       int hit = 0;
+       int i, j, k;
+       int len = sb->len;
+       int first = 1;
+       const char *buf = sb->buf;
+
+       for (i = len - 1; i > 0; i--) {
+               if (hit && buf[i] == '\n')
+                       break;
+               hit = (buf[i] == '\n');
+       }
+
+       while (i < len - 1 && buf[i] == '\n')
+               i++;
+
+       for (; i < len; i = k) {
+               for (k = i; k < len && buf[k] != '\n'; k++)
+                       ; /* do nothing */
+               k++;
+
+               if ((buf[k] == ' ' || buf[k] == '\t') && !first)
+                       continue;
+
+               first = 0;
+
+               for (j = 0; i + j < len; j++) {
+                       ch = buf[i + j];
+                       if (ch == ':')
+                               break;
+                       if (isalnum(ch) ||
+                           (ch == '-'))
+                               continue;
+                       return 0;
+               }
+       }
+       return 1;
+}
+
 static int prepare_to_commit(const char *index_file, const char *prefix,
                             struct wt_status *s)
 {
@@ -520,7 +560,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
                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))
+                       if (!i || !ends_rfc2822_footer(&sb))
                                strbuf_addch(&sb, '\n');
                        strbuf_addbuf(&sb, &sob);
                }
@@ -715,8 +755,10 @@ static const char *find_author_by_nickname(const char *name)
        prepare_revision_walk(&revs);
        commit = get_revision(&revs);
        if (commit) {
+               struct pretty_print_context ctx = {0};
+               ctx.date_mode = DATE_NORMAL;
                strbuf_release(&buf);
-               format_commit_message(commit, "%an <%ae>", &buf, DATE_NORMAL);
+               format_commit_message(commit, "%an <%ae>", &buf, &ctx);
                return strbuf_detach(&buf, NULL);
        }
        die("No existing author found with '%s'", name);
@@ -750,6 +792,9 @@ static int parse_and_validate_options(int argc, const char *argv[],
        if (force_author && !strchr(force_author, '>'))
                force_author = find_author_by_nickname(force_author);
 
+       if (force_author && renew_authorship)
+               die("Using both --reset-author and --author does not make sense");
+
        if (logfile || message.len || use_message)
                use_editor = 0;
        if (edit_flag)
@@ -760,9 +805,6 @@ static int parse_and_validate_options(int argc, const char *argv[],
        if (get_sha1("HEAD", head_sha1))
                initial_commit = 1;
 
-       if (!get_sha1("MERGE_HEAD", merge_head_sha1))
-               in_merge = 1;
-
        /* Sanity check options */
        if (amend && initial_commit)
                die("You have nothing to amend.");
@@ -783,6 +825,8 @@ static int parse_and_validate_options(int argc, const char *argv[],
                use_message = edit_message;
        if (amend && !use_message)
                use_message = "HEAD";
+       if (!use_message && renew_authorship)
+               die("--reset-author can be used only with -C, -c or --amend.");
        if (use_message) {
                unsigned char sha1[20];
                static char utf8[] = "UTF-8";
@@ -883,7 +927,7 @@ static int parse_status_slot(const char *var, int offset)
                return WT_STATUS_NOBRANCH;
        if (!strcasecmp(var+offset, "unmerged"))
                return WT_STATUS_UNMERGED;
-       die("bad config variable '%s'", var);
+       return -1;
 }
 
 static int git_status_config(const char *k, const char *v, void *cb)
@@ -903,6 +947,8 @@ static int git_status_config(const char *k, const char *v, void *cb)
        }
        if (!prefixcmp(k, "status.color.") || !prefixcmp(k, "color.status.")) {
                int slot = parse_status_slot(k, 13);
+               if (slot < 0)
+                       return 0;
                if (!v)
                        return config_error_nonbool(k);
                color_parse(v, k, s->color_palette[slot]);
@@ -928,97 +974,6 @@ static int git_status_config(const char *k, const char *v, void *cb)
        return git_diff_ui_config(k, v, NULL);
 }
 
-#define quote_path quote_path_relative
-
-static void short_unmerged(int null_termination, struct string_list_item *it,
-                          struct wt_status *s)
-{
-       struct wt_status_change_data *d = it->util;
-       const char *how = "??";
-
-       switch (d->stagemask) {
-       case 1: how = "DD"; break; /* both deleted */
-       case 2: how = "AU"; break; /* added by us */
-       case 3: how = "UD"; break; /* deleted by them */
-       case 4: how = "UA"; break; /* added by them */
-       case 5: how = "DU"; break; /* deleted by us */
-       case 6: how = "AA"; break; /* both added */
-       case 7: how = "UU"; break; /* both modified */
-       }
-       printf("%s ", how);
-       if (null_termination) {
-               fprintf(stdout, "%s%c", it->string, 0);
-       } else {
-               struct strbuf onebuf = STRBUF_INIT;
-               const char *one;
-               one = quote_path(it->string, -1, &onebuf, s->prefix);
-               printf("%s\n", one);
-               strbuf_release(&onebuf);
-       }
-}
-
-static void short_status(int null_termination, struct string_list_item *it,
-                        struct wt_status *s)
-{
-       struct wt_status_change_data *d = it->util;
-
-       printf("%c%c ",
-              !d->index_status ? ' ' : d->index_status,
-              !d->worktree_status ? ' ' : d->worktree_status);
-       if (null_termination) {
-               fprintf(stdout, "%s%c", it->string, 0);
-               if (d->head_path)
-                       fprintf(stdout, "%s%c", d->head_path, 0);
-       } else {
-               struct strbuf onebuf = STRBUF_INIT;
-               const char *one;
-               if (d->head_path) {
-                       one = quote_path(d->head_path, -1, &onebuf, s->prefix);
-                       printf("%s -> ", one);
-                       strbuf_release(&onebuf);
-               }
-               one = quote_path(it->string, -1, &onebuf, s->prefix);
-               printf("%s\n", one);
-               strbuf_release(&onebuf);
-       }
-}
-
-static void short_untracked(int null_termination, struct string_list_item *it,
-                           struct wt_status *s)
-{
-       if (null_termination) {
-               fprintf(stdout, "?? %s%c", it->string, 0);
-       } else {
-               struct strbuf onebuf = STRBUF_INIT;
-               const char *one;
-               one = quote_path(it->string, -1, &onebuf, s->prefix);
-               printf("?? %s\n", one);
-               strbuf_release(&onebuf);
-       }
-}
-
-static void short_print(struct wt_status *s, int null_termination)
-{
-       int i;
-       for (i = 0; i < s->change.nr; i++) {
-               struct wt_status_change_data *d;
-               struct string_list_item *it;
-
-               it = &(s->change.items[i]);
-               d = it->util;
-               if (d->stagemask)
-                       short_unmerged(null_termination, it, s);
-               else
-                       short_status(null_termination, it, s);
-       }
-       for (i = 0; i < s->untracked.nr; i++) {
-               struct string_list_item *it;
-
-               it = &(s->untracked.items[i]);
-               short_untracked(null_termination, it, s);
-       }
-}
-
 int cmd_status(int argc, const char **argv, const char *prefix)
 {
        struct wt_status s;
@@ -1044,6 +999,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
 
        wt_status_prepare(&s);
        git_config(git_status_config, &s);
+       in_merge = file_exists(git_path("MERGE_HEAD"));
        argc = parse_options(argc, argv, prefix,
                             builtin_status_options,
                             builtin_status_usage, 0);
@@ -1055,25 +1011,25 @@ int cmd_status(int argc, const char **argv, const char *prefix)
        read_cache();
        refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
        s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;
+       s.in_merge = in_merge;
        wt_status_collect(&s);
 
+       if (s.relative_paths)
+               s.prefix = prefix;
+       if (s.use_color == -1)
+               s.use_color = git_use_color_default;
+       if (diff_use_color_default == -1)
+               diff_use_color_default = git_use_color_default;
+
        switch (status_format) {
        case STATUS_FORMAT_SHORT:
-               if (s.relative_paths)
-                       s.prefix = prefix;
-               short_print(&s, null_termination);
+               wt_shortstatus_print(&s, null_termination);
                break;
        case STATUS_FORMAT_PORCELAIN:
-               short_print(&s, null_termination);
+               wt_porcelain_print(&s, null_termination);
                break;
        case STATUS_FORMAT_LONG:
                s.verbose = verbose;
-               if (s.relative_paths)
-                       s.prefix = prefix;
-               if (s.use_color == -1)
-                       s.use_color = git_use_color_default;
-               if (diff_use_color_default == -1)
-                       diff_use_color_default = git_use_color_default;
                wt_status_print(&s);
                break;
        }
@@ -1120,8 +1076,10 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
                initial_commit ? " (root-commit)" : "");
 
        if (!log_tree_commit(&rev, commit)) {
+               struct pretty_print_context ctx = {0};
                struct strbuf buf = STRBUF_INIT;
-               format_commit_message(commit, format + 7, &buf, DATE_NORMAL);
+               ctx.date_mode = DATE_NORMAL;
+               format_commit_message(commit, format + 7, &buf, &ctx);
                printf("%s\n", buf.buf);
                strbuf_release(&buf);
        }
@@ -1132,7 +1090,7 @@ static int git_commit_config(const char *k, const char *v, void *cb)
        struct wt_status *s = cb;
 
        if (!strcmp(k, "commit.template"))
-               return git_config_string(&template_file, k, v);
+               return git_config_pathname(&template_file, k, v);
 
        return git_status_config(k, v, s);
 }
@@ -1151,10 +1109,11 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
 
        wt_status_prepare(&s);
        git_config(git_commit_config, &s);
+       in_merge = file_exists(git_path("MERGE_HEAD"));
+       s.in_merge = in_merge;
 
        if (s.use_color == -1)
                s.use_color = git_use_color_default;
-
        argc = parse_and_validate_options(argc, argv, builtin_commit_usage,
                                          prefix, &s);
        if (dry_run) {