t3001: test ls-files -o ignored/dir
[gitweb.git] / builtin-commit.c
index ffdee31bbfe1f123dd4e0868f1cc75578fb33521..073fe90ba1e04c3845fe49521db39b28339acb43 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,8 +52,8 @@ 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 char *untracked_files_arg;
+static int quiet, verbose, no_verify, allow_empty, dry_run, renew_authorship;
+static char *untracked_files_arg, *force_date;
 /*
  * The default commit message cleanup mode will remove the lines
  * beginning with # (shell comments) and leading and trailing
@@ -72,6 +72,13 @@ static int use_editor = 1, initial_commit, in_merge;
 static const char *only_include_assumed;
 static struct strbuf message;
 
+static int null_termination;
+static enum {
+       STATUS_FORMAT_LONG,
+       STATUS_FORMAT_SHORT,
+       STATUS_FORMAT_PORCELAIN,
+} status_format = STATUS_FORMAT_LONG;
+
 static int opt_parse_m(const struct option *opt, const char *arg, int unset)
 {
        struct strbuf *buf = opt->value;
@@ -87,16 +94,20 @@ static int opt_parse_m(const struct option *opt, const char *arg, int unset)
 static struct option builtin_commit_options[] = {
        OPT__QUIET(&quiet),
        OPT__VERBOSE(&verbose),
-       OPT_GROUP("Commit message options"),
 
+       OPT_GROUP("Commit message options"),
        OPT_FILENAME('F', "file", &logfile, "read log from file"),
        OPT_STRING(0, "author", &force_author, "AUTHOR", "override author for commit"),
+       OPT_STRING(0, "date", &force_date, "DATE", "override date 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"),
+       OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"),
+       /* end commit message options */
 
        OPT_GROUP("Commit contents options"),
        OPT_BOOLEAN('a', "all", &all, "commit all changed files"),
@@ -105,10 +116,16 @@ static struct option builtin_commit_options[] = {
        OPT_BOOLEAN('o', "only", &only, "commit only specified files"),
        OPT_BOOLEAN('n', "no-verify", &no_verify, "bypass pre-commit hook"),
        OPT_BOOLEAN(0, "dry-run", &dry_run, "show what would be committed"),
+       OPT_SET_INT(0, "short", &status_format, "show status concisely",
+                   STATUS_FORMAT_SHORT),
+       OPT_SET_INT(0, "porcelain", &status_format,
+                   "show porcelain output format", STATUS_FORMAT_PORCELAIN),
+       OPT_BOOLEAN('z', "null", &null_termination,
+                   "terminate entries with NUL"),
        OPT_BOOLEAN(0, "amend", &amend, "amend previous commit"),
        { OPTION_STRING, 'u', "untracked-files", &untracked_files_arg, "mode", "show untracked files, optional modes: all, normal, no. (Default: all)", PARSE_OPT_OPTARG, NULL, (intptr_t)"all" },
        OPT_BOOLEAN(0, "allow-empty", &allow_empty, "ok to record an empty change"),
-       OPT_STRING(0, "cleanup", &cleanup_arg, "default", "how to strip spaces and #comments from message"),
+       /* end commit contents options */
 
        OPT_END()
 };
@@ -306,7 +323,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));
@@ -363,7 +380,18 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
        s->is_initial = get_sha1(s->reference, sha1) ? 1 : 0;
 
        wt_status_collect(s);
-       wt_status_print(s);
+
+       switch (status_format) {
+       case STATUS_FORMAT_SHORT:
+               wt_shortstatus_print(s, null_termination);
+               break;
+       case STATUS_FORMAT_PORCELAIN:
+               wt_porcelain_print(s, null_termination);
+               break;
+       case STATUS_FORMAT_LONG:
+               wt_status_print(s);
+               break;
+       }
 
        return s->commitable;
 }
@@ -386,7 +414,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 ");
@@ -414,11 +442,55 @@ static void determine_author_info(void)
                email = xstrndup(lb + 2, rb - (lb + 2));
        }
 
+       if (force_date)
+               date = force_date;
+
        author_name = name;
        author_email = email;
        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)
 {
@@ -494,7 +566,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);
                }
@@ -689,8 +761,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);
@@ -724,6 +798,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)
@@ -734,9 +811,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.");
@@ -757,6 +831,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";
@@ -821,6 +897,11 @@ static int parse_and_validate_options(int argc, const char *argv[],
        else if (interactive && argc > 0)
                die("Paths with --interactive does not make sense.");
 
+       if (null_termination && status_format == STATUS_FORMAT_LONG)
+               status_format = STATUS_FORMAT_PORCELAIN;
+       if (status_format != STATUS_FORMAT_LONG)
+               dry_run = 1;
+
        return argc;
 }
 
@@ -852,7 +933,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)
@@ -872,6 +953,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]);
@@ -897,106 +980,9 @@ 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;
-       static int null_termination;
-       static enum {
-               STATUS_FORMAT_LONG,
-               STATUS_FORMAT_SHORT,
-               STATUS_FORMAT_PORCELAIN,
-       } status_format = STATUS_FORMAT_LONG;
        unsigned char sha1[20];
        static struct option builtin_status_options[] = {
                OPT__VERBOSE(&verbose),
@@ -1019,6 +1005,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);
@@ -1030,23 +1017,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:
-               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;
        }
@@ -1093,8 +1082,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);
        }
@@ -1105,7 +1096,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);
 }
@@ -1124,10 +1115,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) {