am: improve author-script error reporting
[gitweb.git] / builtin / am.c
index 5e866d17c7c7b46ccf671916c853be228bf38ad6..ffca4479d7b230b58d6ed25f3fcd282b2b3994d4 100644 (file)
@@ -270,8 +270,11 @@ static int parse_key_value_squoted(char *buf, struct string_list *list)
                struct string_list_item *item;
                char *np;
                char *cp = strchr(buf, '=');
-               if (!cp)
-                       return -1;
+               if (!cp) {
+                       np = strchrnul(buf, '\n');
+                       return error(_("unable to parse '%.*s'"),
+                                    (int) (np - buf), buf);
+               }
                np = strchrnul(cp, '\n');
                *cp++ = '\0';
                item = string_list_append(list, buf);
@@ -280,7 +283,8 @@ static int parse_key_value_squoted(char *buf, struct string_list *list)
                *np = '\0';
                cp = sq_dequote(cp);
                if (!cp)
-                       return -1;
+                       return error(_("unable to dequote value of '%s'"),
+                                    item->string);
                item->util = xstrdup(cp);
        }
        return 0;
@@ -308,6 +312,7 @@ static int read_author_script(struct am_state *state)
        struct strbuf buf = STRBUF_INIT;
        struct string_list kv = STRING_LIST_INIT_DUP;
        int retval = -1; /* assume failure */
+       int i, name_i = -2, email_i = -2, date_i = -2, err = 0;
        int fd;
 
        assert(!state->author_name);
@@ -318,21 +323,46 @@ static int read_author_script(struct am_state *state)
        if (fd < 0) {
                if (errno == ENOENT)
                        return 0;
-               die_errno(_("could not open '%s' for reading"), filename);
+               return error_errno(_("could not open '%s' for reading"),
+                                  filename);
        }
        strbuf_read(&buf, fd, 0);
        close(fd);
        if (parse_key_value_squoted(buf.buf, &kv))
                goto finish;
 
-       if (kv.nr != 3 ||
-           strcmp(kv.items[0].string, "GIT_AUTHOR_NAME") ||
-           strcmp(kv.items[1].string, "GIT_AUTHOR_EMAIL") ||
-           strcmp(kv.items[2].string, "GIT_AUTHOR_DATE"))
+       for (i = 0; i < kv.nr; i++) {
+               if (!strcmp(kv.items[i].string, "GIT_AUTHOR_NAME")) {
+                       if (name_i != -2)
+                               name_i = error(_("'GIT_AUTHOR_NAME' already given"));
+                       else
+                               name_i = i;
+               } else if (!strcmp(kv.items[i].string, "GIT_AUTHOR_EMAIL")) {
+                       if (email_i != -2)
+                               email_i = error(_("'GIT_AUTHOR_EMAIL' already given"));
+                       else
+                               email_i = i;
+               } else if (!strcmp(kv.items[i].string, "GIT_AUTHOR_DATE")) {
+                       if (date_i != -2)
+                               date_i = error(_("'GIT_AUTHOR_DATE' already given"));
+                       else
+                               date_i = i;
+               } else {
+                       err = error(_("unknown variable '%s'"),
+                                   kv.items[i].string);
+               }
+       }
+       if (name_i == -2)
+               error(_("missing 'GIT_AUTHOR_NAME'"));
+       if (email_i == -2)
+               error(_("missing 'GIT_AUTHOR_EMAIL'"));
+       if (date_i == -2)
+               error(_("missing 'GIT_AUTHOR_DATE'"));
+       if (date_i < 0 || email_i < 0 || date_i < 0 || err)
                goto finish;
-       state->author_name = kv.items[0].util;
-       state->author_email = kv.items[1].util;
-       state->author_date = kv.items[2].util;
+       state->author_name = kv.items[name_i].util;
+       state->author_email = kv.items[email_i].util;
+       state->author_date = kv.items[date_i].util;
        retval = 0;
 finish:
        string_list_clear(&kv, !!retval);
@@ -1244,6 +1274,10 @@ static int parse_mail(struct am_state *state, const char *mail)
        fclose(mi.input);
        fclose(mi.output);
 
+       if (mi.format_flowed)
+               warning(_("Patch sent with format=flowed; "
+                         "space at the end of lines might be lost."));
+
        /* Extract message and author information */
        fp = xfopen(am_path(state, "info"), "r");
        while (!strbuf_getline_lf(&sb, fp)) {
@@ -2078,7 +2112,7 @@ static int safe_to_abort(const struct am_state *state)
        if (get_oid("HEAD", &head))
                oidclr(&head);
 
-       if (!oidcmp(&head, &abort_safety))
+       if (oideq(&head, &abort_safety))
                return 1;
 
        warning(_("You seem to have moved HEAD since the last 'am' failure.\n"