Re: [PATCH] pretty.c: Don't expand %N without --show-notes

2 messages, 2 authors, 2016-06-15 · open the first message on its own page

Re: [PATCH] pretty.c: Don't expand %N without --show-notes

From: Junio C Hamano <hidden>
Date: 2016-06-15 22:48:36

Johannes Gilger [off-list ref] writes:
The %N placeholder will only work if --show-notes was provided to log.
By not expanding the user is given feedback that he won't be shown any
notes.
I kind of like the simplicity of this approach, but this probably cuts
both ways.  I can hear some users screaming "But but but I already told
you that I am interested in notes---how else would I say %N in the format
string?" while others say "Yeah, that way I can keep using the same format
and when I don't give --show-notes it won't show extra information in the
output.  Very nice".
quoted hunk
diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
index 1686a54..bf7813f 100644
--- a/Documentation/pretty-formats.txt
+++ b/Documentation/pretty-formats.txt
@@ -143,7 +143,8 @@ NOTE: Some placeholders may depend on other options given to the
 revision traversal engine. For example, the `%g*` reflog options will
 insert an empty string unless we are traversing reflog entries (e.g., by
 `git log -g`). The `%d` placeholder will use the "short" decoration
-format if `--decorate` was not already provided on the command line.
+format if `--decorate` was not already provided on the command line. The %N
+placeholder won't be expanded unless `--show-notes` was provided.
 
 If you add a `{plus}` (plus sign) after '%' of a placeholder, a line-feed
 is inserted immediately before the expansion if and only if the
diff --git a/pretty.c b/pretty.c
index 6ba3da8..b39e2d5 100644
--- a/pretty.c
+++ b/pretty.c
@@ -775,6 +775,9 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
 		}
 		return 0;	/* unknown %g placeholder */
 	case 'N':
+		if (!c->pretty_ctx->show_notes)
+			return 0;
+
 		format_display_notes(commit->object.sha1, sb,
 			    git_log_output_encoding ? git_log_output_encoding
 						    : git_commit_encoding, 0);
-- 
1.7.0.2.201.g80978

[PATCH] Notes: Connect the %N flag to --{show,no}-notes

From: Johannes Gilger <hidden>
Date: 2016-06-15 22:48:36

This improves the behaviour of the %N flag when using --pretty:

- If only --pretty is used, notes will only be initialized if %N is
  actually part of the format.
- If %N and --no-notes is used, %N will expand to the empty string.
- Behaviour for regular log with --no-notes and --show-notes remains the
  same.

Signed-off-by: Johannes Gilger <redacted>
---
I reread a lot of code and think I've found a solution that's intuitive and
doesn't automatically initialize the trees with --pretty unless %N is actually
used. Basically what could happen is this:

- --show-notes (with eventual options) is supplied and
  init_display_notes(&rev.notes_opt) is called from log.c, %N is sucessfully
  expanded

- Only %N is used, no --no-notes was supplied. format_display_notes is called,
  notices the missing notes_trees and initializes it. this obviously happens
  only if an %N is used, otherwise no display trees will be initialized.
  init_display_notes could not have been called with any other options, since
  no --show-notes was given, so NULL is fine here.

- %N is used and --no-notes is supplied, format_display_notes is not
  called and %N is expanded to the empty string. This last case also
  applies to --pretty=full etc.

I tested a lot of cases and tried to make sure I understood all the possible
caveats. If you have any further questions feel free to ask.

 builtin/log.c |    4 ++--
 notes.c       |    4 +++-
 pretty.c      |    7 ++++---
 3 files changed, 9 insertions(+), 6 deletions(-)
diff --git a/builtin/log.c b/builtin/log.c
index b706a5f..029d7b8 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -58,9 +58,9 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
 		usage(builtin_log_usage);
 	argc = setup_revisions(argc, argv, rev, opt);
 
-	if (!rev->show_notes_given && !rev->pretty_given)
+	if (!rev->show_notes_given)
 		rev->show_notes = 1;
-	if (rev->show_notes)
+	if (rev->show_notes && (!rev->pretty_given || rev->show_notes_given))
 		init_display_notes(&rev->notes_opt);
 
 	if (rev->diffopt.pickaxe || rev->diffopt.filter)
diff --git a/notes.c b/notes.c
index e425e19..ad14a8b 100644
--- a/notes.c
+++ b/notes.c
@@ -1183,7 +1183,9 @@ void format_display_notes(const unsigned char *object_sha1,
 			  struct strbuf *sb, const char *output_encoding, int flags)
 {
 	int i;
-	assert(display_notes_trees);
+	if(!display_notes_trees)
+		init_display_notes(NULL);
+
 	for (i = 0; display_notes_trees[i]; i++)
 		format_note(display_notes_trees[i], object_sha1, sb,
 			    output_encoding, flags);
diff --git a/pretty.c b/pretty.c
index 6ba3da8..8e828a1 100644
--- a/pretty.c
+++ b/pretty.c
@@ -775,9 +775,10 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
 		}
 		return 0;	/* unknown %g placeholder */
 	case 'N':
-		format_display_notes(commit->object.sha1, sb,
-			    git_log_output_encoding ? git_log_output_encoding
-						    : git_commit_encoding, 0);
+		if (c->pretty_ctx->show_notes)
+			format_display_notes(commit->object.sha1, sb,
+					     git_log_output_encoding ? git_log_output_encoding
+					     : git_commit_encoding, 0);
 		return 1;
 	}
 
-- 
1.7.0.2.201.g80978
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help