Skip to content

Commit 4b3aa17

Browse files
rscharfegitster
authored andcommitted
sha1_name: simplify strbuf handling in interpret_nth_prior_checkout()
Pass the target strbuf to the callback function grab_nth_branch_switch() by reference so that it can add the result string directly instead of having it put the string into a temporary strbuf first. This gets rid of an extra allocation and a string copy. Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5fa0f52 commit 4b3aa17

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

sha1-name.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,7 @@ static int get_oid_oneline(struct repository *r,
12891289

12901290
struct grab_nth_branch_switch_cbdata {
12911291
int remaining;
1292-
struct strbuf buf;
1292+
struct strbuf *sb;
12931293
};
12941294

12951295
static int grab_nth_branch_switch(struct object_id *ooid, struct object_id *noid,
@@ -1307,8 +1307,8 @@ static int grab_nth_branch_switch(struct object_id *ooid, struct object_id *noid
13071307
return 0;
13081308
if (--(cb->remaining) == 0) {
13091309
len = target - match;
1310-
strbuf_reset(&cb->buf);
1311-
strbuf_add(&cb->buf, match, len);
1310+
strbuf_reset(cb->sb);
1311+
strbuf_add(cb->sb, match, len);
13121312
return 1; /* we are done */
13131313
}
13141314
return 0;
@@ -1341,18 +1341,15 @@ static int interpret_nth_prior_checkout(struct repository *r,
13411341
if (nth <= 0)
13421342
return -1;
13431343
cb.remaining = nth;
1344-
strbuf_init(&cb.buf, 20);
1344+
cb.sb = buf;
13451345

13461346
retval = refs_for_each_reflog_ent_reverse(get_main_ref_store(r),
13471347
"HEAD", grab_nth_branch_switch, &cb);
13481348
if (0 < retval) {
1349-
strbuf_reset(buf);
1350-
strbuf_addbuf(buf, &cb.buf);
13511349
retval = brace - name + 1;
13521350
} else
13531351
retval = 0;
13541352

1355-
strbuf_release(&cb.buf);
13561353
return retval;
13571354
}
13581355

0 commit comments

Comments
 (0)