Skip to content

Commit 442c36b

Browse files
phillipwoodgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 28224c2 commit 442c36b

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

builtin/am.c

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,11 @@ static int parse_key_value_squoted(char *buf, struct string_list *list)
270270
struct string_list_item *item;
271271
char *np;
272272
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+
}
275278
np = strchrnul(cp, '\n');
276279
*cp++ = '\0';
277280
item = string_list_append(list, buf);
@@ -280,7 +283,8 @@ static int parse_key_value_squoted(char *buf, struct string_list *list)
280283
*np = '\0';
281284
cp = sq_dequote(cp);
282285
if (!cp)
283-
return -1;
286+
return error(_("unable to dequote value of '%s'"),
287+
item->string);
284288
item->util = xstrdup(cp);
285289
}
286290
return 0;
@@ -308,6 +312,7 @@ static int read_author_script(struct am_state *state)
308312
struct strbuf buf = STRBUF_INIT;
309313
struct string_list kv = STRING_LIST_INIT_DUP;
310314
int retval = -1; /* assume failure */
315+
int i, name_i = -2, email_i = -2, date_i = -2, err = 0;
311316
int fd;
312317

313318
assert(!state->author_name);
@@ -326,14 +331,38 @@ static int read_author_script(struct am_state *state)
326331
if (parse_key_value_squoted(buf.buf, &kv))
327332
goto finish;
328333

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)
333362
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;
337366
retval = 0;
338367
finish:
339368
string_list_clear(&kv, !!retval);

0 commit comments

Comments
 (0)