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 <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Sep 18, 2019 at 18:35 UTC 4b3aa170d14279aaf40db95bf3d93d43099e9614
1 file changed +4 -7
sha1-name.c
+4 -7
@@ -1289,7 +1289,7 @@ static int get_oid_oneline(struct repository *r,
1289
1290 struct grab_nth_branch_switch_cbdata {
1291 int remaining;
1292 - struct strbuf buf;
1292 + struct strbuf *sb;
1293 };
1294
1295 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
1307 return 0;
1308 if (--(cb->remaining) == 0) {
1309 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);
1312 return 1; /* we are done */
1313 }
1314 return 0;
@@ -1341,18 +1341,15 @@ static int interpret_nth_prior_checkout(struct repository *r,
1341 if (nth <= 0)
1342 return -1;
1343 cb.remaining = nth;
1344 - strbuf_init(&cb.buf, 20);
1344 + cb.sb = buf;
1345
1346 retval = refs_for_each_reflog_ent_reverse(get_main_ref_store(r),
1347 "HEAD", grab_nth_branch_switch, &cb);
1348 if (0 < retval) {
1349 - strbuf_reset(buf);
1350 - strbuf_addbuf(buf, &cb.buf);
1349 retval = brace - name + 1;
1350 } else
1351 retval = 0;
1352
1355 - strbuf_release(&cb.buf);
1353 return retval;
1354 }
1355