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

[PATCHv3] pretty: Initialize notes if %N is used

From
Yy@vger.kernel.org <y@vger.kernel.org>
Date
Apr 13, 2010, 10:57 UTC
Message-ID
<1271156274-6984-1-git-send-email-y>
In-Reply-To
<20100413103611.GA4181@dualtron.lan>
From: Johannes Gilger <heipei@hackvalue.de>

When using git log --pretty='%N' without an explicit --show-notes, git would segfault. This patches fixes this behaviour by loading the needed notes datastructures if --pretty is used and the format contains %N. When --pretty='%N' is used together with --no-notes, %N won't be expanded.

This is an extension to a proposed patch by Jeff King.
Signed-off-by: Johannes Gilger <heipei@hackvalue.de>
---
Introduced space when calling userformat_find_requirements and dealt with %+N
and %-N during the strbuf_expand phase. I hope strncmp is the right way to do
it here. strbuf is NUL-terminated so there should not be a problem.

Greetings, Jojo

 builtin/log.c |    5 ++++-
 commit.h      |    5 +++++
 pretty.c      |   40 ++++++++++++++++++++++++++++++++++++----
 3 files changed, 45 insertions(+), 5 deletions(-)
diff --git a/builtin/log.c b/builtin/log.c
index b706a5f..6e6bc09 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -36,6 +36,7 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
 {
 	int i;
 	int decoration_style = 0;
+	struct userformat_want w;
 
 	rev->abbrev = DEFAULT_ABBREV;
 	rev->commit_format = CMIT_FMT_DEFAULT;
@@ -58,7 +59,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)
+	userformat_find_requirements(NULL, &w);
+
+	if (!rev->show_notes_given && (!rev->pretty_given || w.notes))
 		rev->show_notes = 1;
 	if (rev->show_notes)
 		init_display_notes(&rev->notes_opt);
diff --git a/commit.h b/commit.h
index 3cf5166..26ec8c0 100644
--- a/commit.h
+++ b/commit.h
@@ -74,11 +74,16 @@ struct pretty_print_context
 	struct reflog_walk_info *reflog_info;
 };
 
+struct userformat_want {
+	unsigned notes:1;
+};
+
 extern int has_non_ascii(const char *text);
 struct rev_info; /* in revision.h, it circularly uses enum cmit_fmt */
 extern char *reencode_commit_message(const struct commit *commit,
 				     const char **encoding_p);
 extern void get_commit_format(const char *arg, struct rev_info *);
+extern void userformat_find_requirements(const char *fmt, struct userformat_want *w);
 extern void format_commit_message(const struct commit *commit,
 				  const char *format, struct strbuf *sb,
 				  const struct pretty_print_context *context);
diff --git a/pretty.c b/pretty.c
index 6ba3da8..31cef5c 100644
--- a/pretty.c
+++ b/pretty.c
@@ -775,10 +775,13 @@ 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);
-		return 1;
+		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;
+		}
+		return 0;
 	}
 
 	/* For the rest we have to parse the commit header. */
@@ -855,6 +858,35 @@ static size_t format_commit_item(struct strbuf *sb, const char *placeholder,
 	return consumed + 1;
 }
 
+static size_t userformat_want_item(struct strbuf *sb, const char *placeholder,
+				   void *context)
+{
+	struct userformat_want *w = context;
+
+	switch (*placeholder) {
+		case '-':
+		case '+':
+			if (!strncmp(placeholder+1, "N", 1))
+				w->notes = 1;
+		case 'N': w->notes = 1;
+	}
+	return 0;
+}
+
+void userformat_find_requirements(const char *fmt, struct userformat_want *w)
+{
+	struct strbuf dummy = STRBUF_INIT;
+
+	memset(w, 0, sizeof(*w));
+	if (!fmt) {
+		if (!user_format)
+			return;
+		fmt = user_format;
+	}
+	strbuf_expand(&dummy, user_format, userformat_want_item, w);
+	strbuf_release(&dummy);
+}
+
 void format_commit_message(const struct commit *commit,
 			   const char *format, struct strbuf *sb,
 			   const struct pretty_print_context *pretty_ctx)
-- 
1.7.1.rc1
Previous: y@vger.kernel.orgNext: Johannes Gilger
Message 20 of 26 in “Initialize notes trees if %N is used and no --show-notes given”
  1. Initialize notes trees if %N is used and no --show-notes givenJohannes Gilger, Apr 5, 2010
  2. Jeff KingApr 6, 2010
  3. Thomas RastApr 6, 2010
  4. Johannes GilgerApr 6, 2010
  5. Thomas RastApr 6, 2010
  6. Jeff KingApr 6, 2010
  7. Junio C HamanoApr 7, 2010
  8. Jeff KingApr 7, 2010
  9. pretty.c: Don't expand %N without --show-notesJohannes Gilger, Apr 10, 2010
  10. Junio C HamanoApr 10, 2010
  11. Notes: Connect the %N flag to --{show,no}-notesJohannes Gilger, Apr 10, 2010
  12. Junio C HamanoApr 10, 2010
  13. Jeff KingApr 10, 2010
  14. pretty: Initialize notes if %N is usedJohannes Gilger, Apr 11, 2010
  15. Jeff KingApr 12, 2010
  16. [PATCHv2] pretty: Initialize notes if %N is usedJohannes Gilger, Apr 13, 2010
  17. Jeff KingApr 13, 2010
  18. Johannes GilgerApr 13, 2010
  19. [PATCHv3] pretty: Initialize notes if %N is usedy@vger.kernel.org, Apr 13, 2010
  20. [PATCHv3] pretty: Initialize notes if %N is usedy@vger.kernel.org, Apr 13, 2010
  21. [PATCHv3] pretty: Initialize notes if %N is usedJohannes Gilger, Apr 13, 2010
  22. Jeff KingApr 13, 2010
  23. [PATCHv4] pretty: Initialize notes if %N is usedJohannes Gilger, Apr 13, 2010
  24. Junio C HamanoApr 13, 2010
  25. [PATCHv5] pretty: Initialize notes if %N is usedJohannes Gilger, Apr 13, 2010
  26. Johannes GilgerApr 10, 2010

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.