Skip to content

Commit 2e2bbb9

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f131db9 commit 2e2bbb9

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

builtin/am.c

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,40 +1376,35 @@ static int get_mail_commit_oid(struct object_id *commit_id, const char *mail)
13761376
*/
13771377
static void get_commit_info(struct am_state *state, struct commit *commit)
13781378
{
1379-
const char *buffer, *ident_line, *author_date, *msg;
1379+
const char *buffer, *ident_line, *msg;
13801380
size_t ident_len;
13811381
struct ident_split ident_split;
1382-
struct strbuf sb = STRBUF_INIT;
13831382

13841383
buffer = logmsg_reencode(commit, NULL, get_commit_output_encoding());
13851384

13861385
ident_line = find_commit_header(buffer, "author", &ident_len);
13871386

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);
13921389

13931390
assert(!state->author_name);
13941391
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);
13981395
} else
13991396
state->author_name = xstrdup("");
14001397

14011398
assert(!state->author_email);
14021399
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);
14061403
} else
14071404
state->author_email = xstrdup("");
14081405

1409-
author_date = show_ident_date(&ident_split, DATE_MODE(NORMAL));
1410-
strbuf_addstr(&sb, author_date);
14111406
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)));
14131408

14141409
assert(!state->msg);
14151410
msg = strstr(buffer, "\n\n");

0 commit comments

Comments
 (0)