Re: [PATCH] unit-tests: use xstrfmt() instead of a char buffer in t-reftable-stack
- From
Eric Sunshine <sunshine@sunshineco.com>
- Date
- Oct 1, 2024, 19:32 UTC
- Message-ID
- <CAPig+cQGz3TFGfmLwdpYHNMOqKbqXZpCRMj=bT3pvaZD=oyVSQ@mail.gmail.com>
- In-Reply-To
- <xmqqo743vd9q.fsf@gitster.g>
On Tue, Oct 1, 2024 at 3:23 PM Junio C Hamano <gitster@pobox.com> wrote:
Show 23 quoted lines
> One thing that this worsens is that we now have two copies of the
> literal "branch%02"PRIuMAX string. If we ever want to change one of
> them, we must remember to change the other to match.
>
> Perhaps with another constant, this patch would become perfect, like
> this?
>
> + static const char fmt[] = "branch%02"PRIuMAX;
>
> > - snprintf(buf, sizeof(buf), "branch%02"PRIuMAX, (uintmax_t)i);
> > - refs[i].refname = xstrdup(buf);
> > + refs[i].refname = xstrfmt("branch%02"PRIuMAX, (uintmax_t)i);
>
> + refs[i].refname = xstrfmt(fmt, (uintmax_t)i);
>
> > refs[i].update_index = i + 1;
> > refs[i].value_type = REFTABLE_REF_VAL1;
> > t_reftable_set_hash(refs[i].value.val1, i, GIT_SHA1_FORMAT_ID);
> >
> > - logs[i].refname = xstrdup(buf);
> > + logs[i].refname = xstrfmt("branch%02"PRIuMAX, (uintmax_t)i);
>
> + logs[i].refname = xstrfmt(fmt, (uintmax_t)i);Even simpler would be merely to xstrdup() the string which was already formatted by xstrfmt(), I would think.
refs[i].refname = xstrfmt("branch%02"PRIuMAX, (uintmax_t)i);
...
logs[i].refname = xstrdup(refs[i].refname);