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

Re: [PATCH v2 4/4] Cleanse reflog messages in the refs.c layer

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 10, 2020, 21:06 UTC
Message-ID
<xmqqtuyfrsg0.fsf@gitster.c.googlers.com>
In-Reply-To
<6ca5b99c8d7af1a3f35b3a7d25db284c879a2f10.1594401593.git.gitgitgadget@gmail.com>
"Junio C Hamano via GitGitGadget" <gitgitgadget@gmail.com> writes:
Show 6 quoted lines
> Currently, the cleansing of the reflog message is done by the files
> backend, before the log is written out.  This is sufficient with the
> current code, as that is the only backend that writes reflogs.  But
> new backends can be added that write reflogs, and we'd want the
> resulting log message we would read out of "log -g" the same no
> matter what backend is used.

I _think_ there is no correctness impact, but this slightly changes the semantics. We used to take whatever random string from the caller and at the lowest layer of the files backend, i.e.

>  	strbuf_addf(&sb, "%s %s %s", oid_to_hex(old_oid), oid_to_hex(new_oid), committer);
>  	if (msg && *msg)
> -		copy_reflog_msg(&sb, msg);
> +		strbuf_addstr(&sb, msg);
cleansed and added the message to strbuf.
With the updated code, normalize_reflog_message() is defined like so
Show 8 quoted lines
> +static char *normalize_reflog_message(const char *msg)
> +{
> +	struct strbuf sb = STRBUF_INIT;
> +
> +	if (msg && *msg)
> +		copy_reflog_msg(&sb, msg);
> +	return strbuf_detach(&sb, NULL);
> +}

to always return a non-NULL but an empty string, even if the caller gave us a NULL pointer. We always pass a non-NULL string around, and the updated low-level code avoids calling strbuf_addstr() because msg is not NULL, but *msg is '\0'.

We _might_ be able to optimize by tweaking the normalizer a bit further, perhaps like so:

    static char *normalize_reflog_message(const char *msg)
    {
            struct strbuf sb = STRBUF_INIT;
            if (msg && *msg)
                    copy_reflog_msg(&sb, msg);
            if (sb.len) {
                    return strbuf_detach(&sb, NULL);
            } else {
                    strbuf_release(&sb);
                    return NULL;
            }
    }

to avoid allocating 1 byte '\0' on the heap by strbuf_detach() when sb here ends up being truly empty.

But it would be a premature optimization to worry about such a thing at this point, I think.

Thanks.
Previous: Junio C Hamano via GitGitGadgetNext: Junio C Hamano
Message 12 of 13 in “Preliminary fixes for reftable support”
  1. 0/4 Preliminary fixes for reftable supportHan-Wen Nienhuys via GitGitGadget, Jun 30, 2020
  2. 1/4 lib-t6000.sh: write tag using git-update-refHan-Wen Nienhuys via GitGitGadget, Jun 30, 2020
  3. 2/4 t3432: use git-reflog to inspect the reflog for HEADHan-Wen Nienhuys via GitGitGadget, Jun 30, 2020
  4. 3/4 checkout: add '\n' to reflog messageHan-Wen Nienhuys via GitGitGadget, Jun 30, 2020
  5. 4/4 Treat BISECT_HEAD as a pseudo refHan-Wen Nienhuys via GitGitGadget, Jun 30, 2020
  6. Junio C HamanoJul 7, 2020
  7. 0/4 Preliminary fixes for reftable supportHan-Wen Nienhuys via GitGitGadget, Jul 10, 2020
  8. 1/4 lib-t6000.sh: write tag using git-update-refHan-Wen Nienhuys via GitGitGadget, Jul 10, 2020
  9. 3/4 Treat BISECT_HEAD as a pseudo refHan-Wen Nienhuys via GitGitGadget, Jul 10, 2020
  10. 2/4 t3432: use git-reflog to inspect the reflog for HEADHan-Wen Nienhuys via GitGitGadget, Jul 10, 2020
  11. 4/4 Cleanse reflog messages in the refs.c layerJunio C Hamano via GitGitGadget, Jul 10, 2020
  12. Junio C HamanoJul 10, 2020
  13. Junio C HamanoJul 10, 2020

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.