git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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);
Previous: Junio C Hamano
Message 3 of 3 in “unit-tests: use xstrfmt() instead of a char buffer in t-reftable-stack”
  1. unit-tests: use xstrfmt() instead of a char buffer in t-reftable-stackChandra Pratap, Oct 1, 2024
  2. Junio C HamanoOct 1, 2024
  3. Eric SunshineOct 1, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.