git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johannes Gilger <heipei@hackvalue.de>
To: Git ML <git@vger.kernel.org>
Cc: Thomas Rast <trast@student.ethz.ch>, Jeff King <peff@peff.net>,
	Junio C Hamano <gitster@pobox.com>,
	Johannes Gilger <heipei@hackvalue.de>
Subject: [PATCH] pretty: Initialize notes if %N is used
Date: Sun, 11 Apr 2010 16:54:22 +0200	[thread overview]
Message-ID: <1270997662-25430-1-git-send-email-heipei@hackvalue.de> (raw)
In-Reply-To: <20100410220843.GA29987@coredump.intra.peff.net>

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>
---
Hey Jeff,

something like this? I didn't see why userformat_fill_want had to have an extra
argument for the format, since the user_format variable is static in pretty.c.

Sorry for the many very different patches to the bug, as you can see I'm not
really familiar with best-practices in git.git.

Greetings,
Jojo

 builtin/log.c |    6 +++++-
 commit.h      |    5 +++++
 pretty.c      |   31 +++++++++++++++++++++++++++----
 3 files changed, 37 insertions(+), 5 deletions(-)

diff --git a/builtin/log.c b/builtin/log.c
index b706a5f..f8f5d22 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -58,7 +58,11 @@ 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)
+	struct userformat_want w;
+	if (rev->commit_format == CMIT_FMT_USERFORMAT)
+		userformat_fill_want(&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..fc1c504 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_fill_want(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..0e3ae98 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,26 @@ 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 'N': w->notes = 1;
+	}
+	return 0;
+}
+
+void userformat_fill_want(struct userformat_want *w)
+{
+	if (!user_format)
+		return;
+	struct strbuf dummy = STRBUF_INIT;
+	memset(w, 0, sizeof(*w));
+	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.0.2.201.g80978

  reply	other threads:[~2010-04-11 14:54 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-04-05 11:55 [PATCH] Initialize notes trees if %N is used and no --show-notes given Johannes Gilger
2010-04-06  5:32 ` Jeff King
2010-04-06  9:27 ` Thomas Rast
2010-04-06 11:19   ` Johannes Gilger
2010-04-06 11:52     ` Thomas Rast
2010-04-06 16:22       ` Jeff King
2010-04-07  6:18       ` Junio C Hamano
2010-04-07  6:36         ` Jeff King
2010-04-10  7:05   ` [PATCH] pretty.c: Don't expand %N without --show-notes Johannes Gilger
2010-04-10 20:00     ` Junio C Hamano
2010-04-10 21:30       ` [PATCH] Notes: Connect the %N flag to --{show,no}-notes Johannes Gilger
2010-04-10 21:51         ` Junio C Hamano
2010-04-10 22:08           ` Jeff King
2010-04-11 14:54             ` Johannes Gilger [this message]
2010-04-12  8:56               ` [PATCH] pretty: Initialize notes if %N is used Jeff King
2010-04-13  8:59                 ` [PATCHv2] " Johannes Gilger
2010-04-13 10:03                   ` Jeff King
2010-04-13 10:36                     ` Johannes Gilger
2010-04-13 10:57                       ` [PATCHv3] " y
2010-04-13 10:57                       ` y
2010-04-13 11:01                       ` Johannes Gilger
2010-04-13 11:07                         ` Jeff King
2010-04-13 11:26                           ` [PATCHv4] " Johannes Gilger
2010-04-13 20:01                             ` Junio C Hamano
2010-04-13 20:31                               ` [PATCHv5] " Johannes Gilger
2010-04-10 22:20           ` [PATCH] Notes: Connect the %N flag to --{show,no}-notes Johannes Gilger

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1270997662-25430-1-git-send-email-heipei@hackvalue.de \
    --to=heipei@hackvalue.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=peff@peff.net \
    --cc=trast@student.ethz.ch \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).