Skip to content

Commit 779f946

Browse files
committed
Merge branch 'jg/auto-initialize-notes-with-percent-n-in-format'
* jg/auto-initialize-notes-with-percent-n-in-format: t3301: add tests to use --format="%N" pretty: Initialize notes if %N is used
2 parents fab4502 + 636db2c commit 779f946

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

builtin/log.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
3636
{
3737
int i;
3838
int decoration_style = 0;
39+
struct userformat_want w;
3940

4041
rev->abbrev = DEFAULT_ABBREV;
4142
rev->commit_format = CMIT_FMT_DEFAULT;
@@ -58,7 +59,10 @@ static void cmd_log_init(int argc, const char **argv, const char *prefix,
5859
usage(builtin_log_usage);
5960
argc = setup_revisions(argc, argv, rev, opt);
6061

61-
if (!rev->show_notes_given && !rev->pretty_given)
62+
memset(&w, 0, sizeof(w));
63+
userformat_find_requirements(NULL, &w);
64+
65+
if (!rev->show_notes_given && (!rev->pretty_given || w.notes))
6266
rev->show_notes = 1;
6367
if (rev->show_notes)
6468
init_display_notes(&rev->notes_opt);

commit.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,16 @@ struct pretty_print_context
7474
struct reflog_walk_info *reflog_info;
7575
};
7676

77+
struct userformat_want {
78+
unsigned notes:1;
79+
};
80+
7781
extern int has_non_ascii(const char *text);
7882
struct rev_info; /* in revision.h, it circularly uses enum cmit_fmt */
7983
extern char *reencode_commit_message(const struct commit *commit,
8084
const char **encoding_p);
8185
extern void get_commit_format(const char *arg, struct rev_info *);
86+
extern void userformat_find_requirements(const char *fmt, struct userformat_want *w);
8287
extern void format_commit_message(const struct commit *commit,
8388
const char *format, struct strbuf *sb,
8489
const struct pretty_print_context *context);

pretty.c

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -775,10 +775,13 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
775775
}
776776
return 0; /* unknown %g placeholder */
777777
case 'N':
778-
format_display_notes(commit->object.sha1, sb,
779-
git_log_output_encoding ? git_log_output_encoding
780-
: git_commit_encoding, 0);
781-
return 1;
778+
if (c->pretty_ctx->show_notes) {
779+
format_display_notes(commit->object.sha1, sb,
780+
git_log_output_encoding ? git_log_output_encoding
781+
: git_commit_encoding, 0);
782+
return 1;
783+
}
784+
return 0;
782785
}
783786

784787
/* 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,
855858
return consumed + 1;
856859
}
857860

861+
static size_t userformat_want_item(struct strbuf *sb, const char *placeholder,
862+
void *context)
863+
{
864+
struct userformat_want *w = context;
865+
866+
if (*placeholder == '+' || *placeholder == '-')
867+
placeholder++;
868+
869+
switch (*placeholder) {
870+
case 'N':
871+
w->notes = 1;
872+
break;
873+
}
874+
return 0;
875+
}
876+
877+
void userformat_find_requirements(const char *fmt, struct userformat_want *w)
878+
{
879+
struct strbuf dummy = STRBUF_INIT;
880+
881+
if (!fmt) {
882+
if (!user_format)
883+
return;
884+
fmt = user_format;
885+
}
886+
strbuf_expand(&dummy, user_format, userformat_want_item, w);
887+
strbuf_release(&dummy);
888+
}
889+
858890
void format_commit_message(const struct commit *commit,
859891
const char *format, struct strbuf *sb,
860892
const struct pretty_print_context *pretty_ctx)

t/t3301-notes.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ test_expect_success 'handle empty notes gracefully' '
5555
git notes show ; test 1 = $?
5656
'
5757

58+
test_expect_success 'show non-existent notes entry with %N' '
59+
for l in A B
60+
do
61+
echo "$l"
62+
done >expect &&
63+
git show -s --format='A%n%NB' >output &&
64+
test_cmp expect output
65+
'
66+
5867
test_expect_success 'create notes' '
5968
git config core.notesRef refs/notes/commits &&
6069
MSG=b4 git notes add &&
@@ -65,6 +74,15 @@ test_expect_success 'create notes' '
6574
test_must_fail git notes show HEAD^
6675
'
6776

77+
test_expect_success 'show notes entry with %N' '
78+
for l in A b4 B
79+
do
80+
echo "$l"
81+
done >expect &&
82+
git show -s --format='A%n%NB' >output &&
83+
test_cmp expect output
84+
'
85+
6886
cat >expect <<EOF
6987
d423f8c refs/notes/commits@{0}: notes: Notes added by 'git notes add'
7088
EOF

0 commit comments

Comments
 (0)