ref: initialize ref name outside of check functions

We passes "refs_check_dir" to the "files_fsck_refs_name" function which allows it to create the checked ref name later. However, when we introduce a new check function, we have to allocate redundant memory and re-calculate the ref name. It's bad for us to allocate redundant memory and duplicate logic. Instead, we should allocate and calculate it only once and pass the ref name to the check functions. In order not to do repeat calculation, rename "refs_check_dir" to "refname". And in "files_fsck_refs_dir", create a new strbuf "refname", thus whenever we handle a new ref, calculate the name and call the check functions one by one. Mentored-by: Patrick Steinhardt <ps@pks.im> Mentored-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: shejialuo <shejialuo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

shejialuo committed Nov 20, 2024 at 19:51 UTC 56ca6039576294b6efb95345dafe81733bfb61d5
1 file changed +13 -8
refs/files-backend.c
+13 -8
@@ -3501,12 +3501,12 @@ static int files_ref_store_remove_on_disk(struct ref_store *ref_store,
3501 */
3502 typedef int (*files_fsck_refs_fn)(struct ref_store *ref_store,
3503 struct fsck_options *o,
3504 - const char *refs_check_dir,
3504 + const char *refname,
3505 struct dir_iterator *iter);
3506
3507 static int files_fsck_refs_name(struct ref_store *ref_store UNUSED,
3508 struct fsck_options *o,
3509 - const char *refs_check_dir,
3509 + const char *refname,
3510 struct dir_iterator *iter)
3511 {
3512 struct strbuf sb = STRBUF_INIT;
@@ -3522,11 +3522,10 @@ static int files_fsck_refs_name(struct ref_store *ref_store UNUSED,
3522 /*
3523 * This works right now because we never check the root refs.
3524 */
3525 - strbuf_addf(&sb, "%s/%s", refs_check_dir, iter->relative_path);
3526 - if (check_refname_format(sb.buf, 0)) {
3525 + if (check_refname_format(refname, 0)) {
3526 struct fsck_ref_report report = { 0 };
3527
3529 - report.path = sb.buf;
3528 + report.path = refname;
3529 ret = fsck_report_ref(o, &report,
3530 FSCK_MSG_BAD_REF_NAME,
3531 "invalid refname format");
@@ -3542,6 +3541,7 @@ static int files_fsck_refs_dir(struct ref_store *ref_store,
3541 const char *refs_check_dir,
3542 files_fsck_refs_fn *fsck_refs_fn)
3543 {
3544 + struct strbuf refname = STRBUF_INIT;
3545 struct strbuf sb = STRBUF_INIT;
3546 struct dir_iterator *iter;
3547 int iter_status;
@@ -3560,11 +3560,15 @@ static int files_fsck_refs_dir(struct ref_store *ref_store,
3560 continue;
3561 } else if (S_ISREG(iter->st.st_mode) ||
3562 S_ISLNK(iter->st.st_mode)) {
3563 + strbuf_reset(&refname);
3564 + strbuf_addf(&refname, "%s/%s", refs_check_dir,
3565 + iter->relative_path);
3566 +
3567 if (o->verbose)
3564 - fprintf_ln(stderr, "Checking %s/%s",
3565 - refs_check_dir, iter->relative_path);
3568 + fprintf_ln(stderr, "Checking %s", refname.buf);
3569 +
3570 for (size_t i = 0; fsck_refs_fn[i]; i++) {
3567 - if (fsck_refs_fn[i](ref_store, o, refs_check_dir, iter))
3571 + if (fsck_refs_fn[i](ref_store, o, refname.buf, iter))
3572 ret = -1;
3573 }
3574 } else {
@@ -3581,6 +3585,7 @@ static int files_fsck_refs_dir(struct ref_store *ref_store,
3585
3586 out:
3587 strbuf_release(&sb);
3588 + strbuf_release(&refname);
3589 return ret;
3590 }
3591