am: simplify allocations in get_commit_info()

After we call split_ident_line(), we have several begin/end pairs for various parts of the ident. We then copy each into a strbuf to create a single string, and then detach that string. We can instead skip the strbuf entirely and just duplicate the strings directly. This is shorter, and it makes it more obvious that we are not leaking the strbuf (we were not before, because every code path either died or hit a strbuf_detach). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Apr 26, 2017 at 23:27 UTC 2e2bbb9624e10537560c3ac45ff6820ff773b3d6
1 file changed +10 -15
builtin/am.c
+10 -15
@@ -1376,40 +1376,35 @@ static int get_mail_commit_oid(struct object_id *commit_id, const char *mail)
1376 */
1377 static void get_commit_info(struct am_state *state, struct commit *commit)
1378 {
1379 - const char *buffer, *ident_line, *author_date, *msg;
1379 + const char *buffer, *ident_line, *msg;
1380 size_t ident_len;
1381 struct ident_split ident_split;
1382 - struct strbuf sb = STRBUF_INIT;
1382
1383 buffer = logmsg_reencode(commit, NULL, get_commit_output_encoding());
1384
1385 ident_line = find_commit_header(buffer, "author", &ident_len);
1386
1388 - if (split_ident_line(&ident_split, ident_line, ident_len) < 0) {
1389 - strbuf_add(&sb, ident_line, ident_len);
1390 - die(_("invalid ident line: %s"), sb.buf);
1391 - }
1387 + if (split_ident_line(&ident_split, ident_line, ident_len) < 0)
1388 + die(_("invalid ident line: %.*s"), (int)ident_len, ident_line);
1389
1390 assert(!state->author_name);
1391 if (ident_split.name_begin) {
1395 - strbuf_add(&sb, ident_split.name_begin,
1396 - ident_split.name_end - ident_split.name_begin);
1397 - state->author_name = strbuf_detach(&sb, NULL);
1392 + state->author_name =
1393 + xmemdupz(ident_split.name_begin,
1394 + ident_split.name_end - ident_split.name_begin);
1395 } else
1396 state->author_name = xstrdup("");
1397
1398 assert(!state->author_email);
1399 if (ident_split.mail_begin) {
1403 - strbuf_add(&sb, ident_split.mail_begin,
1404 - ident_split.mail_end - ident_split.mail_begin);
1405 - state->author_email = strbuf_detach(&sb, NULL);
1400 + state->author_email =
1401 + xmemdupz(ident_split.mail_begin,
1402 + ident_split.mail_end - ident_split.mail_begin);
1403 } else
1404 state->author_email = xstrdup("");
1405
1409 - author_date = show_ident_date(&ident_split, DATE_MODE(NORMAL));
1410 - strbuf_addstr(&sb, author_date);
1406 assert(!state->author_date);
1412 - state->author_date = strbuf_detach(&sb, NULL);
1407 + state->author_date = xstrdup(show_ident_date(&ident_split, DATE_MODE(NORMAL)));
1408
1409 assert(!state->msg);
1410 msg = strstr(buffer, "\n\n");