git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH 0/5] format-patch: improve handling of `format.notes`
@ 2019-12-09 13:10 Denton Liu
  2019-12-09 13:10 ` [PATCH 1/5] notes: rename to load_display_notes() Denton Liu
                   ` (6 more replies)
  0 siblings, 7 replies; 11+ messages in thread
From: Denton Liu @ 2019-12-09 13:10 UTC (permalink / raw)
  To: Git Mailing List; +Cc: Elijah Newren, Junio C Hamano, Beat Bolli, Pavel Roskin

In this email[1], Elijah pointed out that the handling of multiple
`format.notes` configurations could be buggy. If we had
`format.notes = <ref1>`, `format.notes = false` and
`format.notes = <ref2>`, the behaviour is ambiguous. This series uses
the way `--notes=<ref1> --no-notes --notes=<ref2>` is handled as a model
and structures the handling of `format.notes` in a similar manner,
allowing one `format.notes = false` to override previous configs.

Also, in the same email, it was pointed out that git_config() should be
called before repo_init_revisions(). In 13cdf78094 (format-patch: teach
format.notes config option, 2019-05-16), the order was reversed. This
series changes it back such that git_config() is called before
repo_init_revisions().

This series is based on top of 'dl/format-patch-notes-config'.

It has minor textual conflicts with 'pu'. The merge resolution can be found at
https://github.com/Denton-L/git.git on branch
'published/published/pu-format-patch-notes-config'.

[1]: https://lore.kernel.org/git/CABPp-BF44+6gvZVNimKf-k7AWbOjw3OK-cJeFunNR96wvZGkcw@mail.gmail.com/

Denton Liu (5):
  notes: rename to load_display_notes()
  notes: create init_display_notes() helper
  notes: extract logic into set_display_notes()
  format-patch: use --notes behavior for format.notes
  format-patch: move git_config() before repo_init_revisions()

 builtin/log.c           | 26 +++++++++-----------------
 notes.c                 | 30 ++++++++++++++++++++++++++++++
 notes.h                 | 21 ++++++++++++++++++---
 revision.c              | 22 +++++-----------------
 t/t4014-format-patch.sh | 32 ++++++++++++++++++++++++++++++++
 5 files changed, 94 insertions(+), 37 deletions(-)

-- 
2.24.0.627.geba02921db


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 1/5] notes: rename to load_display_notes()
  2019-12-09 13:10 [PATCH 0/5] format-patch: improve handling of `format.notes` Denton Liu
@ 2019-12-09 13:10 ` Denton Liu
  2019-12-09 13:10 ` [PATCH 2/5] notes: create init_display_notes() helper Denton Liu
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 11+ messages in thread
From: Denton Liu @ 2019-12-09 13:10 UTC (permalink / raw)
  To: Git Mailing List; +Cc: Elijah Newren, Junio C Hamano, Beat Bolli, Pavel Roskin

According to the function comment, init_display_notes() was supposed to
"Load the notes machinery for displaying several notes trees." Rename
this function to load_display_notes() so that its use is more accurately
represented.

This is done because, in a future commit, we will reuse the name
init_display_notes().

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 builtin/log.c | 4 ++--
 notes.c       | 2 +-
 notes.h       | 6 +++---
 3 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/builtin/log.c b/builtin/log.c
index dad63cffc6..622d6a6cb1 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -202,7 +202,7 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
 	if (!rev->show_notes_given && (!rev->pretty_given || w.notes))
 		rev->show_notes = 1;
 	if (rev->show_notes)
-		init_display_notes(&rev->notes_opt);
+		load_display_notes(&rev->notes_opt);
 
 	if ((rev->diffopt.pickaxe_opts & DIFF_PICKAXE_KINDS_MASK) ||
 	    rev->diffopt.filter || rev->diffopt.flags.follow_renames)
@@ -1749,7 +1749,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 		rev.diffopt.flags.binary = 1;
 
 	if (rev.show_notes)
-		init_display_notes(&rev.notes_opt);
+		load_display_notes(&rev.notes_opt);
 
 	if (!output_directory && !use_stdout)
 		output_directory = config_output_directory;
diff --git a/notes.c b/notes.c
index 532ec37865..fd6cef14a3 100644
--- a/notes.c
+++ b/notes.c
@@ -1039,7 +1039,7 @@ struct notes_tree **load_notes_trees(struct string_list *refs, int flags)
 	return trees;
 }
 
