From 5b16360330822527eac1fa84131d185ff784c9fb Mon Sep 17 00:00:00 2001 From: Johannes Gilger Date: Tue, 13 Apr 2010 22:31:12 +0200 Subject: [PATCH 1/2] pretty: Initialize notes if %N is used 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 Signed-off-by: Junio C Hamano --- builtin/log.c | 6 +++++- commit.h | 5 +++++ pretty.c | 40 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/builtin/log.c b/builtin/log.c index b706a5ff88..6208703c06 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,10 @@ 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) + memset(&w, 0, sizeof(w)); + 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 3cf5166581..26ec8c0d1c 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 6ba3da89b7..7cb3a2af50 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; + + if (*placeholder == '+' || *placeholder == '-') + placeholder++; + + switch (*placeholder) { + case 'N': + w->notes = 1; + break; + } + return 0; +} + +void userformat_find_requirements(const char *fmt, struct userformat_want *w) +{ + struct strbuf dummy = STRBUF_INIT; + + 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) From 636db2c036d0f26bd0b1139b7abe04bdecf3e036 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sun, 18 Apr 2010 11:19:39 -0700 Subject: [PATCH 2/2] t3301: add tests to use --format="%N" Signed-off-by: Junio C Hamano --- t/t3301-notes.sh | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/t/t3301-notes.sh b/t/t3301-notes.sh index b2e7b07039..64f32ad94d 100755 --- a/t/t3301-notes.sh +++ b/t/t3301-notes.sh @@ -55,6 +55,15 @@ test_expect_success 'handle empty notes gracefully' ' git notes show ; test 1 = $? ' +test_expect_success 'show non-existent notes entry with %N' ' + for l in A B + do + echo "$l" + done >expect && + git show -s --format='A%n%NB' >output && + test_cmp expect output +' + test_expect_success 'create notes' ' git config core.notesRef refs/notes/commits && MSG=b4 git notes add && @@ -65,6 +74,15 @@ test_expect_success 'create notes' ' test_must_fail git notes show HEAD^ ' +test_expect_success 'show notes entry with %N' ' + for l in A b4 B + do + echo "$l" + done >expect && + git show -s --format='A%n%NB' >output && + test_cmp expect output +' + cat >expect <