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

Re: [PATCH] diff-tree.c: load notes machinery when required

From
Jeff King <peff@peff.net>
Date
Apr 16, 2020, 04:56 UTC
Message-ID
<20200416045630.GA21547@coredump.intra.peff.net>
In-Reply-To
<ce1ba0641e37ac84a104cd44af63e759324feb14.1586997354.git.me@ttaylorr.com>
On Wed, Apr 15, 2020 at 06:37:38PM -0600, Taylor Blau wrote:
Show 18 quoted lines
> Since its introduction in 7249e91 (revision.c: support --notes
> command-line option, 2011-03-29), combining '--notes' with any option
> that causes us to format notes (e.g., '--pretty', '--format="%N"', etc)
> results in a failed assertion at runtime.
> 
>   $ git rev-list HEAD | git diff-tree --stdin --pretty=medium --notes
>   commit 8f3d9f354286745c751374f5f1fcafee6b3f3136
>   git: notes.c:1308: format_display_notes: Assertion `display_notes_trees' failed.
>   Aborted
> 
> This failure is due to diff-tree not calling 'load_display_notes' to
> initialize the notes machinery.
> 
> Ordinarily, this failure isn't triggered, because it requires passing
> both '--notes' and another of the above mentioned options. In the case
> of '--pretty', for example, we set 'opt->verbose_header', causing
> 'show_log()' to eventually call 'format_display_notes()', which expects
> a non-NULL 'display_note_trees'.
This looks much better. One question, though...
Show 12 quoted lines
> @@ -127,6 +128,14 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
>  	precompose_argv(argc, argv);
>  	argc = setup_revisions(argc, argv, opt, &s_r_opt);
> 
> +	memset(&w, 0, sizeof(w));
> +	userformat_find_requirements(NULL, &w);
> +
> +	if (!opt->show_notes_given && (!opt->pretty_given || w.notes))
> +		opt->show_notes = 1;
> +	if (opt->show_notes)
> +		load_display_notes(&opt->notes_opt);
> +

I assume these lines were lifted from builtin/log.c. But what is pretty_given doing here?

In git-log, it's turning on notes for the default case when no format is given. But here, if no format has been given we _wouldn't_ want to show notes.

I think it's relatively harmless, since our default format is not to do any pretty-printing, and therefore we wouldn't look at the notes. But it does cause us to call load_display_notes() when we don't need to. I think that conditional can be simplified to just:

  if (!opt->show_notes_given && w.notes)
	opt->show_notes = 1;
> Fix this by initializing the notes machinery after parsing our options,
> and harden this behavior against regression with a test in t4013. (Note
> that the added ref in this test requires updating two unrelated tests
> which use 'log --all', and thus need to learn about the new refs).

This makes the test update rather hard to follow, but I don't think there's an easy way around it. I wondered if we could insert and remove our notes just for our tests, but it's hard to do with the big while loop in that script.

I also thought it might not be that bad to just add a few tests at the end, but there are some complications (like removing sha1s from the output; easily done with a custom format, but the point is to test --pretty).

So what you have is probably the most sensible thing (and the new commit would make it easier to test other commands around notes later, if we chose to).

Show 8 quoted lines
> @@ -398,6 +407,8 @@ diff --no-index --raw --no-abbrev dir2 dir
> 
>  diff-tree --pretty --root --stat --compact-summary initial
>  diff-tree --pretty -R --root --stat --compact-summary initial
> +diff-tree --pretty --notes note
> +diff-tree --format=%N note
>  diff-tree --stat --compact-summary initial mode
>  diff-tree -R --stat --compact-summary initial mode
Perhaps worth testing that:
  diff-tree --pretty note
does not print any notes by default?
-Peff
Previous: Junio C HamanoNext: Taylor Blau
Message 3 of 8 in “diff-tree.c: load notes machinery when required”
  1. diff-tree.c: load notes machinery when requiredTaylor Blau, Apr 16, 2020
  2. Junio C HamanoApr 16, 2020
  3. Jeff KingApr 16, 2020
  4. Taylor BlauApr 21, 2020
  5. Taylor BlauApr 21, 2020
  6. Jeff KingApr 21, 2020
  7. Taylor BlauApr 21, 2020
  8. Jeff KingApr 21, 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.