Merge branch 'cn/bom-in-gitignore'
[gitweb.git] / builtin / commit.c
index e108c5301564a86d396d72169f40cd24a9916a1f..da79ac4bc7a7247017e2f952b35642a1976c8101 100644 (file)
 #include "mailmap.h"
 
 static const char * const builtin_commit_usage[] = {
-       N_("git commit [options] [--] <pathspec>..."),
+       N_("git commit [<options>] [--] <pathspec>..."),
        NULL
 };
 
 static const char * const builtin_status_usage[] = {
-       N_("git status [options] [--] <pathspec>..."),
+       N_("git status [<options>] [--] <pathspec>..."),
        NULL
 };
 
@@ -229,7 +229,7 @@ static int commit_index_files(void)
 static int list_paths(struct string_list *list, const char *with_tree,
                      const char *prefix, const struct pathspec *pattern)
 {
-       int i;
+       int i, ret;
        char *m;
 
        if (!pattern->nr)
@@ -256,7 +256,9 @@ static int list_paths(struct string_list *list, const char *with_tree,
                        item->util = item; /* better a valid pointer than a fake one */
        }
 
-       return report_path_error(m, pattern, prefix);
+       ret = report_path_error(m, pattern, prefix);
+       free(m);
+       return ret;
 }
 
 static void add_remove_files(struct string_list *list)
@@ -522,6 +524,12 @@ static int is_a_merge(const struct commit *current_head)
        return !!(current_head->parents && current_head->parents->next);
 }
 
+static void assert_split_ident(struct ident_split *id, const struct strbuf *buf)
+{
+       if (split_ident_line(id, buf->buf, buf->len) || !id->date_begin)
+               die("BUG: unable to parse our own ident: %s", buf->buf);
+}
+
 static void export_one(const char *var, const char *s, const char *e, int hack)
 {
        struct strbuf buf = STRBUF_INIT;
@@ -532,20 +540,6 @@ static void export_one(const char *var, const char *s, const char *e, int hack)
        strbuf_release(&buf);
 }
 
-static int sane_ident_split(struct ident_split *person)
-{
-       if (!person->name_begin || !person->name_end ||
-           person->name_begin == person->name_end)
-               return 0; /* no human readable name */
-       if (!person->mail_begin || !person->mail_end ||
-           person->mail_begin == person->mail_end)
-               return 0; /* no usable mail */
-       if (!person->date_begin || !person->date_end ||
-           !person->tz_begin || !person->tz_end)
-               return 0;
-       return 1;
-}
-
 static int parse_force_date(const char *in, struct strbuf *out)
 {
        strbuf_addch(out, '@');
@@ -567,20 +561,14 @@ static void set_ident_var(char **buf, char *val)
        *buf = val;
 }
 
-static char *envdup(const char *var)
-{
-       const char *val = getenv(var);
-       return val ? xstrdup(val) : NULL;
-}
-
 static void determine_author_info(struct strbuf *author_ident)
 {
        char *name, *email, *date;
        struct ident_split author;
 
-       name = envdup("GIT_AUTHOR_NAME");
-       email = envdup("GIT_AUTHOR_EMAIL");
-       date = envdup("GIT_AUTHOR_DATE");
+       name = xstrdup_or_null(getenv("GIT_AUTHOR_NAME"));
+       email = xstrdup_or_null(getenv("GIT_AUTHOR_EMAIL"));
+       date = xstrdup_or_null(getenv("GIT_AUTHOR_DATE"));
 
        if (author_message) {
                struct ident_split ident;
@@ -623,25 +611,15 @@ static void determine_author_info(struct strbuf *author_ident)
        }
 
        strbuf_addstr(author_ident, fmt_ident(name, email, date, IDENT_STRICT));
-       if (!split_ident_line(&author, author_ident->buf, author_ident->len) &&
-           sane_ident_split(&author)) {
-               export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0);
-               export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0);
-               export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@');
-       }
-
+       assert_split_ident(&author, author_ident);
+       export_one("GIT_AUTHOR_NAME", author.name_begin, author.name_end, 0);
+       export_one("GIT_AUTHOR_EMAIL", author.mail_begin, author.mail_end, 0);
+       export_one("GIT_AUTHOR_DATE", author.date_begin, author.tz_end, '@');
        free(name);
        free(email);
        free(date);
 }
 
-static void split_ident_or_die(struct ident_split *id, const struct strbuf *buf)
-{
-       if (split_ident_line(id, buf->buf, buf->len) ||
-           !sane_ident_split(id))
-               die(_("Malformed ident string: '%s'"), buf->buf);
-}
-
 static int author_date_is_interesting(void)
 {
        return author_message || force_date;
@@ -800,32 +778,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
        if (clean_message_contents)
                stripspace(&sb, 0);
 
-       if (signoff) {
-               /*
-                * See if we have a Conflicts: block at the end. If yes, count
-                * its size, so we can ignore it.
-                */
-               int ignore_footer = 0;
-               int i, eol, previous = 0;
-               const char *nl;
-
-               for (i = 0; i < sb.len; i++) {
-                       nl = memchr(sb.buf + i, '\n', sb.len - i);
-                       if (nl)
-                               eol = nl - sb.buf;
-                       else
-                               eol = sb.len;
-                       if (starts_with(sb.buf + previous, "\nConflicts:\n")) {
-                               ignore_footer = sb.len - previous;
-                               break;
-                       }
-                       while (i < eol)
-                               i++;
-                       previous = eol;
-               }
-
-               append_signoff(&sb, ignore_footer, 0);
-       }
+       if (signoff)
+               append_signoff(&sb, ignore_non_trailer(&sb), 0);
 
        if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
                die_errno(_("could not write commit template"));
@@ -880,8 +834,14 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
                        status_printf_ln(s, GIT_COLOR_NORMAL,
                                        "%s", only_include_assumed);
 
-               split_ident_or_die(&ai, author_ident);
-               split_ident_or_die(&ci, &committer_ident);
+               /*
+                * These should never fail because they come from our own
+                * fmt_ident. They may fail the sane_ident test, but we know
+                * that the name and mail pointers will at least be valid,
+                * which is enough for our tests and printing here.
+                */
+               assert_split_ident(&ai, author_ident);
+               assert_split_ident(&ci, &committer_ident);
 
                if (ident_cmp(&ai, &ci))
                        status_printf_ln(s, GIT_COLOR_NORMAL,
@@ -1092,7 +1052,7 @@ static const char *find_author_by_nickname(const char *name)
                clear_mailmap(&mailmap);
                return strbuf_detach(&buf, NULL);
        }
-       die(_("No existing author found with '%s'"), name);
+       die(_("--author '%s' is not 'Name <email>' and matches no existing author"), name);
 }
 
 
@@ -1808,8 +1768,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
        if (!transaction ||
            ref_transaction_update(transaction, "HEAD", sha1,
                                   current_head
-                                  ? current_head->object.sha1 : NULL,
-                                  0, !!current_head, sb.buf, &err) ||
+                                  ? current_head->object.sha1 : null_sha1,
+                                  0, sb.buf, &err) ||
            ref_transaction_commit(transaction, &err)) {
                rollback_index_files();
                die("%s", err.buf);