@matheusbsbs / matheus-bagliano / commits / 5778b22b3d
Fork 0

diff-tree.c: load notes machinery when required

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'. Without initializing the notes machinery, 'display_note_trees' remains NULL, and thus triggers an assertion failure. 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). Reported-by: Jeff King <peff@peff.net> Signed-off-by: Taylor Blau <me@ttaylorr.com> Acked-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Apr 20, 2020 at 18:13 UTC 5778b22b3d690495e724276663c36ccd5329da4d
7 files changed +78
builtin/diff-tree.c
+9
@@ -109,6 +109,7 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
109 struct object *tree1, *tree2;
110 static struct rev_info *opt = &log_tree_opt;
111 struct setup_revision_opt s_r_opt;
112 + struct userformat_want w;
113 int read_stdin = 0;
114
115 if (argc == 2 && !strcmp(argv[1], "-h"))
@@ -127,6 +128,14 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix)
128 precompose_argv(argc, argv);
129 argc = setup_revisions(argc, argv, opt, &s_r_opt);
130
131 + memset(&w, 0, sizeof(w));
132 + userformat_find_requirements(NULL, &w);
133 +
134 + if (!opt->show_notes_given && w.notes)
135 + opt->show_notes = 1;
136 + if (opt->show_notes)
137 + load_display_notes(&opt->notes_opt);
138 +
139 while (--argc > 0) {
140 const char *arg = *++argv;
141
t/t4013-diff-various.sh
+12
@@ -95,6 +95,15 @@ test_expect_success setup '
95 git commit -m "update mode" &&
96 git checkout -f master &&
97
98 + GIT_AUTHOR_DATE="2006-06-26 00:06:00 +0000" &&
99 + GIT_COMMITTER_DATE="2006-06-26 00:06:00 +0000" &&
100 + export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
101 + git checkout -b note initial &&
102 + git update-index --chmod=+x file2 &&
103 + git commit -m "update mode (file2)" &&
104 + git notes add -m "note" &&
105 + git checkout -f master &&
106 +
107 # Same merge as master, but with parents reversed. Hide it in a
108 # pseudo-ref to avoid impacting tests with --all.
109 commit=$(echo reverse |
@@ -372,6 +381,9 @@ diff --no-index --raw --no-abbrev dir2 dir
381
382 diff-tree --pretty --root --stat --compact-summary initial
383 diff-tree --pretty -R --root --stat --compact-summary initial
384 +diff-tree --pretty note
385 +diff-tree --pretty --notes note
386 +diff-tree --format=%N note
387 diff-tree --stat --compact-summary initial mode
388 diff-tree -R --stat --compact-summary initial mode
389 EOF
t/t4013/diff.diff-tree_--format=%N_note new
+6
@@ -0,0 +1,6 @@
1 +$ git diff-tree --format=%N note
2 +note
3 +
4 +
5 +:100644 100755 01e79c32a8c99c557f0757da7cb6d65b3414466d 01e79c32a8c99c557f0757da7cb6d65b3414466d M file2
6 +$
t/t4013/diff.diff-tree_--pretty_--notes_note new
+12
@@ -0,0 +1,12 @@
1 +$ git diff-tree --pretty --notes note
2 +commit a6f364368ca320bc5a92e18912e16fa6b3dff598
3 +Author: A U Thor <author@example.com>
4 +Date: Mon Jun 26 00:06:00 2006 +0000
5 +
6 + update mode (file2)
7 +
8 +Notes:
9 + note
10 +
11 +:100644 100755 01e79c32a8c99c557f0757da7cb6d65b3414466d 01e79c32a8c99c557f0757da7cb6d65b3414466d M file2
12 +$
t/t4013/diff.diff-tree_--pretty_note new
+9
@@ -0,0 +1,9 @@
1 +$ git diff-tree --pretty note
2 +commit a6f364368ca320bc5a92e18912e16fa6b3dff598
3 +Author: A U Thor <author@example.com>
4 +Date: Mon Jun 26 00:06:00 2006 +0000
5 +
6 + update mode (file2)
7 +
8 +:100644 100755 01e79c32a8c99c557f0757da7cb6d65b3414466d 01e79c32a8c99c557f0757da7cb6d65b3414466d M file2
9 +$
t/t4013/diff.log_--decorate=full_--all
+15
@@ -5,12 +5,27 @@ Date: Mon Jun 26 00:06:00 2006 +0000
5
6 update mode
7
8 +commit a6f364368ca320bc5a92e18912e16fa6b3dff598 (refs/heads/note)
9 +Author: A U Thor <author@example.com>
10 +Date: Mon Jun 26 00:06:00 2006 +0000
11 +
12 + update mode (file2)
13 +
14 +Notes:
15 + note
16 +
17 commit cd4e72fd96faed3f0ba949dc42967430374e2290 (refs/heads/rearrange)
18 Author: A U Thor <author@example.com>
19 Date: Mon Jun 26 00:06:00 2006 +0000
20
21 Rearranged lines in dir/sub
22
23 +commit cbacedd14cb8b89255a2c02b59e77a2e9a8021a0 (refs/notes/commits)
24 +Author: A U Thor <author@example.com>
25 +Date: Mon Jun 26 00:06:00 2006 +0000
26 +
27 + Notes added by 'git notes add'
28 +
29 commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (HEAD -> refs/heads/master)
30 Merge: 9a6d494 c7a2ab9
31 Author: A U Thor <author@example.com>
t/t4013/diff.log_--decorate_--all
+15
@@ -5,12 +5,27 @@ Date: Mon Jun 26 00:06:00 2006 +0000
5
6 update mode
7
8 +commit a6f364368ca320bc5a92e18912e16fa6b3dff598 (note)
9 +Author: A U Thor <author@example.com>
10 +Date: Mon Jun 26 00:06:00 2006 +0000
11 +
12 + update mode (file2)
13 +
14 +Notes:
15 + note
16 +
17 commit cd4e72fd96faed3f0ba949dc42967430374e2290 (rearrange)
18 Author: A U Thor <author@example.com>
19 Date: Mon Jun 26 00:06:00 2006 +0000
20
21 Rearranged lines in dir/sub
22
23 +commit cbacedd14cb8b89255a2c02b59e77a2e9a8021a0 (refs/notes/commits)
24 +Author: A U Thor <author@example.com>
25 +Date: Mon Jun 26 00:06:00 2006 +0000
26 +
27 + Notes added by 'git notes add'
28 +
29 commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (HEAD -> master)
30 Merge: 9a6d494 c7a2ab9
31 Author: A U Thor <author@example.com>