Skip to content

Commit 17066be

Browse files
committed
Merge branch 'dl/format-patch-notes-config-fixup'
"git format-patch" can take a set of configured format.notes values to specify which notes refs to use in the log message part of the output. The behaviour of this was not consistent with multiple --notes command line options, which has been corrected. * dl/format-patch-notes-config-fixup: notes.h: fix typos in comment notes: break set_display_notes() into smaller functions config/format.txt: clarify behavior of multiple format.notes format-patch: move git_config() before repo_init_revisions() format-patch: use --notes behavior for format.notes notes: extract logic into set_display_notes() notes: create init_display_notes() helper notes: rename to load_display_notes()
2 parents 135365d + e0f9095 commit 17066be

File tree

7 files changed

+125
-39
lines changed

7 files changed

+125
-39
lines changed

Documentation/config/format.txt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,20 @@ If one wishes to use the ref `ref/notes/true`, please use that literal
106106
instead.
107107
+
108108
This configuration can be specified multiple times in order to allow
109-
multiple notes refs to be included.
109+
multiple notes refs to be included. In that case, it will behave
110+
similarly to multiple `--[no-]notes[=]` options passed in. That is, a
111+
value of `true` will show the default notes, a value of `<ref>` will
112+
also show notes from that notes ref and a value of `false` will negate
113+
previous configurations and not show notes.
114+
+
115+
For example,
116+
+
117+
------------
118+
[format]
119+
notes = true
120+
notes = foo
121+
notes = false
122+
notes = bar
123+
------------
124+
+
125+
will only show notes from `refs/notes/bar`.

builtin/log.c

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
208208
if (!rev->show_notes_given && (!rev->pretty_given || w.notes))
209209
rev->show_notes = 1;
210210
if (rev->show_notes)
211-
init_display_notes(&rev->notes_opt);
211+
load_display_notes(&rev->notes_opt);
212212