-void init_display_notes(struct display_notes_opt *opt)
+void load_display_notes(struct display_notes_opt *opt)
 {
 	char *display_ref_env;
 	int load_config_refs = 0;
diff --git a/notes.h b/notes.h
index 414bc6855a..1ce528442a 100644
--- a/notes.h
+++ b/notes.h
@@ -272,18 +272,18 @@ struct display_notes_opt {
  * - extra_notes_refs may contain a list of globs (in the same style
  *   as notes.displayRef) where notes should be loaded from.
  */
-void init_display_notes(struct display_notes_opt *opt);
+void load_display_notes(struct display_notes_opt *opt);
 
 /*
  * Append notes for the given 'object_sha1' from all trees set up by
- * init_display_notes() to 'sb'.  The 'flags' are a bitwise
+ * load_display_notes() to 'sb'.  The 'flags' are a bitwise
  * combination of
  *
  * - NOTES_SHOW_HEADER: add a 'Notes (refname):' header
  *
  * - NOTES_INDENT: indent the notes by 4 places
  *
- * You *must* call init_display_notes() before using this function.
+ * You *must* call load_display_notes() before using this function.
  */
 void format_display_notes(const struct object_id *object_oid,
 			  struct strbuf *sb, const char *output_encoding, int raw);
-- 
2.24.0.627.geba02921db


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 2/5] notes: create init_display_notes() helper
  2019-12-09 13:10 [PATCH 0/5] format-patch: improve handling of `format.notes` Denton Liu
  2019-12-09 13:10 ` [PATCH 1/5] notes: rename to load_display_notes() Denton Liu
@ 2019-12-09 13:10 ` Denton Liu
  2019-12-09 13:10 ` [PATCH 3/5] notes: extract logic into set_display_notes() Denton Liu
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 11+ messages in thread
From: Denton Liu @ 2019-12-09 13:10 UTC (permalink / raw)
  To: Git Mailing List; +Cc: Elijah Newren, Junio C Hamano, Beat Bolli, Pavel Roskin

We currently open code the initialization for revs->notes_opt. Abstract
this away into a helper function so that the logic can be reused in a
future commit.

This is slightly wasteful as we memset the struct twice but this is only
run once so it shouldn't have any major effect.

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 notes.c    | 6 ++++++
 notes.h    | 5 +++++
 revision.c | 2 +-
 3 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/notes.c b/notes.c
index fd6cef14a3..53d1e7767c 100644
--- a/notes.c
+++ b/notes.c
@@ -1039,6 +1039,12 @@ struct notes_tree **load_notes_trees(struct string_list *refs, int flags)
 	return trees;
 }
 
+void init_display_notes(struct display_notes_opt *opt)
+{
+	memset(opt, 0, sizeof(*opt));
+	opt->use_default_notes = -1;
+}
+
 void load_display_notes(struct display_notes_opt *opt)
 {
 	char *display_ref_env;
diff --git a/notes.h b/notes.h
index 1ce528442a..c0b712371c 100644
--- a/notes.h
+++ b/notes.h
@@ -260,6 +260,11 @@ struct display_notes_opt {
 	struct string_list extra_notes_refs;
 };
 
+/*
+ * Initialize a display_notes_opt to its default value.
+ */
+void init_display_notes(struct display_notes_opt *opt);
+
 /*
  * Load the notes machinery for displaying several notes trees.
  *
diff --git a/revision.c b/revision.c
index d4aaf0ef25..24ad974590 100644
--- a/revision.c
+++ b/revision.c
@@ -1637,7 +1637,7 @@ void repo_init_revisions(struct repository *r,
 		revs->diffopt.prefix_length = strlen(prefix);
 	}
 
-	revs->notes_opt.use_default_notes = -1;
+	init_display_notes(&revs->notes_opt);
 }
 
 static void add_pending_commit_list(struct rev_info *revs,
-- 
2.24.0.627.geba02921db


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 3/5] notes: extract logic into set_display_notes()
  2019-12-09 13:10 [PATCH 0/5] format-patch: improve handling of `format.notes` Denton Liu
  2019-12-09 13:10 ` [PATCH 1/5] notes: rename to load_display_notes() Denton Liu
  2019-12-09 13:10 ` [PATCH 2/5] notes: create init_display_notes() helper Denton Liu
@ 2019-12-09 13:10 ` Denton Liu
  2019-12-09 16:26   ` Eric Sunshine
  2019-12-09 13:10 ` [PATCH 4/5] format-patch: use --notes behavior for format.notes Denton Liu
                   ` (3 subsequent siblings)
  6 siblings, 1 reply; 11+ messages in thread
From: Denton Liu @ 2019-12-09 13:10 UTC (permalink / raw)
  To: Git Mailing List; +Cc: Elijah Newren, Junio C Hamano, Beat Bolli, Pavel Roskin

Instead of open coding the logic that tweaks the variables in
`struct display_notes_opt` within handle_revision_opt(), abstract away the
logic into set_display_notes() so that it can be reused.

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 notes.c    | 24 ++++++++++++++++++++++++
 notes.h    | 10 ++++++++++
 revision.c | 20 ++++----------------
 3 files changed, 38 insertions(+), 16 deletions(-)

diff --git a/notes.c b/notes.c
index 53d1e7767c..c93feff4ab 100644
--- a/notes.c
+++ b/notes.c
@@ -1045,6 +1045,30 @@ void init_display_notes(struct display_notes_opt *opt)
 	opt->use_default_notes = -1;
 }
 
+int set_display_notes(struct display_notes_opt *opt, int show_notes, const char *opt_ref)
+{
+	if (show_notes) {
+		if (opt_ref) {
+			struct strbuf buf = STRBUF_INIT;
+			strbuf_addstr(&buf, opt_ref);
+			expand_notes_ref(&buf);
+			string_list_append(&opt->extra_notes_refs,
+					   strbuf_detach(&buf, NULL));
+		} else {
+			opt->use_default_notes = 1;
+		}
+	} else {
+		opt->use_default_notes = -1;
+		/* we have been strdup'ing ourselves, so trick
+		 * string_list into free()ing strings */
+		opt->extra_notes_refs.strdup_strings = 1;
+		string_list_clear(&opt->extra_notes_refs, 0);
+		opt->extra_notes_refs.strdup_strings = 0;
+	}
+
+	return !!show_notes;
+}
+
 void load_display_notes(struct display_notes_opt *opt)
 {
 	char *display_ref_env;
diff --git a/notes.h b/notes.h
index c0b712371c..a476bfa066 100644
--- a/notes.h
+++ b/notes.h
@@ -265,6 +265,16 @@ struct display_notes_opt {
  */
 void init_display_notes(struct display_notes_opt *opt);
 
+/*
+ * Set a display_notes_opt to a given state. 'show_notes' is a boolean
+ * representing whether or not to show notes. 'opt_ref' points to a
+ * string for the notes ref, or is NULL if the default notes should be
+ * used.
+ *
+ * Return 'show_notes' normalized to 1 or 0.
+ */
+int set_display_notes(struct display_notes_opt *opt, int show_notes, const char *opt_ref);
+
 /*
  * Load the notes machinery for displaying several notes trees.
  *
diff --git a/revision.c b/revision.c
index 24ad974590..c2d8d24939 100644
--- a/revision.c
+++ b/revision.c
@@ -2172,9 +2172,8 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
 			die("'%s': not a non-negative integer", arg);
 		revs->expand_tabs_in_log = val;
 	} else if (!strcmp(arg, "--show-notes") || !strcmp(arg, "--notes")) {
-		revs->show_notes = 1;
+		revs->show_notes = set_display_notes(&revs->notes_opt, 1, NULL);
 		revs->show_notes_given = 1;
-		revs->notes_opt.use_default_notes = 1;
 	} else if (!strcmp(arg, "--show-signature")) {
 		revs->show_signature = 1;
 	} else if (!strcmp(arg, "--no-show-signature")) {
@@ -2189,25 +2188,14 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
 		revs->track_first_time = 1;
 	} else if (skip_prefix(arg, "--show-notes=", &optarg) ||
 		   skip_prefix(arg, "--notes=", &optarg)) {
-		struct strbuf buf = STRBUF_INIT;
-		revs->show_notes = 1;
-		revs->show_notes_given = 1;
 		if (starts_with(arg, "--show-notes=") &&
 		    revs->notes_opt.use_default_notes < 0)
 			revs->notes_opt.use_default_notes = 1;
-		strbuf_addstr(&buf, optarg);
-		expand_notes_ref(&buf);
-		string_list_append(&revs->notes_opt.extra_notes_refs,
-				   strbuf_detach(&buf, NULL));
+		revs->show_notes = set_display_notes(&revs->notes_opt, 1, optarg);
+		revs->show_notes_given = 1;
 	} else if (!strcmp(arg, "--no-notes")) {
-		revs->show_notes = 0;
+		revs->show_notes = set_display_notes(&revs->notes_opt, 0, NULL);
 		revs->show_notes_given = 1;
-		revs->notes_opt.use_default_notes = -1;
-		/* we have been strdup'ing ourselves, so trick
-		 * string_list into free()ing strings */
-		revs->notes_opt.extra_notes_refs.strdup_strings = 1;
-		string_list_clear(&revs->notes_opt.extra_notes_refs, 0);
-		revs->notes_opt.extra_notes_refs.strdup_strings = 0;
 	} else if (!strcmp(arg, "--standard-notes")) {
 		revs->show_notes_given = 1;
 		revs->notes_opt.use_default_notes = 1;
-- 
2.24.0.627.geba02921db


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 4/5] format-patch: use --notes behavior for format.notes
  2019-12-09 13:10 [PATCH 0/5] format-patch: improve handling of `format.notes` Denton Liu
                   ` (2 preceding siblings ...)
  2019-12-09 13:10 ` [PATCH 3/5] notes: extract logic into set_display_notes() Denton Liu
@ 2019-12-09 13:10 ` Denton Liu
  2019-12-09 13:10 ` [PATCH 5/5] format-patch: move git_config() before repo_init_revisions() Denton Liu
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 11+ messages in thread
From: Denton Liu @ 2019-12-09 13:10 UTC (permalink / raw)
  To: Git Mailing List; +Cc: Elijah Newren, Junio C Hamano, Beat Bolli, Pavel Roskin

When we had multiple `format.notes` config values where we had `<ref1>`,
`false`, `<ref2>` (in that order), then we would print out the notes for
both `<ref1>` and `<ref2>`. This doesn't make sense, however, since we
parse the config in a top-down manner and a `false` should be able to
override previous configurations, just like how `--no-notes` will
override previous `--notes`.

Duplicate the logic that handles the `--[no-]notes[=]` option to
`format.notes` for consistency. As a result, when parsing the config
from top to bottom, `format.notes = true` will behave like `--notes`,
`format.notes = <ref>` will behave like `--notes=<ref>` and
`format.notes = false` will behave like `--no-notes`.

This change isn't strictly backwards compatible but since it is an edge
case where a sane user would not mix notes refs with `false` and this
feature is relatively new (released only in v2.23.0), this change should
be harmless.

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 builtin/log.c           | 13 +------------
 t/t4014-format-patch.sh | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 12 deletions(-)

diff --git a/builtin/log.c b/builtin/log.c
index 622d6a6cb1..1f0405f72b 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -867,19 +867,8 @@ static int git_format_config(const char *var, const char *value, void *cb)
 		return 0;
 	}
 	if (!strcmp(var, "format.notes")) {
-		struct strbuf buf = STRBUF_INIT;
 		int b = git_parse_maybe_bool(value);
-		if (!b)
-			return 0;
-		rev->show_notes = 1;
-		if (b < 0) {
-			strbuf_addstr(&buf, value);
-			expand_notes_ref(&buf);
-			string_list_append(&rev->notes_opt.extra_notes_refs,
-					strbuf_detach(&buf, NULL));
-		} else {
-			rev->notes_opt.use_default_notes = 1;
-		}
+		rev->show_notes = set_display_notes(&rev->notes_opt, b, b < 0 ? value : NULL);
 		return 0;
 	}
 
diff --git a/t/t4014-format-patch.sh b/t/t4014-format-patch.sh
index 4d5719fe2c..5c40ea4397 100755
--- a/t/t4014-format-patch.sh
+++ b/t/t4014-format-patch.sh
@@ -808,6 +808,38 @@ test_expect_success 'format-patch with multiple notes refs' '
 	! grep "this is note 2" out
 '
 
+test_expect_success 'format-patch with multiple notes refs in config' '
+	test_when_finished "test_unconfig format.notes" &&
+
+	git notes --ref note1 add -m "this is note 1" HEAD &&
+	test_when_finished git notes --ref note1 remove HEAD &&
+	git notes --ref note2 add -m "this is note 2" HEAD &&
+	test_when_finished git notes --ref note2 remove HEAD &&
+
+	git config format.notes note1 &&
+	git format-patch -1 --stdout >out &&
+	grep "this is note 1" out &&
+	! grep "this is note 2" out &&
+	git config format.notes note2 &&
+	git format-patch -1 --stdout >out &&
+	! grep "this is note 1" out &&
+	grep "this is note 2" out &&
+	git config --add format.notes note1 &&
+	git format-patch -1 --stdout >out &&
+	grep "this is note 1" out &&
+	grep "this is note 2" out &&
+
+	git config --replace-all format.notes note1 &&
+	git config --add format.notes false &&
+	git format-patch -1 --stdout >out &&
+	! grep "this is note 1" out &&
+	! grep "this is note 2" out &&
+	git config --add format.notes note2 &&
+	git format-patch -1 --stdout >out &&
+	! grep "this is note 1" out &&
+	grep "this is note 2" out
+'
+
 echo "fatal: --name-only does not make sense" > expect.name-only
 echo "fatal: --name-status does not make sense" > expect.name-status
 echo "fatal: --check does not make sense" > expect.check
-- 
2.24.0.627.geba02921db


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH 5/5] format-patch: move git_config() before repo_init_revisions()
  2019-12-09 13:10 [PATCH 0/5] format-patch: improve handling of `format.notes` Denton Liu
                   ` (3 preceding siblings ...)
  2019-12-09 13:10 ` [PATCH 4/5] format-patch: use --notes behavior for format.notes Denton Liu
@ 2019-12-09 13:10 ` Denton Liu
  2019-12-09 21:36 ` [PATCH 0/5] format-patch: improve handling of `format.notes` Junio C Hamano
  2019-12-09 21:42 ` Elijah Newren
  6 siblings, 0 replies; 11+ messages in thread
From: Denton Liu @ 2019-12-09 13:10 UTC (permalink / raw)
  To: Git Mailing List; +Cc: Elijah Newren, Junio C Hamano, Beat Bolli, Pavel Roskin

In 13cdf78094 (format-patch: teach format.notes config option,
2019-05-16), the order in which git_config() and repo_init_revisions()
were swapped so that `rev.notes_opt` would be initialized before
git_config() was called. This is problematic, however, as git_config()
should generally be called before repo_init_revisions().

Break this circular dependency by creating `show_notes` and `notes_opt`
which git_config() reads into. Then, copy these values over to
`rev.show_notes` and `rev.notes_opt` after repo_init_revisions() is
called.

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 builtin/log.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/builtin/log.c b/builtin/log.c
index 1f0405f72b..4225615e7f 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -769,6 +769,8 @@ static const char *signature = git_version_string;
 static const char *signature_file;
 static int config_cover_letter;
 static const char *config_output_directory;
+static int show_notes;
+static struct display_notes_opt notes_opt;
 
 enum {
 	COVER_UNSET,
@@ -779,8 +781,6 @@ enum {
 
 static int git_format_config(const char *var, const char *value, void *cb)
 {
-	struct rev_info *rev = cb;
-
 	if (!strcmp(var, "format.headers")) {
 		if (!value)
 			die(_("format.headers without value"));
@@ -868,7 +868,7 @@ static int git_format_config(const char *var, const char *value, void *cb)
 	}
 	if (!strcmp(var, "format.notes")) {
 		int b = git_parse_maybe_bool(value);
-		rev->show_notes = set_display_notes(&rev->notes_opt, b, b < 0 ? value : NULL);
+		show_notes = set_display_notes(&notes_opt, b, b < 0 ? value : NULL);
 		return 0;
 	}
 
@@ -1624,8 +1624,11 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
 	extra_to.strdup_strings = 1;
 	extra_cc.strdup_strings = 1;
 	init_log_defaults();
+	init_display_notes(&notes_opt);
+	git_config(git_format_config, NULL);
 	repo_init_revisions(the_repository, &rev, prefix);
-	git_config(git_format_config, &rev);
+	rev.show_notes = show_notes;
+	memcpy(&rev.notes_opt, &notes_opt, sizeof(notes_opt));
 	rev.commit_format = CMIT_FMT_EMAIL;
 	rev.expand_tabs_in_log_default = 0;
 	rev.verbose_header = 1;
-- 
2.24.0.627.geba02921db


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH 3/5] notes: extract logic into set_display_notes()
  2019-12-09 13:10 ` [PATCH 3/5] notes: extract logic into set_display_notes() Denton Liu
@ 2019-12-09 16:26   ` Eric Sunshine
  2019-12-09 19:19     ` Denton Liu
  0 siblings, 1 reply; 11+ messages in thread
From: Eric Sunshine @ 2019-12-09 16:26 UTC (permalink / raw)
  To: Denton Liu
  Cc: Git Mailing List, Elijah Newren, Junio C Hamano, Beat Bolli,
	Pavel Roskin

On Mon, Dec 9, 2019 at 8:11 AM Denton Liu <liu.denton@gmail.com> wrote:
> Instead of open coding the logic that tweaks the variables in
> `struct display_notes_opt` within handle_revision_opt(), abstract away the
> logic into set_display_notes() so that it can be reused.
>
> Signed-off-by: Denton Liu <liu.denton@gmail.com>
> ---
> diff --git a/notes.c b/notes.c
> @@ -1045,6 +1045,30 @@ void init_display_notes(struct display_notes_opt *opt)
> +int set_display_notes(struct display_notes_opt *opt, int show_notes, const char *opt_ref)
> +{
> +       if (show_notes) {
> +               if (opt_ref) {
> +                       struct strbuf buf = STRBUF_INIT;
> +                       strbuf_addstr(&buf, opt_ref);
> +                       expand_notes_ref(&buf);
> +                       string_list_append(&opt->extra_notes_refs,
> +                                          strbuf_detach(&buf, NULL));
> +               } else {
> +                       opt->use_default_notes = 1;
> +               }
> +       } else {
> +               opt->use_default_notes = -1;
> +               /* we have been strdup'ing ourselves, so trick
> +                * string_list into free()ing strings */
> +               opt->extra_notes_refs.strdup_strings = 1;
> +               string_list_clear(&opt->extra_notes_refs, 0);
> +               opt->extra_notes_refs.strdup_strings = 0;
> +       }
> +
> +       return !!show_notes;
> +}

When you find yourself creating a function in which the entire body is
(effectively) a single giant 'if' statement and in which the 'then'
and 'else' arms are chosen by an input argument to that function (and
remaining input arguments are used only by one or the other branch),
it is usually a good sign that you should really be creating two
distinct functions. Doing so would reduce cognitive load of people
reading and trying to understand the code (as well as reduce the
indentation level). For instance, you might introduce these functions:

    void enable_display_notes(struct display_notes_opt *opt, const
char *opt_ref);
    void disable_display_notes(struct display_notes_opt *opt);

> diff --git a/notes.h b/notes.h
> @@ -265,6 +265,16 @@ struct display_notes_opt {
> +/*
> + * Set a display_notes_opt to a given state. 'show_notes' is a boolean
> + * representing whether or not to show notes. 'opt_ref' points to a
> + * string for the notes ref, or is NULL if the default notes should be
> + * used.
> + *
> + * Return 'show_notes' normalized to 1 or 0.
> + */
> +int set_display_notes(struct display_notes_opt *opt, int show_notes, const char *opt_ref);

Please drop the meaningless return value. While I understand you did
this as a convenience to make calling code a bit more concise, it
nevertheless doesn't belong here since it conflates that convenience
code with the true purpose of this function (which to enable or
disable note display). Worse, it increases cognitive load on people
trying to comprehend the code since it requires that they consult the
documentation for set_display_notes() to understand what is going on.
That is, this is far less obvious:

    revs->show_notes = set_display_notes(&revs->notes_opt, 1, optarg);

than this:

    revs->show_notes = 1;
    enable_display_notes(&revs->notes_opt, optarg);

Alternately, if revs->show_notes and revs->notes_opt really ought
always be set in lockstep, then maybe it makes more sense to have the
"enable" and "disable" functions accept 'revs' directly in order to be
able to adjust both revs->show_notes and revs_notes_opt together:

    void enable_display_notes(struct rev_info *revs, const char *opt_ref);
    void disable_display_notes(struct rev_info *revs);

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 3/5] notes: extract logic into set_display_notes()
  2019-12-09 16:26   ` Eric Sunshine
@ 2019-12-09 19:19     ` Denton Liu
  2019-12-10 11:22       ` Philip Oakley
  0 siblings, 1 reply; 11+ messages in thread
From: Denton Liu @ 2019-12-09 19:19 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Git Mailing List, Elijah Newren, Junio C Hamano, Beat Bolli,
	Pavel Roskin

Hi Eric,

On Mon, Dec 09, 2019 at 11:26:15AM -0500, Eric Sunshine wrote:
> On Mon, Dec 9, 2019 at 8:11 AM Denton Liu <liu.denton@gmail.com> wrote:
> > Instead of open coding the logic that tweaks the variables in
> > `struct display_notes_opt` within handle_revision_opt(), abstract away the
> > logic into set_display_notes() so that it can be reused.
> >
> > Signed-off-by: Denton Liu <liu.denton@gmail.com>
> > ---
> > diff --git a/notes.c b/notes.c
> > @@ -1045,6 +1045,30 @@ void init_display_notes(struct display_notes_opt *opt)
> > +int set_display_notes(struct display_notes_opt *opt, int show_notes, const char *opt_ref)
> > +{
> > +       if (show_notes) {
> > +               if (opt_ref) {
> > +                       struct strbuf buf = STRBUF_INIT;
> > +                       strbuf_addstr(&buf, opt_ref);
> > +                       expand_notes_ref(&buf);
> > +                       string_list_append(&opt->extra_notes_refs,
> > +                                          strbuf_detach(&buf, NULL));
> > +               } else {
> > +                       opt->use_default_notes = 1;
> > +               }
> > +       } else {
> > +               opt->use_default_notes = -1;
> > +               /* we have been strdup'ing ourselves, so trick
> > +                * string_list into free()ing strings */
> > +               opt->extra_notes_refs.strdup_strings = 1;
> > +               string_list_clear(&opt->extra_notes_refs, 0);
> > +               opt->extra_notes_refs.strdup_strings = 0;
> > +       }
> > +
> > +       return !!show_notes;
> > +}
> 
> When you find yourself creating a function in which the entire body is
> (effectively) a single giant 'if' statement and in which the 'then'
> and 'else' arms are chosen by an input argument to that function (and
> remaining input arguments are used only by one or the other branch),
> it is usually a good sign that you should really be creating two
> distinct functions. Doing so would reduce cognitive load of people
> reading and trying to understand the code (as well as reduce the
> indentation level). For instance, you might introduce these functions:
> 
>     void enable_display_notes(struct display_notes_opt *opt, const
> char *opt_ref);
>     void disable_display_notes(struct display_notes_opt *opt);

Makes sense. I'll probably split it off into disable_display_notes(),
enable_default_display_notes() and enable_ref_display_notes() since
there is no logic shared between when opt_ref is NULL and when it isn't.

> 
> > diff --git a/notes.h b/notes.h
> > @@ -265,6 +265,16 @@ struct display_notes_opt {
> > +/*
> > + * Set a display_notes_opt to a given state. 'show_notes' is a boolean
> > + * representing whether or not to show notes. 'opt_ref' points to a
> > + * string for the notes ref, or is NULL if the default notes should be
> > + * used.
> > + *
> > + * Return 'show_notes' normalized to 1 or 0.
> > + */
> > +int set_display_notes(struct display_notes_opt *opt, int show_notes, const char *opt_ref);
> 
> Please drop the meaningless return value. While I understand you did
> this as a convenience to make calling code a bit more concise, it
> nevertheless doesn't belong here since it conflates that convenience
> code with the true purpose of this function (which to enable or
> disable note display). Worse, it increases cognitive load on people
> trying to comprehend the code since it requires that they consult the
> documentation for set_display_notes() to understand what is going on.
> That is, this is far less obvious:
> 
>     revs->show_notes = set_display_notes(&revs->notes_opt, 1, optarg);
> 
> than this:
> 
>     revs->show_notes = 1;
>     enable_display_notes(&revs->notes_opt, optarg);
> 
> Alternately, if revs->show_notes and revs->notes_opt really ought
> always be set in lockstep, then maybe it makes more sense to have the
> "enable" and "disable" functions accept 'revs' directly in order to be
> able to adjust both revs->show_notes and revs_notes_opt together:
> 
>     void enable_display_notes(struct rev_info *revs, const char *opt_ref);
>     void disable_display_notes(struct rev_info *revs);

Hmm, the "lockstep" thing was what I was going for. That's why I ended
up using one monolithic function instead of several smaller ones. That
way, the caller can just blindly pass in values and then use the values
returned to set its own state (i.e. how 4/5 does it). Also, it would
ensure that future developers using this function won't forget to set
the corresponding show_notes variable to whatever value is appropriate.

The reason why we can't accept `revs` is because this series attempts to
stop depending on `revs` for the notes configuration entirely. That way,
we can call git_config() before repo_init_revisions() since we won't
need to have `revs` initialised.

I considered accepting an `int *` instead of using the return value to
make the intent more explicit but I didn't do that because the return
value seemed easier to deal with. Perhaps we could revive that idea
with:

     void enable_display_notes(struct display_notes_opt *opt,
	     int *show_notes, const char *opt_ref);
     void disable_display_notes(struct display_notes_opt *opt,
	     int *show_notes);

I'll brood over the alternatives for a little bit before sending a
reroll.

Thanks,

Denton

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 0/5] format-patch: improve handling of `format.notes`
  2019-12-09 13:10 [PATCH 0/5] format-patch: improve handling of `format.notes` Denton Liu
                   ` (4 preceding siblings ...)
  2019-12-09 13:10 ` [PATCH 5/5] format-patch: move git_config() before repo_init_revisions() Denton Liu
@ 2019-12-09 21:36 ` Junio C Hamano
  2019-12-09 21:42 ` Elijah Newren
  6 siblings, 0 replies; 11+ messages in thread
From: Junio C Hamano @ 2019-12-09 21:36 UTC (permalink / raw)
  To: Denton Liu; +Cc: Git Mailing List, Elijah Newren, Beat Bolli, Pavel Roskin

Denton Liu <liu.denton@gmail.com> writes:

> ... This series uses
> the way `--notes=<ref1> --no-notes --notes=<ref2>` is handled as a model
> and structures the handling of `format.notes` in a similar manner,
> allowing one `format.notes = false` to override previous configs.

Makes sense.

> Also, in the same email, it was pointed out that git_config() should be
> called before repo_init_revisions(). In 13cdf78094 (format-patch: teach
> format.notes config option, 2019-05-16), the order was reversed. This
> series changes it back such that git_config() is called before
> repo_init_revisions().
>
> This series is based on top of 'dl/format-patch-notes-config'.
>
> It has minor textual conflicts with 'pu'. The merge resolution can be found at
> https://github.com/Denton-L/git.git on branch
> 'published/published/pu-format-patch-notes-config'.
>
> [1]: https://lore.kernel.org/git/CABPp-BF44+6gvZVNimKf-k7AWbOjw3OK-cJeFunNR96wvZGkcw@mail.gmail.com/
>
> Denton Liu (5):
>   notes: rename to load_display_notes()
>   notes: create init_display_notes() helper
>   notes: extract logic into set_display_notes()
>   format-patch: use --notes behavior for format.notes
>   format-patch: move git_config() before repo_init_revisions()
>
>  builtin/log.c           | 26 +++++++++-----------------
>  notes.c                 | 30 ++++++++++++++++++++++++++++++
>  notes.h                 | 21 ++++++++++++++++++---
>  revision.c              | 22 +++++-----------------
>  t/t4014-format-patch.sh | 32 ++++++++++++++++++++++++++++++++
>  5 files changed, 94 insertions(+), 37 deletions(-)

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 0/5] format-patch: improve handling of `format.notes`
  2019-12-09 13:10 [PATCH 0/5] format-patch: improve handling of `format.notes` Denton Liu
                   ` (5 preceding siblings ...)
  2019-12-09 21:36 ` [PATCH 0/5] format-patch: improve handling of `format.notes` Junio C Hamano
@ 2019-12-09 21:42 ` Elijah Newren
  6 siblings, 0 replies; 11+ messages in thread
From: Elijah Newren @ 2019-12-09 21:42 UTC (permalink / raw)
  To: Denton Liu; +Cc: Git Mailing List, Junio C Hamano, Beat Bolli, Pavel Roskin

On Mon, Dec 9, 2019 at 5:10 AM Denton Liu <liu.denton@gmail.com> wrote:
>
> In this email[1], Elijah pointed out that the handling of multiple
> `format.notes` configurations could be buggy. If we had
> `format.notes = <ref1>`, `format.notes = false` and
> `format.notes = <ref2>`, the behaviour is ambiguous. This series uses
> the way `--notes=<ref1> --no-notes --notes=<ref2>` is handled as a model
> and structures the handling of `format.notes` in a similar manner,
> allowing one `format.notes = false` to override previous configs.
>
> Also, in the same email, it was pointed out that git_config() should be
> called before repo_init_revisions(). In 13cdf78094 (format-patch: teach
> format.notes config option, 2019-05-16), the order was reversed. This
> series changes it back such that git_config() is called before
> repo_init_revisions().

Sweet, thanks for working on this.  I took a very cursory glance over
the patches and didn't spot anything egregiously wrong, though I'm
also not familiar with the notes machinery.  Mostly I'm just piping up
to thank you for tackling this so quickly.  :-)

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 3/5] notes: extract logic into set_display_notes()
  2019-12-09 19:19     ` Denton Liu
@ 2019-12-10 11:22       ` Philip Oakley
  0 siblings, 0 replies; 11+ messages in thread
From: Philip Oakley @ 2019-12-10 11:22 UTC (permalink / raw)
  To: Denton Liu, Eric Sunshine
  Cc: Git Mailing List, Elijah Newren, Junio C Hamano, Beat Bolli,
	Pavel Roskin

HI Denton

On 09/12/2019 19:19, Denton Liu wrote:
> the "lockstep" thing was what I was going for. That's why I ended
> up using one monolithic function instead of several smaller ones. That
> way, the caller can just blindly pass in values and then use the values
> returned to set its own state (i.e. how 4/5 does it). Also, it would
> ensure that future developers using this function won't forget to set
> the corresponding show_notes variable to whatever value is appropriate.
>
> The reason why we can't accept `revs` is because this series attempts to
> stop depending on `revs` for the notes configuration entirely. That way,
> we can call git_config() before repo_init_revisions() since we won't
> need to have `revs` initialised.
>
> I considered accepting an `int *` instead of using the return value to
> make the intent more explicit but I didn't do that because the return
> value seemed easier to deal with.
Does your explanation: "this series attempts to stop depending on `revs` 
for the notes configuration entirely." need adding adding to the commit 
message? I.e. the "abstract away" phrase probably doesn't carry enough 
information/context.p

Maybe pick out some of the explanations for the commit message for 
future readers?

Philip

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2019-12-10 11:22 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-09 13:10 [PATCH 0/5] format-patch: improve handling of `format.notes` Denton Liu
2019-12-09 13:10 ` [PATCH 1/5] notes: rename to load_display_notes() Denton Liu
2019-12-09 13:10 ` [PATCH 2/5] notes: create init_display_notes() helper Denton Liu
2019-12-09 13:10 ` [PATCH 3/5] notes: extract logic into set_display_notes() Denton Liu
2019-12-09 16:26   ` Eric Sunshine
2019-12-09 19:19     ` Denton Liu
2019-12-10 11:22       ` Philip Oakley
2019-12-09 13:10 ` [PATCH 4/5] format-patch: use --notes behavior for format.notes Denton Liu
2019-12-09 13:10 ` [PATCH 5/5] format-patch: move git_config() before repo_init_revisions() Denton Liu
2019-12-09 21:36 ` [PATCH 0/5] format-patch: improve handling of `format.notes` Junio C Hamano
2019-12-09 21:42 ` Elijah Newren

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).