am: improve author-script error reporting

If there are errors in a user edited author-script there was no indication of what was wrong. This commit adds some specific error messages depending on the problem. It also relaxes the requirement that the variables appear in a specific order in the file to match the behavior of 'rebase --interactive'. Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Oct 31, 2018 at 10:15 UTC 442c36bd08a283b158995fd75d340e6981b80e34
1 file changed +39 -10
builtin/am.c
+39 -10
@@ -270,8 +270,11 @@ static int parse_key_value_squoted(char *buf, struct string_list *list)
270 struct string_list_item *item;
271 char *np;
272 char *cp = strchr(buf, '=');
273 - if (!cp)
274 - return -1;
273 + if (!cp) {
274 + np = strchrnul(buf, '\n');
275 + return error(_("unable to parse '%.*s'"),
276 + (int) (np - buf), buf);
277 + }
278 np = strchrnul(cp, '\n');
279 *cp++ = '\0';
280 item = string_list_append(list, buf);
@@ -280,7 +283,8 @@ static int parse_key_value_squoted(char *buf, struct string_list *list)
283 *np = '\0';
284 cp = sq_dequote(cp);
285 if (!cp)
283 - return -1;
286 + return error(_("unable to dequote value of '%s'"),
287 + item->string);
288 item->util = xstrdup(cp);
289 }
290 return 0;
@@ -308,6 +312,7 @@ static int read_author_script(struct am_state *state)
312 struct strbuf buf = STRBUF_INIT;
313 struct string_list kv = STRING_LIST_INIT_DUP;
314 int retval = -1; /* assume failure */
315 + int i, name_i = -2, email_i = -2, date_i = -2, err = 0;
316 int fd;
317
318 assert(!state->author_name);
@@ -326,14 +331,38 @@ static int read_author_script(struct am_state *state)
331 if (parse_key_value_squoted(buf.buf, &kv))
332 goto finish;
333
329 - if (kv.nr != 3 ||
330 - strcmp(kv.items[0].string, "GIT_AUTHOR_NAME") ||
331 - strcmp(kv.items[1].string, "GIT_AUTHOR_EMAIL") ||
332 - strcmp(kv.items[2].string, "GIT_AUTHOR_DATE"))
334 + for (i = 0; i < kv.nr; i++) {
335 + if (!strcmp(kv.items[i].string, "GIT_AUTHOR_NAME")) {
336 + if (name_i != -2)
337 + name_i = error(_("'GIT_AUTHOR_NAME' already given"));
338 + else
339 + name_i = i;
340 + } else if (!strcmp(kv.items[i].string, "GIT_AUTHOR_EMAIL")) {
341 + if (email_i != -2)
342 + email_i = error(_("'GIT_AUTHOR_EMAIL' already given"));
343 + else
344 + email_i = i;
345 + } else if (!strcmp(kv.items[i].string, "GIT_AUTHOR_DATE")) {
346 + if (date_i != -2)
347 + date_i = error(_("'GIT_AUTHOR_DATE' already given"));
348 + else
349 + date_i = i;
350 + } else {
351 + err = error(_("unknown variable '%s'"),
352 + kv.items[i].string);
353 + }
354 + }
355 + if (name_i == -2)
356 + error(_("missing 'GIT_AUTHOR_NAME'"));
357 + if (email_i == -2)
358 + error(_("missing 'GIT_AUTHOR_EMAIL'"));
359 + if (date_i == -2)
360 + error(_("missing 'GIT_AUTHOR_DATE'"));
361 + if (date_i < 0 || email_i < 0 || date_i < 0 || err)
362 goto finish;
334 - state->author_name = kv.items[0].util;
335 - state->author_email = kv.items[1].util;
336 - state->author_date = kv.items[2].util;
363 + state->author_name = kv.items[name_i].util;
364 + state->author_email = kv.items[email_i].util;
365 + state->author_date = kv.items[date_i].util;
366 retval = 0;
367 finish:
368 string_list_clear(&kv, !!retval);