213213
if ((rev->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
214214
rev->diffopt.filter || rev->diffopt.flags.follow_renames)
@@ -795,6 +795,8 @@ static const char *signature_file;
795795
static enum cover_setting config_cover_letter;
796796
static const char *config_output_directory;
797797
static enum cover_from_description cover_from_description_mode = COVER_FROM_MESSAGE;
798+
static int show_notes;
799+
static struct display_notes_opt notes_opt;
798800

799801
static enum cover_from_description parse_cover_from_description(const char *arg)
800802
{
@@ -814,8 +816,6 @@ static enum cover_from_description parse_cover_from_description(const char *arg)
814816

815817
static int git_format_config(const char *var, const char *value, void *cb)
816818
{
817-
struct rev_info *rev = cb;
818-
819819
if (!strcmp(var, "format.headers")) {
820820
if (!value)
821821
die(_("format.headers without value"));
@@ -902,19 +902,13 @@ static int git_format_config(const char *var, const char *value, void *cb)
902902
return 0;
903903
}
904904
if (!strcmp(var, "format.notes")) {
905-
struct strbuf buf = STRBUF_INIT;
906905
int b = git_parse_maybe_bool(value);
907-
if (!b)
908-
return 0;
909-
rev->show_notes = 1;
910-
if (b < 0) {
911-
strbuf_addstr(&buf, value);
912-
expand_notes_ref(&buf);
913-
string_list_append(&rev->notes_opt.extra_notes_refs,
914-
strbuf_detach(&buf, NULL));
915-
} else {
916-
rev->notes_opt.use_default_notes = 1;
917-
}
906+
if (b < 0)
907+
enable_ref_display_notes(&notes_opt, &show_notes, value);
908+
else if (b)
909+
enable_default_display_notes(&notes_opt, &show_notes);
910+
else
911+
disable_display_notes(&notes_opt, &show_notes);
918912
return 0;
919913
}
920914
if (!strcmp(var, "format.coverfromdescription")) {
@@ -1719,8 +1713,11 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
17191713
extra_to.strdup_strings = 1;
17201714
extra_cc.strdup_strings = 1;
17211715
init_log_defaults();
1716+
init_display_notes(&notes_opt);
1717+
git_config(git_format_config, NULL);
17221718
repo_init_revisions(the_repository, &rev, prefix);
1723-
git_config(git_format_config, &rev);
1719+
rev.show_notes = show_notes;
1720+
memcpy(&rev.notes_opt, &notes_opt, sizeof(notes_opt));
17241721
rev.commit_format = CMIT_FMT_EMAIL;
17251722
rev.expand_tabs_in_log_default = 0;
17261723
rev.verbose_header = 1;
@@ -1839,7 +1836,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
18391836
rev.diffopt.flags.binary = 1;
18401837

18411838
if (rev.show_notes)
1842-
init_display_notes(&rev.notes_opt);
1839+
load_display_notes(&rev.notes_opt);
18431840

18441841
if (!output_directory && !use_stdout)
18451842
output_directory = config_output_directory;

notes.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,39 @@ struct notes_tree **load_notes_trees(struct string_list *refs, int flags)
10421042
}
10431043

10441044
void init_display_notes(struct display_notes_opt *opt)
1045+
{
1046+
memset(opt, 0, sizeof(*opt));
1047+
opt->use_default_notes = -1;
1048+
}
1049+
1050+
void enable_default_display_notes(struct display_notes_opt *opt, int *show_notes)
1051+
{
1052+
opt->use_default_notes = 1;
1053+
*show_notes = 1;
1054+
}
1055+
1056+
void enable_ref_display_notes(struct display_notes_opt *opt, int *show_notes,
1057+
const char *ref) {
1058+
struct strbuf buf = STRBUF_INIT;
1059+
strbuf_addstr(&buf, ref);
1060+
expand_notes_ref(&buf);
1061+
string_list_append(&opt->extra_notes_refs,
1062+
strbuf_detach(&buf, NULL));
1063+
*show_notes = 1;
1064+
}
1065+
1066+
void disable_display_notes(struct display_notes_opt *opt, int *show_notes)
1067+
{
1068+
opt->use_default_notes = -1;
1069+
/* we have been strdup'ing ourselves, so trick
1070+
* string_list into free()ing strings */
1071+
opt->extra_notes_refs.strdup_strings = 1;
1072+
string_list_clear(&opt->extra_notes_refs, 0);
1073+
opt->extra_notes_refs.strdup_strings = 0;
1074+
*show_notes = 0;
1075+
}
1076+
1077+
void load_display_notes(struct display_notes_opt *opt)
10451078
{
10461079
char *display_ref_env;
10471080
int load_config_refs = 0;

notes.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,26 @@ struct display_notes_opt {
260260
struct string_list extra_notes_refs;
261261
};
262262

263+
/*
264+
* Initialize a display_notes_opt to its default value.
265+
*/
266+
void init_display_notes(struct display_notes_opt *opt);
267+
268+
/*
269+
* This family of functions enables or disables the display of notes. In
270+
* particular, 'enable_default_display_notes' will display the default notes,
271+
* 'enable_ref_display_notes' will display the notes ref 'ref' and
272+
* 'disable_display_notes' will disable notes, including those added by previous
273+
* invocations of the 'enable_*_display_notes' functions.
274+
*
275+
* 'show_notes' is a pointer to a boolean which will be set to 1 if notes are
276+
* displayed, else 0. It must not be NULL.
277+
*/
278+
void enable_default_display_notes(struct display_notes_opt *opt, int *show_notes);
279+
void enable_ref_display_notes(struct display_notes_opt *opt, int *show_notes,
280+
const char *ref);
281+
void disable_display_notes(struct display_notes_opt *opt, int *show_notes);
282+
263283
/*
264284
* Load the notes machinery for displaying several notes trees.
265285
*
@@ -272,16 +292,16 @@ struct display_notes_opt {
272292
* - extra_notes_refs may contain a list of globs (in the same style
273293
* as notes.displayRef) where notes should be loaded from.
274294
*/
275-
void init_display_notes(struct display_notes_opt *opt);
295+
void load_display_notes(struct display_notes_opt *opt);
276296

277297
/*
278298
* Append notes for the given 'object_sha1' from all trees set up by
279-
* init_display_notes() to 'sb'.
299+
* load_display_notes() to 'sb'.
280300
*
281301
* If 'raw' is false the note will be indented by 4 places and
282302
* a 'Notes (refname):' header added.
283303
*
284-
* You *must* call init_display_notes() before using this function.
304+
* You *must* call load_display_notes() before using this function.
285305
*/
286306
void format_display_notes(const struct object_id *object_oid,
287307
struct strbuf *sb, const char *output_encoding, int raw);

revision.c

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,7 @@ void repo_init_revisions(struct repository *r,
16681668
revs->diffopt.prefix_length = strlen(prefix);
16691669
}
16701670

1671-
revs->notes_opt.use_default_notes = -1;
1671+
init_display_notes(&revs->notes_opt);
16721672
}
16731673

16741674
static void add_pending_commit_list(struct rev_info *revs,
@@ -2203,9 +2203,8 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
22032203
die("'%s': not a non-negative integer", arg);
22042204
revs->expand_tabs_in_log = val;
22052205
} else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
2206-
revs->show_notes = 1;
2206+
enable_default_display_notes(&revs->notes_opt, &revs->show_notes);
22072207
revs->show_notes_given = 1;
2208-
revs->notes_opt.use_default_notes = 1;
22092208
} else if (!strcmp(arg, "--show-signature")) {
22102209
revs->show_signature = 1;
22112210
} else if (!strcmp(arg, "--no-show-signature")) {
@@ -2220,25 +2219,14 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
22202219
revs->track_first_time = 1;
22212220
} else if (skip_prefix(arg, "--show-notes=", &optarg) ||
22222221
skip_prefix(arg, "--notes=", &optarg)) {
2223-
struct strbuf buf = STRBUF_INIT;
2224-
revs->show_notes = 1;
2225-
revs->show_notes_given = 1;
22262222
if (starts_with(arg, "--show-notes=") &&
22272223
revs->notes_opt.use_default_notes < 0)
22282224
revs->notes_opt.use_default_notes = 1;
2229-
strbuf_addstr(&buf, optarg);
2230-
expand_notes_ref(&buf);
2231-
string_list_append(&revs->notes_opt.extra_notes_refs,
2232-
strbuf_detach(&buf, NULL));
2225+
enable_ref_display_notes(&revs->notes_opt, &revs->show_notes, optarg);
2226+
revs->show_notes_given = 1;
22332227
} else if (!strcmp(arg, "--no-notes")) {
2234-
revs->show_notes = 0;
2228+
disable_display_notes(&revs->notes_opt, &revs->show_notes);
22352229
revs->show_notes_given = 1;
2236-
revs->notes_opt.use_default_notes = -1;
2237-
/* we have been strdup'ing ourselves, so trick
2238-
* string_list into free()ing strings */
2239-
revs->notes_opt.extra_notes_refs.strdup_strings = 1;
2240-
string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
2241-
revs->notes_opt.extra_notes_refs.strdup_strings = 0;
22422230
} else if (!strcmp(arg, "--standard-notes")) {
22432231
revs->show_notes_given = 1;
22442232
revs->notes_opt.use_default_notes = 1;

revision.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,10 @@ struct rev_info {
190190
always_show_header:1;
191191

192192
/* Format info */
193+
int show_notes;
193194
unsigned int shown_one:1,
194195
shown_dashes:1,
195196
show_merge:1,
196-
show_notes:1,
197197
show_notes_given:1,
198198
show_signature:1,
199199
pretty_given:1,

t/t4014-format-patch.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,38 @@ test_expect_success 'format-patch with multiple notes refs' '
793793
! grep "this is note 2" out
794794
'
795795

796+
test_expect_success 'format-patch with multiple notes refs in config' '
797+
test_when_finished "test_unconfig format.notes" &&
798+
799+
git notes --ref note1 add -m "this is note 1" HEAD &&
800+
test_when_finished git notes --ref note1 remove HEAD &&
801+
git notes --ref note2 add -m "this is note 2" HEAD &&
802+
test_when_finished git notes --ref note2 remove HEAD &&
803+
804+
git config format.notes note1 &&
805+
git format-patch -1 --stdout >out &&
806+
grep "this is note 1" out &&
807+
! grep "this is note 2" out &&
808+
git config format.notes note2 &&
809+
git format-patch -1 --stdout >out &&
810+
! grep "this is note 1" out &&
811+
grep "this is note 2" out &&
812+
git config --add format.notes note1 &&
813+
git format-patch -1 --stdout >out &&
814+
grep "this is note 1" out &&
815+
grep "this is note 2" out &&
816+
817+
git config --replace-all format.notes note1 &&
818+
git config --add format.notes false &&
819+
git format-patch -1 --stdout >out &&
820+
! grep "this is note 1" out &&
821+
! grep "this is note 2" out &&
822+
git config --add format.notes note2 &&
823+
git format-patch -1 --stdout >out &&
824+
! grep "this is note 1" out &&
825+
grep "this is note 2" out
826+
'
827+
796828
echo "fatal: --name-only does not make sense" >expect.name-only
797829
echo "fatal: --name-status does not make sense" >expect.name-status
798830
echo "fatal: --check does not make sense" >expect.check

0 commit comments

Comments
 (0)