Skip to content

Commit 56ca603

Browse files
shejialuogitster
authored andcommitted
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 <[email protected]> Mentored-by: Karthik Nayak <[email protected]> Signed-off-by: shejialuo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 32dc1c7 commit 56ca603

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

refs/files-backend.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3501,12 +3501,12 @@ static int files_ref_store_remove_on_disk(struct ref_store *ref_store,
35013501
*/
35023502
typedef int (*files_fsck_refs_fn)(struct ref_store *ref_store,
35033503
struct fsck_options *o,
3504-
const char *refs_check_dir,
3504+
const char *refname,
35053505
struct dir_iterator *iter);
35063506

35073507
static int files_fsck_refs_name(struct ref_store *ref_store UNUSED,
35083508
struct fsck_options *o,
3509-
const char *refs_check_dir,
3509+
const char *refname,
35103510
struct dir_iterator *iter)
35113511
{
35123512
struct strbuf sb = STRBUF_INIT;
@@ -3522,11 +3522,10 @@ static int files_fsck_refs_name(struct ref_store *ref_store UNUSED,
35223522
/*
35233523
* This works right now because we never check the root refs.
35243524
*/
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)) {
35273526
struct fsck_ref_report report = { 0 };
35283527

3529-
report.path = sb.buf;
3528+
report.path = refname;
35303529
ret = fsck_report_ref(o, &report,
35313530
FSCK_MSG_BAD_REF_NAME,
35323531
"invalid refname format");
@@ -3542,6 +3541,7 @@ static int files_fsck_refs_dir(struct ref_store *ref_store,
35423541
const char *refs_check_dir,
35433542
files_fsck_refs_fn *fsck_refs_fn)
35443543
{
3544+
struct strbuf refname = STRBUF_INIT;
35453545
struct strbuf sb = STRBUF_INIT;
35463546
struct dir_iterator *iter;
35473547
int iter_status;
@@ -3560,11 +3560,15 @@ static int files_fsck_refs_dir(struct ref_store *ref_store,
35603560
continue;
35613561
} else if (S_ISREG(iter->st.st_mode) ||
35623562
S_ISLNK(iter->st.st_mode)) {
3563+
strbuf_reset(&refname);
3564+
strbuf_addf(&refname, "%s/%s", refs_check_dir,
3565+
iter->relative_path);
3566+
35633567
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+
35663570
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))
35683572
ret = -1;
35693573
}
35703574
} else {
@@ -3581,6 +3585,7 @@ static int files_fsck_refs_dir(struct ref_store *ref_store,
35813585

35823586
out:
35833587
strbuf_release(&sb);
3588+
strbuf_release(&refname);
35843589
return ret;
35853590
}
35863591

0 commit comments

Comments
 (0)