git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / Atom feed
* [PATCH] advice: refactor advise API
@ 2020-02-10  5:04 Heba Waly via GitGitGadget
  2020-02-10 14:38 ` Derrick Stolee
                   ` (3 more replies)
  0 siblings, 4 replies; 50+ messages in thread
From: Heba Waly via GitGitGadget @ 2020-02-10  5:04 UTC (permalink / raw)
  To: git; +Cc: Heba Waly, Heba Waly

From: Heba Waly <heba.waly@gmail.com>

Add a new advise_ng function that can check the visibility of advice
messages before printing.

Currently it's very easy for the callers to miss checking the
visibility step. Also, it makes more sense for this step to be handled
by the advice library.

Also change the advise call in tag library from advise() to advise_ng()
to construct an example of the usage of the new API.

Signed-off-by: Heba Waly <heba.waly@gmail.com>
---
    [RFC][Outreachy] advice: refactor advise API
    
    The advice API is currently a little bit confusing to call. quoting from
    [1]:
    
    When introducing a new advice message, you would
    
     * come up with advice.frotz configuration variable
    
     * define and declare advice_frotz global variable that defaults to
       true
    
     * sprinkle calls like this:
    
      if (advice_frotz)
        advise(_("helpful message about frotz"));
    
    A new approach was suggested in [1] which this patch is based upon.
    
    A new advise_ng() is introduced to gradually replace advise()
    
    pros of the new advise():
    
     * The caller doesn't need to define a new global variable when
       introducing a new message.
     * The caller doesn't need to check the visibility of the message before
       calling advise_ng().
     * The caller still needs to come up with advice.frotz config variable
       and will call advice_ng as follows: advice_ng("advice.frotz",
       _("helpful message about frotz"));
    
    After this patch the plan is to migrate the rest of the advise calls to
    advise_ng and then finally remove advise() and rename advise_ng() to
    advise()
    
    [1] 
    https://public-inbox.org/git/xmqqzhf5cw69.fsf@gitster-ct.c.googlers.com/

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-548%2FHebaWaly%2Fadvice_refactoring-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-548/HebaWaly/advice_refactoring-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/548

 Makefile               |  1 +
 advice.c               | 37 +++++++++++++++++++++++++++++++++++--
 advice.h               |  6 +++++-
 builtin/tag.c          |  4 ++--
 t/helper/test-advise.c | 15 +++++++++++++++
 t/helper/test-tool.c   |  1 +
 t/helper/test-tool.h   |  1 +
 t/t0018-advice.sh      | 29 +++++++++++++++++++++++++++++
 t/t7004-tag.sh         |  2 ++
 9 files changed, 91 insertions(+), 5 deletions(-)
 create mode 100644 t/helper/test-advise.c
 create mode 100755 t/t0018-advice.sh

diff --git a/Makefile b/Makefile
index 09f98b777c..ed923a3e81 100644
--- a/Makefile
+++ b/Makefile
@@ -695,6 +695,7 @@ X =
 
 PROGRAMS += $(patsubst %.o,git-%$X,$(PROGRAM_OBJS))
 
+TEST_BUILTINS_OBJS += test-advise.o
 TEST_BUILTINS_OBJS += test-chmtime.o
 TEST_BUILTINS_OBJS += test-config.o
 TEST_BUILTINS_OBJS += test-ctype.o
diff --git a/advice.c b/advice.c
index 249c60dcf3..4793e59223 100644
--- a/advice.c
+++ b/advice.c
@@ -29,7 +29,6 @@ int advice_ignored_hook = 1;
 int advice_waiting_for_editor = 1;
 int advice_graft_file_deprecated = 1;
 int advice_checkout_ambiguous_remote_branch_name = 1;
-int advice_nested_tag = 1;
 int advice_submodule_alternate_error_strategy_die = 1;
 
 static int advice_use_color = -1;
@@ -89,7 +88,6 @@ static struct {
 	{ "waitingForEditor", &advice_waiting_for_editor },
 	{ "graftFileDeprecated", &advice_graft_file_deprecated },
 	{ "checkoutAmbiguousRemoteBranchName", &advice_checkout_ambiguous_remote_branch_name },
-	{ "nestedTag", &advice_nested_tag },
 	{ "submoduleAlternateErrorStrategyDie", &advice_submodule_alternate_error_strategy_die },
 
 	/* make this an alias for backward compatibility */
@@ -118,6 +116,41 @@ void advise(const char *advice, ...)
 	strbuf_release(&buf);
 }
 
+static const char turn_off_instructions[] =
+N_("\n"
+"Turn this message off by running\n"
+"\"git config %s false\"");
+
+void advise_ng(const char *key, const char *advice, ...)
+{
+	int value = 1;
+	struct strbuf buf = STRBUF_INIT;
+	va_list params;
+	const char *cp, *np;
+	
+	git_config_get_bool(key, &value);
+	
+	if(value)
+	{
+		va_start(params, advice);
+		strbuf_vaddf(&buf, advice, params);
+		va_end(params);
+
+		strbuf_addf(&buf, turn_off_instructions, key);
+		
+		for (cp = buf.buf; *cp; cp = np) {
+			np = strchrnul(cp, '\n');
+			fprintf(stderr,	_("%shint: %.*s%s\n"),
+				advise_get_color(ADVICE_COLOR_HINT),
+				(int)(np - cp), cp,
+				advise_get_color(ADVICE_COLOR_RESET));
+			if (*np)
+				np++;
+		}
+		strbuf_release(&buf);
+	}
+}
+
 int git_default_advice_config(const char *var, const char *value)
 {
 	const char *k, *slot_name;
diff --git a/advice.h b/advice.h
index b706780614..ad4da2d65d 100644
--- a/advice.h
+++ b/advice.h
@@ -29,12 +29,16 @@ extern int advice_ignored_hook;
 extern int advice_waiting_for_editor;
 extern int advice_graft_file_deprecated;
 extern int advice_checkout_ambiguous_remote_branch_name;
-extern int advice_nested_tag;
 extern int advice_submodule_alternate_error_strategy_die;
 
 int git_default_advice_config(const char *var, const char *value);
 __attribute__((format (printf, 1, 2)))
 void advise(const char *advice, ...);
+
+/**
+ Checks the visibility of the advice before priniting.
+ */
+void advise_ng(const char *key, const char *advice, ...);
 int error_resolve_conflict(const char *me);
 void NORETURN die_resolve_conflict(const char *me);
 void NORETURN die_conclude_merge(void);
diff --git a/builtin/tag.c b/builtin/tag.c
index e0a4c25382..7fe1ff3ed0 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -231,8 +231,8 @@ static void create_tag(const struct object_id *object, const char *object_ref,
 	if (type <= OBJ_NONE)
 		die(_("bad object type."));
 
-	if (type == OBJ_TAG && advice_nested_tag)
-		advise(_(message_advice_nested_tag), tag, object_ref);
+	if (type == OBJ_TAG)
+		advise_ng("advice.nestedTag", _(message_advice_nested_tag), tag, object_ref);
 
 	strbuf_addf(&header,
 		    "object %s\n"
diff --git a/t/helper/test-advise.c b/t/helper/test-advise.c
new file mode 100644
index 0000000000..b6ec90fd18
--- /dev/null
+++ b/t/helper/test-advise.c
@@ -0,0 +1,15 @@
+#include "test-tool.h"
+#include "cache.h"
+#include "advice.h"
+
+int cmd__advise_ng(int argc, const char **argv)
+{
+	if (!argv[1] || !argv[2])
+	die("usage: %s <key> <advice>", argv[0]);
+
+	setup_git_directory();
+
+	advise_ng(argv[1], argv[2]);
+
+	return 0;
+}
diff --git a/t/helper/test-tool.c b/t/helper/test-tool.c
index f20989d449..f903f32bb6 100644
--- a/t/helper/test-tool.c
+++ b/t/helper/test-tool.c
@@ -14,6 +14,7 @@ struct test_cmd {
 };
 
 static struct test_cmd cmds[] = {
+	{ "advise", cmd__advise_ng },
 	{ "chmtime", cmd__chmtime },
 	{ "config", cmd__config },
 	{ "ctype", cmd__ctype },
diff --git a/t/helper/test-tool.h b/t/helper/test-tool.h
index 8ed2af71d1..e5e955beb9 100644
--- a/t/helper/test-tool.h
+++ b/t/helper/test-tool.h
@@ -4,6 +4,7 @@
 #define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "git-compat-util.h"
 
+int cmd__advise_ng(int argc, const char **argv);
 int cmd__chmtime(int argc, const char **argv);
 int cmd__config(int argc, const char **argv);
 int cmd__ctype(int argc, const char **argv);
diff --git a/t/t0018-advice.sh b/t/t0018-advice.sh
new file mode 100755
index 0000000000..d012db515d
--- /dev/null
+++ b/t/t0018-advice.sh
@@ -0,0 +1,29 @@
+#!/bin/sh
+
+test_description='Test advise_ng functionality'
+
+. ./test-lib.sh
+
+cat > expected <<EOF
+hint: This is a piece of advice
+hint: Turn this message off by running
+hint: "git config advice.configVariable false"
+EOF
+test_expect_success 'advise should be printed when config variable is unset' '
+	test-tool advise "advice.configVariable" "This is a piece of advice" 2>actual &&
+	test_i18ncmp expected actual
+'
+
+test_expect_success 'advise should be printed when config variable is set to true' '
+	test_config advice.configVariable true &&
+	test-tool advise "advice.configVariable" "This is a piece of advice" 2>actual &&
+	test_i18ncmp expected actual
+'
+
+test_expect_success 'advise should not be printed when config variable is set to false' '
+	test_config advice.configVariable false &&
+	test-tool advise "advice.configVariable" "This is a piece of advice" 2>actual &&
+	test_must_be_empty actual
+'
+
+test_done
diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
index 6db92bd3ba..b7c8d41899 100755
--- a/t/t7004-tag.sh
+++ b/t/t7004-tag.sh
@@ -1726,6 +1726,8 @@ test_expect_success 'recursive tagging should give advice' '
 	hint: already a tag. If you meant to tag the object that it points to, use:
 	hint: |
 	hint: 	git tag -f nested annotated-v4.0^{}
+	hint: Turn this message off by running
+	hint: "git config advice.nestedTag false"
 	EOF
 	git tag -m nested nested annotated-v4.0 2>actual &&
 	test_i18ncmp expect actual

base-commit: c7a62075917b3340f908093f63f1161c44ed1475
-- 
gitgitgadget

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

* Re: [PATCH] advice: refactor advise API
  2020-02-10  5:04 [PATCH] advice: refactor advise API Heba Waly via GitGitGadget
@ 2020-02-10 14:38 ` Derrick Stolee
  2020-02-10 19:30   ` Junio C Hamano
  2020-02-10 23:56   ` Heba Waly
  2020-02-10 20:37 ` Jeff King
                   ` (2 subsequent siblings)
  3 siblings, 2 replies; 50+ messages in thread
From: Derrick Stolee @ 2020-02-10 14:38 UTC (permalink / raw)
  To: Heba Waly via GitGitGadget, git; +Cc: Heba Waly

On 2/10/2020 12:04 AM, Heba Waly via GitGitGadget wrote:
> From: Heba Waly <heba.waly@gmail.com>
> 
> Add a new advise_ng function that can check the visibility of advice
> messages before printing.
>
> Currently it's very easy for the callers to miss checking the
> visibility step. Also, it makes more sense for this step to be handled
> by the advice library.

This makes the advice API much easier and its uses much cleaner. Thanks!
 
> Also change the advise call in tag library from advise() to advise_ng()
> to construct an example of the usage of the new API.

This is a good example case.

> +static const char turn_off_instructions[] =
> +N_("\n"
> +"Turn this message off by running\n"
> +"\"git config %s false\"");

I have mixed feelings on the use of these instructions. Perhaps at
minimum the addition of these instructions could be left to a
separate patch than the creation of advise_ng().

My biggest concern is that this adds unexpected noise to users who
want the advice to stay. I'm calling attention to it, because this
part isn't a simple refactor like the rest of the patch.

If it _does_ stay, then I recommend condensing the message to
a single line. For example:

	Disable this message with "git config %d false"

> +void advise_ng(const char *key, const char *advice, ...)
> +{
> +	int value = 1;
> +	struct strbuf buf = STRBUF_INIT;
> +	va_list params;
> +	const char *cp, *np;
> +	
> +	git_config_get_bool(key, &value);
> +	
> +	if(value)
> +	{

Style: spacing, and opening braces are on the same line as the if:

	if (value) {

But also, this method would be simpler if the opposite case was
an early return:

	if (!value)
		return;

Then the rest could have one less indentation.

> +		va_start(params, advice);
> +		strbuf_vaddf(&buf, advice, params);
> +		va_end(params);
> +
> +		strbuf_addf(&buf, turn_off_instructions, key);
> +
> +		for (cp = buf.buf; *cp; cp = np) {
> +			np = strchrnul(cp, '\n');
> +			fprintf(stderr,	_("%shint: %.*s%s\n"),
> +				advise_get_color(ADVICE_COLOR_HINT),
> +				(int)(np - cp), cp,
> +				advise_get_color(ADVICE_COLOR_RESET));
> +			if (*np)
> +				np++;
> +		}
> +		strbuf_release(&buf);

This loop looks like it was copied from advise(). Perhaps we could
re-use that code better by creating a new vadvise() method that
takes a va_list, and have advise() and advise_ng() call it instead?
I include a patch at the end of this method that does this conversion.
(Feel free to incorporate it into your next version, if you want, but
be sure to add your sign-off.) Then, your advise_ng() can call these:

	vadvise(advice, params);
	advise(turn_off_instructions, key);

removing the need to re-implement the for loop.

> diff --git a/t/helper/test-advise.c b/t/helper/test-advise.c
> new file mode 100644
> index 0000000000..b6ec90fd18
> --- /dev/null
> +++ b/t/helper/test-advise.c
> @@ -0,0 +1,15 @@
> +#include "test-tool.h"
> +#include "cache.h"
> +#include "advice.h"
> +
> +int cmd__advise_ng(int argc, const char **argv)
> +{
> +	if (!argv[1] || !argv[2])
> +	die("usage: %s <key> <advice>", argv[0]);
> +
> +	setup_git_directory();
> +
> +	advise_ng(argv[1], argv[2]);
> +
> +	return 0;
> +}

I definitely tend to recommend more tests than most, but perhaps this
unit test is overkill? You demonstrate a good test below using a real
Git command, which should be sufficient. If the "turn this message off"
part gets removed, then you will still have coverage of your method.
It just won't require a test change because it would not modify behavior.

> diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
> index 6db92bd3ba..b7c8d41899 100755
> --- a/t/t7004-tag.sh
> +++ b/t/t7004-tag.sh
> @@ -1726,6 +1726,8 @@ test_expect_success 'recursive tagging should give advice' '
>  	hint: already a tag. If you meant to tag the object that it points to, use:
>  	hint: |
>  	hint: 	git tag -f nested annotated-v4.0^{}
> +	hint: Turn this message off by running
> +	hint: "git config advice.nestedTag false"
>  	EOF
>  	git tag -m nested nested annotated-v4.0 2>actual &&
>  	test_i18ncmp expect actual
> 
> base-commit: c7a62075917b3340f908093f63f1161c44ed1475

Thanks,
-Stolee

-->8--

From: Derrick Stolee <dstolee@microsoft.com>
Date: Mon, 10 Feb 2020 09:33:20 -0500
Subject: [PATCH] advice: extract vadvise() from advise()

In preparation for a new advice method, extract a version of advise()
that uses an explict 'va_list' parameter. Call it from advise() for a
functionally equivalent version.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
---
 advice.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/advice.c b/advice.c
index 249c60dcf3..fd836332da 100644
--- a/advice.c
+++ b/advice.c
@@ -96,15 +96,12 @@ static struct {
 	{ "pushNonFastForward", &advice_push_update_rejected }
 };
 
-void advise(const char *advice, ...)
+static void vadvise(const char *advice, va_list params)
 {
 	struct strbuf buf = STRBUF_INIT;
-	va_list params;
 	const char *cp, *np;
 
-	va_start(params, advice);
 	strbuf_vaddf(&buf, advice, params);
-	va_end(params);
 
 	for (cp = buf.buf; *cp; cp = np) {
 		np = strchrnul(cp, '\n');
@@ -118,6 +115,14 @@ void advise(const char *advice, ...)
 	strbuf_release(&buf);
 }
 
+void advise(const char *advice, ...)
+{
+	va_list params;
+	va_start(params, advice);
+	vadvise(advice, params);
+	va_end(params);
+}
+
 int git_default_advice_config(const char *var, const char *value)
 {
 	const char *k, *slot_name;
-- 
2.25.0.vfs.1.1.1.g9906319d24.dirty




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

* Re: [PATCH] advice: refactor advise API
  2020-02-10 14:38 ` Derrick Stolee
@ 2020-02-10 19:30   ` Junio C Hamano
  2020-02-10 19:42     ` Taylor Blau
  2020-02-10 23:56   ` Heba Waly
  1 sibling, 1 reply; 50+ messages in thread
From: Junio C Hamano @ 2020-02-10 19:30 UTC (permalink / raw)
  To: Derrick Stolee; +Cc: Heba Waly via GitGitGadget, git, Heba Waly

Derrick Stolee <stolee@gmail.com> writes:

>> +static const char turn_off_instructions[] =
>> +N_("\n"
>> +"Turn this message off by running\n"
>> +"\"git config %s false\"");
>
> I have mixed feelings on the use of these instructions. Perhaps at
> minimum the addition of these instructions could be left to a
> separate patch than the creation of advise_ng().
>
> My biggest concern is that this adds unexpected noise to users who
> want the advice to stay. I'm calling attention to it, because this
> part isn't a simple refactor like the rest of the patch.
> ...
> I definitely tend to recommend more tests than most, but perhaps this
> unit test is overkill? You demonstrate a good test below using a real
> Git command, which should be sufficient. If the "turn this message off"
> part gets removed, then you will still have coverage of your method.
> It just won't require a test change because it would not modify behavior.
> ...

All good suggestions.  Thanks for an excellent review.

Another thing.  

advise_ng() may have been a good name for illustration but is a
horrible name for real-world use (imagine we need to revamp the API
one more time in the future---what would it be called, which has to
say that it is newer than the "next generation"?
advise_3rd_try()?).

Thanks.

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

* Re: [PATCH] advice: refactor advise API
  2020-02-10 19:30   ` Junio C Hamano
@ 2020-02-10 19:42     ` Taylor Blau
  2020-02-10 22:29       ` Emily Shaffer
  2020-02-11  0:08       ` Heba Waly
  0 siblings, 2 replies; 50+ messages in thread
From: Taylor Blau @ 2020-02-10 19:42 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Derrick Stolee, Heba Waly via GitGitGadget, git, Heba Waly

On Mon, Feb 10, 2020 at 11:30:46AM -0800, Junio C Hamano wrote:
> Derrick Stolee <stolee@gmail.com> writes:
>
> >> +static const char turn_off_instructions[] =
> >> +N_("\n"
> >> +"Turn this message off by running\n"
> >> +"\"git config %s false\"");
> >
> > I have mixed feelings on the use of these instructions. Perhaps at
> > minimum the addition of these instructions could be left to a
> > separate patch than the creation of advise_ng().
> >
> > My biggest concern is that this adds unexpected noise to users who
> > want the advice to stay. I'm calling attention to it, because this
> > part isn't a simple refactor like the rest of the patch.
> > ...
> > I definitely tend to recommend more tests than most, but perhaps this
> > unit test is overkill? You demonstrate a good test below using a real
> > Git command, which should be sufficient. If the "turn this message off"
> > part gets removed, then you will still have coverage of your method.
> > It just won't require a test change because it would not modify behavior.
> > ...
>
> All good suggestions.  Thanks for an excellent review.
>
> Another thing.
>
> advise_ng() may have been a good name for illustration but is a
> horrible name for real-world use (imagine we need to revamp the API
> one more time in the future---what would it be called, which has to
> say that it is newer than the "next generation"?
> advise_3rd_try()?).

What about calling this new API 'advise()'? The first patch could call
it 'advise_ng' or whatever other temporary name we feel comfortable
using, and then each subsequent patch would update callers of 'advise()'
to use 'advise_ng()'. Once those patches have been applied, and no other
callers of 'advise()' exist, a final patch can be applied on top to
rename 'advise_ng()' to 'advise()', and update the names of all of the
callers.

This makes for a rather noisy final patch, but the intermediate states
are much clearer, and it would make this series rather self-contained.

On the other hand, having a version of 'advise_ng()' on master makes
this topic more incremental, meaning that we can pick it up and put it
down at ease and have more self-contained projects.

I don't really have a preference between the two approaches, but if we
go with the latter, I do think we need something better than
'advise_ng'. Maybe 'advise_warn'? I don't know.

> Thanks.

Thanks,
Taylor

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

* Re: [PATCH] advice: refactor advise API
  2020-02-10  5:04 [PATCH] advice: refactor advise API Heba Waly via GitGitGadget
  2020-02-10 14:38 ` Derrick Stolee
@ 2020-02-10 20:37 ` Jeff King
  2020-02-10 22:55   ` Emily Shaffer
  2020-02-11  2:20   ` Heba Waly
  2020-02-10 22:46 ` Junio C Hamano
  2020-02-16 21:39 ` [PATCH v2 0/2] [RFC][Outreachy] " Heba Waly via GitGitGadget
  3 siblings, 2 replies; 50+ messages in thread
From: Jeff King @ 2020-02-10 20:37 UTC (permalink / raw)
  To: Heba Waly via GitGitGadget; +Cc: git, Heba Waly

On Mon, Feb 10, 2020 at 05:04:09AM +0000, Heba Waly via GitGitGadget wrote:

>     The advice API is currently a little bit confusing to call. quoting from
>     [1]:
>     
>     When introducing a new advice message, you would
>     
>      * come up with advice.frotz configuration variable
>     
>      * define and declare advice_frotz global variable that defaults to
>        true
>     
>      * sprinkle calls like this:
>     
>       if (advice_frotz)
>         advise(_("helpful message about frotz"));
>     
>     A new approach was suggested in [1] which this patch is based upon.

I agree that the current procedure is a bit painful, and I think this is
a step in the right direction. But...

>     After this patch the plan is to migrate the rest of the advise calls to
>     advise_ng and then finally remove advise() and rename advise_ng() to
>     advise()

...this step may not be possible, for a few reasons:

  1. Some of the sites do more than just advise(). E.g., branch.c checks
     the flag and calls both error() and advise().

  2. Some callers may have to do work to generate the arguments. If I
     have:

       advise("advice.foo", "some data: %s", generate_data());

     then we'll call generate_data() even if we'll throw away the result
     in the end.

Similarly, some users of advice_* variables do not call advise() at all
(some call die(), some like builtin/rm.c stuff the result in a strbuf,
and I don't even know what's going on with wt_status.hints. :)

So I think you may need to phase it in a bit more, like:

  a. introduce want_advice() which decides whether or not to show the
     advice based on a config key. I'd also suggest making the "advice."
     part of the key implicit, just to make life easier for the callers.

  b. introduce advise_key() which uses want_advice() and advise() under
     the hood to do what your advise_ng() is doing here.

  c. convert simple patterns of:

       if (advice_foo)
          advise("bar");

     into:

       advise_key("foo", "bar");

     and drop advice_foo where possible.

  d. handle more complex cases one-by-one. For example, with something
     like:

       if (advice_foo)
         die("bar");

     we probably want:

       if (want_advice("foo"))
         die("bar");

     instead. Using string literals is more accident-prone than
     variables (because the compiler doesn't notice if we misspell them)
     but I think is OK for cases where we just refer to the key once.
     For others (e.g., advice_commit_before_merge has 13 mentions),
     either keep the variable. Or alternatively make a wrapper like:

       int want_advice_commit_before_merge(void)
       {
               return want_advice("commitbeforemerge");
       }

     if we want to drop the existing mechanism to load all of the
     variables at the beginning.

-Peff

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

* Re: [PATCH] advice: refactor advise API
  2020-02-10 19:42     ` Taylor Blau
@ 2020-02-10 22:29       ` Emily Shaffer
  2020-02-11  0:08       ` Heba Waly
  1 sibling, 0 replies; 50+ messages in thread
From: Emily Shaffer @ 2020-02-10 22:29 UTC (permalink / raw)
  To: Taylor Blau
  Cc: Junio C Hamano, Derrick Stolee, Heba Waly via GitGitGadget, git,
	Heba Waly

On Mon, Feb 10, 2020 at 11:42:53AM -0800, Taylor Blau wrote:
> On Mon, Feb 10, 2020 at 11:30:46AM -0800, Junio C Hamano wrote:
> > Derrick Stolee <stolee@gmail.com> writes:
> >
> > >> +static const char turn_off_instructions[] =
> > >> +N_("\n"
> > >> +"Turn this message off by running\n"
> > >> +"\"git config %s false\"");
> > >
> > > I have mixed feelings on the use of these instructions. Perhaps at
> > > minimum the addition of these instructions could be left to a
> > > separate patch than the creation of advise_ng().
> > >
> > > My biggest concern is that this adds unexpected noise to users who
> > > want the advice to stay. I'm calling attention to it, because this
> > > part isn't a simple refactor like the rest of the patch.
> > > ...
> > > I definitely tend to recommend more tests than most, but perhaps this
> > > unit test is overkill? You demonstrate a good test below using a real
> > > Git command, which should be sufficient. If the "turn this message off"
> > > part gets removed, then you will still have coverage of your method.
> > > It just won't require a test change because it would not modify behavior.
> > > ...
> >
> > All good suggestions.  Thanks for an excellent review.
> >
> > Another thing.
> >
> > advise_ng() may have been a good name for illustration but is a
> > horrible name for real-world use (imagine we need to revamp the API
> > one more time in the future---what would it be called, which has to
> > say that it is newer than the "next generation"?
> > advise_3rd_try()?).
> 
> What about calling this new API 'advise()'? The first patch could call
> it 'advise_ng' or whatever other temporary name we feel comfortable
> using, and then each subsequent patch would update callers of 'advise()'
> to use 'advise_ng()'. Once those patches have been applied, and no other
> callers of 'advise()' exist, a final patch can be applied on top to
> rename 'advise_ng()' to 'advise()', and update the names of all of the
> callers.

I think this is the precise strategy called out in the patch
description.
https://lore.kernel.org/git/pull.548.git.1581311049547.gitgitgadget@gmail.com

> This makes for a rather noisy final patch, but the intermediate states
> are much clearer, and it would make this series rather self-contained.
> 
> On the other hand, having a version of 'advise_ng()' on master makes
> this topic more incremental, meaning that we can pick it up and put it
> down at ease and have more self-contained projects.
> 
> I don't really have a preference between the two approaches, but if we
> go with the latter, I do think we need something better than
> 'advise_ng'. Maybe 'advise_warn'? I don't know.

I like that this opens up the possibility of advise_err(), advise_die(),
whatever to meet Peff's suggestion.

 - Emily

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

* Re: [PATCH] advice: refactor advise API
  2020-02-10  5:04 [PATCH] advice: refactor advise API Heba Waly via GitGitGadget
  2020-02-10 14:38 ` Derrick Stolee
  2020-02-10 20:37 ` Jeff King
@ 2020-02-10 22:46 ` Junio C Hamano
  2020-02-11  2:01   ` Heba Waly
  2020-02-16 21:39 ` [PATCH v2 0/2] [RFC][Outreachy] " Heba Waly via GitGitGadget
  3 siblings, 1 reply; 50+ messages in thread
From: Junio C Hamano @ 2020-02-10 22:46 UTC (permalink / raw)
  To: Heba Waly via GitGitGadget; +Cc: git, Heba Waly

"Heba Waly via GitGitGadget" <gitgitgadget@gmail.com> writes:

>     A new advise_ng() is introduced to gradually replace advise()
>     
>     pros of the new advise():
>     
>      * The caller doesn't need to define a new global variable when
>        introducing a new message.
>      * The caller doesn't need to check the visibility of the message before
>        calling advise_ng().
>      * The caller still needs to come up with advice.frotz config variable
>        and will call advice_ng as follows: advice_ng("advice.frotz",
>        _("helpful message about frotz"));

Readers would expect to see "cons of the same" to follow "pros".

>     After this patch the plan is to migrate the rest of the advise calls to
>     advise_ng and then finally remove advise() and rename advise_ng() to
>     advise()

As I outlined in [1], I think the over-simplified
"advise_ng(<advise.key>, _(<message>), ...)"  would be too limited
to replace the current users, without a pair of helper functions,
one to just check for the guarding advise.key, and the other to
unconditionally show the message (i.e. the latter is what the
current advise() is).

>     [1] 
>     https://public-inbox.org/git/xmqqzhf5cw69.fsf@gitster-ct.c.googlers.com/

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

* Re: [PATCH] advice: refactor advise API
  2020-02-10 20:37 ` Jeff King
@ 2020-02-10 22:55   ` Emily Shaffer
  2020-02-11  2:35     ` Heba Waly
  2020-02-11 19:49     ` Jeff King
  2020-02-11  2:20   ` Heba Waly
  1 sibling, 2 replies; 50+ messages in thread
From: Emily Shaffer @ 2020-02-10 22:55 UTC (permalink / raw)
  To: Jeff King; +Cc: Heba Waly via GitGitGadget, git, Heba Waly

On Mon, Feb 10, 2020 at 03:37:25PM -0500, Jeff King wrote:
> On Mon, Feb 10, 2020 at 05:04:09AM +0000, Heba Waly via GitGitGadget wrote:
> 
> >     The advice API is currently a little bit confusing to call. quoting from
> >     [1]:
> >     
> >     When introducing a new advice message, you would
> >     
> >      * come up with advice.frotz configuration variable
> >     
> >      * define and declare advice_frotz global variable that defaults to
> >        true
> >     
> >      * sprinkle calls like this:
> >     
> >       if (advice_frotz)
> >         advise(_("helpful message about frotz"));
> >     
> >     A new approach was suggested in [1] which this patch is based upon.
> 
> I agree that the current procedure is a bit painful, and I think this is
> a step in the right direction. But...
> 
> >     After this patch the plan is to migrate the rest of the advise calls to
> >     advise_ng and then finally remove advise() and rename advise_ng() to
> >     advise()
> 
> ...this step may not be possible, for a few reasons:
> 
>   1. Some of the sites do more than just advise(). E.g., branch.c checks
>      the flag and calls both error() and advise().
> 
>   2. Some callers may have to do work to generate the arguments. If I
>      have:
> 
>        advise("advice.foo", "some data: %s", generate_data());
> 
>      then we'll call generate_data() even if we'll throw away the result
>      in the end.
> 
> Similarly, some users of advice_* variables do not call advise() at all
> (some call die(), some like builtin/rm.c stuff the result in a strbuf,
> and I don't even know what's going on with wt_status.hints. :)
> 
> So I think you may need to phase it in a bit more, like:
> 
>   a. introduce want_advice() which decides whether or not to show the
>      advice based on a config key. I'd also suggest making the "advice."
>      part of the key implicit, just to make life easier for the callers.
> 
>   b. introduce advise_key() which uses want_advice() and advise() under
>      the hood to do what your advise_ng() is doing here.
> 
>   c. convert simple patterns of:
> 
>        if (advice_foo)
>           advise("bar");
> 
>      into:
> 
>        advise_key("foo", "bar");
> 
>      and drop advice_foo where possible.
> 
>   d. handle more complex cases one-by-one. For example, with something
>      like:
> 
>        if (advice_foo)
>          die("bar");
> 
>      we probably want:
> 
>        if (want_advice("foo"))
>          die("bar");
> 
>      instead. Using string literals is more accident-prone than
>      variables (because the compiler doesn't notice if we misspell them)
>      but I think is OK for cases where we just refer to the key once.
>      For others (e.g., advice_commit_before_merge has 13 mentions),
>      either keep the variable. Or alternatively make a wrapper like:
> 
>        int want_advice_commit_before_merge(void)
>        {
>                return want_advice("commitbeforemerge");
>        }
> 
>      if we want to drop the existing mechanism to load all of the
>      variables at the beginning.

I tend to disagree on both counts. I'd personally rather see something
like 'void advise_key(enum advice, char *format, ...)'.

As I understand it, Heba wanted to avoid that pattern so that people
adding a new advice didn't need to modify the advice library. However, I
think there's value to having a centralized list of all possible advices
(besides the documentation). The per-advice wrapper is harder to iterate
than an enum, and will also result in a lot of samey code if we decide
we want to use that pattern for more advices.

(In fact, with git-bugreport I'm running into a lot of regret that hooks
are invoked in the way Peff describes - 'find_hook("pre-commit")' -
rather than with an enum naming the hook; it's very hard to check all
possible hooks, and hard to examine the codebase and determine which
hooks do and don't exist.)

When Heba began to describe this project I had hoped for a final product
like 'void show_advice(enum advice_config)' which looked up the
appropriate string from the advice library instead of asking the caller
to provide it, although seeing the need for varargs has demonstrated to
me that that's not feasible :) But I think taking the advice config key
as an argument is possibly too far the other direction. At that point,
it starts to beg the question, "why isn't this function in config.h and
called print_if_configured(cfgname, message, ...)?"

Although, take this all with a grain of salt. I think I lean towards
this much encapsulation after a sordid history with C++ and an
enlightened C developer may not choose it ;)

 - Emily

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

* Re: [PATCH] advice: refactor advise API
  2020-02-10 14:38 ` Derrick Stolee
  2020-02-10 19:30   ` Junio C Hamano
@ 2020-02-10 23:56   ` Heba Waly
  2020-02-11  2:39     ` Derrick Stolee
  1 sibling, 1 reply; 50+ messages in thread
From: Heba Waly @ 2020-02-10 23:56 UTC (permalink / raw)
  To: Derrick Stolee; +Cc: Heba Waly via GitGitGadget, Git Mailing List

On Tue, Feb 11, 2020 at 3:38 AM Derrick Stolee <stolee@gmail.com> wrote:
>
> On 2/10/2020 12:04 AM, Heba Waly via GitGitGadget wrote:
> > From: Heba Waly <heba.waly@gmail.com>
> >
> > Add a new advise_ng function that can check the visibility of advice
> > messages before printing.
> >
> > Currently it's very easy for the callers to miss checking the
> > visibility step. Also, it makes more sense for this step to be handled
> > by the advice library.
>
> This makes the advice API much easier and its uses much cleaner. Thanks!
>
> > Also change the advise call in tag library from advise() to advise_ng()
> > to construct an example of the usage of the new API.
>
> This is a good example case.
>
> > +static const char turn_off_instructions[] =
> > +N_("\n"
> > +"Turn this message off by running\n"
> > +"\"git config %s false\"");
>
> I have mixed feelings on the use of these instructions. Perhaps at
> minimum the addition of these instructions could be left to a
> separate patch than the creation of advise_ng().
>
> My biggest concern is that this adds unexpected noise to users who
> want the advice to stay. I'm calling attention to it, because this
> part isn't a simple refactor like the rest of the patch.
>
> If it _does_ stay, then I recommend condensing the message to
> a single line. For example:
>
>         Disable this message with "git config %d false"
>

I agree with you, I had mixed feelings about it too when suggested on
a previous patch [2].
But then I realized that it's hard for the user to find the right
config variable to turn off from the doc only.
So I like the compromise of condensing it to a single line.

> > +     if(value)
> > +     {
>
> Style: spacing, and opening braces are on the same line as the if:
>
>         if (value) {
>
> But also, this method would be simpler if the opposite case was
> an early return:
>
>         if (!value)
>                 return;
> Then the rest could have one less indentation.

Agree

> This loop looks like it was copied from advise(). Perhaps we could
> re-use that code better by creating a new vadvise() method that
> takes a va_list, and have advise() and advise_ng() call it instead?
> I include a patch at the end of this method that does this conversion.
> (Feel free to incorporate it into your next version, if you want, but
> be sure to add your sign-off.) Then, your advise_ng() can call these:
>
>         vadvise(advice, params);
>         advise(turn_off_instructions, key);
>
> removing the need to re-implement the for loop.

My intention was to replace advise() by advise_ng(), so I didn't mind
a temp code repetition during the transition phase.
But as it seems like some folks would rather keep both, then yes of
course a vadvise() function is the way to go, thanks.

> > diff --git a/t/helper/test-advise.c b/t/helper/test-advise.c
> > new file mode 100644
> > index 0000000000..b6ec90fd18
> > --- /dev/null
> > +++ b/t/helper/test-advise.c
> > @@ -0,0 +1,15 @@
> > +#include "test-tool.h"
> > +#include "cache.h"
> > +#include "advice.h"
> > +
> > +int cmd__advise_ng(int argc, const char **argv)
> > +{
> > +     if (!argv[1] || !argv[2])
> > +     die("usage: %s <key> <advice>", argv[0]);
> > +
> > +     setup_git_directory();
> > +
> > +     advise_ng(argv[1], argv[2]);
> > +
> > +     return 0;
> > +}
>
> I definitely tend to recommend more tests than most, but perhaps this
> unit test is overkill? You demonstrate a good test below using a real
> Git command, which should be sufficient. If the "turn this message off"
> part gets removed, then you will still have coverage of your method.
> It just won't require a test change because it would not modify behavior.
>

I see your point but I wanted to make sure advise_ng honors the config
variable using tests 2 & 3 in `t0018-advice.sh`
and `t7004-tag.sh` didn't seem like a good place to add these tests.

> > diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
> > index 6db92bd3ba..b7c8d41899 100755
> > --- a/t/t7004-tag.sh
> > +++ b/t/t7004-tag.sh
> > @@ -1726,6 +1726,8 @@ test_expect_success 'recursive tagging should give advice' '
> >       hint: already a tag. If you meant to tag the object that it points to, use:
> >       hint: |
> >       hint:   git tag -f nested annotated-v4.0^{}
> > +     hint: Turn this message off by running
> > +     hint: "git config advice.nestedTag false"
> >       EOF
> >       git tag -m nested nested annotated-v4.0 2>actual &&
> >       test_i18ncmp expect actual
> >
> > base-commit: c7a62075917b3340f908093f63f1161c44ed1475
>
> Thanks,
> -Stolee
>
> -->8--
>
> From: Derrick Stolee <dstolee@microsoft.com>
> Date: Mon, 10 Feb 2020 09:33:20 -0500
> Subject: [PATCH] advice: extract vadvise() from advise()
>
> In preparation for a new advice method, extract a version of advise()
> that uses an explict 'va_list' parameter. Call it from advise() for a
> functionally equivalent version.
>
> Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
> ---
>  advice.c | 13 +++++++++----
>  1 file changed, 9 insertions(+), 4 deletions(-)
>
> diff --git a/advice.c b/advice.c
> index 249c60dcf3..fd836332da 100644
> --- a/advice.c
> +++ b/advice.c
> @@ -96,15 +96,12 @@ static struct {
>         { "pushNonFastForward", &advice_push_update_rejected }
>  };
>
> -void advise(const char *advice, ...)
> +static void vadvise(const char *advice, va_list params)
>  {
>         struct strbuf buf = STRBUF_INIT;
> -       va_list params;
>         const char *cp, *np;
>
> -       va_start(params, advice);
>         strbuf_vaddf(&buf, advice, params);
> -       va_end(params);
>
>         for (cp = buf.buf; *cp; cp = np) {
>                 np = strchrnul(cp, '\n');
> @@ -118,6 +115,14 @@ void advise(const char *advice, ...)
>         strbuf_release(&buf);
>  }
>
> +void advise(const char *advice, ...)
> +{
> +       va_list params;
> +       va_start(params, advice);
> +       vadvise(advice, params);
> +       va_end(params);
> +}
> +
>  int git_default_advice_config(const char *var, const char *value)
>  {
>         const char *k, *slot_name;
> --
> 2.25.0.vfs.1.1.1.g9906319d24.dirty
>
>
>

[2] https://lore.kernel.org/git/CACg5j26DEXuxwqRYHi5UOBUpRwsu_2A9LwgyKq4qB9wxqasD7g@mail.gmail.com/

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

* Re: [PATCH] advice: refactor advise API
  2020-02-10 19:42     ` Taylor Blau
  2020-02-10 22:29       ` Emily Shaffer
@ 2020-02-11  0:08       ` Heba Waly
  2020-02-12 20:57         ` Taylor Blau
  1 sibling, 1 reply; 50+ messages in thread
From: Heba Waly @ 2020-02-11  0:08 UTC (permalink / raw)
  To: Taylor Blau
  Cc: Junio C Hamano, Derrick Stolee, Heba Waly via GitGitGadget,
	Git Mailing List

On Tue, Feb 11, 2020 at 8:42 AM Taylor Blau <me@ttaylorr.com> wrote:
>
> On Mon, Feb 10, 2020 at 11:30:46AM -0800, Junio C Hamano wrote:
> > Another thing.
> >
> > advise_ng() may have been a good name for illustration but is a
> > horrible name for real-world use (imagine we need to revamp the API
> > one more time in the future---what would it be called, which has to
> > say that it is newer than the "next generation"?
> > advise_3rd_try()?).

As I mentioned earlier, this patch is meant to be used as a transition
between the current advise() and the refactored one.
So the name is just temporary and it'll be renamed to advise() once
the transition is done.
But if we want to keep both functions, or want a better name because
it's an open-source project and the author might not complete the
transition, then I'll try to think of another name.

> What about calling this new API 'advise()'? The first patch could call
> it 'advise_ng' or whatever other temporary name we feel comfortable
> using, and then each subsequent patch would update callers of 'advise()'
> to use 'advise_ng()'. Once those patches have been applied, and no other
> callers of 'advise()' exist, a final patch can be applied on top to
> rename 'advise_ng()' to 'advise()', and update the names of all of the
> callers.
>

Yes, that's what I would like to do.

> This makes for a rather noisy final patch, but the intermediate states
> are much clearer, and it would make this series rather self-contained.
>
> On the other hand, having a version of 'advise_ng()' on master makes
> this topic more incremental, meaning that we can pick it up and put it
> down at ease and have more self-contained projects.
>
> I don't really have a preference between the two approaches, but if we
> go with the latter, I do think we need something better than
> 'advise_ng'. Maybe 'advise_warn'? I don't know.
>
> > Thanks.
>
> Thanks,
> Taylor

Thanks,
Heba

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

* Re: [PATCH] advice: refactor advise API
  2020-02-10 22:46 ` Junio C Hamano
@ 2020-02-11  2:01   ` Heba Waly
  2020-02-11  6:08     ` Junio C Hamano
  0 siblings, 1 reply; 50+ messages in thread
From: Heba Waly @ 2020-02-11  2:01 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Heba Waly via GitGitGadget, Git Mailing List

On Tue, Feb 11, 2020 at 11:46 AM Junio C Hamano <gitster@pobox.com> wrote:
>
>
> As I outlined in [1], I think the over-simplified
> "advise_ng(<advise.key>, _(<message>), ...)"  would be too limited
> to replace the current users, without a pair of helper functions,
> one to just check for the guarding advise.key, and the other to
> unconditionally show the message (i.e. the latter is what the
> current advise() is).
>

I agree with adding the first helper, specially after Peff's comments,
but I don't see why we would keep the current advise() which
unconditionally shows the message...
As far as I understand from a previous discussion [3], that's a wrong
usage that we need to avoid, quoting from [3]:
```
If there are many other places that calls to advise() are made
without getting guarded by the toggles defined in advice.c, we
should fix them, I think.
```

> >     [1]
> >     https://public-inbox.org/git/xmqqzhf5cw69.fsf@gitster-ct.c.googlers.com/
[3] https://public-inbox.org/git/xmqqpng1eisc.fsf@gitster-ct.c.googlers.com/

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

* Re: [PATCH] advice: refactor advise API
  2020-02-10 20:37 ` Jeff King
  2020-02-10 22:55   ` Emily Shaffer
@ 2020-02-11  2:20   ` Heba Waly
  1 sibling, 0 replies; 50+ messages in thread
From: Heba Waly @ 2020-02-11  2:20 UTC (permalink / raw)
  To: Jeff King; +Cc: Heba Waly via GitGitGadget, Git Mailing List

On Tue, Feb 11, 2020 at 9:37 AM Jeff King <peff@peff.net> wrote:
>
> On Mon, Feb 10, 2020 at 05:04:09AM +0000, Heba Waly via GitGitGadget wrote:
>
> >     The advice API is currently a little bit confusing to call. quoting from
> >     [1]:
> >
> >     When introducing a new advice message, you would
> >
> >      * come up with advice.frotz configuration variable
> >
> >      * define and declare advice_frotz global variable that defaults to
> >        true
> >
> >      * sprinkle calls like this:
> >
> >       if (advice_frotz)
> >         advise(_("helpful message about frotz"));
> >
> >     A new approach was suggested in [1] which this patch is based upon.
>
> I agree that the current procedure is a bit painful, and I think this is
> a step in the right direction. But...
>
> >     After this patch the plan is to migrate the rest of the advise calls to
> >     advise_ng and then finally remove advise() and rename advise_ng() to
> >     advise()
>
> ...this step may not be possible, for a few reasons:
>
>   1. Some of the sites do more than just advise(). E.g., branch.c checks
>      the flag and calls both error() and advise().
>
>   2. Some callers may have to do work to generate the arguments. If I
>      have:
>
>        advise("advice.foo", "some data: %s", generate_data());
>
>      then we'll call generate_data() even if we'll throw away the result
>      in the end.
>
> Similarly, some users of advice_* variables do not call advise() at all
> (some call die(), some like builtin/rm.c stuff the result in a strbuf,
> and I don't even know what's going on with wt_status.hints. :)
>
> So I think you may need to phase it in a bit more, like:
>
>   a. introduce want_advice() which decides whether or not to show the
>      advice based on a config key. I'd also suggest making the "advice."
>      part of the key implicit, just to make life easier for the callers.
>

yes, I agree.

>   b. introduce advise_key() which uses want_advice() and advise() under
>      the hood to do what your advise_ng() is doing here.
>
>   c. convert simple patterns of:
>
>        if (advice_foo)
>           advise("bar");
>
>      into:
>
>        advise_key("foo", "bar");
>
>      and drop advice_foo where possible.
>
>   d. handle more complex cases one-by-one. For example, with something
>      like:
>
>        if (advice_foo)
>          die("bar");
>
>      we probably want:
>
>        if (want_advice("foo"))
>          die("bar");
>
>      instead. Using string literals is more accident-prone than
>      variables (because the compiler doesn't notice if we misspell them)
>      but I think is OK for cases where we just refer to the key once.
>      For others (e.g., advice_commit_before_merge has 13 mentions),
>      either keep the variable. Or alternatively make a wrapper like:
>
>        int want_advice_commit_before_merge(void)
>        {
>                return want_advice("commitbeforemerge");
>        }
>
>      if we want to drop the existing mechanism to load all of the
>      variables at the beginning.
>

All make sense to me, thanks for the feedback.

> -Peff

Heba

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

* Re: [PATCH] advice: refactor advise API
  2020-02-10 22:55   ` Emily Shaffer
@ 2020-02-11  2:35     ` Heba Waly
  2020-02-11 19:49     ` Jeff King
  1 sibling, 0 replies; 50+ messages in thread
From: Heba Waly @ 2020-02-11  2:35 UTC (permalink / raw)
  To: Emily Shaffer; +Cc: Jeff King, Heba Waly via GitGitGadget, Git Mailing List

On Tue, Feb 11, 2020 at 11:55 AM Emily Shaffer <emilyshaffer@google.com> wrote:
>
> On Mon, Feb 10, 2020 at 03:37:25PM -0500, Jeff King wrote:
> >
> >      instead. Using string literals is more accident-prone than
> >      variables (because the compiler doesn't notice if we misspell them)
> >      but I think is OK for cases where we just refer to the key once.
> >      For others (e.g., advice_commit_before_merge has 13 mentions),
> >      either keep the variable. Or alternatively make a wrapper like:
> >
> >        int want_advice_commit_before_merge(void)
> >        {
> >                return want_advice("commitbeforemerge");
> >        }
> >
> >      if we want to drop the existing mechanism to load all of the
> >      variables at the beginning.
>
> I tend to disagree on both counts. I'd personally rather see something
> like 'void advise_key(enum advice, char *format, ...)'.
>
> As I understand it, Heba wanted to avoid that pattern so that people
> adding a new advice didn't need to modify the advice library. However, I
> think there's value to having a centralized list of all possible advices
> (besides the documentation). The per-advice wrapper is harder to iterate
> than an enum, and will also result in a lot of samey code if we decide
> we want to use that pattern for more advices.
>

I think Peff is suggesting the wrapper for only those rare cases where
a single advice config variable is being checked several times around
the code base, but this doesn't mean we'll have many of them. In my
own opinion, I don't see the need for the list of advices, I think
it'll add unneeded complexity to the advice library and the
introduction of new advice messages. Mainly because I don't see a
scenario where we'd need to iterate through them, so I don't know ...

> (In fact, with git-bugreport I'm running into a lot of regret that hooks
> are invoked in the way Peff describes - 'find_hook("pre-commit")' -
> rather than with an enum naming the hook; it's very hard to check all
> possible hooks, and hard to examine the codebase and determine which
> hooks do and don't exist.)
>
> When Heba began to describe this project I had hoped for a final product
> like 'void show_advice(enum advice_config)' which looked up the
> appropriate string from the advice library instead of asking the caller
> to provide it, although seeing the need for varargs has demonstrated to
> me that that's not feasible :) But I think taking the advice config key
> as an argument is possibly too far the other direction. At that point,
> it starts to beg the question, "why isn't this function in config.h and
> called print_if_configured(cfgname, message, ...)?"
>
> Although, take this all with a grain of salt. I think I lean towards
> this much encapsulation after a sordid history with C++ and an
> enlightened C developer may not choose it ;)
>
>  - Emily

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

* Re: [PATCH] advice: refactor advise API
  2020-02-10 23:56   ` Heba Waly
@ 2020-02-11  2:39     ` Derrick Stolee
  0 siblings, 0 replies; 50+ messages in thread
From: Derrick Stolee @ 2020-02-11  2:39 UTC (permalink / raw)
  To: Heba Waly; +Cc: Heba Waly via GitGitGadget, Git Mailing List

On 2/10/2020 6:56 PM, Heba Waly wrote:
> On Tue, Feb 11, 2020 at 3:38 AM Derrick Stolee <stolee@gmail.com> wrote:
>>
>> I definitely tend to recommend more tests than most, but perhaps this
>> unit test is overkill? You demonstrate a good test below using a real
>> Git command, which should be sufficient. If the "turn this message off"
>> part gets removed, then you will still have coverage of your method.
>> It just won't require a test change because it would not modify behavior.
>>
> 
> I see your point but I wanted to make sure advise_ng honors the config
> variable using tests 2 & 3 in `t0018-advice.sh`
> and `t7004-tag.sh` didn't seem like a good place to add these tests.

You're right. I wasn't considering the case of not showing the message
with respect to the config.

-Stolee

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

* Re: [PATCH] advice: refactor advise API
  2020-02-11  2:01   ` Heba Waly
@ 2020-02-11  6:08     ` Junio C Hamano
  0 siblings, 0 replies; 50+ messages in thread
From: Junio C Hamano @ 2020-02-11  6:08 UTC (permalink / raw)
  To: Heba Waly; +Cc: Heba Waly via GitGitGadget, Git Mailing List

Heba Waly <heba.waly@gmail.com> writes:

> On Tue, Feb 11, 2020 at 11:46 AM Junio C Hamano <gitster@pobox.com> wrote:
>>
>>
>> As I outlined in [1], I think the over-simplified
>> "advise_ng(<advise.key>, _(<message>), ...)"  would be too limited
>> to replace the current users, without a pair of helper functions,
>> one to just check for the guarding advise.key, and the other to
>> unconditionally show the message (i.e. the latter is what the
>> current advise() is).
>>
>
> I agree with adding the first helper, specially after Peff's comments,
> but I don't see why we would keep the current advise() which
> unconditionally shows the message...

Look again at the message you referenced in your message that
started this round, and read its comment:

	if (advise_ng_enabled("frotz")) {
		char *result = expensive_computation(...);

		/*
                 * advise_ng("frotz", _("message %s about frotz", result));
                 * is fine as well, but slightly less efficient as
                 * it would involve another call to *_enabled(), so use
		 * the unconditional form of the call
		 */
		advise_ng_raw(_("message %s about frotz", result));

		free(result);
	}


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

* Re: [PATCH] advice: refactor advise API
  2020-02-10 22:55   ` Emily Shaffer
  2020-02-11  2:35     ` Heba Waly
@ 2020-02-11 19:49     ` Jeff King
  2020-02-11 19:51       ` Jeff King
  1 sibling, 1 reply; 50+ messages in thread
From: Jeff King @ 2020-02-11 19:49 UTC (permalink / raw)
  To: Emily Shaffer; +Cc: Heba Waly via GitGitGadget, git, Heba Waly

On Mon, Feb 10, 2020 at 02:55:28PM -0800, Emily Shaffer wrote:

> I tend to disagree on both counts. I'd personally rather see something
> like 'void advise_key(enum advice, char *format, ...)'.

Yeah, I don't mind that at all. It's mutually exclusive with not making
people add new enums when they want to add new advice types, but as you
note we do get some code maintenance benefit by having the variables,
even if _adding_ a new one is a slightly larger pain.

-Peff

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

* Re: [PATCH] advice: refactor advise API
  2020-02-11 19:49     ` Jeff King
@ 2020-02-11 19:51       ` Jeff King
  0 siblings, 0 replies; 50+ messages in thread
From: Jeff King @ 2020-02-11 19:51 UTC (permalink / raw)
  To: Emily Shaffer; +Cc: Heba Waly via GitGitGadget, git, Heba Waly

On Tue, Feb 11, 2020 at 02:49:43PM -0500, Jeff King wrote:

> On Mon, Feb 10, 2020 at 02:55:28PM -0800, Emily Shaffer wrote:
> 
> > I tend to disagree on both counts. I'd personally rather see something
> > like 'void advise_key(enum advice, char *format, ...)'.
> 
> Yeah, I don't mind that at all. It's mutually exclusive with not making
> people add new enums when they want to add new advice types, but as you
> note we do get some code maintenance benefit by having the variables,
> even if _adding_ a new one is a slightly larger pain.

Thinking on it a bit more, one argument in favor of having enums or
variables or whatever is that we _do_ need a master list of all of the
variables in one spot: the documentation.

So one direction we _could_ go is either generating the enum from the
documentation or vice versa (or generating both from a master list).
That would give us one place to define a new key along with its
description.

-Peff

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

* Re: [PATCH] advice: refactor advise API
  2020-02-11  0:08       ` Heba Waly
@ 2020-02-12 20:57         ` Taylor Blau
  0 siblings, 0 replies; 50+ messages in thread
From: Taylor Blau @ 2020-02-12 20:57 UTC (permalink / raw)
  To: Heba Waly
  Cc: Taylor Blau, Junio C Hamano, Derrick Stolee,
	Heba Waly via GitGitGadget, Git Mailing List

On Tue, Feb 11, 2020 at 01:08:48PM +1300, Heba Waly wrote:
> On Tue, Feb 11, 2020 at 8:42 AM Taylor Blau <me@ttaylorr.com> wrote:
> >
> > On Mon, Feb 10, 2020 at 11:30:46AM -0800, Junio C Hamano wrote:
> > > Another thing.
> > >
> > > advise_ng() may have been a good name for illustration but is a
> > > horrible name for real-world use (imagine we need to revamp the API
> > > one more time in the future---what would it be called, which has to
> > > say that it is newer than the "next generation"?
> > > advise_3rd_try()?).
>
> As I mentioned earlier, this patch is meant to be used as a transition
> between the current advise() and the refactored one.

Ah, thanks for pointing it out, and I'm sorry that I missed reading it
in my first review. Your idea sounds quite good to me, and thanks for
working on this.

> So the name is just temporary and it'll be renamed to advise() once
> the transition is done.
> But if we want to keep both functions, or want a better name because
> it's an open-source project and the author might not complete the
> transition, then I'll try to think of another name.
>
> > What about calling this new API 'advise()'? The first patch could call
> > it 'advise_ng' or whatever other temporary name we feel comfortable
> > using, and then each subsequent patch would update callers of 'advise()'
> > to use 'advise_ng()'. Once those patches have been applied, and no other
> > callers of 'advise()' exist, a final patch can be applied on top to
> > rename 'advise_ng()' to 'advise()', and update the names of all of the
> > callers.
> >
>
> Yes, that's what I would like to do.
>
> > This makes for a rather noisy final patch, but the intermediate states
> > are much clearer, and it would make this series rather self-contained.
> >
> > On the other hand, having a version of 'advise_ng()' on master makes
> > this topic more incremental, meaning that we can pick it up and put it
> > down at ease and have more self-contained projects.
> >
> > I don't really have a preference between the two approaches, but if we
> > go with the latter, I do think we need something better than
> > 'advise_ng'. Maybe 'advise_warn'? I don't know.
> >
> > > Thanks.
> >
> > Thanks,
> > Taylor
>
> Thanks,
> Heba

Thanks,
Taylor

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

* [PATCH v2 0/2] [RFC][Outreachy] advice: refactor advise API
  2020-02-10  5:04 [PATCH] advice: refactor advise API Heba Waly via GitGitGadget
                   ` (2 preceding siblings ...)
  2020-02-10 22:46 ` Junio C Hamano
@ 2020-02-16 21:39 ` " Heba Waly via GitGitGadget
  2020-02-16 21:39   ` [PATCH v2 1/2] " Heba Waly via GitGitGadget
                     ` (3 more replies)
  3 siblings, 4 replies; 50+ messages in thread
From: Heba Waly via GitGitGadget @ 2020-02-16 21:39 UTC (permalink / raw)
  To: git; +Cc: Heba Waly

Main changes in V2: 1- Rename advise_ng to advise_if_enabled. 2- Add a new
advise_enabled() helper. 3- Add a list of config variables names to replace
advice_config[] (used by list_config_advices()). 4- Send an enum parameter
to the new advise helpers instead of strings. 5- Extract vadvise() from
advise() and advise_if enabled().


----------------------------------------------------------------------------

The advice API is currently a little bit confusing to call. quoting from
[1]:

When introducing a new advice message, you would

 * come up with advice.frotz configuration variable

 * define and declare advice_frotz global variable that defaults to
   true

 * sprinkle calls like this:

  if (advice_frotz)
    advise(_("helpful message about frotz"));

A new approach was suggested in [1] which this patch is based upon.

A new advise_if_enabled() is introduced to gradually replace advise()
advice_enabled() helper is also introduced to be used by those callers who:

 * Only need to check the visibility without calling advise() (they call
   die() or error() instead for example)
 * Need to carry out some heavy processing to display an advice, in this
   case they'll do: if(advice_enabled(advice_type))  advise("some advice message");
   
   

To introduce a new advice message, the caller needs to:

 * Add a new_advice_type to 'enum advice_type'
 * Come up with a new config variable name and add this name to 
   advice_config_keys[]
 * Call advise_if_enabled(new_advice_type, "advice message to be printed")
 * Or call advice_enabled(new_advice_type) first and then follow is by
   advice("advice message to be printed") as explained earlier.
 * Add the new config variable to Documentation/config/advice.txt

The reason a new list of configuration variables was added to the library is
to be used by the list_config_advices() function instead of advice_config[].
And we should get rid of advice_config[] once we migrate all the callers to
use the new APIs instead of checking the global variables (which we'll get
rid of as well).

In the future, we can investigate generating the documentation from the list
of config variables or vice versa to make introducing a new advice much
easier, but this approach will do it for now.

V2 makes the process of introducing a new advice longer than V1 and almost
as long as the original library, but having the advice library responsible
for checking the message visibility is still an improvement and in my own
opinion the new structure makes better sense and makes the library less
confusing to use.

After this patch the plan is to change the advise() calls to
advise_if_enabled() whenever possible, or at least replace the global
variables checks by advise_enabled() when advise_if_enabled() is not
suitable.

[1] https://public-inbox.org/git/xmqqzhf5cw69.fsf@gitster-ct.c.googlers.com/

Heba Waly (2):
  advice: refactor advise API
  advice: extract vadvise() from advise()

 Makefile               |  1 +
 advice.c               | 89 ++++++++++++++++++++++++++++++++++++++----
 advice.h               | 58 ++++++++++++++++++++++++++-
 builtin/tag.c          |  4 +-
 t/helper/test-advise.c | 16 ++++++++
 t/helper/test-tool.c   |  1 +
 t/helper/test-tool.h   |  1 +
 t/t0018-advice.sh      | 28 +++++++++++++
 t/t7004-tag.sh         |  1 +
 9 files changed, 188 insertions(+), 11 deletions(-)
 create mode 100644 t/helper/test-advise.c
 create mode 100755 t/t0018-advice.sh


base-commit: c7a62075917b3340f908093f63f1161c44ed1475
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-548%2FHebaWaly%2Fadvice_refactoring-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-548/HebaWaly/advice_refactoring-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/548

Range-diff vs v1:

 1:  0dcab7b4eb3 ! 1:  080d12b5c69 advice: refactor advise API
     @@ -2,15 +2,25 @@
      
          advice: refactor advise API
      
     -    Add a new advise_ng function that can check the visibility of advice
     -    messages before printing.
     +    Currently it's very easy for the advice library's callers to miss
     +    checking the visibility step before printing an advice. Also, it makes
     +    more sense for this step to be handled by the advice library.
      
     -    Currently it's very easy for the callers to miss checking the
     -    visibility step. Also, it makes more sense for this step to be handled
     -    by the advice library.
     +    Add a new advise_if_enabled function that checks the visibility of
     +    advice messages before printing.
      
     -    Also change the advise call in tag library from advise() to advise_ng()
     -    to construct an example of the usage of the new API.
     +    Add a new helper advise_enabled to check the visibility of the advice
     +    if the caller needs to carry out complicated processing based on that
     +    value.
     +
     +    A list of config variables 'advice_config_keys' is added to be used by
     +    list_config_advices() instead of 'advice_config[]' because we'll get
     +    rid of 'advice_config[]' and the global variables once we migrate all
     +    the callers to use the new APIs.
     +
     +    Also change the advise call in tag library from advise() to
     +    advise_if_enabled() to construct an example of the usage of the new
     +    API.
      
          Signed-off-by: Heba Waly <heba.waly@gmail.com>
      
     @@ -45,48 +55,112 @@
       	{ "submoduleAlternateErrorStrategyDie", &advice_submodule_alternate_error_strategy_die },
       
       	/* make this an alias for backward compatibility */
     + 	{ "pushNonFastForward", &advice_push_update_rejected }
     + };
     + 
     ++static const char *advice_config_keys[] = {
     ++	[FETCH_SHOW_FORCED_UPDATES]		 = "fetchShowForcedUpdates",
     ++	[PUSH_UPDATE_REJECTED]			 = "pushUpdateRejected",
     ++	/* make this an alias for backward compatibility */
     ++	[PUSH_UPDATE_REJECTED_ALIAS]		 = "pushNonFastForward",
     ++	
     ++	[PUSH_NON_FF_CURRENT]			 = "pushNonFFCurrent",
     ++	[PUSH_NON_FF_MATCHING]			 = "pushNonFFMatching",
     ++	[PUSH_ALREADY_EXISTS]			 = "pushAlreadyExists",
     ++	[PUSH_FETCH_FIRST]			 = "pushFetchFirst",
     ++	[PUSH_NEEDS_FORCE]			 = "pushNeedsForce",
     ++	[PUSH_UNQUALIFIED_REF_NAME]		 = "pushUnqualifiedRefName",
     ++	[STATUS_HINTS]				 = "statusHints",
     ++	[STATUS_U_OPTION]			 = "statusUoption",
     ++	[STATUS_AHEAD_BEHIND_WARNING]		 = "statusAheadBehindWarning",
     ++	[COMMIT_BEFORE_MERGE]			 = "commitBeforeMerge",
     ++	[RESET_QUIET_WARNING]			 = "resetQuiet",
     ++	[RESOLVE_CONFLICT]			 = "resolveConflict",
     ++	[SEQUENCER_IN_USE]			 = "sequencerInUse",
     ++	[IMPLICIT_IDENTITY]			 = "implicitIdentity",
     ++	[DETACHED_HEAD]				 = "detachedHead",
     ++	[SET_UPSTREAM_FAILURE]			 = "setupStreamFailure",
     ++	[OBJECT_NAME_WARNING]			 = "objectNameWarning",
     ++	[AMWORKDIR]				 = "amWorkDir",
     ++	[RM_HINTS]				 = "rmHints",
     ++	[ADD_EMBEDDED_REPO]			 = "addEmbeddedRepo",
     ++	[IGNORED_HOOK]				 = "ignoredHook",
     ++	[WAITING_FOR_EDITOR] 			 = "waitingForEditor",
     ++	[GRAFT_FILE_DEPRECATED]			 = "graftFileDeprecated",
     ++	[CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME]	 = "checkoutAmbiguousRemoteBranchName",
     ++	[NESTED_TAG]				 = "nestedTag",
     ++	[SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = "submoduleAlternateErrorStrategyDie"
     ++};
     ++
     + void advise(const char *advice, ...)
     + {
     + 	struct strbuf buf = STRBUF_INIT;
      @@
       	strbuf_release(&buf);
       }
       
     ++int advice_enabled(enum advice_type type)
     ++{
     ++	int value = 1;
     ++	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
     ++	git_config_get_bool(key, &value);
     ++	return value;
     ++}
     ++
     ++int advice_push_update_rejected_enabled(void)
     ++{
     ++	return advice_enabled(PUSH_UPDATE_REJECTED) ||
     ++	       advice_enabled(PUSH_UPDATE_REJECTED_ALIAS);
     ++	
     ++}
     ++
      +static const char turn_off_instructions[] =
      +N_("\n"
     -+"Turn this message off by running\n"
     -+"\"git config %s false\"");
     ++   "Disable this message with \"git config %s false\"");
      +
     -+void advise_ng(const char *key, const char *advice, ...)
     ++void advise_if_enabled(enum advice_type type, const char *advice, ...)
      +{
     -+	int value = 1;
      +	struct strbuf buf = STRBUF_INIT;
     ++	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
      +	va_list params;
      +	const char *cp, *np;
      +	
     -+	git_config_get_bool(key, &value);
     ++	if(!advice_enabled(type))
     ++		return;
     ++
     ++	va_start(params, advice);
     ++	strbuf_vaddf(&buf, advice, params);
     ++	va_end(params);
     ++
     ++	strbuf_addf(&buf, turn_off_instructions, key);
      +	
     -+	if(value)
     -+	{
     -+		va_start(params, advice);
     -+		strbuf_vaddf(&buf, advice, params);
     -+		va_end(params);
     -+
     -+		strbuf_addf(&buf, turn_off_instructions, key);
     -+		
     -+		for (cp = buf.buf; *cp; cp = np) {
     -+			np = strchrnul(cp, '\n');
     -+			fprintf(stderr,	_("%shint: %.*s%s\n"),
     -+				advise_get_color(ADVICE_COLOR_HINT),
     -+				(int)(np - cp), cp,
     -+				advise_get_color(ADVICE_COLOR_RESET));
     -+			if (*np)
     -+				np++;
     -+		}
     -+		strbuf_release(&buf);
     ++	for (cp = buf.buf; *cp; cp = np) {
     ++		np = strchrnul(cp, '\n');
     ++		fprintf(stderr,	_("%shint: %.*s%s\n"),
     ++			advise_get_color(ADVICE_COLOR_HINT),
     ++			(int)(np - cp), cp,
     ++			advise_get_color(ADVICE_COLOR_RESET));
     ++		if (*np)
     ++			np++;
      +	}
     ++	strbuf_release(&buf);
     ++
      +}
      +
       int git_default_advice_config(const char *var, const char *value)
       {
       	const char *k, *slot_name;
     +@@
     + {
     + 	int i;
     + 
     +-	for (i = 0; i < ARRAY_SIZE(advice_config); i++)
     +-		list_config_item(list, prefix, advice_config[i].name);
     ++	for (i = 0; i < ARRAY_SIZE(advice_config_keys); i++)
     ++		list_config_item(list, prefix, advice_config_keys[i]);
     + }
     + 
     + int error_resolve_conflict(const char *me)
      
       diff --git a/advice.h b/advice.h
       --- a/advice.h
     @@ -98,14 +172,66 @@
      -extern int advice_nested_tag;
       extern int advice_submodule_alternate_error_strategy_die;
       
     ++/**
     ++ To add a new advice, you need to:
     ++ - Define an advice_type.
     ++ - Add a new entry to advice_config_keys list.
     ++ - Add the new config variable to Documentation/config/advice.txt.
     ++ - Call advise_if_enabled to print your advice.
     ++ */
     ++enum advice_type {
     ++	FETCH_SHOW_FORCED_UPDATES = 0,
     ++	PUSH_UPDATE_REJECTED = 1,
     ++	PUSH_UPDATE_REJECTED_ALIAS = 2,
     ++	PUSH_NON_FF_CURRENT = 3,
     ++	PUSH_NON_FF_MATCHING = 4,
     ++	PUSH_ALREADY_EXISTS = 5,
     ++	PUSH_FETCH_FIRST = 6,
     ++	PUSH_NEEDS_FORCE = 7,
     ++	PUSH_UNQUALIFIED_REF_NAME = 8,
     ++	STATUS_HINTS = 9,
     ++	STATUS_U_OPTION = 10,
     ++	STATUS_AHEAD_BEHIND_WARNING = 11,
     ++	COMMIT_BEFORE_MERGE = 12,
     ++	RESET_QUIET_WARNING = 13,
     ++	RESOLVE_CONFLICT = 14,
     ++	SEQUENCER_IN_USE = 15,
     ++	IMPLICIT_IDENTITY = 16,
     ++	DETACHED_HEAD = 17,
     ++	SET_UPSTREAM_FAILURE = 18,
     ++	OBJECT_NAME_WARNING = 19,
     ++	AMWORKDIR = 20,
     ++	RM_HINTS = 21,
     ++	ADD_EMBEDDED_REPO = 22,
     ++	IGNORED_HOOK = 23,
     ++	WAITING_FOR_EDITOR = 24,
     ++	GRAFT_FILE_DEPRECATED = 25,
     ++	CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME = 26,
     ++	NESTED_TAG = 27,
     ++	SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE = 28,
     ++};
     ++
     ++
       int git_default_advice_config(const char *var, const char *value);
       __attribute__((format (printf, 1, 2)))
       void advise(const char *advice, ...);
      +
      +/**
     ++ Checks if advice type is enabled (can be printed to the user).
     ++ Should be called before advise().
     ++ */
     ++int advice_enabled(enum advice_type type);
     ++
     ++/**
     ++ Checks if PUSH_UPDATE_REJECTED advice type is enabled.
     ++ */
     ++int advice_push_update_rejected_enabled(void);
     ++
     ++/**
      + Checks the visibility of the advice before priniting.
      + */
     -+void advise_ng(const char *key, const char *advice, ...);
     ++void advise_if_enabled(enum advice_type type, const char *advice, ...);
     ++
       int error_resolve_conflict(const char *me);
       void NORETURN die_resolve_conflict(const char *me);
       void NORETURN die_conclude_merge(void);
     @@ -120,7 +246,7 @@
      -	if (type == OBJ_TAG && advice_nested_tag)
      -		advise(_(message_advice_nested_tag), tag, object_ref);
      +	if (type == OBJ_TAG)
     -+		advise_ng("advice.nestedTag", _(message_advice_nested_tag), tag, object_ref);
     ++		advise_if_enabled(NESTED_TAG, _(message_advice_nested_tag), tag, object_ref);
       
       	strbuf_addf(&header,
       		    "object %s\n"
     @@ -134,14 +260,15 @@
      +#include "cache.h"
      +#include "advice.h"
      +
     -+int cmd__advise_ng(int argc, const char **argv)
     ++int cmd__advise_if_enabled(int argc, const char **argv)
      +{
     -+	if (!argv[1] || !argv[2])
     -+	die("usage: %s <key> <advice>", argv[0]);
     ++	if (!argv[1])
     ++	die("usage: %s <advice>", argv[0]);
      +
      +	setup_git_directory();
     -+
     -+	advise_ng(argv[1], argv[2]);
     ++	
     ++	//use any advice type for testing
     ++	advise_if_enabled(NESTED_TAG, argv[1]);
      +
      +	return 0;
      +}
     @@ -153,7 +280,7 @@
       };
       
       static struct test_cmd cmds[] = {
     -+	{ "advise", cmd__advise_ng },
     ++	{ "advise", cmd__advise_if_enabled },
       	{ "chmtime", cmd__chmtime },
       	{ "config", cmd__config },
       	{ "ctype", cmd__ctype },
     @@ -165,7 +292,7 @@
       #define USE_THE_INDEX_COMPATIBILITY_MACROS
       #include "git-compat-util.h"
       
     -+int cmd__advise_ng(int argc, const char **argv);
     ++int cmd__advise_if_enabled(int argc, const char **argv);
       int cmd__chmtime(int argc, const char **argv);
       int cmd__config(int argc, const char **argv);
       int cmd__ctype(int argc, const char **argv);
     @@ -177,29 +304,28 @@
      @@
      +#!/bin/sh
      +
     -+test_description='Test advise_ng functionality'
     ++test_description='Test advise_if_enabled functionality'
      +
      +. ./test-lib.sh
      +
      +cat > expected <<EOF
      +hint: This is a piece of advice
     -+hint: Turn this message off by running
     -+hint: "git config advice.configVariable false"
     ++hint: Disable this message with "git config advice.nestedTag false"
      +EOF
      +test_expect_success 'advise should be printed when config variable is unset' '
     -+	test-tool advise "advice.configVariable" "This is a piece of advice" 2>actual &&
     ++	test-tool advise "This is a piece of advice" 2>actual &&
      +	test_i18ncmp expected actual
      +'
      +
      +test_expect_success 'advise should be printed when config variable is set to true' '
     -+	test_config advice.configVariable true &&
     -+	test-tool advise "advice.configVariable" "This is a piece of advice" 2>actual &&
     ++	test_config advice.nestedTag true &&
     ++	test-tool advise "This is a piece of advice" 2>actual &&
      +	test_i18ncmp expected actual
      +'
      +
      +test_expect_success 'advise should not be printed when config variable is set to false' '
     -+	test_config advice.configVariable false &&
     -+	test-tool advise "advice.configVariable" "This is a piece of advice" 2>actual &&
     ++	test_config advice.nestedTag false &&
     ++	test-tool advise "This is a piece of advice" 2>actual &&
      +	test_must_be_empty actual
      +'
      +
     @@ -212,8 +338,7 @@
       	hint: already a tag. If you meant to tag the object that it points to, use:
       	hint: |
       	hint: 	git tag -f nested annotated-v4.0^{}
     -+	hint: Turn this message off by running
     -+	hint: "git config advice.nestedTag false"
     ++	hint: Disable this message with "git config advice.nestedTag false"
       	EOF
       	git tag -m nested nested annotated-v4.0 2>actual &&
       	test_i18ncmp expect actual
 -:  ----------- > 2:  3e4f52e5526 advice: extract vadvise() from advise()

-- 
gitgitgadget

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

* [PATCH v2 1/2] advice: refactor advise API
  2020-02-16 21:39 ` [PATCH v2 0/2] [RFC][Outreachy] " Heba Waly via GitGitGadget
@ 2020-02-16 21:39   ` " Heba Waly via GitGitGadget
  2020-02-17  3:28     ` Junio C Hamano
  2020-02-19  9:59     ` Heba Waly
  2020-02-16 21:39   ` [PATCH v2 2/2] advice: extract vadvise() from advise() Heba Waly via GitGitGadget
                     ` (2 subsequent siblings)
  3 siblings, 2 replies; 50+ messages in thread
From: Heba Waly via GitGitGadget @ 2020-02-16 21:39 UTC (permalink / raw)
  To: git; +Cc: Heba Waly, Heba Waly

From: Heba Waly <heba.waly@gmail.com>

Currently it's very easy for the advice library's callers to miss
checking the visibility step before printing an advice. Also, it makes
more sense for this step to be handled by the advice library.

Add a new advise_if_enabled function that checks the visibility of
advice messages before printing.

Add a new helper advise_enabled to check the visibility of the advice
if the caller needs to carry out complicated processing based on that
value.

A list of config variables 'advice_config_keys' is added to be used by
list_config_advices() instead of 'advice_config[]' because we'll get
rid of 'advice_config[]' and the global variables once we migrate all
the callers to use the new APIs.

Also change the advise call in tag library from advise() to
advise_if_enabled() to construct an example of the usage of the new
API.

Signed-off-by: Heba Waly <heba.waly@gmail.com>
---
 Makefile               |  1 +
 advice.c               | 88 ++++++++++++++++++++++++++++++++++++++++--
 advice.h               | 58 +++++++++++++++++++++++++++-
 builtin/tag.c          |  4 +-
 t/helper/test-advise.c | 16 ++++++++
 t/helper/test-tool.c   |  1 +
 t/helper/test-tool.h   |  1 +
 t/t0018-advice.sh      | 28 ++++++++++++++
 t/t7004-tag.sh         |  1 +
 9 files changed, 191 insertions(+), 7 deletions(-)
 create mode 100644 t/helper/test-advise.c
 create mode 100755 t/t0018-advice.sh

diff --git a/Makefile b/Makefile
index 09f98b777ca..ed923a3e818 100644
--- a/Makefile
+++ b/Makefile
@@ -695,6 +695,7 @@ X =
 
 PROGRAMS += $(patsubst %.o,git-%$X,$(PROGRAM_OBJS))
 
+TEST_BUILTINS_OBJS += test-advise.o
 TEST_BUILTINS_OBJS += test-chmtime.o
 TEST_BUILTINS_OBJS += test-config.o
 TEST_BUILTINS_OBJS += test-ctype.o
diff --git a/advice.c b/advice.c
index 249c60dcf32..8cedc649afa 100644
--- a/advice.c
+++ b/advice.c
@@ -29,7 +29,6 @@ int advice_ignored_hook = 1;
 int advice_waiting_for_editor = 1;
 int advice_graft_file_deprecated = 1;
 int advice_checkout_ambiguous_remote_branch_name = 1;
-int advice_nested_tag = 1;
 int advice_submodule_alternate_error_strategy_die = 1;
 
 static int advice_use_color = -1;
@@ -89,13 +88,46 @@ static struct {
 	{ "waitingForEditor", &advice_waiting_for_editor },
 	{ "graftFileDeprecated", &advice_graft_file_deprecated },
 	{ "checkoutAmbiguousRemoteBranchName", &advice_checkout_ambiguous_remote_branch_name },
-	{ "nestedTag", &advice_nested_tag },
 	{ "submoduleAlternateErrorStrategyDie", &advice_submodule_alternate_error_strategy_die },
 
 	/* make this an alias for backward compatibility */
 	{ "pushNonFastForward", &advice_push_update_rejected }
 };
 
+static const char *advice_config_keys[] = {
+	[FETCH_SHOW_FORCED_UPDATES]		 = "fetchShowForcedUpdates",
+	[PUSH_UPDATE_REJECTED]			 = "pushUpdateRejected",
+	/* make this an alias for backward compatibility */
+	[PUSH_UPDATE_REJECTED_ALIAS]		 = "pushNonFastForward",
+	
+	[PUSH_NON_FF_CURRENT]			 = "pushNonFFCurrent",
+	[PUSH_NON_FF_MATCHING]			 = "pushNonFFMatching",
+	[PUSH_ALREADY_EXISTS]			 = "pushAlreadyExists",
+	[PUSH_FETCH_FIRST]			 = "pushFetchFirst",
+	[PUSH_NEEDS_FORCE]			 = "pushNeedsForce",
+	[PUSH_UNQUALIFIED_REF_NAME]		 = "pushUnqualifiedRefName",
+	[STATUS_HINTS]				 = "statusHints",
+	[STATUS_U_OPTION]			 = "statusUoption",
+	[STATUS_AHEAD_BEHIND_WARNING]		 = "statusAheadBehindWarning",
+	[COMMIT_BEFORE_MERGE]			 = "commitBeforeMerge",
+	[RESET_QUIET_WARNING]			 = "resetQuiet",
+	[RESOLVE_CONFLICT]			 = "resolveConflict",
+	[SEQUENCER_IN_USE]			 = "sequencerInUse",
+	[IMPLICIT_IDENTITY]			 = "implicitIdentity",
+	[DETACHED_HEAD]				 = "detachedHead",
+	[SET_UPSTREAM_FAILURE]			 = "setupStreamFailure",
+	[OBJECT_NAME_WARNING]			 = "objectNameWarning",
+	[AMWORKDIR]				 = "amWorkDir",
+	[RM_HINTS]				 = "rmHints",
+	[ADD_EMBEDDED_REPO]			 = "addEmbeddedRepo",
+	[IGNORED_HOOK]				 = "ignoredHook",
+	[WAITING_FOR_EDITOR] 			 = "waitingForEditor",
+	[GRAFT_FILE_DEPRECATED]			 = "graftFileDeprecated",
+	[CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME]	 = "checkoutAmbiguousRemoteBranchName",
+	[NESTED_TAG]				 = "nestedTag",
+	[SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = "submoduleAlternateErrorStrategyDie"
+};
+
 void advise(const char *advice, ...)
 {
 	struct strbuf buf = STRBUF_INIT;
@@ -118,6 +150,54 @@ void advise(const char *advice, ...)
 	strbuf_release(&buf);
 }
 
+int advice_enabled(enum advice_type type)
+{
+	int value = 1;
+	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
+	git_config_get_bool(key, &value);
+	return value;
+}
+
+int advice_push_update_rejected_enabled(void)
+{
+	return advice_enabled(PUSH_UPDATE_REJECTED) ||
+	       advice_enabled(PUSH_UPDATE_REJECTED_ALIAS);
+	
+}
+
+static const char turn_off_instructions[] =
+N_("\n"
+   "Disable this message with \"git config %s false\"");
+
+void advise_if_enabled(enum advice_type type, const char *advice, ...)
+{
+	struct strbuf buf = STRBUF_INIT;
+	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
+	va_list params;
+	const char *cp, *np;
+	
+	if(!advice_enabled(type))
+		return;
+
+	va_start(params, advice);
+	strbuf_vaddf(&buf, advice, params);
+	va_end(params);
+
+	strbuf_addf(&buf, turn_off_instructions, key);
+	
+	for (cp = buf.buf; *cp; cp = np) {
+		np = strchrnul(cp, '\n');
+		fprintf(stderr,	_("%shint: %.*s%s\n"),
+			advise_get_color(ADVICE_COLOR_HINT),
+			(int)(np - cp), cp,
+			advise_get_color(ADVICE_COLOR_RESET));
+		if (*np)
+			np++;
+	}
+	strbuf_release(&buf);
+
+}
+
 int git_default_advice_config(const char *var, const char *value)
 {
 	const char *k, *slot_name;
@@ -154,8 +234,8 @@ void list_config_advices(struct string_list *list, const char *prefix)
 {
 	int i;
 
-	for (i = 0; i < ARRAY_SIZE(advice_config); i++)
-		list_config_item(list, prefix, advice_config[i].name);
+	for (i = 0; i < ARRAY_SIZE(advice_config_keys); i++)
+		list_config_item(list, prefix, advice_config_keys[i]);
 }
 
 int error_resolve_conflict(const char *me)
diff --git a/advice.h b/advice.h
index b706780614d..d2ac3084067 100644
--- a/advice.h
+++ b/advice.h
@@ -29,12 +29,68 @@ extern int advice_ignored_hook;
 extern int advice_waiting_for_editor;
 extern int advice_graft_file_deprecated;
 extern int advice_checkout_ambiguous_remote_branch_name;
-extern int advice_nested_tag;
 extern int advice_submodule_alternate_error_strategy_die;
 
+/**
+ To add a new advice, you need to:
+ - Define an advice_type.
+ - Add a new entry to advice_config_keys list.
+ - Add the new config variable to Documentation/config/advice.txt.
+ - Call advise_if_enabled to print your advice.
+ */
+enum advice_type {
+	FETCH_SHOW_FORCED_UPDATES = 0,
+	PUSH_UPDATE_REJECTED = 1,
+	PUSH_UPDATE_REJECTED_ALIAS = 2,
+	PUSH_NON_FF_CURRENT = 3,
+	PUSH_NON_FF_MATCHING = 4,
+	PUSH_ALREADY_EXISTS = 5,
+	PUSH_FETCH_FIRST = 6,
+	PUSH_NEEDS_FORCE = 7,
+	PUSH_UNQUALIFIED_REF_NAME = 8,
+	STATUS_HINTS = 9,
+	STATUS_U_OPTION = 10,
+	STATUS_AHEAD_BEHIND_WARNING = 11,
+	COMMIT_BEFORE_MERGE = 12,
+	RESET_QUIET_WARNING = 13,
+	RESOLVE_CONFLICT = 14,
+	SEQUENCER_IN_USE = 15,
+	IMPLICIT_IDENTITY = 16,
+	DETACHED_HEAD = 17,
+	SET_UPSTREAM_FAILURE = 18,
+	OBJECT_NAME_WARNING = 19,
+	AMWORKDIR = 20,
+	RM_HINTS = 21,
+	ADD_EMBEDDED_REPO = 22,
+	IGNORED_HOOK = 23,
+	WAITING_FOR_EDITOR = 24,
+	GRAFT_FILE_DEPRECATED = 25,
+	CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME = 26,
+	NESTED_TAG = 27,
+	SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE = 28,
+};
+
+
 int git_default_advice_config(const char *var, const char *value);
 __attribute__((format (printf, 1, 2)))
 void advise(const char *advice, ...);
+
+/**
+ Checks if advice type is enabled (can be printed to the user).
+ Should be called before advise().
+ */
+int advice_enabled(enum advice_type type);
+
+/**
+ Checks if PUSH_UPDATE_REJECTED advice type is enabled.
+ */
+int advice_push_update_rejected_enabled(void);
+
+/**
+ Checks the visibility of the advice before priniting.
+ */
+void advise_if_enabled(enum advice_type type, const char *advice, ...);
+
 int error_resolve_conflict(const char *me);
 void NORETURN die_resolve_conflict(const char *me);
 void NORETURN die_conclude_merge(void);
diff --git a/builtin/tag.c b/builtin/tag.c
index e0a4c253828..247d9075e19 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -231,8 +231,8 @@ static void create_tag(const struct object_id *object, const char *object_ref,
 	if (type <= OBJ_NONE)
 		die(_("bad object type."));
 
-	if (type == OBJ_TAG && advice_nested_tag)
-		advise(_(message_advice_nested_tag), tag, object_ref);
+	if (type == OBJ_TAG)
+		advise_if_enabled(NESTED_TAG, _(message_advice_nested_tag), tag, object_ref);
 
 	strbuf_addf(&header,
 		    "object %s\n"
diff --git a/t/helper/test-advise.c b/t/helper/test-advise.c
new file mode 100644
index 00000000000..c49be89f970
--- /dev/null
+++ b/t/helper/test-advise.c
@@ -0,0 +1,16 @@
+#include "test-tool.h"
+#include "cache.h"
+#include "advice.h"
+
+int cmd__advise_if_enabled(int argc, const char **argv)
+{
+	if (!argv[1])
+	die("usage: %s <advice>", argv[0]);
+
+	setup_git_directory();
+	
+	//use any advice type for testing
+	advise_if_enabled(NESTED_TAG, argv[1]);
+
+	return 0;
+}
diff --git a/t/helper/test-tool.c b/t/helper/test-tool.c
index f20989d4497..6977badc690 100644
--- a/t/helper/test-tool.c
+++ b/t/helper/test-tool.c
@@ -14,6 +14,7 @@ struct test_cmd {
 };
 
 static struct test_cmd cmds[] = {
+	{ "advise", cmd__advise_if_enabled },
 	{ "chmtime", cmd__chmtime },
 	{ "config", cmd__config },
 	{ "ctype", cmd__ctype },
diff --git a/t/helper/test-tool.h b/t/helper/test-tool.h
index 8ed2af71d1b..ca5e33b842f 100644
--- a/t/helper/test-tool.h
+++ b/t/helper/test-tool.h
@@ -4,6 +4,7 @@
 #define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "git-compat-util.h"
 
+int cmd__advise_if_enabled(int argc, const char **argv);
 int cmd__chmtime(int argc, const char **argv);
 int cmd__config(int argc, const char **argv);
 int cmd__ctype(int argc, const char **argv);
diff --git a/t/t0018-advice.sh b/t/t0018-advice.sh
new file mode 100755
index 00000000000..f4cdb649d51
--- /dev/null
+++ b/t/t0018-advice.sh
@@ -0,0 +1,28 @@
+#!/bin/sh
+
+test_description='Test advise_if_enabled functionality'
+
+. ./test-lib.sh
+
+cat > expected <<EOF
+hint: This is a piece of advice
+hint: Disable this message with "git config advice.nestedTag false"
+EOF
+test_expect_success 'advise should be printed when config variable is unset' '
+	test-tool advise "This is a piece of advice" 2>actual &&
+	test_i18ncmp expected actual
+'
+
+test_expect_success 'advise should be printed when config variable is set to true' '
+	test_config advice.nestedTag true &&
+	test-tool advise "This is a piece of advice" 2>actual &&
+	test_i18ncmp expected actual
+'
+
+test_expect_success 'advise should not be printed when config variable is set to false' '
+	test_config advice.nestedTag false &&
+	test-tool advise "This is a piece of advice" 2>actual &&
+	test_must_be_empty actual
+'
+
+test_done
diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
index 6db92bd3ba6..74b637deb25 100755
--- a/t/t7004-tag.sh
+++ b/t/t7004-tag.sh
@@ -1726,6 +1726,7 @@ test_expect_success 'recursive tagging should give advice' '
 	hint: already a tag. If you meant to tag the object that it points to, use:
 	hint: |
 	hint: 	git tag -f nested annotated-v4.0^{}
+	hint: Disable this message with "git config advice.nestedTag false"
 	EOF
 	git tag -m nested nested annotated-v4.0 2>actual &&
 	test_i18ncmp expect actual
-- 
gitgitgadget


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

* [PATCH v2 2/2] advice: extract vadvise() from advise()
  2020-02-16 21:39 ` [PATCH v2 0/2] [RFC][Outreachy] " Heba Waly via GitGitGadget
  2020-02-16 21:39   ` [PATCH v2 1/2] " Heba Waly via GitGitGadget
@ 2020-02-16 21:39   ` Heba Waly via GitGitGadget
  2020-02-17  0:09   ` [PATCH v2 0/2] [RFC][Outreachy] advice: refactor advise API Junio C Hamano
  2020-02-19 20:33   ` [PATCH v3 0/2] [Outreachy] advice: revamp " Heba Waly via GitGitGadget
  3 siblings, 0 replies; 50+ messages in thread
From: Heba Waly via GitGitGadget @ 2020-02-16 21:39 UTC (permalink / raw)
  To: git; +Cc: Heba Waly, Heba Waly

From: Heba Waly <heba.waly@gmail.com>

extract a version of advise() that uses an explict 'va_list' parameter.
Call it from advise() and advise_if_enabled() for a functionally
equivalent version.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Heba Waly <heba.waly@gmail.com>
---
 advice.c | 45 +++++++++++++++++++--------------------------
 1 file changed, 19 insertions(+), 26 deletions(-)

diff --git a/advice.c b/advice.c
index 8cedc649afa..6c0be19a7c5 100644
--- a/advice.c
+++ b/advice.c
@@ -128,15 +128,20 @@ static const char *advice_config_keys[] = {
 	[SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = "submoduleAlternateErrorStrategyDie"
 };
 
-void advise(const char *advice, ...)
+static const char turn_off_instructions[] =
+N_("\n"
+   "Disable this message with \"git config %s false\"");
+
+static void vadvise(const char *advice, va_list params,
+		    int display_instructions, char *key)
 {
 	struct strbuf buf = STRBUF_INIT;
-	va_list params;
 	const char *cp, *np;
 
-	va_start(params, advice);
 	strbuf_vaddf(&buf, advice, params);
-	va_end(params);
+
+	if(display_instructions)
+		strbuf_addf(&buf, turn_off_instructions, key);
 
 	for (cp = buf.buf; *cp; cp = np) {
 		np = strchrnul(cp, '\n');
@@ -165,37 +170,25 @@ int advice_push_update_rejected_enabled(void)
 	
 }
 
-static const char turn_off_instructions[] =
-N_("\n"
-   "Disable this message with \"git config %s false\"");
+void advise(const char *advice, ...)
+{
+	va_list params;
+	va_start(params, advice);
+	vadvise(advice, params, 0, "");
+	va_end(params);
+}
 
 void advise_if_enabled(enum advice_type type, const char *advice, ...)
 {
-	struct strbuf buf = STRBUF_INIT;
-	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
 	va_list params;
-	const char *cp, *np;
-	
+	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
+
 	if(!advice_enabled(type))
 		return;
 
 	va_start(params, advice);
-	strbuf_vaddf(&buf, advice, params);
+	vadvise(advice, params, 1, key);
 	va_end(params);
-
-	strbuf_addf(&buf, turn_off_instructions, key);
-	
-	for (cp = buf.buf; *cp; cp = np) {
-		np = strchrnul(cp, '\n');
-		fprintf(stderr,	_("%shint: %.*s%s\n"),
-			advise_get_color(ADVICE_COLOR_HINT),
-			(int)(np - cp), cp,
-			advise_get_color(ADVICE_COLOR_RESET));
-		if (*np)
-			np++;
-	}
-	strbuf_release(&buf);
-
 }
 
 int git_default_advice_config(const char *var, const char *value)
-- 
gitgitgadget

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

* Re: [PATCH v2 0/2] [RFC][Outreachy] advice: refactor advise API
  2020-02-16 21:39 ` [PATCH v2 0/2] [RFC][Outreachy] " Heba Waly via GitGitGadget
  2020-02-16 21:39   ` [PATCH v2 1/2] " Heba Waly via GitGitGadget
  2020-02-16 21:39   ` [PATCH v2 2/2] advice: extract vadvise() from advise() Heba Waly via GitGitGadget
@ 2020-02-17  0:09   ` Junio C Hamano
  2020-02-19 20:33   ` [PATCH v3 0/2] [Outreachy] advice: revamp " Heba Waly via GitGitGadget
  3 siblings, 0 replies; 50+ messages in thread
From: Junio C Hamano @ 2020-02-17  0:09 UTC (permalink / raw)
  To: Heba Waly via GitGitGadget; +Cc: git, Heba Waly

"Heba Waly via GitGitGadget" <gitgitgadget@gmail.com> writes:

> Main changes in V2: 1- Rename advise_ng to advise_if_enabled. 2- Add a new
> advise_enabled() helper. 3- Add a list of config variables names to replace
> advice_config[] (used by list_config_advices()). 4- Send an enum parameter
> to the new advise helpers instead of strings. 5- Extract vadvise() from
> advise() and advise_if enabled().


Nice.  There is not much point for me to correct mistakes in the
title of the cover letter, but anyway... I think these changes are
not "refactoring" the API, but these are enhancing, extending or
even revamping the API.  

> To introduce a new advice message, the caller needs to:
>
>  * Add a new_advice_type to 'enum advice_type'
>  * Come up with a new config variable name and add this name to 
>    advice_config_keys[]
>  * Call advise_if_enabled(new_advice_type, "advice message to be printed")
>  * Or call advice_enabled(new_advice_type) first and then follow is by
>    advice("advice message to be printed") as explained earlier.
>  * Add the new config variable to Documentation/config/advice.txt

And I see that now you are going all the way to discard the
string-based keys to enumeration, I think this deserves to be called
"revamp advise API".

> In the future, we can investigate generating the documentation from the list
> of config variables or vice versa to make introducing a new advice much
> easier, but this approach will do it for now.

Yup.  One step at a time.

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

* Re: [PATCH v2 1/2] advice: refactor advise API
  2020-02-16 21:39   ` [PATCH v2 1/2] " Heba Waly via GitGitGadget
@ 2020-02-17  3:28     ` Junio C Hamano
  2020-02-17 10:03       ` Heba Waly
  2020-02-19  9:59     ` Heba Waly
  1 sibling, 1 reply; 50+ messages in thread
From: Junio C Hamano @ 2020-02-17  3:28 UTC (permalink / raw)
  To: Heba Waly via GitGitGadget; +Cc: git, Heba Waly

There seem to be a handful of lines with trailing whitespaces.  I
think I've fixed them all on the receiving end, but please proofread
before you come up with the next round.

Thanks.

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

* Re: [PATCH v2 1/2] advice: refactor advise API
  2020-02-17  3:28     ` Junio C Hamano
@ 2020-02-17 10:03       ` Heba Waly
  0 siblings, 0 replies; 50+ messages in thread
From: Heba Waly @ 2020-02-17 10:03 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Heba Waly via GitGitGadget, Git Mailing List

On Mon, Feb 17, 2020 at 4:28 PM Junio C Hamano <gitster@pobox.com> wrote:
>
>And I see that now you are going all the way to discard the
>string-based keys to enumeration, I think this deserves to be called
>"revamp advise API".

Fair enough.

> There seem to be a handful of lines with trailing whitespaces.  I
> think I've fixed them all on the receiving end, but please proofread
> before you come up with the next round.
>

Sorry about that, I fixed this setting in my IDE a while ago but looks
like it has a bug.
Will keep an eye on it in the future, thanks

> Thanks.

Heba

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

* Re: [PATCH v2 1/2] advice: refactor advise API
  2020-02-16 21:39   ` [PATCH v2 1/2] " Heba Waly via GitGitGadget
  2020-02-17  3:28     ` Junio C Hamano
@ 2020-02-19  9:59     ` Heba Waly
  1 sibling, 0 replies; 50+ messages in thread
From: Heba Waly @ 2020-02-19  9:59 UTC (permalink / raw)
  To: Heba Waly via GitGitGadget; +Cc: Git Mailing List

On Mon, Feb 17, 2020 at 10:39 AM Heba Waly via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> +int advice_enabled(enum advice_type type)
> +{
> +       int value = 1;
> +       char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
> +       git_config_get_bool(key, &value);
> +       return value;
> +}
> +
> +int advice_push_update_rejected_enabled(void)
> +{
> +       return advice_enabled(PUSH_UPDATE_REJECTED) ||
> +              advice_enabled(PUSH_UPDATE_REJECTED_ALIAS);
> +
> +}

I just realized that the return value in this function should be &&
instead of ||
(if any of the config variables is 0, then the advice should be
disabled), will fix that in the next round.

I'll probably remove this function and add a switch case to
advice_enabled() to handle this case as well as another one that I
came across while migrating the rest of the calls to the new helpers.
Will post an updated version soon.

> --
> gitgitgadget
>

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

* [PATCH v3 0/2] [Outreachy] advice: revamp advise API
  2020-02-16 21:39 ` [PATCH v2 0/2] [RFC][Outreachy] " Heba Waly via GitGitGadget
                     ` (2 preceding siblings ...)
  2020-02-17  0:09   ` [PATCH v2 0/2] [RFC][Outreachy] advice: refactor advise API Junio C Hamano
@ 2020-02-19 20:33   ` " Heba Waly via GitGitGadget
  2020-02-19 20:34     ` [PATCH v3 1/2] " Heba Waly via GitGitGadget
                       ` (2 more replies)
  3 siblings, 3 replies; 50+ messages in thread
From: Heba Waly via GitGitGadget @ 2020-02-19 20:33 UTC (permalink / raw)
  To: git; +Cc: Heba Waly

Changes in V3:

 * Remove the new wrapper advice_push_update_rejected_enabled() (which was
   added in V2 to handle a special case of having a config variable alias),
   and replace it by adding switch cases to advice_enabled() (The reason
   behind this change is that another special case came up while I was
   migrating the rest of the advise calls to the new APIs.)
 * Remove trailing whitespaces.


----------------------------------------------------------------------------

Main changes in V2:

 * Rename advise_ng to advise_if_enabled.
 * Add a new advise_enabled() helper.
 * Add a list of config variables names to replace advice_config[] (used by
   list_config_advices()).
 * Send an enum parameter to the new advise helpers instead of strings.
 * Extract vadvise() from advise() and advise_if enabled().


----------------------------------------------------------------------------

The advice API is currently a little bit confusing to call. quoting from
[1]:

When introducing a new advice message, you would

 * come up with advice.frotz configuration variable

 * define and declare advice_frotz global variable that defaults to
   true

 * sprinkle calls like this:

  if (advice_frotz)
    advise(_("helpful message about frotz"));

A new approach was suggested in [1] which this patch is based upon.

A new advise_if_enabled() is introduced to gradually replace advise()
advice_enabled() helper is also introduced to be used by those callers who:

 * Only need to check the visibility without calling advise() (they call
   die() or error() instead for example)
 * Need to carry out some heavy processing to display an advice, in this
   case they'll do: if(advice_enabled(advice_type))  advise("some advice message");
   
   

To introduce a new advice message, the caller needs to:

 * Add a new_advice_type to 'enum advice_type'
 * Come up with a new config variable name and add this name to 
   advice_config_keys[]
 * Call advise_if_enabled(new_advice_type, "advice message to be printed")
 * Or call advice_enabled(new_advice_type) first and then follow is by
   advice("advice message to be printed") as explained earlier.
 * Add the new config variable to Documentation/config/advice.txt

The reason a new list of configuration variables was added to the library is
to be used by the list_config_advices() function instead of advice_config[].
And we should get rid of advice_config[] once we migrate all the callers to
use the new APIs instead of checking the global variables (which we'll get
rid of as well).

In the future, we can investigate generating the documentation from the list
of config variables or vice versa to make introducing a new advice much
easier, but this approach will do it for now.

V2 makes the process of introducing a new advice longer than V1 and almost
as long as the original library, but having the advice library responsible
for checking the message visibility is still an improvement and in my own
opinion the new structure makes better sense and makes the library less
confusing to use.

After this patch the plan is to change the advise() calls to
advise_if_enabled() whenever possible, or at least replace the global
variables checks by advise_enabled() when advise_if_enabled() is not
suitable.

[1] https://public-inbox.org/git/xmqqzhf5cw69.fsf@gitster-ct.c.googlers.com/

Heba Waly (2):
  advice: revamp advise API
  advice: extract vadvise() from advise()

 Makefile               |  1 +
 advice.c               | 93 ++++++++++++++++++++++++++++++++++++++----
 advice.h               | 53 +++++++++++++++++++++++-
 builtin/tag.c          |  4 +-
 t/helper/test-advise.c | 16 ++++++++
 t/helper/test-tool.c   |  1 +
 t/helper/test-tool.h   |  1 +
 t/t0018-advice.sh      | 28 +++++++++++++
 t/t7004-tag.sh         |  1 +
 9 files changed, 187 insertions(+), 11 deletions(-)
 create mode 100644 t/helper/test-advise.c
 create mode 100755 t/t0018-advice.sh


base-commit: c7a62075917b3340f908093f63f1161c44ed1475
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-548%2FHebaWaly%2Fadvice_refactoring-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-548/HebaWaly/advice_refactoring-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/548

Range-diff vs v2:

 1:  080d12b5c69 ! 1:  4ab141426f3 advice: refactor advise API
     @@ -1,6 +1,6 @@
      Author: Heba Waly <heba.waly@gmail.com>
      
     -    advice: refactor advise API
     +    advice: revamp advise API
      
          Currently it's very easy for the advice library's callers to miss
          checking the visibility step before printing an advice. Also, it makes
     @@ -63,7 +63,7 @@
      +	[PUSH_UPDATE_REJECTED]			 = "pushUpdateRejected",
      +	/* make this an alias for backward compatibility */
      +	[PUSH_UPDATE_REJECTED_ALIAS]		 = "pushNonFastForward",
     -+	
     ++
      +	[PUSH_NON_FF_CURRENT]			 = "pushNonFFCurrent",
      +	[PUSH_NON_FF_MATCHING]			 = "pushNonFFMatching",
      +	[PUSH_ALREADY_EXISTS]			 = "pushAlreadyExists",
     @@ -99,7 +99,7 @@
       	strbuf_release(&buf);
       }
       
     -+int advice_enabled(enum advice_type type)
     ++static int get_config_value(enum advice_type type)
      +{
      +	int value = 1;
      +	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
     @@ -107,11 +107,15 @@
      +	return value;
      +}
      +
     -+int advice_push_update_rejected_enabled(void)
     ++int advice_enabled(enum advice_type type)
      +{
     -+	return advice_enabled(PUSH_UPDATE_REJECTED) ||
     -+	       advice_enabled(PUSH_UPDATE_REJECTED_ALIAS);
     -+	
     ++	switch(type) {
     ++	case PUSH_UPDATE_REJECTED:
     ++		return get_config_value(PUSH_UPDATE_REJECTED) &&
     ++		       get_config_value(PUSH_UPDATE_REJECTED_ALIAS);
     ++	default:
     ++		return get_config_value(type);
     ++	}
      +}
      +
      +static const char turn_off_instructions[] =
     @@ -223,12 +227,7 @@
      +int advice_enabled(enum advice_type type);
      +
      +/**
     -+ Checks if PUSH_UPDATE_REJECTED advice type is enabled.
     -+ */
     -+int advice_push_update_rejected_enabled(void);
     -+
     -+/**
     -+ Checks the visibility of the advice before priniting.
     ++ Checks the visibility of the advice before printing.
      + */
      +void advise_if_enabled(enum advice_type type, const char *advice, ...);
      +
     @@ -266,7 +265,7 @@
      +	die("usage: %s <advice>", argv[0]);
      +
      +	setup_git_directory();
     -+	
     ++
      +	//use any advice type for testing
      +	advise_if_enabled(NESTED_TAG, argv[1]);
      +
 2:  3e4f52e5526 ! 2:  a2a145c705e advice: extract vadvise() from advise()
     @@ -38,7 +38,7 @@
       	for (cp = buf.buf; *cp; cp = np) {
       		np = strchrnul(cp, '\n');
      @@
     - 	
     + 	}
       }
       
      -static const char turn_off_instructions[] =

-- 
gitgitgadget

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

* [PATCH v3 1/2] advice: revamp advise API
  2020-02-19 20:33   ` [PATCH v3 0/2] [Outreachy] advice: revamp " Heba Waly via GitGitGadget
@ 2020-02-19 20:34     ` " Heba Waly via GitGitGadget
  2020-02-20  1:37       ` Emily Shaffer
  2020-02-19 20:34     ` [PATCH v3 2/2] advice: extract vadvise() from advise() Heba Waly via GitGitGadget
  2020-02-24 15:13     ` [PATCH v4 0/3] [Outreachy] advice: revamp advise API Heba Waly via GitGitGadget
  2 siblings, 1 reply; 50+ messages in thread
From: Heba Waly via GitGitGadget @ 2020-02-19 20:34 UTC (permalink / raw)
  To: git; +Cc: Heba Waly, Heba Waly

From: Heba Waly <heba.waly@gmail.com>

Currently it's very easy for the advice library's callers to miss
checking the visibility step before printing an advice. Also, it makes
more sense for this step to be handled by the advice library.

Add a new advise_if_enabled function that checks the visibility of
advice messages before printing.

Add a new helper advise_enabled to check the visibility of the advice
if the caller needs to carry out complicated processing based on that
value.

A list of config variables 'advice_config_keys' is added to be used by
list_config_advices() instead of 'advice_config[]' because we'll get
rid of 'advice_config[]' and the global variables once we migrate all
the callers to use the new APIs.

Also change the advise call in tag library from advise() to
advise_if_enabled() to construct an example of the usage of the new
API.

Signed-off-by: Heba Waly <heba.waly@gmail.com>
---
 Makefile               |  1 +
 advice.c               | 92 ++++++++++++++++++++++++++++++++++++++++--
 advice.h               | 53 +++++++++++++++++++++++-
 builtin/tag.c          |  4 +-
 t/helper/test-advise.c | 16 ++++++++
 t/helper/test-tool.c   |  1 +
 t/helper/test-tool.h   |  1 +
 t/t0018-advice.sh      | 28 +++++++++++++
 t/t7004-tag.sh         |  1 +
 9 files changed, 190 insertions(+), 7 deletions(-)
 create mode 100644 t/helper/test-advise.c
 create mode 100755 t/t0018-advice.sh

diff --git a/Makefile b/Makefile
index 09f98b777ca..ed923a3e818 100644
--- a/Makefile
+++ b/Makefile
@@ -695,6 +695,7 @@ X =
 
 PROGRAMS += $(patsubst %.o,git-%$X,$(PROGRAM_OBJS))
 
+TEST_BUILTINS_OBJS += test-advise.o
 TEST_BUILTINS_OBJS += test-chmtime.o
 TEST_BUILTINS_OBJS += test-config.o
 TEST_BUILTINS_OBJS += test-ctype.o
diff --git a/advice.c b/advice.c
index 249c60dcf32..345319005ac 100644
--- a/advice.c
+++ b/advice.c
@@ -29,7 +29,6 @@ int advice_ignored_hook = 1;
 int advice_waiting_for_editor = 1;
 int advice_graft_file_deprecated = 1;
 int advice_checkout_ambiguous_remote_branch_name = 1;
-int advice_nested_tag = 1;
 int advice_submodule_alternate_error_strategy_die = 1;
 
 static int advice_use_color = -1;
@@ -89,13 +88,46 @@ static struct {
 	{ "waitingForEditor", &advice_waiting_for_editor },
 	{ "graftFileDeprecated", &advice_graft_file_deprecated },
 	{ "checkoutAmbiguousRemoteBranchName", &advice_checkout_ambiguous_remote_branch_name },
-	{ "nestedTag", &advice_nested_tag },
 	{ "submoduleAlternateErrorStrategyDie", &advice_submodule_alternate_error_strategy_die },
 
 	/* make this an alias for backward compatibility */
 	{ "pushNonFastForward", &advice_push_update_rejected }
 };
 
+static const char *advice_config_keys[] = {
+	[FETCH_SHOW_FORCED_UPDATES]		 = "fetchShowForcedUpdates",
+	[PUSH_UPDATE_REJECTED]			 = "pushUpdateRejected",
+	/* make this an alias for backward compatibility */
+	[PUSH_UPDATE_REJECTED_ALIAS]		 = "pushNonFastForward",
+
+	[PUSH_NON_FF_CURRENT]			 = "pushNonFFCurrent",
+	[PUSH_NON_FF_MATCHING]			 = "pushNonFFMatching",
+	[PUSH_ALREADY_EXISTS]			 = "pushAlreadyExists",
+	[PUSH_FETCH_FIRST]			 = "pushFetchFirst",
+	[PUSH_NEEDS_FORCE]			 = "pushNeedsForce",
+	[PUSH_UNQUALIFIED_REF_NAME]		 = "pushUnqualifiedRefName",
+	[STATUS_HINTS]				 = "statusHints",
+	[STATUS_U_OPTION]			 = "statusUoption",
+	[STATUS_AHEAD_BEHIND_WARNING]		 = "statusAheadBehindWarning",
+	[COMMIT_BEFORE_MERGE]			 = "commitBeforeMerge",
+	[RESET_QUIET_WARNING]			 = "resetQuiet",
+	[RESOLVE_CONFLICT]			 = "resolveConflict",
+	[SEQUENCER_IN_USE]			 = "sequencerInUse",
+	[IMPLICIT_IDENTITY]			 = "implicitIdentity",
+	[DETACHED_HEAD]				 = "detachedHead",
+	[SET_UPSTREAM_FAILURE]			 = "setupStreamFailure",
+	[OBJECT_NAME_WARNING]			 = "objectNameWarning",
+	[AMWORKDIR]				 = "amWorkDir",
+	[RM_HINTS]				 = "rmHints",
+	[ADD_EMBEDDED_REPO]			 = "addEmbeddedRepo",
+	[IGNORED_HOOK]				 = "ignoredHook",
+	[WAITING_FOR_EDITOR] 			 = "waitingForEditor",
+	[GRAFT_FILE_DEPRECATED]			 = "graftFileDeprecated",
+	[CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME]	 = "checkoutAmbiguousRemoteBranchName",
+	[NESTED_TAG]				 = "nestedTag",
+	[SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = "submoduleAlternateErrorStrategyDie"
+};
+
 void advise(const char *advice, ...)
 {
 	struct strbuf buf = STRBUF_INIT;
@@ -118,6 +150,58 @@ void advise(const char *advice, ...)
 	strbuf_release(&buf);
 }
 
+static int get_config_value(enum advice_type type)
+{
+	int value = 1;
+	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
+	git_config_get_bool(key, &value);
+	return value;
+}
+
+int advice_enabled(enum advice_type type)
+{
+	switch(type) {
+	case PUSH_UPDATE_REJECTED:
+		return get_config_value(PUSH_UPDATE_REJECTED) &&
+		       get_config_value(PUSH_UPDATE_REJECTED_ALIAS);
+	default:
+		return get_config_value(type);
+	}
+}
+
+static const char turn_off_instructions[] =
+N_("\n"
+   "Disable this message with \"git config %s false\"");
+
+void advise_if_enabled(enum advice_type type, const char *advice, ...)
+{
+	struct strbuf buf = STRBUF_INIT;
+	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
+	va_list params;
+	const char *cp, *np;
+	
+	if(!advice_enabled(type))
+		return;
+
+	va_start(params, advice);
+	strbuf_vaddf(&buf, advice, params);
+	va_end(params);
+
+	strbuf_addf(&buf, turn_off_instructions, key);
+	
+	for (cp = buf.buf; *cp; cp = np) {
+		np = strchrnul(cp, '\n');
+		fprintf(stderr,	_("%shint: %.*s%s\n"),
+			advise_get_color(ADVICE_COLOR_HINT),
+			(int)(np - cp), cp,
+			advise_get_color(ADVICE_COLOR_RESET));
+		if (*np)
+			np++;
+	}
+	strbuf_release(&buf);
+
+}
+
 int git_default_advice_config(const char *var, const char *value)
 {
 	const char *k, *slot_name;
@@ -154,8 +238,8 @@ void list_config_advices(struct string_list *list, const char *prefix)
 {
 	int i;
 
-	for (i = 0; i < ARRAY_SIZE(advice_config); i++)
-		list_config_item(list, prefix, advice_config[i].name);
+	for (i = 0; i < ARRAY_SIZE(advice_config_keys); i++)
+		list_config_item(list, prefix, advice_config_keys[i]);
 }
 
 int error_resolve_conflict(const char *me)
diff --git a/advice.h b/advice.h
index b706780614d..c8be662c4b1 100644
--- a/advice.h
+++ b/advice.h
@@ -29,12 +29,63 @@ extern int advice_ignored_hook;
 extern int advice_waiting_for_editor;
 extern int advice_graft_file_deprecated;
 extern int advice_checkout_ambiguous_remote_branch_name;
-extern int advice_nested_tag;
 extern int advice_submodule_alternate_error_strategy_die;
 
+/**
+ To add a new advice, you need to:
+ - Define an advice_type.
+ - Add a new entry to advice_config_keys list.
+ - Add the new config variable to Documentation/config/advice.txt.
+ - Call advise_if_enabled to print your advice.
+ */
+enum advice_type {
+	FETCH_SHOW_FORCED_UPDATES = 0,
+	PUSH_UPDATE_REJECTED = 1,
+	PUSH_UPDATE_REJECTED_ALIAS = 2,
+	PUSH_NON_FF_CURRENT = 3,
+	PUSH_NON_FF_MATCHING = 4,
+	PUSH_ALREADY_EXISTS = 5,
+	PUSH_FETCH_FIRST = 6,
+	PUSH_NEEDS_FORCE = 7,
+	PUSH_UNQUALIFIED_REF_NAME = 8,
+	STATUS_HINTS = 9,
+	STATUS_U_OPTION = 10,
+	STATUS_AHEAD_BEHIND_WARNING = 11,
+	COMMIT_BEFORE_MERGE = 12,
+	RESET_QUIET_WARNING = 13,
+	RESOLVE_CONFLICT = 14,
+	SEQUENCER_IN_USE = 15,
+	IMPLICIT_IDENTITY = 16,
+	DETACHED_HEAD = 17,
+	SET_UPSTREAM_FAILURE = 18,
+	OBJECT_NAME_WARNING = 19,
+	AMWORKDIR = 20,
+	RM_HINTS = 21,
+	ADD_EMBEDDED_REPO = 22,
+	IGNORED_HOOK = 23,
+	WAITING_FOR_EDITOR = 24,
+	GRAFT_FILE_DEPRECATED = 25,
+	CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME = 26,
+	NESTED_TAG = 27,
+	SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE = 28,
+};
+
+
 int git_default_advice_config(const char *var, const char *value);
 __attribute__((format (printf, 1, 2)))
 void advise(const char *advice, ...);
+
+/**
+ Checks if advice type is enabled (can be printed to the user).
+ Should be called before advise().
+ */
+int advice_enabled(enum advice_type type);
+
+/**
+ Checks the visibility of the advice before printing.
+ */
+void advise_if_enabled(enum advice_type type, const char *advice, ...);
+
 int error_resolve_conflict(const char *me);
 void NORETURN die_resolve_conflict(const char *me);
 void NORETURN die_conclude_merge(void);
diff --git a/builtin/tag.c b/builtin/tag.c
index e0a4c253828..247d9075e19 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -231,8 +231,8 @@ static void create_tag(const struct object_id *object, const char *object_ref,
 	if (type <= OBJ_NONE)
 		die(_("bad object type."));
 
-	if (type == OBJ_TAG && advice_nested_tag)
-		advise(_(message_advice_nested_tag), tag, object_ref);
+	if (type == OBJ_TAG)
+		advise_if_enabled(NESTED_TAG, _(message_advice_nested_tag), tag, object_ref);
 
 	strbuf_addf(&header,
 		    "object %s\n"
diff --git a/t/helper/test-advise.c b/t/helper/test-advise.c
new file mode 100644
index 00000000000..6d28c9cd5aa
--- /dev/null
+++ b/t/helper/test-advise.c
@@ -0,0 +1,16 @@
+#include "test-tool.h"
+#include "cache.h"
+#include "advice.h"
+
+int cmd__advise_if_enabled(int argc, const char **argv)
+{
+	if (!argv[1])
+	die("usage: %s <advice>", argv[0]);
+
+	setup_git_directory();
+
+	//use any advice type for testing
+	advise_if_enabled(NESTED_TAG, argv[1]);
+
+	return 0;
+}
diff --git a/t/helper/test-tool.c b/t/helper/test-tool.c
index f20989d4497..6977badc690 100644
--- a/t/helper/test-tool.c
+++ b/t/helper/test-tool.c
@@ -14,6 +14,7 @@ struct test_cmd {
 };
 
 static struct test_cmd cmds[] = {
+	{ "advise", cmd__advise_if_enabled },
 	{ "chmtime", cmd__chmtime },
 	{ "config", cmd__config },
 	{ "ctype", cmd__ctype },
diff --git a/t/helper/test-tool.h b/t/helper/test-tool.h
index 8ed2af71d1b..ca5e33b842f 100644
--- a/t/helper/test-tool.h
+++ b/t/helper/test-tool.h
@@ -4,6 +4,7 @@
 #define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "git-compat-util.h"
 
+int cmd__advise_if_enabled(int argc, const char **argv);
 int cmd__chmtime(int argc, const char **argv);
 int cmd__config(int argc, const char **argv);
 int cmd__ctype(int argc, const char **argv);
diff --git a/t/t0018-advice.sh b/t/t0018-advice.sh
new file mode 100755
index 00000000000..f4cdb649d51
--- /dev/null
+++ b/t/t0018-advice.sh
@@ -0,0 +1,28 @@
+#!/bin/sh
+
+test_description='Test advise_if_enabled functionality'
+
+. ./test-lib.sh
+
+cat > expected <<EOF
+hint: This is a piece of advice
+hint: Disable this message with "git config advice.nestedTag false"
+EOF
+test_expect_success 'advise should be printed when config variable is unset' '
+	test-tool advise "This is a piece of advice" 2>actual &&
+	test_i18ncmp expected actual
+'
+
+test_expect_success 'advise should be printed when config variable is set to true' '
+	test_config advice.nestedTag true &&
+	test-tool advise "This is a piece of advice" 2>actual &&
+	test_i18ncmp expected actual
+'
+
+test_expect_success 'advise should not be printed when config variable is set to false' '
+	test_config advice.nestedTag false &&
+	test-tool advise "This is a piece of advice" 2>actual &&
+	test_must_be_empty actual
+'
+
+test_done
diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
index 6db92bd3ba6..74b637deb25 100755
--- a/t/t7004-tag.sh
+++ b/t/t7004-tag.sh
@@ -1726,6 +1726,7 @@ test_expect_success 'recursive tagging should give advice' '
 	hint: already a tag. If you meant to tag the object that it points to, use:
 	hint: |
 	hint: 	git tag -f nested annotated-v4.0^{}
+	hint: Disable this message with "git config advice.nestedTag false"
 	EOF
 	git tag -m nested nested annotated-v4.0 2>actual &&
 	test_i18ncmp expect actual
-- 
gitgitgadget


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

* [PATCH v3 2/2] advice: extract vadvise() from advise()
  2020-02-19 20:33   ` [PATCH v3 0/2] [Outreachy] advice: revamp " Heba Waly via GitGitGadget
  2020-02-19 20:34     ` [PATCH v3 1/2] " Heba Waly via GitGitGadget
@ 2020-02-19 20:34     ` Heba Waly via GitGitGadget
  2020-02-20  1:42       ` Emily Shaffer
  2020-02-24 15:13     ` [PATCH v4 0/3] [Outreachy] advice: revamp advise API Heba Waly via GitGitGadget
  2 siblings, 1 reply; 50+ messages in thread
From: Heba Waly via GitGitGadget @ 2020-02-19 20:34 UTC (permalink / raw)
  To: git; +Cc: Heba Waly, Heba Waly

From: Heba Waly <heba.waly@gmail.com>

extract a version of advise() that uses an explict 'va_list' parameter.
Call it from advise() and advise_if_enabled() for a functionally
equivalent version.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Heba Waly <heba.waly@gmail.com>
---
 advice.c | 45 +++++++++++++++++++--------------------------
 1 file changed, 19 insertions(+), 26 deletions(-)

diff --git a/advice.c b/advice.c
index 345319005ac..0c144c69487 100644
--- a/advice.c
+++ b/advice.c
@@ -128,15 +128,20 @@ static const char *advice_config_keys[] = {
 	[SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = "submoduleAlternateErrorStrategyDie"
 };
 
-void advise(const char *advice, ...)
+static const char turn_off_instructions[] =
+N_("\n"
+   "Disable this message with \"git config %s false\"");
+
+static void vadvise(const char *advice, va_list params,
+		    int display_instructions, char *key)
 {
 	struct strbuf buf = STRBUF_INIT;
-	va_list params;
 	const char *cp, *np;
 
-	va_start(params, advice);
 	strbuf_vaddf(&buf, advice, params);
-	va_end(params);
+
+	if(display_instructions)
+		strbuf_addf(&buf, turn_off_instructions, key);
 
 	for (cp = buf.buf; *cp; cp = np) {
 		np = strchrnul(cp, '\n');
@@ -169,37 +174,25 @@ int advice_enabled(enum advice_type type)
 	}
 }
 
-static const char turn_off_instructions[] =
-N_("\n"
-   "Disable this message with \"git config %s false\"");
+void advise(const char *advice, ...)
+{
+	va_list params;
+	va_start(params, advice);
+	vadvise(advice, params, 0, "");
+	va_end(params);
+}
 
 void advise_if_enabled(enum advice_type type, const char *advice, ...)
 {
-	struct strbuf buf = STRBUF_INIT;
-	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
 	va_list params;
-	const char *cp, *np;
-	
+	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
+
 	if(!advice_enabled(type))
 		return;
 
 	va_start(params, advice);
-	strbuf_vaddf(&buf, advice, params);
+	vadvise(advice, params, 1, key);
 	va_end(params);
-
-	strbuf_addf(&buf, turn_off_instructions, key);
-	
-	for (cp = buf.buf; *cp; cp = np) {
-		np = strchrnul(cp, '\n');
-		fprintf(stderr,	_("%shint: %.*s%s\n"),
-			advise_get_color(ADVICE_COLOR_HINT),
-			(int)(np - cp), cp,
-			advise_get_color(ADVICE_COLOR_RESET));
-		if (*np)
-			np++;
-	}
-	strbuf_release(&buf);
-
 }
 
 int git_default_advice_config(const char *var, const char *value)
-- 
gitgitgadget

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

* Re: [PATCH v3 1/2] advice: revamp advise API
  2020-02-19 20:34     ` [PATCH v3 1/2] " Heba Waly via GitGitGadget
@ 2020-02-20  1:37       ` Emily Shaffer
  2020-02-21  0:31         ` Heba Waly
  0 siblings, 1 reply; 50+ messages in thread
From: Emily Shaffer @ 2020-02-20  1:37 UTC (permalink / raw)
  To: Heba Waly via GitGitGadget; +Cc: git, Heba Waly

On Wed, Feb 19, 2020 at 08:34:00PM +0000, Heba Waly via GitGitGadget wrote:
> From: Heba Waly <heba.waly@gmail.com>
> 
> Currently it's very easy for the advice library's callers to miss
> checking the visibility step before printing an advice. Also, it makes
> more sense for this step to be handled by the advice library.
> 
> Add a new advise_if_enabled function that checks the visibility of
> advice messages before printing.
> 
> Add a new helper advise_enabled to check the visibility of the advice
> if the caller needs to carry out complicated processing based on that
> value.
> 
> A list of config variables 'advice_config_keys' is added to be used by
> list_config_advices() instead of 'advice_config[]' because we'll get
> rid of 'advice_config[]' and the global variables once we migrate all
> the callers to use the new APIs.
> 
> Also change the advise call in tag library from advise() to
> advise_if_enabled() to construct an example of the usage of the new
> API.
> 
> Signed-off-by: Heba Waly <heba.waly@gmail.com>
> ---
>  Makefile               |  1 +
>  advice.c               | 92 ++++++++++++++++++++++++++++++++++++++++--
>  advice.h               | 53 +++++++++++++++++++++++-
>  builtin/tag.c          |  4 +-
>  t/helper/test-advise.c | 16 ++++++++
>  t/helper/test-tool.c   |  1 +
>  t/helper/test-tool.h   |  1 +
>  t/t0018-advice.sh      | 28 +++++++++++++
>  t/t7004-tag.sh         |  1 +
>  9 files changed, 190 insertions(+), 7 deletions(-)
>  create mode 100644 t/helper/test-advise.c
>  create mode 100755 t/t0018-advice.sh
> 
> diff --git a/Makefile b/Makefile
> index 09f98b777ca..ed923a3e818 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -695,6 +695,7 @@ X =
>  
>  PROGRAMS += $(patsubst %.o,git-%$X,$(PROGRAM_OBJS))
>  
> +TEST_BUILTINS_OBJS += test-advise.o
>  TEST_BUILTINS_OBJS += test-chmtime.o
>  TEST_BUILTINS_OBJS += test-config.o
>  TEST_BUILTINS_OBJS += test-ctype.o
> diff --git a/advice.c b/advice.c
> index 249c60dcf32..345319005ac 100644
> --- a/advice.c
> +++ b/advice.c
> @@ -29,7 +29,6 @@ int advice_ignored_hook = 1;
>  int advice_waiting_for_editor = 1;
>  int advice_graft_file_deprecated = 1;
>  int advice_checkout_ambiguous_remote_branch_name = 1;
> -int advice_nested_tag = 1;
>  int advice_submodule_alternate_error_strategy_die = 1;
>  
>  static int advice_use_color = -1;
> @@ -89,13 +88,46 @@ static struct {
>  	{ "waitingForEditor", &advice_waiting_for_editor },
>  	{ "graftFileDeprecated", &advice_graft_file_deprecated },
>  	{ "checkoutAmbiguousRemoteBranchName", &advice_checkout_ambiguous_remote_branch_name },
> -	{ "nestedTag", &advice_nested_tag },
>  	{ "submoduleAlternateErrorStrategyDie", &advice_submodule_alternate_error_strategy_die },
>  
>  	/* make this an alias for backward compatibility */
>  	{ "pushNonFastForward", &advice_push_update_rejected }
>  };
>  
> +static const char *advice_config_keys[] = {
> +	[FETCH_SHOW_FORCED_UPDATES]		 = "fetchShowForcedUpdates",
> +	[PUSH_UPDATE_REJECTED]			 = "pushUpdateRejected",
> +	/* make this an alias for backward compatibility */
> +	[PUSH_UPDATE_REJECTED_ALIAS]		 = "pushNonFastForward",
> +
> +	[PUSH_NON_FF_CURRENT]			 = "pushNonFFCurrent",
> +	[PUSH_NON_FF_MATCHING]			 = "pushNonFFMatching",
> +	[PUSH_ALREADY_EXISTS]			 = "pushAlreadyExists",
> +	[PUSH_FETCH_FIRST]			 = "pushFetchFirst",
> +	[PUSH_NEEDS_FORCE]			 = "pushNeedsForce",
> +	[PUSH_UNQUALIFIED_REF_NAME]		 = "pushUnqualifiedRefName",
> +	[STATUS_HINTS]				 = "statusHints",
> +	[STATUS_U_OPTION]			 = "statusUoption",
> +	[STATUS_AHEAD_BEHIND_WARNING]		 = "statusAheadBehindWarning",
> +	[COMMIT_BEFORE_MERGE]			 = "commitBeforeMerge",
> +	[RESET_QUIET_WARNING]			 = "resetQuiet",
> +	[RESOLVE_CONFLICT]			 = "resolveConflict",
> +	[SEQUENCER_IN_USE]			 = "sequencerInUse",
> +	[IMPLICIT_IDENTITY]			 = "implicitIdentity",
> +	[DETACHED_HEAD]				 = "detachedHead",
> +	[SET_UPSTREAM_FAILURE]			 = "setupStreamFailure",
> +	[OBJECT_NAME_WARNING]			 = "objectNameWarning",
> +	[AMWORKDIR]				 = "amWorkDir",
> +	[RM_HINTS]				 = "rmHints",
> +	[ADD_EMBEDDED_REPO]			 = "addEmbeddedRepo",
> +	[IGNORED_HOOK]				 = "ignoredHook",
> +	[WAITING_FOR_EDITOR] 			 = "waitingForEditor",
> +	[GRAFT_FILE_DEPRECATED]			 = "graftFileDeprecated",
> +	[CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME]	 = "checkoutAmbiguousRemoteBranchName",
> +	[NESTED_TAG]				 = "nestedTag",
> +	[SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = "submoduleAlternateErrorStrategyDie"
> +};
> +
>  void advise(const char *advice, ...)
>  {
>  	struct strbuf buf = STRBUF_INIT;
> @@ -118,6 +150,58 @@ void advise(const char *advice, ...)
>  	strbuf_release(&buf);
>  }
>  
> +static int get_config_value(enum advice_type type)
> +{
> +	int value = 1;
> +	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);

Same comment as your other call for xstrfmt. I think you need to manage
the output.
> +	git_config_get_bool(key, &value);
> +	return value;
> +}
> +
> +int advice_enabled(enum advice_type type)
> +{
> +	switch(type) {
> +	case PUSH_UPDATE_REJECTED:
> +		return get_config_value(PUSH_UPDATE_REJECTED) &&
> +		       get_config_value(PUSH_UPDATE_REJECTED_ALIAS);
> +	default:
> +		return get_config_value(type);
> +	}
> +}
> +
> +static const char turn_off_instructions[] =
> +N_("\n"
> +   "Disable this message with \"git config %s false\"");
> +
> +void advise_if_enabled(enum advice_type type, const char *advice, ...)
> +{
> +	struct strbuf buf = STRBUF_INIT;
> +	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);

Hmm, doesn't this leak?

On the surface it looks like xstrfmt can save you a strbuf allocation,
but if you check in strbuf.c, it actually allocates and detaches a
strbuf for you anyways. I'd argue that it's easier to tell whether
you're leaking a strbuf than the result of this call, so you might as
well do it that way.

> +	va_list params;
> +	const char *cp, *np;
> +	
> +	if(!advice_enabled(type))
> +		return;
> +
> +	va_start(params, advice);
> +	strbuf_vaddf(&buf, advice, params);
> +	va_end(params);
> +
> +	strbuf_addf(&buf, turn_off_instructions, key);
> +	
> +	for (cp = buf.buf; *cp; cp = np) {
> +		np = strchrnul(cp, '\n');
> +		fprintf(stderr,	_("%shint: %.*s%s\n"),
> +			advise_get_color(ADVICE_COLOR_HINT),
> +			(int)(np - cp), cp,
> +			advise_get_color(ADVICE_COLOR_RESET));
> +		if (*np)
> +			np++;
> +	}

Hm. This seems like something the project would use if strbuf knew how
to do it. Something like strbuf_prefix_lines(strbuf, prefix, delimiter)
and strbuf_suffix_lines(strbuf, suffix, delimiter)? I think there are
other types of output which need to prepend each line like this?

> +	strbuf_release(&buf);
> +
> +}
> +
>  int git_default_advice_config(const char *var, const char *value)
>  {
>  	const char *k, *slot_name;
> @@ -154,8 +238,8 @@ void list_config_advices(struct string_list *list, const char *prefix)
>  {
>  	int i;
>  
> -	for (i = 0; i < ARRAY_SIZE(advice_config); i++)
> -		list_config_item(list, prefix, advice_config[i].name);
> +	for (i = 0; i < ARRAY_SIZE(advice_config_keys); i++)
> +		list_config_item(list, prefix, advice_config_keys[i]);
>  }
>  
>  int error_resolve_conflict(const char *me)
> diff --git a/advice.h b/advice.h
> index b706780614d..c8be662c4b1 100644
> --- a/advice.h
> +++ b/advice.h
> @@ -29,12 +29,63 @@ extern int advice_ignored_hook;
>  extern int advice_waiting_for_editor;
>  extern int advice_graft_file_deprecated;
>  extern int advice_checkout_ambiguous_remote_branch_name;
> -extern int advice_nested_tag;
>  extern int advice_submodule_alternate_error_strategy_die;
>  
> +/**
> + To add a new advice, you need to:
> + - Define an advice_type.
> + - Add a new entry to advice_config_keys list.
> + - Add the new config variable to Documentation/config/advice.txt.
> + - Call advise_if_enabled to print your advice.
> + */
> +enum advice_type {
> +	FETCH_SHOW_FORCED_UPDATES = 0,
> +	PUSH_UPDATE_REJECTED = 1,
> +	PUSH_UPDATE_REJECTED_ALIAS = 2,
> +	PUSH_NON_FF_CURRENT = 3,
> +	PUSH_NON_FF_MATCHING = 4,
> +	PUSH_ALREADY_EXISTS = 5,
> +	PUSH_FETCH_FIRST = 6,
> +	PUSH_NEEDS_FORCE = 7,
> +	PUSH_UNQUALIFIED_REF_NAME = 8,
> +	STATUS_HINTS = 9,
> +	STATUS_U_OPTION = 10,
> +	STATUS_AHEAD_BEHIND_WARNING = 11,
> +	COMMIT_BEFORE_MERGE = 12,
> +	RESET_QUIET_WARNING = 13,
> +	RESOLVE_CONFLICT = 14,
> +	SEQUENCER_IN_USE = 15,
> +	IMPLICIT_IDENTITY = 16,
> +	DETACHED_HEAD = 17,
> +	SET_UPSTREAM_FAILURE = 18,
> +	OBJECT_NAME_WARNING = 19,
> +	AMWORKDIR = 20,
> +	RM_HINTS = 21,
> +	ADD_EMBEDDED_REPO = 22,
> +	IGNORED_HOOK = 23,
> +	WAITING_FOR_EDITOR = 24,
> +	GRAFT_FILE_DEPRECATED = 25,
> +	CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME = 26,
> +	NESTED_TAG = 27,
> +	SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE = 28,
> +};

Hmm. I wanted to say, "Our codebase uses ALL_CAPS or snake_case in enums
so you could use lowers if you wanted" - but based on 'git grep -A4
"^enum"' it's actually pretty unusual to see enums with lower-case
members. Dang :)

> +
> +
>  int git_default_advice_config(const char *var, const char *value);
>  __attribute__((format (printf, 1, 2)))
>  void advise(const char *advice, ...);
> +
> +/**
> + Checks if advice type is enabled (can be printed to the user).
> + Should be called before advise().
> + */
> +int advice_enabled(enum advice_type type);
> +
> +/**
> + Checks the visibility of the advice before printing.
> + */
> +void advise_if_enabled(enum advice_type type, const char *advice, ...);
> +
>  int error_resolve_conflict(const char *me);
>  void NORETURN die_resolve_conflict(const char *me);
>  void NORETURN die_conclude_merge(void);
> diff --git a/builtin/tag.c b/builtin/tag.c
> index e0a4c253828..247d9075e19 100644
> --- a/builtin/tag.c
> +++ b/builtin/tag.c
> @@ -231,8 +231,8 @@ static void create_tag(const struct object_id *object, const char *object_ref,
>  	if (type <= OBJ_NONE)
>  		die(_("bad object type."));
>  
> -	if (type == OBJ_TAG && advice_nested_tag)
> -		advise(_(message_advice_nested_tag), tag, object_ref);
> +	if (type == OBJ_TAG)
> +		advise_if_enabled(NESTED_TAG, _(message_advice_nested_tag), tag, object_ref);

Hm, if it was me, I would have put this bit in its own commit. But I see
that it's a pretty tiny change...

>  
>  	strbuf_addf(&header,
>  		    "object %s\n"
> diff --git a/t/helper/test-advise.c b/t/helper/test-advise.c
> new file mode 100644
> index 00000000000..6d28c9cd5aa
> --- /dev/null
> +++ b/t/helper/test-advise.c
> @@ -0,0 +1,16 @@
> +#include "test-tool.h"
> +#include "cache.h"
> +#include "advice.h"
> +
> +int cmd__advise_if_enabled(int argc, const char **argv)
> +{
> +	if (!argv[1])
> +	die("usage: %s <advice>", argv[0]);
> +
> +	setup_git_directory();
> +
> +	//use any advice type for testing
I think this might be misleading - your t0018 does quite a few checks
explicitly for the NESTED_TAG advice. Maybe it's better to say something
like "Make sure this agrees with t0018"? Also nit, I've been told off a
few times for using //c++ style comments.
> +	advise_if_enabled(NESTED_TAG, argv[1]);
> +
> +	return 0;
> +}
> diff --git a/t/helper/test-tool.c b/t/helper/test-tool.c
> index f20989d4497..6977badc690 100644
> --- a/t/helper/test-tool.c
> +++ b/t/helper/test-tool.c
> @@ -14,6 +14,7 @@ struct test_cmd {
>  };
>  
>  static struct test_cmd cmds[] = {
> +	{ "advise", cmd__advise_if_enabled },
>  	{ "chmtime", cmd__chmtime },
>  	{ "config", cmd__config },
>  	{ "ctype", cmd__ctype },
> diff --git a/t/helper/test-tool.h b/t/helper/test-tool.h
> index 8ed2af71d1b..ca5e33b842f 100644
> --- a/t/helper/test-tool.h
> +++ b/t/helper/test-tool.h
> @@ -4,6 +4,7 @@
>  #define USE_THE_INDEX_COMPATIBILITY_MACROS
>  #include "git-compat-util.h"
>  
> +int cmd__advise_if_enabled(int argc, const char **argv);
>  int cmd__chmtime(int argc, const char **argv);
>  int cmd__config(int argc, const char **argv);
>  int cmd__ctype(int argc, const char **argv);
> diff --git a/t/t0018-advice.sh b/t/t0018-advice.sh
> new file mode 100755
> index 00000000000..f4cdb649d51
> --- /dev/null
> +++ b/t/t0018-advice.sh
> @@ -0,0 +1,28 @@
> +#!/bin/sh
> +
> +test_description='Test advise_if_enabled functionality'
> +
> +. ./test-lib.sh
> +
> +cat > expected <<EOF
> +hint: This is a piece of advice
> +hint: Disable this message with "git config advice.nestedTag false"
> +EOF
> +test_expect_success 'advise should be printed when config variable is unset' '
> +	test-tool advise "This is a piece of advice" 2>actual &&
> +	test_i18ncmp expected actual
> +'
> +
> +test_expect_success 'advise should be printed when config variable is set to true' '
> +	test_config advice.nestedTag true &&
> +	test-tool advise "This is a piece of advice" 2>actual &&
> +	test_i18ncmp expected actual
> +'
> +
> +test_expect_success 'advise should not be printed when config variable is set to false' '
> +	test_config advice.nestedTag false &&
> +	test-tool advise "This is a piece of advice" 2>actual &&
> +	test_must_be_empty actual
> +'
> +
> +test_done
> diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
> index 6db92bd3ba6..74b637deb25 100755
> --- a/t/t7004-tag.sh
> +++ b/t/t7004-tag.sh
> @@ -1726,6 +1726,7 @@ test_expect_success 'recursive tagging should give advice' '
>  	hint: already a tag. If you meant to tag the object that it points to, use:
>  	hint: |
>  	hint: 	git tag -f nested annotated-v4.0^{}
> +	hint: Disable this message with "git config advice.nestedTag false"
>  	EOF
>  	git tag -m nested nested annotated-v4.0 2>actual &&
>  	test_i18ncmp expect actual
> -- 
> gitgitgadget
> 

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

* Re: [PATCH v3 2/2] advice: extract vadvise() from advise()
  2020-02-19 20:34     ` [PATCH v3 2/2] advice: extract vadvise() from advise() Heba Waly via GitGitGadget
@ 2020-02-20  1:42       ` Emily Shaffer
  2020-02-21  0:34         ` Heba Waly
  0 siblings, 1 reply; 50+ messages in thread
From: Emily Shaffer @ 2020-02-20  1:42 UTC (permalink / raw)
  To: Heba Waly via GitGitGadget; +Cc: git, Heba Waly

On Wed, Feb 19, 2020 at 08:34:01PM +0000, Heba Waly via GitGitGadget wrote:
> From: Heba Waly <heba.waly@gmail.com>
> 
> extract a version of advise() that uses an explict 'va_list' parameter.
> Call it from advise() and advise_if_enabled() for a functionally
> equivalent version.

Hm, I'd put this patch before the advise_if_enabled() one, so that each
commit makes sense by itself (rather than adding a bunch of code last
patch only to remove it in this patch).

> 
> Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
> Signed-off-by: Heba Waly <heba.waly@gmail.com>
> ---
> -	for (cp = buf.buf; *cp; cp = np) {
> -		np = strchrnul(cp, '\n');
> -		fprintf(stderr,	_("%shint: %.*s%s\n"),
> -			advise_get_color(ADVICE_COLOR_HINT),
> -			(int)(np - cp), cp,
> -			advise_get_color(ADVICE_COLOR_RESET));
> -		if (*np)
> -			np++;
> -	}

I see - this hunk that I commented on in the other review is actually
duplicated from advise(). Hm, I still think it'd be useful to put this
functionality into strbuf, but I guess since it's not new code you're
adding there's not a lot of need to sweat about it.

 - Emily

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

* Re: [PATCH v3 1/2] advice: revamp advise API
  2020-02-20  1:37       ` Emily Shaffer
@ 2020-02-21  0:31         ` Heba Waly
  0 siblings, 0 replies; 50+ messages in thread
From: Heba Waly @ 2020-02-21  0:31 UTC (permalink / raw)
  To: Emily Shaffer; +Cc: Heba Waly via GitGitGadget, Git Mailing List

On Thu, Feb 20, 2020 at 2:37 PM Emily Shaffer <emilyshaffer@google.com> wrote:
>
> On Wed, Feb 19, 2020 at 08:34:00PM +0000, Heba Waly via GitGitGadget wrote:
> > From: Heba Waly <heba.waly@gmail.com>
> >
> > +static int get_config_value(enum advice_type type)
> > +{
> > +     int value = 1;
> > +     char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
>
> Same comment as your other call for xstrfmt. I think you need to manage
> the output.

Got it.

> > +void advise_if_enabled(enum advice_type type, const char *advice, ...)
> > +{
> > +     struct strbuf buf = STRBUF_INIT;
> > +     char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
>
> Hmm, doesn't this leak?
>
> On the surface it looks like xstrfmt can save you a strbuf allocation,
> but if you check in strbuf.c, it actually allocates and detaches a
> strbuf for you anyways. I'd argue that it's easier to tell whether
> you're leaking a strbuf than the result of this call, so you might as
> well do it that way.
>

You're right.

> > +     SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE = 28,
> > +};
>
> Hmm. I wanted to say, "Our codebase uses ALL_CAPS or snake_case in enums
> so you could use lowers if you wanted" - but based on 'git grep -A4
> "^enum"' it's actually pretty unusual to see enums with lower-case
> members. Dang :)
>

Yeah I thought the same at first too but reached the same result.

> > +
> > +             advise_if_enabled(NESTED_TAG, _(message_advice_nested_tag), tag, object_ref);
>
> Hm, if it was me, I would have put this bit in its own commit. But I see
> that it's a pretty tiny change...
>

That'd have been better of course, don't know why I didn't think of that :)

> > +     //use any advice type for testing
> I think this might be misleading - your t0018 does quite a few checks
> explicitly for the NESTED_TAG advice. Maybe it's better to say something
> like "Make sure this agrees with t0018"?

I see your point, I'll need to think more about this one.

> Also nit, I've been told off a
> few times for using //c++ style comments.

Oh ok.

Thanks a lot, Emily

Heba

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

* Re: [PATCH v3 2/2] advice: extract vadvise() from advise()
  2020-02-20  1:42       ` Emily Shaffer
@ 2020-02-21  0:34         ` Heba Waly
  0 siblings, 0 replies; 50+ messages in thread
From: Heba Waly @ 2020-02-21  0:34 UTC (permalink / raw)
  To: Emily Shaffer; +Cc: Heba Waly via GitGitGadget, Git Mailing List

On Thu, Feb 20, 2020 at 2:42 PM Emily Shaffer <emilyshaffer@google.com> wrote:
>
> On Wed, Feb 19, 2020 at 08:34:01PM +0000, Heba Waly via GitGitGadget wrote:
> > From: Heba Waly <heba.waly@gmail.com>
> >
> > extract a version of advise() that uses an explict 'va_list' parameter.
> > Call it from advise() and advise_if_enabled() for a functionally
> > equivalent version.
>
> Hm, I'd put this patch before the advise_if_enabled() one, so that each
> commit makes sense by itself (rather than adding a bunch of code last
> patch only to remove it in this patch).
>

You're right, that was me avoiding the commits re-order conflicts but
I'll give it a second try.

> >
> > Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
> > Signed-off-by: Heba Waly <heba.waly@gmail.com>
> > ---
> > -     for (cp = buf.buf; *cp; cp = np) {
> > -             np = strchrnul(cp, '\n');
> > -             fprintf(stderr, _("%shint: %.*s%s\n"),
> > -                     advise_get_color(ADVICE_COLOR_HINT),
> > -                     (int)(np - cp), cp,
> > -                     advise_get_color(ADVICE_COLOR_RESET));
> > -             if (*np)
> > -                     np++;
> > -     }
>
> I see - this hunk that I commented on in the other review is actually
> duplicated from advise(). Hm, I still think it'd be useful to put this
> functionality into strbuf, but I guess since it's not new code you're
> adding there's not a lot of need to sweat about it.
>

Agree.

>  - Emily

Thank you Emily

Heba

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

* [PATCH v4 0/3] [Outreachy] advice: revamp advise API
  2020-02-19 20:33   ` [PATCH v3 0/2] [Outreachy] advice: revamp " Heba Waly via GitGitGadget
  2020-02-19 20:34     ` [PATCH v3 1/2] " Heba Waly via GitGitGadget
  2020-02-19 20:34     ` [PATCH v3 2/2] advice: extract vadvise() from advise() Heba Waly via GitGitGadget
@ 2020-02-24 15:13     ` Heba Waly via GitGitGadget
  2020-02-24 15:13       ` [PATCH v4 1/3] advice: extract vadvise() from advise() Heba Waly via GitGitGadget
                         ` (3 more replies)
  2 siblings, 4 replies; 50+ messages in thread
From: Heba Waly via GitGitGadget @ 2020-02-24 15:13 UTC (permalink / raw)
  To: git; +Cc: Heba Waly

Main changes in V4:

 * Re-order the commits.
 * Free the output after using xstrfmt().


----------------------------------------------------------------------------

Changes in V3:

 * Remove the new wrapper advice_push_update_rejected_enabled() (which was
   added in V2 to handle a special case of having a config variable alias),
   and replace it by adding switch cases to advice_enabled() (The reason
   behind this change is that another special case came up while I was
   migrating the rest of the advise calls to the new APIs.)
 * Remove trailing whitespaces.


----------------------------------------------------------------------------

Main changes in V2:

 * Rename advise_ng to advise_if_enabled.
 * Add a new advise_enabled() helper.
 * Add a list of config variables names to replace advice_config[] (used by
   list_config_advices()).
 * Send an enum parameter to the new advise helpers instead of strings.
 * Extract vadvise() from advise() and advise_if enabled().


----------------------------------------------------------------------------

The advice API is currently a little bit confusing to call. quoting from
[1]:

When introducing a new advice message, you would

 * come up with advice.frotz configuration variable

 * define and declare advice_frotz global variable that defaults to
   true

 * sprinkle calls like this:

  if (advice_frotz)
    advise(_("helpful message about frotz"));

A new approach was suggested in [1] which this patch is based upon.

A new advise_if_enabled() is introduced to gradually replace advise()
advice_enabled() helper is also introduced to be used by those callers who:

 * Only need to check the visibility without calling advise() (they call
   die() or error() instead for example)
 * Need to carry out some heavy processing to display an advice, in this
   case they'll do: if(advice_enabled(advice_type))  advise("some advice message");
   
   

To introduce a new advice message, the caller needs to:

 * Add a new_advice_type to 'enum advice_type'
 * Come up with a new config variable name and add this name to 
   advice_config_keys[]
 * Call advise_if_enabled(new_advice_type, "advice message to be printed")
 * Or call advice_enabled(new_advice_type) first and then follow is by
   advice("advice message to be printed") as explained earlier.
 * Add the new config variable to Documentation/config/advice.txt

The reason a new list of configuration variables was added to the library is
to be used by the list_config_advices() function instead of advice_config[].
And we should get rid of advice_config[] once we migrate all the callers to
use the new APIs instead of checking the global variables (which we'll get
rid of as well).

In the future, we can investigate generating the documentation from the list
of config variables or vice versa to make introducing a new advice much
easier, but this approach will do it for now.

V2 makes the process of introducing a new advice longer than V1 and almost
as long as the original library, but having the advice library responsible
for checking the message visibility is still an improvement and in my own
opinion the new structure makes better sense and makes the library less
confusing to use.

After this patch the plan is to change the advise() calls to
advise_if_enabled() whenever possible, or at least replace the global
variables checks by advise_enabled() when advise_if_enabled() is not
suitable.

[1] https://public-inbox.org/git/xmqqzhf5cw69.fsf@gitster-ct.c.googlers.com/

Heba Waly (3):
  advice: extract vadvise() from advise()
  advice: revamp advise API
  tag: use new advice API to check visibility

 Makefile               |  1 +
 advice.c               | 95 ++++++++++++++++++++++++++++++++++++++----
 advice.h               | 53 ++++++++++++++++++++++-
 builtin/tag.c          |  5 ++-
 t/helper/test-advise.c | 19 +++++++++
 t/helper/test-tool.c   |  1 +
 t/helper/test-tool.h   |  1 +
 t/t0018-advice.sh      | 28 +++++++++++++
 t/t7004-tag.sh         |  1 +
 9 files changed, 193 insertions(+), 11 deletions(-)
 create mode 100644 t/helper/test-advise.c
 create mode 100755 t/t0018-advice.sh


base-commit: c7a62075917b3340f908093f63f1161c44ed1475
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-548%2FHebaWaly%2Fadvice_refactoring-v4
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-548/HebaWaly/advice_refactoring-v4
Pull-Request: https://github.com/gitgitgadget/git/pull/548

Range-diff vs v3:

 2:  a2a145c705e ! 1:  f668d9b7ca0 advice: extract vadvise() from advise()
     @@ -2,9 +2,9 @@
      
          advice: extract vadvise() from advise()
      
     -    extract a version of advise() that uses an explict 'va_list' parameter.
     -    Call it from advise() and advise_if_enabled() for a functionally
     -    equivalent version.
     +    In preparation for a new advice method, extract a version of advise()
     +    that uses an explict 'va_list' parameter. Call it from advise() for a
     +    functionally equivalent version.
      
          Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
          Signed-off-by: Heba Waly <heba.waly@gmail.com>
     @@ -13,16 +13,11 @@
       --- a/advice.c
       +++ b/advice.c
      @@
     - 	[SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = "submoduleAlternateErrorStrategyDie"
     + 	{ "pushNonFastForward", &advice_push_update_rejected }
       };
       
      -void advise(const char *advice, ...)
     -+static const char turn_off_instructions[] =
     -+N_("\n"
     -+   "Disable this message with \"git config %s false\"");
     -+
     -+static void vadvise(const char *advice, va_list params,
     -+		    int display_instructions, char *key)
     ++static void vadvise(const char *advice, va_list params)
       {
       	struct strbuf buf = STRBUF_INIT;
      -	va_list params;
     @@ -31,57 +26,21 @@
      -	va_start(params, advice);
       	strbuf_vaddf(&buf, advice, params);
      -	va_end(params);
     -+
     -+	if(display_instructions)
     -+		strbuf_addf(&buf, turn_off_instructions, key);
       
       	for (cp = buf.buf; *cp; cp = np) {
       		np = strchrnul(cp, '\n');
      @@
     - 	}
     + 	strbuf_release(&buf);
       }
       
     --static const char turn_off_instructions[] =
     --N_("\n"
     --   "Disable this message with \"git config %s false\"");
      +void advise(const char *advice, ...)
      +{
      +	va_list params;
      +	va_start(params, advice);
     -+	vadvise(advice, params, 0, "");
     ++	vadvise(advice, params);
      +	va_end(params);
      +}
     - 
     - void advise_if_enabled(enum advice_type type, const char *advice, ...)
     - {
     --	struct strbuf buf = STRBUF_INIT;
     --	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
     - 	va_list params;
     --	const char *cp, *np;
     --	
     -+	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
      +
     - 	if(!advice_enabled(type))
     - 		return;
     - 
     - 	va_start(params, advice);
     --	strbuf_vaddf(&buf, advice, params);
     -+	vadvise(advice, params, 1, key);
     - 	va_end(params);
     --
     --	strbuf_addf(&buf, turn_off_instructions, key);
     --	
     --	for (cp = buf.buf; *cp; cp = np) {
     --		np = strchrnul(cp, '\n');
     --		fprintf(stderr,	_("%shint: %.*s%s\n"),
     --			advise_get_color(ADVICE_COLOR_HINT),
     --			(int)(np - cp), cp,
     --			advise_get_color(ADVICE_COLOR_RESET));
     --		if (*np)
     --			np++;
     --	}
     --	strbuf_release(&buf);
     --
     - }
     - 
       int git_default_advice_config(const char *var, const char *value)
     + {
     + 	const char *k, *slot_name;
 1:  4ab141426f3 ! 2:  04c3e5760f6 advice: revamp advise API
     @@ -40,24 +40,10 @@
       --- a/advice.c
       +++ b/advice.c
      @@
     - int advice_waiting_for_editor = 1;
     - int advice_graft_file_deprecated = 1;
     - int advice_checkout_ambiguous_remote_branch_name = 1;
     --int advice_nested_tag = 1;
     - int advice_submodule_alternate_error_strategy_die = 1;
     - 
     - static int advice_use_color = -1;
     -@@
     - 	{ "waitingForEditor", &advice_waiting_for_editor },
     - 	{ "graftFileDeprecated", &advice_graft_file_deprecated },
     - 	{ "checkoutAmbiguousRemoteBranchName", &advice_checkout_ambiguous_remote_branch_name },
     --	{ "nestedTag", &advice_nested_tag },
     - 	{ "submoduleAlternateErrorStrategyDie", &advice_submodule_alternate_error_strategy_die },
     - 
     - 	/* make this an alias for backward compatibility */
       	{ "pushNonFastForward", &advice_push_update_rejected }
       };
       
     +-static void vadvise(const char *advice, va_list params)
      +static const char *advice_config_keys[] = {
      +	[FETCH_SHOW_FORCED_UPDATES]		 = "fetchShowForcedUpdates",
      +	[PUSH_UPDATE_REJECTED]			 = "pushUpdateRejected",
     @@ -92,18 +78,39 @@
      +	[SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = "submoduleAlternateErrorStrategyDie"
      +};
      +
     - void advise(const char *advice, ...)
     ++static const char turn_off_instructions[] =
     ++N_("\n"
     ++   "Disable this message with \"git config %s false\"");
     ++
     ++static void vadvise(const char *advice, va_list params,
     ++		    int display_instructions, char *key)
       {
       	struct strbuf buf = STRBUF_INIT;
     -@@
     - 	strbuf_release(&buf);
     - }
     + 	const char *cp, *np;
       
     + 	strbuf_vaddf(&buf, advice, params);
     + 
     ++	if(display_instructions)
     ++		strbuf_addf(&buf, turn_off_instructions, key);
     ++
     + 	for (cp = buf.buf; *cp; cp = np) {
     + 		np = strchrnul(cp, '\n');
     + 		fprintf(stderr,	_("%shint: %.*s%s\n"),
     +@@
     + {
     + 	va_list params;
     + 	va_start(params, advice);
     +-	vadvise(advice, params);
     ++	vadvise(advice, params, 0, "");
     ++	va_end(params);
     ++}
     ++
      +static int get_config_value(enum advice_type type)
      +{
      +	int value = 1;
      +	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
      +	git_config_get_bool(key, &value);
     ++	free(key);
      +	return value;
      +}
      +
     @@ -118,42 +125,21 @@
      +	}
      +}
      +
     -+static const char turn_off_instructions[] =
     -+N_("\n"
     -+   "Disable this message with \"git config %s false\"");
     -+
      +void advise_if_enabled(enum advice_type type, const char *advice, ...)
      +{
     -+	struct strbuf buf = STRBUF_INIT;
      +	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
      +	va_list params;
     -+	const char *cp, *np;
     -+	
     ++
      +	if(!advice_enabled(type))
      +		return;
      +
      +	va_start(params, advice);
     -+	strbuf_vaddf(&buf, advice, params);
     -+	va_end(params);
     -+
     -+	strbuf_addf(&buf, turn_off_instructions, key);
     -+	
     -+	for (cp = buf.buf; *cp; cp = np) {
     -+		np = strchrnul(cp, '\n');
     -+		fprintf(stderr,	_("%shint: %.*s%s\n"),
     -+			advise_get_color(ADVICE_COLOR_HINT),
     -+			(int)(np - cp), cp,
     -+			advise_get_color(ADVICE_COLOR_RESET));
     -+		if (*np)
     -+			np++;
     -+	}
     -+	strbuf_release(&buf);
     -+
     -+}
     -+
     ++	vadvise(advice, params, 1, key);
     + 	va_end(params);
     ++	free(key);
     + }
     + 
       int git_default_advice_config(const char *var, const char *value)
     - {
     - 	const char *k, *slot_name;
      @@
       {
       	int i;
     @@ -170,10 +156,7 @@
       --- a/advice.h
       +++ b/advice.h
      @@
     - extern int advice_waiting_for_editor;
     - extern int advice_graft_file_deprecated;
     - extern int advice_checkout_ambiguous_remote_branch_name;
     --extern int advice_nested_tag;
     + extern int advice_nested_tag;
       extern int advice_submodule_alternate_error_strategy_die;
       
      +/**
     @@ -235,21 +218,6 @@
       void NORETURN die_resolve_conflict(const char *me);
       void NORETURN die_conclude_merge(void);
      
     - diff --git a/builtin/tag.c b/builtin/tag.c
     - --- a/builtin/tag.c
     - +++ b/builtin/tag.c
     -@@
     - 	if (type <= OBJ_NONE)
     - 		die(_("bad object type."));
     - 
     --	if (type == OBJ_TAG && advice_nested_tag)
     --		advise(_(message_advice_nested_tag), tag, object_ref);
     -+	if (type == OBJ_TAG)
     -+		advise_if_enabled(NESTED_TAG, _(message_advice_nested_tag), tag, object_ref);
     - 
     - 	strbuf_addf(&header,
     - 		    "object %s\n"
     -
       diff --git a/t/helper/test-advise.c b/t/helper/test-advise.c
       new file mode 100644
       --- /dev/null
     @@ -266,7 +234,10 @@
      +
      +	setup_git_directory();
      +
     -+	//use any advice type for testing
     ++	/*
     ++	  Any advice type can be used for testing, but NESTED_TAG was selected
     ++	  here and in t0018 where this command is being executed.
     ++	 */
      +	advise_if_enabled(NESTED_TAG, argv[1]);
      +
      +	return 0;
     @@ -329,15 +300,3 @@
      +'
      +
      +test_done
     -
     - diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
     - --- a/t/t7004-tag.sh
     - +++ b/t/t7004-tag.sh
     -@@
     - 	hint: already a tag. If you meant to tag the object that it points to, use:
     - 	hint: |
     - 	hint: 	git tag -f nested annotated-v4.0^{}
     -+	hint: Disable this message with "git config advice.nestedTag false"
     - 	EOF
     - 	git tag -m nested nested annotated-v4.0 2>actual &&
     - 	test_i18ncmp expect actual
 -:  ----------- > 3:  3cc0a17123d tag: use new advice API to check visibility

-- 
gitgitgadget

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

* [PATCH v4 1/3] advice: extract vadvise() from advise()
  2020-02-24 15:13     ` [PATCH v4 0/3] [Outreachy] advice: revamp advise API Heba Waly via GitGitGadget
@ 2020-02-24 15:13       ` Heba Waly via GitGitGadget
  2020-02-24 22:04         ` Emily Shaffer
  2020-02-24 15:13       ` [PATCH v4 2/3] advice: revamp advise API Heba Waly via GitGitGadget
                         ` (2 subsequent siblings)
  3 siblings, 1 reply; 50+ messages in thread
From: Heba Waly via GitGitGadget @ 2020-02-24 15:13 UTC (permalink / raw)
  To: git; +Cc: Heba Waly, Heba Waly

From: Heba Waly <heba.waly@gmail.com>

In preparation for a new advice method, extract a version of advise()
that uses an explict 'va_list' parameter. Call it from advise() for a
functionally equivalent version.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Heba Waly <heba.waly@gmail.com>
---
 advice.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/advice.c b/advice.c
index 249c60dcf32..fd836332dad 100644
--- a/advice.c
+++ b/advice.c
@@ -96,15 +96,12 @@ static struct {
 	{ "pushNonFastForward", &advice_push_update_rejected }
 };
 
-void advise(const char *advice, ...)
+static void vadvise(const char *advice, va_list params)
 {
 	struct strbuf buf = STRBUF_INIT;
-	va_list params;
 	const char *cp, *np;
 
-	va_start(params, advice);
 	strbuf_vaddf(&buf, advice, params);
-	va_end(params);
 
 	for (cp = buf.buf; *cp; cp = np) {
 		np = strchrnul(cp, '\n');
@@ -118,6 +115,14 @@ void advise(const char *advice, ...)
 	strbuf_release(&buf);
 }
 
+void advise(const char *advice, ...)
+{
+	va_list params;
+	va_start(params, advice);
+	vadvise(advice, params);
+	va_end(params);
+}
+
 int git_default_advice_config(const char *var, const char *value)
 {
 	const char *k, *slot_name;
-- 
gitgitgadget


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

* [PATCH v4 2/3] advice: revamp advise API
  2020-02-24 15:13     ` [PATCH v4 0/3] [Outreachy] advice: revamp advise API Heba Waly via GitGitGadget
  2020-02-24 15:13       ` [PATCH v4 1/3] advice: extract vadvise() from advise() Heba Waly via GitGitGadget
@ 2020-02-24 15:13       ` Heba Waly via GitGitGadget
  2020-02-24 22:05         ` Junio C Hamano
  2020-02-24 23:45         ` Emily Shaffer
  2020-02-24 15:13       ` [PATCH v4 3/3] tag: use new advice API to check visibility Heba Waly via GitGitGadget
  2020-02-25 10:55       ` [PATCH v5 0/3] [Outreachy] advice: revamp advise API Heba Waly via GitGitGadget
  3 siblings, 2 replies; 50+ messages in thread
From: Heba Waly via GitGitGadget @ 2020-02-24 15:13 UTC (permalink / raw)
  To: git; +Cc: Heba Waly, Heba Waly

From: Heba Waly <heba.waly@gmail.com>

Currently it's very easy for the advice library's callers to miss
checking the visibility step before printing an advice. Also, it makes
more sense for this step to be handled by the advice library.

Add a new advise_if_enabled function that checks the visibility of
advice messages before printing.

Add a new helper advise_enabled to check the visibility of the advice
if the caller needs to carry out complicated processing based on that
value.

A list of config variables 'advice_config_keys' is added to be used by
list_config_advices() instead of 'advice_config[]' because we'll get
rid of 'advice_config[]' and the global variables once we migrate all
the callers to use the new APIs.

Also change the advise call in tag library from advise() to
advise_if_enabled() to construct an example of the usage of the new
API.

Signed-off-by: Heba Waly <heba.waly@gmail.com>
---
 Makefile               |  1 +
 advice.c               | 84 ++++++++++++++++++++++++++++++++++++++++--
 advice.h               | 52 ++++++++++++++++++++++++++
 t/helper/test-advise.c | 19 ++++++++++
 t/helper/test-tool.c   |  1 +
 t/helper/test-tool.h   |  1 +
 t/t0018-advice.sh      | 28 ++++++++++++++
 7 files changed, 182 insertions(+), 4 deletions(-)
 create mode 100644 t/helper/test-advise.c
 create mode 100755 t/t0018-advice.sh

diff --git a/Makefile b/Makefile
index 09f98b777ca..ed923a3e818 100644
--- a/Makefile
+++ b/Makefile
@@ -695,6 +695,7 @@ X =
 
 PROGRAMS += $(patsubst %.o,git-%$X,$(PROGRAM_OBJS))
 
+TEST_BUILTINS_OBJS += test-advise.o
 TEST_BUILTINS_OBJS += test-chmtime.o
 TEST_BUILTINS_OBJS += test-config.o
 TEST_BUILTINS_OBJS += test-ctype.o
diff --git a/advice.c b/advice.c
index fd836332dad..ff25087fa7e 100644
--- a/advice.c
+++ b/advice.c
@@ -96,13 +96,55 @@ static struct {
 	{ "pushNonFastForward", &advice_push_update_rejected }
 };
 
-static void vadvise(const char *advice, va_list params)
+static const char *advice_config_keys[] = {
+	[FETCH_SHOW_FORCED_UPDATES]		 = "fetchShowForcedUpdates",
+	[PUSH_UPDATE_REJECTED]			 = "pushUpdateRejected",
+	/* make this an alias for backward compatibility */
+	[PUSH_UPDATE_REJECTED_ALIAS]		 = "pushNonFastForward",
+
+	[PUSH_NON_FF_CURRENT]			 = "pushNonFFCurrent",
+	[PUSH_NON_FF_MATCHING]			 = "pushNonFFMatching",
+	[PUSH_ALREADY_EXISTS]			 = "pushAlreadyExists",
+	[PUSH_FETCH_FIRST]			 = "pushFetchFirst",
+	[PUSH_NEEDS_FORCE]			 = "pushNeedsForce",
+	[PUSH_UNQUALIFIED_REF_NAME]		 = "pushUnqualifiedRefName",
+	[STATUS_HINTS]				 = "statusHints",
+	[STATUS_U_OPTION]			 = "statusUoption",
+	[STATUS_AHEAD_BEHIND_WARNING]		 = "statusAheadBehindWarning",
+	[COMMIT_BEFORE_MERGE]			 = "commitBeforeMerge",
+	[RESET_QUIET_WARNING]			 = "resetQuiet",
+	[RESOLVE_CONFLICT]			 = "resolveConflict",
+	[SEQUENCER_IN_USE]			 = "sequencerInUse",
+	[IMPLICIT_IDENTITY]			 = "implicitIdentity",
+	[DETACHED_HEAD]				 = "detachedHead",
+	[SET_UPSTREAM_FAILURE]			 = "setupStreamFailure",
+	[OBJECT_NAME_WARNING]			 = "objectNameWarning",
+	[AMWORKDIR]				 = "amWorkDir",
+	[RM_HINTS]				 = "rmHints",
+	[ADD_EMBEDDED_REPO]			 = "addEmbeddedRepo",
+	[IGNORED_HOOK]				 = "ignoredHook",
+	[WAITING_FOR_EDITOR] 			 = "waitingForEditor",
+	[GRAFT_FILE_DEPRECATED]			 = "graftFileDeprecated",
+	[CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME]	 = "checkoutAmbiguousRemoteBranchName",
+	[NESTED_TAG]				 = "nestedTag",
+	[SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = "submoduleAlternateErrorStrategyDie"
+};
+
+static const char turn_off_instructions[] =
+N_("\n"
+   "Disable this message with \"git config %s false\"");
+
+static void vadvise(const char *advice, va_list params,
+		    int display_instructions, char *key)
 {
 	struct strbuf buf = STRBUF_INIT;
 	const char *cp, *np;
 
 	strbuf_vaddf(&buf, advice, params);
 
+	if(display_instructions)
+		strbuf_addf(&buf, turn_off_instructions, key);
+
 	for (cp = buf.buf; *cp; cp = np) {
 		np = strchrnul(cp, '\n');
 		fprintf(stderr,	_("%shint: %.*s%s\n"),
@@ -119,8 +161,42 @@ void advise(const char *advice, ...)
 {
 	va_list params;
 	va_start(params, advice);
-	vadvise(advice, params);
+	vadvise(advice, params, 0, "");
+	va_end(params);
+}
+
+static int get_config_value(enum advice_type type)
+{
+	int value = 1;
+	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
+	git_config_get_bool(key, &value);
+	free(key);
+	return value;
+}
+
+int advice_enabled(enum advice_type type)
+{
+	switch(type) {
+	case PUSH_UPDATE_REJECTED:
+		return get_config_value(PUSH_UPDATE_REJECTED) &&
+		       get_config_value(PUSH_UPDATE_REJECTED_ALIAS);
+	default:
+		return get_config_value(type);
+	}
+}
+
+void advise_if_enabled(enum advice_type type, const char *advice, ...)
+{
+	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
+	va_list params;
+
+	if(!advice_enabled(type))
+		return;
+
+	va_start(params, advice);
+	vadvise(advice, params, 1, key);
 	va_end(params);
+	free(key);
 }
 
 int git_default_advice_config(const char *var, const char *value)
@@ -159,8 +235,8 @@ void list_config_advices(struct string_list *list, const char *prefix)
 {
 	int i;
 
-	for (i = 0; i < ARRAY_SIZE(advice_config); i++)
-		list_config_item(list, prefix, advice_config[i].name);
+	for (i = 0; i < ARRAY_SIZE(advice_config_keys); i++)
+		list_config_item(list, prefix, advice_config_keys[i]);
 }
 
 int error_resolve_conflict(const char *me)
diff --git a/advice.h b/advice.h
index b706780614d..61a7ee82827 100644
--- a/advice.h
+++ b/advice.h
@@ -32,9 +32,61 @@ extern int advice_checkout_ambiguous_remote_branch_name;
 extern int advice_nested_tag;
 extern int advice_submodule_alternate_error_strategy_die;
 
+/**
+ To add a new advice, you need to:
+ - Define an advice_type.
+ - Add a new entry to advice_config_keys list.
+ - Add the new config variable to Documentation/config/advice.txt.
+ - Call advise_if_enabled to print your advice.
+ */
+enum advice_type {
+	FETCH_SHOW_FORCED_UPDATES = 0,
+	PUSH_UPDATE_REJECTED = 1,
+	PUSH_UPDATE_REJECTED_ALIAS = 2,
+	PUSH_NON_FF_CURRENT = 3,
+	PUSH_NON_FF_MATCHING = 4,
+	PUSH_ALREADY_EXISTS = 5,
+	PUSH_FETCH_FIRST = 6,
+	PUSH_NEEDS_FORCE = 7,
+	PUSH_UNQUALIFIED_REF_NAME = 8,
+	STATUS_HINTS = 9,
+	STATUS_U_OPTION = 10,
+	STATUS_AHEAD_BEHIND_WARNING = 11,
+	COMMIT_BEFORE_MERGE = 12,
+	RESET_QUIET_WARNING = 13,
+	RESOLVE_CONFLICT = 14,
+	SEQUENCER_IN_USE = 15,
+	IMPLICIT_IDENTITY = 16,
+	DETACHED_HEAD = 17,
+	SET_UPSTREAM_FAILURE = 18,
+	OBJECT_NAME_WARNING = 19,
+	AMWORKDIR = 20,
+	RM_HINTS = 21,
+	ADD_EMBEDDED_REPO = 22,
+	IGNORED_HOOK = 23,
+	WAITING_FOR_EDITOR = 24,
+	GRAFT_FILE_DEPRECATED = 25,
+	CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME = 26,
+	NESTED_TAG = 27,
+	SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE = 28,
+};
+
+
 int git_default_advice_config(const char *var, const char *value);
 __attribute__((format (printf, 1, 2)))
 void advise(const char *advice, ...);
+
+/**
+ Checks if advice type is enabled (can be printed to the user).
+ Should be called before advise().
+ */
+int advice_enabled(enum advice_type type);
+
+/**
+ Checks the visibility of the advice before printing.
+ */
+void advise_if_enabled(enum advice_type type, const char *advice, ...);
+
 int error_resolve_conflict(const char *me);
 void NORETURN die_resolve_conflict(const char *me);
 void NORETURN die_conclude_merge(void);
diff --git a/t/helper/test-advise.c b/t/helper/test-advise.c
new file mode 100644
index 00000000000..279cad6460e
--- /dev/null
+++ b/t/helper/test-advise.c
@@ -0,0 +1,19 @@
+#include "test-tool.h"
+#include "cache.h"
+#include "advice.h"
+
+int cmd__advise_if_enabled(int argc, const char **argv)
+{
+	if (!argv[1])
+	die("usage: %s <advice>", argv[0]);
+
+	setup_git_directory();
+
+	/*
+	  Any advice type can be used for testing, but NESTED_TAG was selected
+	  here and in t0018 where this command is being executed.
+	 */
+	advise_if_enabled(NESTED_TAG, argv[1]);
+
+	return 0;
+}
diff --git a/t/helper/test-tool.c b/t/helper/test-tool.c
index f20989d4497..6977badc690 100644
--- a/t/helper/test-tool.c
+++ b/t/helper/test-tool.c
@@ -14,6 +14,7 @@ struct test_cmd {
 };
 
 static struct test_cmd cmds[] = {
+	{ "advise", cmd__advise_if_enabled },
 	{ "chmtime", cmd__chmtime },
 	{ "config", cmd__config },
 	{ "ctype", cmd__ctype },
diff --git a/t/helper/test-tool.h b/t/helper/test-tool.h
index 8ed2af71d1b..ca5e33b842f 100644
--- a/t/helper/test-tool.h
+++ b/t/helper/test-tool.h
@@ -4,6 +4,7 @@
 #define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "git-compat-util.h"
 
+int cmd__advise_if_enabled(int argc, const char **argv);
 int cmd__chmtime(int argc, const char **argv);
 int cmd__config(int argc, const char **argv);
 int cmd__ctype(int argc, const char **argv);
diff --git a/t/t0018-advice.sh b/t/t0018-advice.sh
new file mode 100755
index 00000000000..f4cdb649d51
--- /dev/null
+++ b/t/t0018-advice.sh
@@ -0,0 +1,28 @@
+#!/bin/sh
+
+test_description='Test advise_if_enabled functionality'
+
+. ./test-lib.sh
+
+cat > expected <<EOF
+hint: This is a piece of advice
+hint: Disable this message with "git config advice.nestedTag false"
+EOF
+test_expect_success 'advise should be printed when config variable is unset' '
+	test-tool advise "This is a piece of advice" 2>actual &&
+	test_i18ncmp expected actual
+'
+
+test_expect_success 'advise should be printed when config variable is set to true' '
+	test_config advice.nestedTag true &&
+	test-tool advise "This is a piece of advice" 2>actual &&
+	test_i18ncmp expected actual
+'
+
+test_expect_success 'advise should not be printed when config variable is set to false' '
+	test_config advice.nestedTag false &&
+	test-tool advise "This is a piece of advice" 2>actual &&
+	test_must_be_empty actual
+'
+
+test_done
-- 
gitgitgadget


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

* [PATCH v4 3/3] tag: use new advice API to check visibility
  2020-02-24 15:13     ` [PATCH v4 0/3] [Outreachy] advice: revamp advise API Heba Waly via GitGitGadget
  2020-02-24 15:13       ` [PATCH v4 1/3] advice: extract vadvise() from advise() Heba Waly via GitGitGadget
  2020-02-24 15:13       ` [PATCH v4 2/3] advice: revamp advise API Heba Waly via GitGitGadget
@ 2020-02-24 15:13       ` Heba Waly via GitGitGadget
  2020-02-24 22:07         ` Junio C Hamano
  2020-02-24 23:46         ` Emily Shaffer
  2020-02-25 10:55       ` [PATCH v5 0/3] [Outreachy] advice: revamp advise API Heba Waly via GitGitGadget
  3 siblings, 2 replies; 50+ messages in thread
From: Heba Waly via GitGitGadget @ 2020-02-24 15:13 UTC (permalink / raw)
  To: git; +Cc: Heba Waly, Heba Waly

From: Heba Waly <heba.waly@gmail.com>

Following the new helpers added to the advice library,
replace the global variable check approach by the new
API calls

Signed-off-by: Heba Waly <heba.waly@gmail.com>
---
 advice.c       | 2 --
 advice.h       | 1 -
 builtin/tag.c  | 5 +++--
 t/t7004-tag.sh | 1 +
 4 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/advice.c b/advice.c
index ff25087fa7e..4af5a4205de 100644
--- a/advice.c
+++ b/advice.c
@@ -29,7 +29,6 @@ int advice_ignored_hook = 1;
 int advice_waiting_for_editor = 1;
 int advice_graft_file_deprecated = 1;
 int advice_checkout_ambiguous_remote_branch_name = 1;
-int advice_nested_tag = 1;
 int advice_submodule_alternate_error_strategy_die = 1;
 
 static int advice_use_color = -1;
@@ -89,7 +88,6 @@ static struct {
 	{ "waitingForEditor", &advice_waiting_for_editor },
 	{ "graftFileDeprecated", &advice_graft_file_deprecated },
 	{ "checkoutAmbiguousRemoteBranchName", &advice_checkout_ambiguous_remote_branch_name },
-	{ "nestedTag", &advice_nested_tag },
 	{ "submoduleAlternateErrorStrategyDie", &advice_submodule_alternate_error_strategy_die },
 
 	/* make this an alias for backward compatibility */
diff --git a/advice.h b/advice.h
index 61a7ee82827..c8be662c4b1 100644
--- a/advice.h
+++ b/advice.h
@@ -29,7 +29,6 @@ extern int advice_ignored_hook;
 extern int advice_waiting_for_editor;
 extern int advice_graft_file_deprecated;
 extern int advice_checkout_ambiguous_remote_branch_name;
-extern int advice_nested_tag;
 extern int advice_submodule_alternate_error_strategy_die;
 
 /**
diff --git a/builtin/tag.c b/builtin/tag.c
index e0a4c253828..45e959d5f8f 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -231,8 +231,9 @@ static void create_tag(const struct object_id *object, const char *object_ref,
 	if (type <= OBJ_NONE)
 		die(_("bad object type."));
 
-	if (type == OBJ_TAG && advice_nested_tag)
-		advise(_(message_advice_nested_tag), tag, object_ref);
+	if (type == OBJ_TAG)
+		advise_if_enabled(NESTED_TAG, _(message_advice_nested_tag),
+				  tag, object_ref);
 
 	strbuf_addf(&header,
 		    "object %s\n"
diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
index 6db92bd3ba6..74b637deb25 100755
--- a/t/t7004-tag.sh
+++ b/t/t7004-tag.sh
@@ -1726,6 +1726,7 @@ test_expect_success 'recursive tagging should give advice' '
 	hint: already a tag. If you meant to tag the object that it points to, use:
 	hint: |
 	hint: 	git tag -f nested annotated-v4.0^{}
+	hint: Disable this message with "git config advice.nestedTag false"
 	EOF
 	git tag -m nested nested annotated-v4.0 2>actual &&
 	test_i18ncmp expect actual
-- 
gitgitgadget

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

* Re: [PATCH v4 1/3] advice: extract vadvise() from advise()
  2020-02-24 15:13       ` [PATCH v4 1/3] advice: extract vadvise() from advise() Heba Waly via GitGitGadget
@ 2020-02-24 22:04         ` Emily Shaffer
  0 siblings, 0 replies; 50+ messages in thread
From: Emily Shaffer @ 2020-02-24 22:04 UTC (permalink / raw)
  To: Heba Waly via GitGitGadget; +Cc: git, Heba Waly

On Mon, Feb 24, 2020 at 03:13:16PM +0000, Heba Waly via GitGitGadget wrote:
> From: Heba Waly <heba.waly@gmail.com>
> 
> In preparation for a new advice method, extract a version of advise()
> that uses an explict 'va_list' parameter. Call it from advise() for a
> functionally equivalent version.
> 
> Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
> Signed-off-by: Heba Waly <heba.waly@gmail.com>

This seems very straightforward and now appears to be in the right
commit order.

Reviewed-by: Emily Shaffer <emilyshaffer@google.com>

> ---
>  advice.c | 13 +++++++++----
>  1 file changed, 9 insertions(+), 4 deletions(-)
> 
> diff --git a/advice.c b/advice.c
> index 249c60dcf32..fd836332dad 100644
> --- a/advice.c
> +++ b/advice.c
> @@ -96,15 +96,12 @@ static struct {
>  	{ "pushNonFastForward", &advice_push_update_rejected }
>  };
>  
> -void advise(const char *advice, ...)
> +static void vadvise(const char *advice, va_list params)
>  {
>  	struct strbuf buf = STRBUF_INIT;
> -	va_list params;
>  	const char *cp, *np;
>  
> -	va_start(params, advice);
>  	strbuf_vaddf(&buf, advice, params);
> -	va_end(params);
>  
>  	for (cp = buf.buf; *cp; cp = np) {
>  		np = strchrnul(cp, '\n');
> @@ -118,6 +115,14 @@ void advise(const char *advice, ...)
>  	strbuf_release(&buf);
>  }
>  
> +void advise(const char *advice, ...)
> +{
> +	va_list params;
> +	va_start(params, advice);
> +	vadvise(advice, params);
> +	va_end(params);
> +}
> +
>  int git_default_advice_config(const char *var, const char *value)
>  {
>  	const char *k, *slot_name;
> -- 
> gitgitgadget
> 

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

* Re: [PATCH v4 2/3] advice: revamp advise API
  2020-02-24 15:13       ` [PATCH v4 2/3] advice: revamp advise API Heba Waly via GitGitGadget
@ 2020-02-24 22:05         ` Junio C Hamano
  2020-02-24 22:11           ` Eric Sunshine
  2020-02-24 23:49           ` Heba Waly
  2020-02-24 23:45         ` Emily Shaffer
  1 sibling, 2 replies; 50+ messages in thread
From: Junio C Hamano @ 2020-02-24 22:05 UTC (permalink / raw)
  To: Heba Waly via GitGitGadget; +Cc: git, Heba Waly

"Heba Waly via GitGitGadget" <gitgitgadget@gmail.com> writes:

> -static void vadvise(const char *advice, va_list params)
> +static const char *advice_config_keys[] = {
> +	[FETCH_SHOW_FORCED_UPDATES]		 = "fetchShowForcedUpdates",
> +	[PUSH_UPDATE_REJECTED]			 = "pushUpdateRejected",
> +	/* make this an alias for backward compatibility */
> +	[PUSH_UPDATE_REJECTED_ALIAS]		 = "pushNonFastForward",
> +...
> +	[CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME]	 = "checkoutAmbiguousRemoteBranchName",
> +	[NESTED_TAG]				 = "nestedTag",
> +	[SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = "submoduleAlternateErrorStrategyDie"
> +};

Terminate the last entry of the array with a trailing comma ',', so
that the next person who adds one new advise key to the table at the
end has to just add only one line without changing any existing lines.

As you are using the designated initializers for this array, we are
free to order the lines in any way that makes most sense to us and
do not have to order the lines in numerical order.  In what order
are these lines sorted right now?  I am tempted to suggest that we
should sort alphabetically on the values, i.e. run the contents of
the table through "LC_ALL=C sort -k2,2 -t=".

> +
> +static const char turn_off_instructions[] =
> +N_("\n"
> +   "Disable this message with \"git config %s false\"");
> +
> +static void vadvise(const char *advice, va_list params,
> +		    int display_instructions, char *key)

It may be just me, but I feel uneasy when I see va_list in the
middle of the parameter list.  As it is a mechanism to allow us
handle "the remainder of the arguments", it logically makes more
sense to have it as the last parameter.

>  {
>  	struct strbuf buf = STRBUF_INIT;
>  	const char *cp, *np;
>  
>  	strbuf_vaddf(&buf, advice, params);
>  
> +	if(display_instructions)
> +		strbuf_addf(&buf, turn_off_instructions, key);

Style.  We always have one SP between a syntactic keyword like "if"
and open parenthesis.

> +
>  	for (cp = buf.buf; *cp; cp = np) {
>  		np = strchrnul(cp, '\n');
>  		fprintf(stderr,	_("%shint: %.*s%s\n"),
> @@ -119,8 +161,42 @@ void advise(const char *advice, ...)
>  {
>  	va_list params;
>  	va_start(params, advice);
> -	vadvise(advice, params);
> +	vadvise(advice, params, 0, "");
> +	va_end(params);
> +}
> +
> +static int get_config_value(enum advice_type type)
> +{
> +	int value = 1;
> +	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);

Have a blank line between the decl and the first statement, i.e. here.

> +	git_config_get_bool(key, &value);
> +	free(key);
> +	return value;
> +}
> +
> +int advice_enabled(enum advice_type type)
> +{
> +	switch(type) {

Style.

> +	case PUSH_UPDATE_REJECTED:
> +		return get_config_value(PUSH_UPDATE_REJECTED) &&
> +		       get_config_value(PUSH_UPDATE_REJECTED_ALIAS);
> +	default:
> +		return get_config_value(type);
> +	}
> +}
> +
> +void advise_if_enabled(enum advice_type type, const char *advice, ...)
> +{
> +	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
> +	va_list params;
> +
> +	if(!advice_enabled(type))

Stile.

> +		return;
> +
> +	va_start(params, advice);
> +	vadvise(advice, params, 1, key);
>  	va_end(params);
> +	free(key);
>  }
>  
>  int git_default_advice_config(const char *var, const char *value)
> @@ -159,8 +235,8 @@ void list_config_advices(struct string_list *list, const char *prefix)
>  {
>  	int i;
>  
> -	for (i = 0; i < ARRAY_SIZE(advice_config); i++)
> -		list_config_item(list, prefix, advice_config[i].name);
> +	for (i = 0; i < ARRAY_SIZE(advice_config_keys); i++)
> +		list_config_item(list, prefix, advice_config_keys[i]);
>  }
>  
>  int error_resolve_conflict(const char *me)
> diff --git a/advice.h b/advice.h
> index b706780614d..61a7ee82827 100644
> --- a/advice.h
> +++ b/advice.h
> @@ -32,9 +32,61 @@ extern int advice_checkout_ambiguous_remote_branch_name;
>  extern int advice_nested_tag;
>  extern int advice_submodule_alternate_error_strategy_die;
>  
> +/**
> + To add a new advice, you need to:
> + - Define an advice_type.
> + - Add a new entry to advice_config_keys list.
> + - Add the new config variable to Documentation/config/advice.txt.
> + - Call advise_if_enabled to print your advice.
> + */

    /*
     * Our multi-line comments should look
     * more like this (multiple style violations
     * in this patch).
     */

> +enum advice_type {
> +	FETCH_SHOW_FORCED_UPDATES = 0,
> +	PUSH_UPDATE_REJECTED = 1,
> +	PUSH_UPDATE_REJECTED_ALIAS = 2,
> +	PUSH_NON_FF_CURRENT = 3,

Do we need to spell out the values, or is it sufficient to let the
compiler automatically count up?  Does any code depend on the exact
numeric value of the advice type, or at least at the source code
level the only thing we care about them is that they are distinct?

I'd really want to get rid of these exact value assignments---they
are source of unnecessary conflicts when two or more topics want to
add new advice types of their own.

I also suggest that these are sorted alphabetically.

> + ...
> +	SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE = 28,
> +};

> +++ b/t/t0018-advice.sh
> @@ -0,0 +1,28 @@
> +#!/bin/sh
> +
> +test_description='Test advise_if_enabled functionality'
> +
> +. ./test-lib.sh
> +
> +cat > expected <<EOF
> +hint: This is a piece of advice
> +hint: Disable this message with "git config advice.nestedTag false"
> +EOF
> +test_expect_success 'advise should be printed when config variable is unset' '
> +	test-tool advise "This is a piece of advice" 2>actual &&
> +	test_i18ncmp expected actual
> +'

 - Prepare the expected output inside test_expect_success block that
   uses it.

 - There should be no SP between a redirection operator and the
   filename.

 - Here-doc that does not use parameter expansion should use a
   quoted EOF marker.

 - The file that gets compared with "actual" is by convention called
   "expect", not "expected".

i.e.

test_expect_success 'advise should be printed when config variable is unset' '
	cat >expect <<-\EOF &&
	hint: ...
	hint: ...
	EOF
	test-tool advise "This is a piece of advice" 2>actual &&
	test_i18ncmp expected actual
'

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

* Re: [PATCH v4 3/3] tag: use new advice API to check visibility
  2020-02-24 15:13       ` [PATCH v4 3/3] tag: use new advice API to check visibility Heba Waly via GitGitGadget
@ 2020-02-24 22:07         ` Junio C Hamano
  2020-02-24 23:46         ` Emily Shaffer
  1 sibling, 0 replies; 50+ messages in thread
From: Junio C Hamano @ 2020-02-24 22:07 UTC (permalink / raw)
  To: Heba Waly via GitGitGadget; +Cc: git, Heba Waly

"Heba Waly via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Heba Waly <heba.waly@gmail.com>
>
> Following the new helpers added to the advice library,
> replace the global variable check approach by the new
> API calls
>
> Signed-off-by: Heba Waly <heba.waly@gmail.com>
> ---
>  advice.c       | 2 --
>  advice.h       | 1 -
>  builtin/tag.c  | 5 +++--
>  t/t7004-tag.sh | 1 +
>  4 files changed, 4 insertions(+), 5 deletions(-)

Nice.

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

* Re: [PATCH v4 2/3] advice: revamp advise API
  2020-02-24 22:05         ` Junio C Hamano
@ 2020-02-24 22:11           ` Eric Sunshine
  2020-02-24 23:51             ` Heba Waly
  2020-02-24 23:49           ` Heba Waly
  1 sibling, 1 reply; 50+ messages in thread
From: Eric Sunshine @ 2020-02-24 22:11 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Heba Waly via GitGitGadget, Git List, Heba Waly

On Mon, Feb 24, 2020 at 5:05 PM Junio C Hamano <gitster@pobox.com> wrote:
> "Heba Waly via GitGitGadget" <gitgitgadget@gmail.com> writes:
> > +test_expect_success 'advise should be printed when config variable is unset' '
> > +     test-tool advise "This is a piece of advice" 2>actual &&
> > +     test_i18ncmp expected actual
> > +'
>
>  - Prepare the expected output inside test_expect_success block that
>    uses it.
>  - There should be no SP between a redirection operator and the
>    filename.
>  - Here-doc that does not use parameter expansion should use a
>    quoted EOF marker.
>  - The file that gets compared with "actual" is by convention called
>    "expect", not "expected".
>
> test_expect_success 'advise should be printed when config variable is unset' '

Also, s/advise/advice/ in the test title.

>         cat >expect <<-\EOF &&
>         hint: ...
>         hint: ...
>         EOF
>         test-tool advise "This is a piece of advice" 2>actual &&
>         test_i18ncmp expected actual
> '

s/expected/expect/

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

* Re: [PATCH v4 2/3] advice: revamp advise API
  2020-02-24 15:13       ` [PATCH v4 2/3] advice: revamp advise API Heba Waly via GitGitGadget
  2020-02-24 22:05         ` Junio C Hamano
@ 2020-02-24 23:45         ` Emily Shaffer
  1 sibling, 0 replies; 50+ messages in thread
From: Emily Shaffer @ 2020-02-24 23:45 UTC (permalink / raw)
  To: Heba Waly via GitGitGadget; +Cc: git, Heba Waly

On Mon, Feb 24, 2020 at 03:13:17PM +0000, Heba Waly via GitGitGadget wrote:
> From: Heba Waly <heba.waly@gmail.com>
> 
> Currently it's very easy for the advice library's callers to miss
> checking the visibility step before printing an advice. Also, it makes
> more sense for this step to be handled by the advice library.
> 
> Add a new advise_if_enabled function that checks the visibility of
> advice messages before printing.
> 
> Add a new helper advise_enabled to check the visibility of the advice
> if the caller needs to carry out complicated processing based on that
> value.
> 
> A list of config variables 'advice_config_keys' is added to be used by
> list_config_advices() instead of 'advice_config[]' because we'll get
> rid of 'advice_config[]' and the global variables once we migrate all
> the callers to use the new APIs.
> 
> Also change the advise call in tag library from advise() to
> advise_if_enabled() to construct an example of the usage of the new
> API.
> 
> Signed-off-by: Heba Waly <heba.waly@gmail.com>

I read Junio's review and agree with that too; but here are some more
thoughts.

> +static int get_config_value(enum advice_type type)
> +{
> +	int value = 1;

So we default to true if the config is unset...

> +	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
> +	git_config_get_bool(key, &value);
...and per config.h, "when the configuration variable `key` is now
found, returns 1 without touching `dest`. Nice, so the default-true
works. If some problem is found when converting the value to a bool,
this function die()s, so you don't have to check the return value.

> +	free(key);
> +	return value;
> +}
> +
> +int advice_enabled(enum advice_type type)
> +{
> +	switch(type) {
> +	case PUSH_UPDATE_REJECTED:
> +		return get_config_value(PUSH_UPDATE_REJECTED) &&
> +		       get_config_value(PUSH_UPDATE_REJECTED_ALIAS);
So I can ask advice_enabled(PUSH_UPDATE_REJECTED) and still be told
'false' if earlier I set "advice.pushNonfastForward" to 0.

I wondered if this was really identical behavior to how this thing
worked before.

Before, it looks like we use the older config callback method
(git_default_advice_config()): we read each config option the user has,
in order, and then we check it against each member of advice_config, and
if it's a match, we set the appropriate value. That means that
advice_push_update_rejected is determined by whichever config is set
last, e.g. a config like so:

global: advice.pushUpdateRejected = 1
local:  advice.pushNonFastForward = 0

results in advice_push_update_rejected == 0.

Now, though, you consider the values of both. In the example above, you
have the same value; but if you reverse the values:

global: advice.pushUpdateRejected = 0
local:  advice.pushNonFastForward = 1

then your new code says advice_enabled(PUSH_UPDATE_REJECTED) == 0, and
the old codepath says advice_push_update_rejected = 1.

Although, using the config lookup methods as you are, I don't think you
can make it stay the same - I don't think there's a way to compare the
relative position of two different configs, is there?

Is this such a big deal? My gut says no; my gut says if someone had
advice.pushNonFastForward set, they aren't touching their config to
unset it, which means they aren't touching their config to fiddle with
advice.pushUpdateRejected either.

Maybe someone who was around when this alias was added can provide some
context?

 - Emily

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

* Re: [PATCH v4 3/3] tag: use new advice API to check visibility
  2020-02-24 15:13       ` [PATCH v4 3/3] tag: use new advice API to check visibility Heba Waly via GitGitGadget
  2020-02-24 22:07         ` Junio C Hamano
@ 2020-02-24 23:46         ` Emily Shaffer
  1 sibling, 0 replies; 50+ messages in thread
From: Emily Shaffer @ 2020-02-24 23:46 UTC (permalink / raw)
  To: Heba Waly via GitGitGadget; +Cc: git, Heba Waly

On Mon, Feb 24, 2020 at 03:13:18PM +0000, Heba Waly via GitGitGadget wrote:
> From: Heba Waly <heba.waly@gmail.com>
> 
> Following the new helpers added to the advice library,
> replace the global variable check approach by the new
> API calls
> 
> Signed-off-by: Heba Waly <heba.waly@gmail.com>
> ---
>  advice.c       | 2 --
>  advice.h       | 1 -
>  builtin/tag.c  | 5 +++--
>  t/t7004-tag.sh | 1 +
>  4 files changed, 4 insertions(+), 5 deletions(-)

More deleted lines than added lines always makes me a little happier ;)

Reviewed-by: Emily Shaffer <emilyshaffer@google.com>

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

* Re: [PATCH v4 2/3] advice: revamp advise API
  2020-02-24 22:05         ` Junio C Hamano
  2020-02-24 22:11           ` Eric Sunshine
@ 2020-02-24 23:49           ` Heba Waly
  1 sibling, 0 replies; 50+ messages in thread
From: Heba Waly @ 2020-02-24 23:49 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Heba Waly via GitGitGadget, Git Mailing List

On Tue, Feb 25, 2020 at 11:05 AM Junio C Hamano <gitster@pobox.com> wrote:
>
> "Heba Waly via GitGitGadget" <gitgitgadget@gmail.com> writes:
>
> > -static void vadvise(const char *advice, va_list params)
> > +static const char *advice_config_keys[] = {
> > +     [FETCH_SHOW_FORCED_UPDATES]              = "fetchShowForcedUpdates",
> > +     [PUSH_UPDATE_REJECTED]                   = "pushUpdateRejected",
> > +     /* make this an alias for backward compatibility */
> > +     [PUSH_UPDATE_REJECTED_ALIAS]             = "pushNonFastForward",
> > +...
> > +     [CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME]  = "checkoutAmbiguousRemoteBranchName",
> > +     [NESTED_TAG]                             = "nestedTag",
> > +     [SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = "submoduleAlternateErrorStrategyDie"
> > +};
>
> Terminate the last entry of the array with a trailing comma ',', so
> that the next person who adds one new advise key to the table at the
> end has to just add only one line without changing any existing lines.
>

Sure, thank you for the explanation, that makes sense.

> As you are using the designated initializers for this array, we are
> free to order the lines in any way that makes most sense to us and
> do not have to order the lines in numerical order.  In what order
> are these lines sorted right now?  I am tempted to suggest that we
> should sort alphabetically on the values, i.e. run the contents of
> the table through "LC_ALL=C sort -k2,2 -t=".
>

Currently it's sorted in the same order that was originally used for
advice_config[] and the global variables, which I assume is the order
in which every config variable was added to the code. Sorting
alphabetically will be neater of course.

> > +
> > +static const char turn_off_instructions[] =
> > +N_("\n"
> > +   "Disable this message with \"git config %s false\"");
> > +
> > +static void vadvise(const char *advice, va_list params,
> > +                 int display_instructions, char *key)
>
> It may be just me, but I feel uneasy when I see va_list in the
> middle of the parameter list.  As it is a mechanism to allow us
> handle "the remainder of the arguments", it logically makes more
> sense to have it as the last parameter.
>

I don't mind it either way, so yeah no problem, I'll change that.

> >  {
> >       struct strbuf buf = STRBUF_INIT;
> >       const char *cp, *np;
> >
> >       strbuf_vaddf(&buf, advice, params);
> >
> > +     if(display_instructions)
> > +             strbuf_addf(&buf, turn_off_instructions, key);
>
> Style.  We always have one SP between a syntactic keyword like "if"
> and open parenthesis.

Noted.

>
> > +
> >       for (cp = buf.buf; *cp; cp = np) {
> >               np = strchrnul(cp, '\n');
> >               fprintf(stderr, _("%shint: %.*s%s\n"),
> > @@ -119,8 +161,42 @@ void advise(const char *advice, ...)
> >  {
> >       va_list params;
> >       va_start(params, advice);
> > -     vadvise(advice, params);
> > +     vadvise(advice, params, 0, "");
> > +     va_end(params);
> > +}
> > +
> > +static int get_config_value(enum advice_type type)
> > +{
> > +     int value = 1;
> > +     char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
>
> Have a blank line between the decl and the first statement, i.e. here.
>

Right, I missed this one.

> > +     git_config_get_bool(key, &value);
> > +     free(key);
> > +     return value;
> > +}
> > +
> > +int advice_enabled(enum advice_type type)
> > +{
> > +     switch(type) {
>
> Style.

Noted.

>
> > +     case PUSH_UPDATE_REJECTED:
> > +             return get_config_value(PUSH_UPDATE_REJECTED) &&
> > +                    get_config_value(PUSH_UPDATE_REJECTED_ALIAS);
> > +     default:
> > +             return get_config_value(type);
> > +     }
> > +}
> > +
> > +void advise_if_enabled(enum advice_type type, const char *advice, ...)
> > +{
> > +     char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
> > +     va_list params;
> > +
> > +     if(!advice_enabled(type))
>
> Stile.

Takes time changing the style I've been using for years, sorry.

>
> > +             return;
> > +
> > +     va_start(params, advice);
> > +     vadvise(advice, params, 1, key);
> >       va_end(params);
> > +     free(key);
> >  }
> >
> >  int git_default_advice_config(const char *var, const char *value)
> > @@ -159,8 +235,8 @@ void list_config_advices(struct string_list *list, const char *prefix)
> >  {
> >       int i;
> >
> > -     for (i = 0; i < ARRAY_SIZE(advice_config); i++)
> > -             list_config_item(list, prefix, advice_config[i].name);
> > +     for (i = 0; i < ARRAY_SIZE(advice_config_keys); i++)
> > +             list_config_item(list, prefix, advice_config_keys[i]);
> >  }
> >
> >  int error_resolve_conflict(const char *me)
> > diff --git a/advice.h b/advice.h
> > index b706780614d..61a7ee82827 100644
> > --- a/advice.h
> > +++ b/advice.h
> > @@ -32,9 +32,61 @@ extern int advice_checkout_ambiguous_remote_branch_name;
> >  extern int advice_nested_tag;
> >  extern int advice_submodule_alternate_error_strategy_die;
> >
> > +/**
> > + To add a new advice, you need to:
> > + - Define an advice_type.
> > + - Add a new entry to advice_config_keys list.
> > + - Add the new config variable to Documentation/config/advice.txt.
> > + - Call advise_if_enabled to print your advice.
> > + */
>
>     /*
>      * Our multi-line comments should look
>      * more like this (multiple style violations
>      * in this patch).
>      */
>

Got it.

> > +enum advice_type {
> > +     FETCH_SHOW_FORCED_UPDATES = 0,
> > +     PUSH_UPDATE_REJECTED = 1,
> > +     PUSH_UPDATE_REJECTED_ALIAS = 2,
> > +     PUSH_NON_FF_CURRENT = 3,
>
> Do we need to spell out the values, or is it sufficient to let the
> compiler automatically count up?  Does any code depend on the exact
> numeric value of the advice type, or at least at the source code
> level the only thing we care about them is that they are distinct?
>
> I'd really want to get rid of these exact value assignments---they
> are source of unnecessary conflicts when two or more topics want to
> add new advice types of their own.
>

Yes, we can get rid of them.

> I also suggest that these are sorted alphabetically.

As we'll get rid of the value assignments, so sorting alphabetically
should not introduce problems when adding new advice types, will do.

> > + ...
> > +     SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE = 28,
> > +};
>
> > +++ b/t/t0018-advice.sh
> > @@ -0,0 +1,28 @@
> > +#!/bin/sh
> > +
> > +test_description='Test advise_if_enabled functionality'
> > +
> > +. ./test-lib.sh
> > +
> > +cat > expected <<EOF
> > +hint: This is a piece of advice
> > +hint: Disable this message with "git config advice.nestedTag false"
> > +EOF
> > +test_expect_success 'advise should be printed when config variable is unset' '
> > +     test-tool advise "This is a piece of advice" 2>actual &&
> > +     test_i18ncmp expected actual
> > +'
>
>  - Prepare the expected output inside test_expect_success block that
>    uses it.
>
>  - There should be no SP between a redirection operator and the
>    filename.
>
>  - Here-doc that does not use parameter expansion should use a
>    quoted EOF marker.
>
>  - The file that gets compared with "actual" is by convention called
>    "expect", not "expected".
>
> i.e.
>
> test_expect_success 'advise should be printed when config variable is unset' '
>         cat >expect <<-\EOF &&
>         hint: ...
>         hint: ...
>         EOF
>         test-tool advise "This is a piece of advice" 2>actual &&
>         test_i18ncmp expected actual
> '

Noted.

Will send an updated version soon.

Thank you,
Heba

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

* Re: [PATCH v4 2/3] advice: revamp advise API
  2020-02-24 22:11           ` Eric Sunshine
@ 2020-02-24 23:51             ` Heba Waly
  0 siblings, 0 replies; 50+ messages in thread
From: Heba Waly @ 2020-02-24 23:51 UTC (permalink / raw)
  To: Eric Sunshine; +Cc: Junio C Hamano, Heba Waly via GitGitGadget, Git List

On Tue, Feb 25, 2020 at 11:11 AM Eric Sunshine <sunshine@sunshineco.com> wrote:
>
> On Mon, Feb 24, 2020 at 5:05 PM Junio C Hamano <gitster@pobox.com> wrote:
> > "Heba Waly via GitGitGadget" <gitgitgadget@gmail.com> writes:
> > > +test_expect_success 'advise should be printed when config variable is unset' '
> > > +     test-tool advise "This is a piece of advice" 2>actual &&
> > > +     test_i18ncmp expected actual
> > > +'
> >
> >  - Prepare the expected output inside test_expect_success block that
> >    uses it.
> >  - There should be no SP between a redirection operator and the
> >    filename.
> >  - Here-doc that does not use parameter expansion should use a
> >    quoted EOF marker.
> >  - The file that gets compared with "actual" is by convention called
> >    "expect", not "expected".
> >
> > test_expect_success 'advise should be printed when config variable is unset' '
>
> Also, s/advise/advice/ in the test title.
>
> >         cat >expect <<-\EOF &&
> >         hint: ...
> >         hint: ...
> >         EOF
> >         test-tool advise "This is a piece of advice" 2>actual &&
> >         test_i18ncmp expected actual
> > '
>
> s/expected/expect/

Noted, thank you.

Heba

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

* [PATCH v5 0/3] [Outreachy] advice: revamp advise API
  2020-02-24 15:13     ` [PATCH v4 0/3] [Outreachy] advice: revamp advise API Heba Waly via GitGitGadget
                         ` (2 preceding siblings ...)
  2020-02-24 15:13       ` [PATCH v4 3/3] tag: use new advice API to check visibility Heba Waly via GitGitGadget
@ 2020-02-25 10:55       ` Heba Waly via GitGitGadget
  2020-02-25 10:55         ` [PATCH v5 1/3] advice: extract vadvise() from advise() Heba Waly via GitGitGadget
                           ` (2 more replies)
  3 siblings, 3 replies; 50+ messages in thread
From: Heba Waly via GitGitGadget @ 2020-02-25 10:55 UTC (permalink / raw)
  To: git; +Cc: Heba Waly

Main changes in V4:

 * Re-order the commits.
 * Free the output after using xstrfmt().


----------------------------------------------------------------------------

Changes in V3:

 * Remove the new wrapper advice_push_update_rejected_enabled() (which was
   added in V2 to handle a special case of having a config variable alias),
   and replace it by adding switch cases to advice_enabled() (The reason
   behind this change is that another special case came up while I was
   migrating the rest of the advise calls to the new APIs.)
 * Remove trailing whitespaces.


----------------------------------------------------------------------------

Main changes in V2:

 * Rename advise_ng to advise_if_enabled.
 * Add a new advise_enabled() helper.
 * Add a list of config variables names to replace advice_config[] (used by
   list_config_advices()).
 * Send an enum parameter to the new advise helpers instead of strings.
 * Extract vadvise() from advise() and advise_if enabled().


----------------------------------------------------------------------------

The advice API is currently a little bit confusing to call. quoting from
[1]:

When introducing a new advice message, you would

 * come up with advice.frotz configuration variable

 * define and declare advice_frotz global variable that defaults to
   true

 * sprinkle calls like this:

  if (advice_frotz)
    advise(_("helpful message about frotz"));

A new approach was suggested in [1] which this patch is based upon.

A new advise_if_enabled() is introduced to gradually replace advise()
advice_enabled() helper is also introduced to be used by those callers who:

 * Only need to check the visibility without calling advise() (they call
   die() or error() instead for example)
 * Need to carry out some heavy processing to display an advice, in this
   case they'll do: if(advice_enabled(advice_type))  advise("some advice message");
   
   

To introduce a new advice message, the caller needs to:

 * Add a new_advice_type to 'enum advice_type'
 * Come up with a new config variable name and add this name to 
   advice_config_keys[]
 * Call advise_if_enabled(new_advice_type, "advice message to be printed")
 * Or call advice_enabled(new_advice_type) first and then follow is by
   advice("advice message to be printed") as explained earlier.
 * Add the new config variable to Documentation/config/advice.txt

The reason a new list of configuration variables was added to the library is
to be used by the list_config_advices() function instead of advice_config[].
And we should get rid of advice_config[] once we migrate all the callers to
use the new APIs instead of checking the global variables (which we'll get
rid of as well).

In the future, we can investigate generating the documentation from the list
of config variables or vice versa to make introducing a new advice much
easier, but this approach will do it for now.

V2 makes the process of introducing a new advice longer than V1 and almost
as long as the original library, but having the advice library responsible
for checking the message visibility is still an improvement and in my own
opinion the new structure makes better sense and makes the library less
confusing to use.

After this patch the plan is to change the advise() calls to
advise_if_enabled() whenever possible, or at least replace the global
variables checks by advise_enabled() when advise_if_enabled() is not
suitable.

[1] https://public-inbox.org/git/xmqqzhf5cw69.fsf@gitster-ct.c.googlers.com/

Heba Waly (3):
  advice: extract vadvise() from advise()
  advice: revamp advise API
  tag: use new advice API to check visibility

 Makefile               |  1 +
 advice.c               | 97 ++++++++++++++++++++++++++++++++++++++----
 advice.h               | 53 ++++++++++++++++++++++-
 builtin/tag.c          |  5 ++-
 t/helper/test-advise.c | 19 +++++++++
 t/helper/test-tool.c   |  1 +
 t/helper/test-tool.h   |  1 +
 t/t0018-advice.sh      | 32 ++++++++++++++
 t/t7004-tag.sh         |  1 +
 9 files changed, 199 insertions(+), 11 deletions(-)
 create mode 100644 t/helper/test-advise.c
 create mode 100755 t/t0018-advice.sh


base-commit: c7a62075917b3340f908093f63f1161c44ed1475
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-548%2FHebaWaly%2Fadvice_refactoring-v5
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-548/HebaWaly/advice_refactoring-v5
Pull-Request: https://github.com/gitgitgadget/git/pull/548

Range-diff vs v4:

 1:  f668d9b7ca0 = 1:  f668d9b7ca0 advice: extract vadvise() from advise()
 2:  04c3e5760f6 ! 2:  b7f10d060a4 advice: revamp advise API
     @@ -45,52 +45,53 @@
       
      -static void vadvise(const char *advice, va_list params)
      +static const char *advice_config_keys[] = {
     ++	[ADD_EMBEDDED_REPO]			 = "addEmbeddedRepo",
     ++	[AMWORKDIR]				 = "amWorkDir",
     ++	[CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME]	 = "checkoutAmbiguousRemoteBranchName",
     ++	[COMMIT_BEFORE_MERGE]			 = "commitBeforeMerge",
     ++	[DETACHED_HEAD]				 = "detachedHead",
      +	[FETCH_SHOW_FORCED_UPDATES]		 = "fetchShowForcedUpdates",
     -+	[PUSH_UPDATE_REJECTED]			 = "pushUpdateRejected",
     ++	[GRAFT_FILE_DEPRECATED]			 = "graftFileDeprecated",
     ++	[IGNORED_HOOK]				 = "ignoredHook",
     ++	[IMPLICIT_IDENTITY]			 = "implicitIdentity",
     ++	[NESTED_TAG]				 = "nestedTag",
     ++	[OBJECT_NAME_WARNING]			 = "objectNameWarning",
     ++	[PUSH_ALREADY_EXISTS]			 = "pushAlreadyExists",
     ++	[PUSH_FETCH_FIRST]			 = "pushFetchFirst",
     ++	[PUSH_NEEDS_FORCE]			 = "pushNeedsForce",
     ++
      +	/* make this an alias for backward compatibility */
      +	[PUSH_UPDATE_REJECTED_ALIAS]		 = "pushNonFastForward",
      +
      +	[PUSH_NON_FF_CURRENT]			 = "pushNonFFCurrent",
      +	[PUSH_NON_FF_MATCHING]			 = "pushNonFFMatching",
     -+	[PUSH_ALREADY_EXISTS]			 = "pushAlreadyExists",
     -+	[PUSH_FETCH_FIRST]			 = "pushFetchFirst",
     -+	[PUSH_NEEDS_FORCE]			 = "pushNeedsForce",
      +	[PUSH_UNQUALIFIED_REF_NAME]		 = "pushUnqualifiedRefName",
     -+	[STATUS_HINTS]				 = "statusHints",
     -+	[STATUS_U_OPTION]			 = "statusUoption",
     -+	[STATUS_AHEAD_BEHIND_WARNING]		 = "statusAheadBehindWarning",
     -+	[COMMIT_BEFORE_MERGE]			 = "commitBeforeMerge",
     ++	[PUSH_UPDATE_REJECTED]			 = "pushUpdateRejected",
      +	[RESET_QUIET_WARNING]			 = "resetQuiet",
      +	[RESOLVE_CONFLICT]			 = "resolveConflict",
     ++	[RM_HINTS]				 = "rmHints",
      +	[SEQUENCER_IN_USE]			 = "sequencerInUse",
     -+	[IMPLICIT_IDENTITY]			 = "implicitIdentity",
     -+	[DETACHED_HEAD]				 = "detachedHead",
      +	[SET_UPSTREAM_FAILURE]			 = "setupStreamFailure",
     -+	[OBJECT_NAME_WARNING]			 = "objectNameWarning",
     -+	[AMWORKDIR]				 = "amWorkDir",
     -+	[RM_HINTS]				 = "rmHints",
     -+	[ADD_EMBEDDED_REPO]			 = "addEmbeddedRepo",
     -+	[IGNORED_HOOK]				 = "ignoredHook",
     ++	[STATUS_AHEAD_BEHIND_WARNING]		 = "statusAheadBehindWarning",
     ++	[STATUS_HINTS]				 = "statusHints",
     ++	[STATUS_U_OPTION]			 = "statusUoption",
     ++	[SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = "submoduleAlternateErrorStrategyDie",
      +	[WAITING_FOR_EDITOR] 			 = "waitingForEditor",
     -+	[GRAFT_FILE_DEPRECATED]			 = "graftFileDeprecated",
     -+	[CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME]	 = "checkoutAmbiguousRemoteBranchName",
     -+	[NESTED_TAG]				 = "nestedTag",
     -+	[SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = "submoduleAlternateErrorStrategyDie"
      +};
      +
      +static const char turn_off_instructions[] =
      +N_("\n"
      +   "Disable this message with \"git config %s false\"");
      +
     -+static void vadvise(const char *advice, va_list params,
     -+		    int display_instructions, char *key)
     ++static void vadvise(const char *advice, int display_instructions,
     ++		    char *key, va_list params)
       {
       	struct strbuf buf = STRBUF_INIT;
       	const char *cp, *np;
       
       	strbuf_vaddf(&buf, advice, params);
       
     -+	if(display_instructions)
     ++	if (display_instructions)
      +		strbuf_addf(&buf, turn_off_instructions, key);
      +
       	for (cp = buf.buf; *cp; cp = np) {
     @@ -101,7 +102,7 @@
       	va_list params;
       	va_start(params, advice);
      -	vadvise(advice, params);
     -+	vadvise(advice, params, 0, "");
     ++	vadvise(advice, 0, "", params);
      +	va_end(params);
      +}
      +
     @@ -109,6 +110,7 @@
      +{
      +	int value = 1;
      +	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
     ++
      +	git_config_get_bool(key, &value);
      +	free(key);
      +	return value;
     @@ -116,7 +118,7 @@
      +
      +int advice_enabled(enum advice_type type)
      +{
     -+	switch(type) {
     ++	switch (type) {
      +	case PUSH_UPDATE_REJECTED:
      +		return get_config_value(PUSH_UPDATE_REJECTED) &&
      +		       get_config_value(PUSH_UPDATE_REJECTED_ALIAS);
     @@ -130,11 +132,11 @@
      +	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
      +	va_list params;
      +
     -+	if(!advice_enabled(type))
     ++	if (!advice_enabled(type))
      +		return;
      +
      +	va_start(params, advice);
     -+	vadvise(advice, params, 1, key);
     ++	vadvise(advice, 1, key, params);
       	va_end(params);
      +	free(key);
       }
     @@ -159,43 +161,43 @@
       extern int advice_nested_tag;
       extern int advice_submodule_alternate_error_strategy_die;
       
     -+/**
     -+ To add a new advice, you need to:
     -+ - Define an advice_type.
     -+ - Add a new entry to advice_config_keys list.
     -+ - Add the new config variable to Documentation/config/advice.txt.
     -+ - Call advise_if_enabled to print your advice.
     ++/*
     ++ * To add a new advice, you need to:
     ++ * Define an advice_type.
     ++ * Add a new entry to advice_config_keys list.
     ++ * Add the new config variable to Documentation/config/advice.txt.
     ++ * Call advise_if_enabled to print your advice.
      + */
      +enum advice_type {
     -+	FETCH_SHOW_FORCED_UPDATES = 0,
     -+	PUSH_UPDATE_REJECTED = 1,
     -+	PUSH_UPDATE_REJECTED_ALIAS = 2,
     -+	PUSH_NON_FF_CURRENT = 3,
     -+	PUSH_NON_FF_MATCHING = 4,
     -+	PUSH_ALREADY_EXISTS = 5,
     -+	PUSH_FETCH_FIRST = 6,
     -+	PUSH_NEEDS_FORCE = 7,
     -+	PUSH_UNQUALIFIED_REF_NAME = 8,
     -+	STATUS_HINTS = 9,
     -+	STATUS_U_OPTION = 10,
     -+	STATUS_AHEAD_BEHIND_WARNING = 11,
     -+	COMMIT_BEFORE_MERGE = 12,
     -+	RESET_QUIET_WARNING = 13,
     -+	RESOLVE_CONFLICT = 14,
     -+	SEQUENCER_IN_USE = 15,
     -+	IMPLICIT_IDENTITY = 16,
     -+	DETACHED_HEAD = 17,
     -+	SET_UPSTREAM_FAILURE = 18,
     -+	OBJECT_NAME_WARNING = 19,
     -+	AMWORKDIR = 20,
     -+	RM_HINTS = 21,
     -+	ADD_EMBEDDED_REPO = 22,
     -+	IGNORED_HOOK = 23,
     -+	WAITING_FOR_EDITOR = 24,
     -+	GRAFT_FILE_DEPRECATED = 25,
     -+	CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME = 26,
     -+	NESTED_TAG = 27,
     -+	SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE = 28,
     ++	ADD_EMBEDDED_REPO,
     ++	AMWORKDIR,
     ++	CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME,
     ++	COMMIT_BEFORE_MERGE,
     ++	DETACHED_HEAD,
     ++	FETCH_SHOW_FORCED_UPDATES,
     ++	GRAFT_FILE_DEPRECATED,
     ++	IGNORED_HOOK,
     ++	IMPLICIT_IDENTITY,
     ++	NESTED_TAG,
     ++	OBJECT_NAME_WARNING,
     ++	PUSH_ALREADY_EXISTS,
     ++	PUSH_FETCH_FIRST,
     ++	PUSH_NEEDS_FORCE,
     ++	PUSH_NON_FF_CURRENT,
     ++	PUSH_NON_FF_MATCHING,
     ++	PUSH_UNQUALIFIED_REF_NAME,
     ++	PUSH_UPDATE_REJECTED_ALIAS,
     ++	PUSH_UPDATE_REJECTED,
     ++	RESET_QUIET_WARNING,
     ++	RESOLVE_CONFLICT,
     ++	RM_HINTS,
     ++	SEQUENCER_IN_USE,
     ++	SET_UPSTREAM_FAILURE,
     ++	STATUS_AHEAD_BEHIND_WARNING,
     ++	STATUS_HINTS,
     ++	STATUS_U_OPTION,
     ++	SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE,
     ++	WAITING_FOR_EDITOR,
      +};
      +
      +
     @@ -278,22 +280,26 @@
      +
      +. ./test-lib.sh
      +
     -+cat > expected <<EOF
     -+hint: This is a piece of advice
     -+hint: Disable this message with "git config advice.nestedTag false"
     -+EOF
     -+test_expect_success 'advise should be printed when config variable is unset' '
     ++test_expect_success 'advice should be printed when config variable is unset' '
     ++	cat >expect <<-\EOF &&
     ++	hint: This is a piece of advice
     ++	hint: Disable this message with "git config advice.nestedTag false"
     ++	EOF
      +	test-tool advise "This is a piece of advice" 2>actual &&
     -+	test_i18ncmp expected actual
     ++	test_i18ncmp expect actual
      +'
      +
     -+test_expect_success 'advise should be printed when config variable is set to true' '
     ++test_expect_success 'advice should be printed when config variable is set to true' '
     ++	cat >expect <<-\EOF &&
     ++	hint: This is a piece of advice
     ++	hint: Disable this message with "git config advice.nestedTag false"
     ++	EOF
      +	test_config advice.nestedTag true &&
      +	test-tool advise "This is a piece of advice" 2>actual &&
     -+	test_i18ncmp expected actual
     ++	test_i18ncmp expect actual
      +'
      +
     -+test_expect_success 'advise should not be printed when config variable is set to false' '
     ++test_expect_success 'advice should not be printed when config variable is set to false' '
      +	test_config advice.nestedTag false &&
      +	test-tool advise "This is a piece of advice" 2>actual &&
      +	test_must_be_empty actual
 3:  3cc0a17123d ! 3:  01b195ebe1d tag: use new advice API to check visibility
     @@ -38,7 +38,7 @@
      -extern int advice_nested_tag;
       extern int advice_submodule_alternate_error_strategy_die;
       
     - /**
     + /*
      
       diff --git a/builtin/tag.c b/builtin/tag.c
       --- a/builtin/tag.c

-- 
gitgitgadget

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

* [PATCH v5 1/3] advice: extract vadvise() from advise()
  2020-02-25 10:55       ` [PATCH v5 0/3] [Outreachy] advice: revamp advise API Heba Waly via GitGitGadget
@ 2020-02-25 10:55         ` Heba Waly via GitGitGadget
  2020-02-25 10:55         ` [PATCH v5 2/3] advice: revamp advise API Heba Waly via GitGitGadget
  2020-02-25 10:55         ` [PATCH v5 3/3] tag: use new advice API to check visibility Heba Waly via GitGitGadget
  2 siblings, 0 replies; 50+ messages in thread
From: Heba Waly via GitGitGadget @ 2020-02-25 10:55 UTC (permalink / raw)
  To: git; +Cc: Heba Waly, Heba Waly

From: Heba Waly <heba.waly@gmail.com>

In preparation for a new advice method, extract a version of advise()
that uses an explict 'va_list' parameter. Call it from advise() for a
functionally equivalent version.

Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Heba Waly <heba.waly@gmail.com>
---
 advice.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/advice.c b/advice.c
index 249c60dcf32..fd836332dad 100644
--- a/advice.c
+++ b/advice.c
@@ -96,15 +96,12 @@ static struct {
 	{ "pushNonFastForward", &advice_push_update_rejected }
 };
 
-void advise(const char *advice, ...)
+static void vadvise(const char *advice, va_list params)
 {
 	struct strbuf buf = STRBUF_INIT;
-	va_list params;
 	const char *cp, *np;
 
-	va_start(params, advice);
 	strbuf_vaddf(&buf, advice, params);
-	va_end(params);
 
 	for (cp = buf.buf; *cp; cp = np) {
 		np = strchrnul(cp, '\n');
@@ -118,6 +115,14 @@ void advise(const char *advice, ...)
 	strbuf_release(&buf);
 }
 
+void advise(const char *advice, ...)
+{
+	va_list params;
+	va_start(params, advice);
+	vadvise(advice, params);
+	va_end(params);
+}
+
 int git_default_advice_config(const char *var, const char *value)
 {
 	const char *k, *slot_name;
-- 
gitgitgadget


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

* [PATCH v5 2/3] advice: revamp advise API
  2020-02-25 10:55       ` [PATCH v5 0/3] [Outreachy] advice: revamp advise API Heba Waly via GitGitGadget
  2020-02-25 10:55         ` [PATCH v5 1/3] advice: extract vadvise() from advise() Heba Waly via GitGitGadget
@ 2020-02-25 10:55         ` Heba Waly via GitGitGadget
  2020-02-25 17:40           ` Junio C Hamano
  2020-02-25 10:55         ` [PATCH v5 3/3] tag: use new advice API to check visibility Heba Waly via GitGitGadget
  2 siblings, 1 reply; 50+ messages in thread
From: Heba Waly via GitGitGadget @ 2020-02-25 10:55 UTC (permalink / raw)
  To: git; +Cc: Heba Waly, Heba Waly

From: Heba Waly <heba.waly@gmail.com>

Currently it's very easy for the advice library's callers to miss
checking the visibility step before printing an advice. Also, it makes
more sense for this step to be handled by the advice library.

Add a new advise_if_enabled function that checks the visibility of
advice messages before printing.

Add a new helper advise_enabled to check the visibility of the advice
if the caller needs to carry out complicated processing based on that
value.

A list of config variables 'advice_config_keys' is added to be used by
list_config_advices() instead of 'advice_config[]' because we'll get
rid of 'advice_config[]' and the global variables once we migrate all
the callers to use the new APIs.

Also change the advise call in tag library from advise() to
advise_if_enabled() to construct an example of the usage of the new
API.

Signed-off-by: Heba Waly <heba.waly@gmail.com>
---
 Makefile               |  1 +
 advice.c               | 86 ++++++++++++++++++++++++++++++++++++++++--
 advice.h               | 52 +++++++++++++++++++++++++
 t/helper/test-advise.c | 19 ++++++++++
 t/helper/test-tool.c   |  1 +
 t/helper/test-tool.h   |  1 +
 t/t0018-advice.sh      | 32 ++++++++++++++++
 7 files changed, 188 insertions(+), 4 deletions(-)
 create mode 100644 t/helper/test-advise.c
 create mode 100755 t/t0018-advice.sh

diff --git a/Makefile b/Makefile
index 09f98b777ca..ed923a3e818 100644
--- a/Makefile
+++ b/Makefile
@@ -695,6 +695,7 @@ X =
 
 PROGRAMS += $(patsubst %.o,git-%$X,$(PROGRAM_OBJS))
 
+TEST_BUILTINS_OBJS += test-advise.o
 TEST_BUILTINS_OBJS += test-chmtime.o
 TEST_BUILTINS_OBJS += test-config.o
 TEST_BUILTINS_OBJS += test-ctype.o
diff --git a/advice.c b/advice.c
index fd836332dad..5c2068b8f8a 100644
--- a/advice.c
+++ b/advice.c
@@ -96,13 +96,56 @@ static struct {
 	{ "pushNonFastForward", &advice_push_update_rejected }
 };
 
-static void vadvise(const char *advice, va_list params)
+static const char *advice_config_keys[] = {
+	[ADD_EMBEDDED_REPO]			 = "addEmbeddedRepo",
+	[AMWORKDIR]				 = "amWorkDir",
+	[CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME]	 = "checkoutAmbiguousRemoteBranchName",
+	[COMMIT_BEFORE_MERGE]			 = "commitBeforeMerge",
+	[DETACHED_HEAD]				 = "detachedHead",
+	[FETCH_SHOW_FORCED_UPDATES]		 = "fetchShowForcedUpdates",
+	[GRAFT_FILE_DEPRECATED]			 = "graftFileDeprecated",
+	[IGNORED_HOOK]				 = "ignoredHook",
+	[IMPLICIT_IDENTITY]			 = "implicitIdentity",
+	[NESTED_TAG]				 = "nestedTag",
+	[OBJECT_NAME_WARNING]			 = "objectNameWarning",
+	[PUSH_ALREADY_EXISTS]			 = "pushAlreadyExists",
+	[PUSH_FETCH_FIRST]			 = "pushFetchFirst",
+	[PUSH_NEEDS_FORCE]			 = "pushNeedsForce",
+
+	/* make this an alias for backward compatibility */
+	[PUSH_UPDATE_REJECTED_ALIAS]		 = "pushNonFastForward",
+
+	[PUSH_NON_FF_CURRENT]			 = "pushNonFFCurrent",
+	[PUSH_NON_FF_MATCHING]			 = "pushNonFFMatching",
+	[PUSH_UNQUALIFIED_REF_NAME]		 = "pushUnqualifiedRefName",
+	[PUSH_UPDATE_REJECTED]			 = "pushUpdateRejected",
+	[RESET_QUIET_WARNING]			 = "resetQuiet",
+	[RESOLVE_CONFLICT]			 = "resolveConflict",
+	[RM_HINTS]				 = "rmHints",
+	[SEQUENCER_IN_USE]			 = "sequencerInUse",
+	[SET_UPSTREAM_FAILURE]			 = "setupStreamFailure",
+	[STATUS_AHEAD_BEHIND_WARNING]		 = "statusAheadBehindWarning",
+	[STATUS_HINTS]				 = "statusHints",
+	[STATUS_U_OPTION]			 = "statusUoption",
+	[SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = "submoduleAlternateErrorStrategyDie",
+	[WAITING_FOR_EDITOR] 			 = "waitingForEditor",
+};
+
+static const char turn_off_instructions[] =
+N_("\n"
+   "Disable this message with \"git config %s false\"");
+
+static void vadvise(const char *advice, int display_instructions,
+		    char *key, va_list params)
 {
 	struct strbuf buf = STRBUF_INIT;
 	const char *cp, *np;
 
 	strbuf_vaddf(&buf, advice, params);
 
+	if (display_instructions)
+		strbuf_addf(&buf, turn_off_instructions, key);
+
 	for (cp = buf.buf; *cp; cp = np) {
 		np = strchrnul(cp, '\n');
 		fprintf(stderr,	_("%shint: %.*s%s\n"),
@@ -119,8 +162,43 @@ void advise(const char *advice, ...)
 {
 	va_list params;
 	va_start(params, advice);
-	vadvise(advice, params);
+	vadvise(advice, 0, "", params);
+	va_end(params);
+}
+
+static int get_config_value(enum advice_type type)
+{
+	int value = 1;
+	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
+
+	git_config_get_bool(key, &value);
+	free(key);
+	return value;
+}
+
+int advice_enabled(enum advice_type type)
+{
+	switch (type) {
+	case PUSH_UPDATE_REJECTED:
+		return get_config_value(PUSH_UPDATE_REJECTED) &&
+		       get_config_value(PUSH_UPDATE_REJECTED_ALIAS);
+	default:
+		return get_config_value(type);
+	}
+}
+
+void advise_if_enabled(enum advice_type type, const char *advice, ...)
+{
+	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
+	va_list params;
+
+	if (!advice_enabled(type))
+		return;
+
+	va_start(params, advice);
+	vadvise(advice, 1, key, params);
 	va_end(params);
+	free(key);
 }
 
 int git_default_advice_config(const char *var, const char *value)
@@ -159,8 +237,8 @@ void list_config_advices(struct string_list *list, const char *prefix)
 {
 	int i;
 
-	for (i = 0; i < ARRAY_SIZE(advice_config); i++)
-		list_config_item(list, prefix, advice_config[i].name);
+	for (i = 0; i < ARRAY_SIZE(advice_config_keys); i++)
+		list_config_item(list, prefix, advice_config_keys[i]);
 }
 
 int error_resolve_conflict(const char *me)
diff --git a/advice.h b/advice.h
index b706780614d..a8461a362a3 100644
--- a/advice.h
+++ b/advice.h
@@ -32,9 +32,61 @@ extern int advice_checkout_ambiguous_remote_branch_name;
 extern int advice_nested_tag;
 extern int advice_submodule_alternate_error_strategy_die;
 
+/*
+ * To add a new advice, you need to:
+ * Define an advice_type.
+ * Add a new entry to advice_config_keys list.
+ * Add the new config variable to Documentation/config/advice.txt.
+ * Call advise_if_enabled to print your advice.
+ */
+enum advice_type {
+	ADD_EMBEDDED_REPO,
+	AMWORKDIR,
+	CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME,
+	COMMIT_BEFORE_MERGE,
+	DETACHED_HEAD,
+	FETCH_SHOW_FORCED_UPDATES,
+	GRAFT_FILE_DEPRECATED,
+	IGNORED_HOOK,
+	IMPLICIT_IDENTITY,
+	NESTED_TAG,
+	OBJECT_NAME_WARNING,
+	PUSH_ALREADY_EXISTS,
+	PUSH_FETCH_FIRST,
+	PUSH_NEEDS_FORCE,
+	PUSH_NON_FF_CURRENT,
+	PUSH_NON_FF_MATCHING,
+	PUSH_UNQUALIFIED_REF_NAME,
+	PUSH_UPDATE_REJECTED_ALIAS,
+	PUSH_UPDATE_REJECTED,
+	RESET_QUIET_WARNING,
+	RESOLVE_CONFLICT,
+	RM_HINTS,
+	SEQUENCER_IN_USE,
+	SET_UPSTREAM_FAILURE,
+	STATUS_AHEAD_BEHIND_WARNING,
+	STATUS_HINTS,
+	STATUS_U_OPTION,
+	SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE,
+	WAITING_FOR_EDITOR,
+};
+
+
 int git_default_advice_config(const char *var, const char *value);
 __attribute__((format (printf, 1, 2)))
 void advise(const char *advice, ...);
+
+/**
+ Checks if advice type is enabled (can be printed to the user).
+ Should be called before advise().
+ */
+int advice_enabled(enum advice_type type);
+
+/**
+ Checks the visibility of the advice before printing.
+ */
+void advise_if_enabled(enum advice_type type, const char *advice, ...);
+
 int error_resolve_conflict(const char *me);
 void NORETURN die_resolve_conflict(const char *me);
 void NORETURN die_conclude_merge(void);
diff --git a/t/helper/test-advise.c b/t/helper/test-advise.c
new file mode 100644
index 00000000000..279cad6460e
--- /dev/null
+++ b/t/helper/test-advise.c
@@ -0,0 +1,19 @@
+#include "test-tool.h"
+#include "cache.h"
+#include "advice.h"
+
+int cmd__advise_if_enabled(int argc, const char **argv)
+{
+	if (!argv[1])
+	die("usage: %s <advice>", argv[0]);
+
+	setup_git_directory();
+
+	/*
+	  Any advice type can be used for testing, but NESTED_TAG was selected
+	  here and in t0018 where this command is being executed.
+	 */
+	advise_if_enabled(NESTED_TAG, argv[1]);
+
+	return 0;
+}
diff --git a/t/helper/test-tool.c b/t/helper/test-tool.c
index f20989d4497..6977badc690 100644
--- a/t/helper/test-tool.c
+++ b/t/helper/test-tool.c
@@ -14,6 +14,7 @@ struct test_cmd {
 };
 
 static struct test_cmd cmds[] = {
+	{ "advise", cmd__advise_if_enabled },
 	{ "chmtime", cmd__chmtime },
 	{ "config", cmd__config },
 	{ "ctype", cmd__ctype },
diff --git a/t/helper/test-tool.h b/t/helper/test-tool.h
index 8ed2af71d1b..ca5e33b842f 100644
--- a/t/helper/test-tool.h
+++ b/t/helper/test-tool.h
@@ -4,6 +4,7 @@
 #define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "git-compat-util.h"
 
+int cmd__advise_if_enabled(int argc, const char **argv);
 int cmd__chmtime(int argc, const char **argv);
 int cmd__config(int argc, const char **argv);
 int cmd__ctype(int argc, const char **argv);
diff --git a/t/t0018-advice.sh b/t/t0018-advice.sh
new file mode 100755
index 00000000000..e03554d2f34
--- /dev/null
+++ b/t/t0018-advice.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+test_description='Test advise_if_enabled functionality'
+
+. ./test-lib.sh
+
+test_expect_success 'advice should be printed when config variable is unset' '
+	cat >expect <<-\EOF &&
+	hint: This is a piece of advice
+	hint: Disable this message with "git config advice.nestedTag false"
+	EOF
+	test-tool advise "This is a piece of advice" 2>actual &&
+	test_i18ncmp expect actual
+'
+
+test_expect_success 'advice should be printed when config variable is set to true' '
+	cat >expect <<-\EOF &&
+	hint: This is a piece of advice
+	hint: Disable this message with "git config advice.nestedTag false"
+	EOF
+	test_config advice.nestedTag true &&
+	test-tool advise "This is a piece of advice" 2>actual &&
+	test_i18ncmp expect actual
+'
+
+test_expect_success 'advice should not be printed when config variable is set to false' '
+	test_config advice.nestedTag false &&
+	test-tool advise "This is a piece of advice" 2>actual &&
+	test_must_be_empty actual
+'
+
+test_done
-- 
gitgitgadget


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

* [PATCH v5 3/3] tag: use new advice API to check visibility
  2020-02-25 10:55       ` [PATCH v5 0/3] [Outreachy] advice: revamp advise API Heba Waly via GitGitGadget
  2020-02-25 10:55         ` [PATCH v5 1/3] advice: extract vadvise() from advise() Heba Waly via GitGitGadget
  2020-02-25 10:55         ` [PATCH v5 2/3] advice: revamp advise API Heba Waly via GitGitGadget
@ 2020-02-25 10:55         ` Heba Waly via GitGitGadget
  2020-02-25 17:48           ` Junio C Hamano
  2 siblings, 1 reply; 50+ messages in thread
From: Heba Waly via GitGitGadget @ 2020-02-25 10:55 UTC (permalink / raw)
  To: git; +Cc: Heba Waly, Heba Waly

From: Heba Waly <heba.waly@gmail.com>

Following the new helpers added to the advice library,
replace the global variable check approach by the new
API calls

Signed-off-by: Heba Waly <heba.waly@gmail.com>
---
 advice.c       | 2 --
 advice.h       | 1 -
 builtin/tag.c  | 5 +++--
 t/t7004-tag.sh | 1 +
 4 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/advice.c b/advice.c
index 5c2068b8f8a..ea6e65c1ce0 100644
--- a/advice.c
+++ b/advice.c
@@ -29,7 +29,6 @@ int advice_ignored_hook = 1;
 int advice_waiting_for_editor = 1;
 int advice_graft_file_deprecated = 1;
 int advice_checkout_ambiguous_remote_branch_name = 1;
-int advice_nested_tag = 1;
 int advice_submodule_alternate_error_strategy_die = 1;
 
 static int advice_use_color = -1;
@@ -89,7 +88,6 @@ static struct {
 	{ "waitingForEditor", &advice_waiting_for_editor },
 	{ "graftFileDeprecated", &advice_graft_file_deprecated },
 	{ "checkoutAmbiguousRemoteBranchName", &advice_checkout_ambiguous_remote_branch_name },
-	{ "nestedTag", &advice_nested_tag },
 	{ "submoduleAlternateErrorStrategyDie", &advice_submodule_alternate_error_strategy_die },
 
 	/* make this an alias for backward compatibility */
diff --git a/advice.h b/advice.h
index a8461a362a3..509b562edb1 100644
--- a/advice.h
+++ b/advice.h
@@ -29,7 +29,6 @@ extern int advice_ignored_hook;
 extern int advice_waiting_for_editor;
 extern int advice_graft_file_deprecated;
 extern int advice_checkout_ambiguous_remote_branch_name;
-extern int advice_nested_tag;
 extern int advice_submodule_alternate_error_strategy_die;
 
 /*
diff --git a/builtin/tag.c b/builtin/tag.c
index e0a4c253828..45e959d5f8f 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -231,8 +231,9 @@ static void create_tag(const struct object_id *object, const char *object_ref,
 	if (type <= OBJ_NONE)
 		die(_("bad object type."));
 
-	if (type == OBJ_TAG && advice_nested_tag)
-		advise(_(message_advice_nested_tag), tag, object_ref);
+	if (type == OBJ_TAG)
+		advise_if_enabled(NESTED_TAG, _(message_advice_nested_tag),
+				  tag, object_ref);
 
 	strbuf_addf(&header,
 		    "object %s\n"
diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
index 6db92bd3ba6..74b637deb25 100755
--- a/t/t7004-tag.sh
+++ b/t/t7004-tag.sh
@@ -1726,6 +1726,7 @@ test_expect_success 'recursive tagging should give advice' '
 	hint: already a tag. If you meant to tag the object that it points to, use:
 	hint: |
 	hint: 	git tag -f nested annotated-v4.0^{}
+	hint: Disable this message with "git config advice.nestedTag false"
 	EOF
 	git tag -m nested nested annotated-v4.0 2>actual &&
 	test_i18ncmp expect actual
-- 
gitgitgadget

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

* Re: [PATCH v5 2/3] advice: revamp advise API
  2020-02-25 10:55         ` [PATCH v5 2/3] advice: revamp advise API Heba Waly via GitGitGadget
@ 2020-02-25 17:40           ` Junio C Hamano
  0 siblings, 0 replies; 50+ messages in thread
From: Junio C Hamano @ 2020-02-25 17:40 UTC (permalink / raw)
  To: Heba Waly via GitGitGadget; +Cc: git, Heba Waly

"Heba Waly via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Heba Waly <heba.waly@gmail.com>
>
> Currently it's very easy for the advice library's callers to miss
> checking the visibility step before printing an advice. Also, it makes
> more sense for this step to be handled by the advice library.
>
> Add a new advise_if_enabled function that checks the visibility of
> advice messages before printing.
>
> Add a new helper advise_enabled to check the visibility of the advice
> if the caller needs to carry out complicated processing based on that
> value.
>
> A list of config variables 'advice_config_keys' is added to be used by
> list_config_advices() instead of 'advice_config[]' because we'll get
> rid of 'advice_config[]' and the global variables once we migrate all
> the callers to use the new APIs.
>


> Also change the advise call in tag library from advise() to
> advise_if_enabled() to construct an example of the usage of the new
> API.

This is for step [3/3], isn't it?  I'll discard this paragraph.

>
> Signed-off-by: Heba Waly <heba.waly@gmail.com>
> ---
>  Makefile               |  1 +
>  advice.c               | 86 ++++++++++++++++++++++++++++++++++++++++--
>  advice.h               | 52 +++++++++++++++++++++++++
>  t/helper/test-advise.c | 19 ++++++++++
>  t/helper/test-tool.c   |  1 +
>  t/helper/test-tool.h   |  1 +
>  t/t0018-advice.sh      | 32 ++++++++++++++++
>  7 files changed, 188 insertions(+), 4 deletions(-)
>  create mode 100644 t/helper/test-advise.c
>  create mode 100755 t/t0018-advice.sh
>
> diff --git a/Makefile b/Makefile
> index 09f98b777ca..ed923a3e818 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -695,6 +695,7 @@ X =
>  
>  PROGRAMS += $(patsubst %.o,git-%$X,$(PROGRAM_OBJS))
>  
> +TEST_BUILTINS_OBJS += test-advise.o
>  TEST_BUILTINS_OBJS += test-chmtime.o
>  TEST_BUILTINS_OBJS += test-config.o
>  TEST_BUILTINS_OBJS += test-ctype.o
> diff --git a/advice.c b/advice.c
> index fd836332dad..5c2068b8f8a 100644
> --- a/advice.c
> +++ b/advice.c
> @@ -96,13 +96,56 @@ static struct {
>  	{ "pushNonFastForward", &advice_push_update_rejected }
>  };
>  
> -static void vadvise(const char *advice, va_list params)
> +static const char *advice_config_keys[] = {
> +	[ADD_EMBEDDED_REPO]			 = "addEmbeddedRepo",
> +	[AMWORKDIR]				 = "amWorkDir",
> +	[CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME]	 = "checkoutAmbiguousRemoteBranchName",
> +	[COMMIT_BEFORE_MERGE]			 = "commitBeforeMerge",
> +	[DETACHED_HEAD]				 = "detachedHead",
> +	[FETCH_SHOW_FORCED_UPDATES]		 = "fetchShowForcedUpdates",
> +	[GRAFT_FILE_DEPRECATED]			 = "graftFileDeprecated",
> +	[IGNORED_HOOK]				 = "ignoredHook",
> +	[IMPLICIT_IDENTITY]			 = "implicitIdentity",
> +	[NESTED_TAG]				 = "nestedTag",
> +	[OBJECT_NAME_WARNING]			 = "objectNameWarning",
> +	[PUSH_ALREADY_EXISTS]			 = "pushAlreadyExists",
> +	[PUSH_FETCH_FIRST]			 = "pushFetchFirst",
> +	[PUSH_NEEDS_FORCE]			 = "pushNeedsForce",
> +
> +	/* make this an alias for backward compatibility */
> +	[PUSH_UPDATE_REJECTED_ALIAS]		 = "pushNonFastForward",
> +
> +	[PUSH_NON_FF_CURRENT]			 = "pushNonFFCurrent",
> +	[PUSH_NON_FF_MATCHING]			 = "pushNonFFMatching",
> +	[PUSH_UNQUALIFIED_REF_NAME]		 = "pushUnqualifiedRefName",
> +	[PUSH_UPDATE_REJECTED]			 = "pushUpdateRejected",
> +	[RESET_QUIET_WARNING]			 = "resetQuiet",
> +	[RESOLVE_CONFLICT]			 = "resolveConflict",
> +	[RM_HINTS]				 = "rmHints",
> +	[SEQUENCER_IN_USE]			 = "sequencerInUse",
> +	[SET_UPSTREAM_FAILURE]			 = "setupStreamFailure",
> +	[STATUS_AHEAD_BEHIND_WARNING]		 = "statusAheadBehindWarning",
> +	[STATUS_HINTS]				 = "statusHints",
> +	[STATUS_U_OPTION]			 = "statusUoption",
> +	[SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = "submoduleAlternateErrorStrategyDie",
> +	[WAITING_FOR_EDITOR] 			 = "waitingForEditor",
> +};
> +
> +static const char turn_off_instructions[] =
> +N_("\n"
> +   "Disable this message with \"git config %s false\"");
> +
> +static void vadvise(const char *advice, int display_instructions,
> +		    char *key, va_list params)
>  {
>  	struct strbuf buf = STRBUF_INIT;
>  	const char *cp, *np;
>  
>  	strbuf_vaddf(&buf, advice, params);
>  
> +	if (display_instructions)
> +		strbuf_addf(&buf, turn_off_instructions, key);
> +
>  	for (cp = buf.buf; *cp; cp = np) {
>  		np = strchrnul(cp, '\n');
>  		fprintf(stderr,	_("%shint: %.*s%s\n"),
> @@ -119,8 +162,43 @@ void advise(const char *advice, ...)
>  {
>  	va_list params;
>  	va_start(params, advice);
> -	vadvise(advice, params);
> +	vadvise(advice, 0, "", params);
> +	va_end(params);
> +}
> +
> +static int get_config_value(enum advice_type type)
> +{
> +	int value = 1;
> +	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
> +
> +	git_config_get_bool(key, &value);
> +	free(key);
> +	return value;
> +}

So, in this hypothetical but quite realistic example:

	if (advice_enabled(ADVICE_FOO)) {
		char *foo = expensive_preparation();
		advice_if_enabled(ADVICE_FOO, "use of %s is discouraged", foo);
	}

we end up formulating the "advice.*" key twice and ask git_config_get_bool()
about the same key twice?

> +int advice_enabled(enum advice_type type)
> +{
> +	switch (type) {
> +	case PUSH_UPDATE_REJECTED:
> +		return get_config_value(PUSH_UPDATE_REJECTED) &&
> +		       get_config_value(PUSH_UPDATE_REJECTED_ALIAS);
> +	default:
> +		return get_config_value(type);
> +	}
> +}

Also, as "enum advice_type" will be part of the public API, and
there is little type checking for enums, we shouldn't be naming them
randomly like these---we'd at least want to use a common prefix,
like "ADVICE_", in front of them.  Those who are focused only on
advice subsystem may feel that names like PUSH_UPDATE_REJECTED are
sufficiently clear, but within the context of the whole system,
there is no cue that these UPCASED_WORDS identifiers belong to the
advice subsystem or somewhere else.

> +void advise_if_enabled(enum advice_type type, const char *advice, ...)
> +{
> +	char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
> +	va_list params;
> +
> +	if (!advice_enabled(type))
> +		return;

Oh, no, make the number of calls to xstrfmr() three times, not
twice, as I said in the previous example.

I wonder if it would make the implementation better to do these:

 - Rename advice_config_keys[] to advice_setting[] that does not
   imply it is only about the keys;

 - This table will know, for each enum advice_type, which
   configuration variable enables it, *and* if it is enabled.

i.e.

        static struct {
                const char *config_key;
                int disabled;
        } advice_setting[] = {
                [ADVICE_ADD_EMBEDED_REPO] = { "addEmbeddedRepo" },
                [ADVICE_AM_WORK_DIR]      = { "amWorkDir" },
                ...
                [ADVICE_WAITING_FOR_EDITOR] = { "waitingForEditor" },
        };


Side Note: you have AMWORKDIR that is unreadable.  If the config
           name uses camelCase by convention, the UPCASED_WORDS
           should be separated with underscore at the same word
           boundary.

Then, upon the first call to advice_enabled(), call git_config()
with a callback like

	static int populate_advice_settings(const char *var, const char *value, void *cb)
	{
		int advice_type;
		const char *name;

		if (!skip_prefix(var, "advice.", &name))
			return 0;
		advice_type = find_advice_type_by_name(advice_setting, name);
		if (advice_type < 0)
			return 0; /* unknown advice.* variable */
		/* advice.foo=false means advice.foo is disabled */
		advice_setting[advice_type].disabled = !git_config_bool(var, value);
	}

only once.  Your get_config_value() would then become a mere lookup
in advice_setting[] array, e.g.

	int advice_enabled(unsigned advice_type)
	{
		static int initialized;

		if (!initialized) {
			initialized = 1;
			git_config(populate_advice_settings, NULL);
		}
		if (ARRAY_SIZE(advice_setting) <= advice_type)
			BUG("OOB advice type requested???");
		return !advice_setting[advice_type].disabled;
	}

with your "push-update-rejected has two names" twist added.

Hmm?

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

* Re: [PATCH v5 3/3] tag: use new advice API to check visibility
  2020-02-25 10:55         ` [PATCH v5 3/3] tag: use new advice API to check visibility Heba Waly via GitGitGadget
@ 2020-02-25 17:48           ` Junio C Hamano
  0 siblings, 0 replies; 50+ messages in thread
From: Junio C Hamano @ 2020-02-25 17:48 UTC (permalink / raw)
  To: Heba Waly via GitGitGadget; +Cc: git, Heba Waly

"Heba Waly via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Heba Waly <heba.waly@gmail.com>
>
> Following the new helpers added to the advice library,
> replace the global variable check approach by the new
> API calls

The last paragraph of the proposed log message you had for [2/3]
described that this step is just an example better than the above
one, which would leave readers puzzled what our plans are for dozens
of existing advise() calls.

> +	if (type == OBJ_TAG)
> +		advise_if_enabled(NESTED_TAG, _(message_advice_nested_tag),
> +				  tag, object_ref);

This is probably a good enough example why OBJ_TAG is a good name
but NESTED_TAG is not---type could be something different from
OBJ_TAG but the other possiblities are all OBJ_<object-type>.  We
want the advice types to have the same property.


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

end of thread, back to index

Thread overview: 50+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-10  5:04 [PATCH] advice: refactor advise API Heba Waly via GitGitGadget
2020-02-10 14:38 ` Derrick Stolee
2020-02-10 19:30   ` Junio C Hamano
2020-02-10 19:42     ` Taylor Blau
2020-02-10 22:29       ` Emily Shaffer
2020-02-11  0:08       ` Heba Waly
2020-02-12 20:57         ` Taylor Blau
2020-02-10 23:56   ` Heba Waly
2020-02-11  2:39     ` Derrick Stolee
2020-02-10 20:37 ` Jeff King
2020-02-10 22:55   ` Emily Shaffer
2020-02-11  2:35     ` Heba Waly
2020-02-11 19:49     ` Jeff King
2020-02-11 19:51       ` Jeff King
2020-02-11  2:20   ` Heba Waly
2020-02-10 22:46 ` Junio C Hamano
2020-02-11  2:01   ` Heba Waly
2020-02-11  6:08     ` Junio C Hamano
2020-02-16 21:39 ` [PATCH v2 0/2] [RFC][Outreachy] " Heba Waly via GitGitGadget
2020-02-16 21:39   ` [PATCH v2 1/2] " Heba Waly via GitGitGadget
2020-02-17  3:28     ` Junio C Hamano
2020-02-17 10:03       ` Heba Waly
2020-02-19  9:59     ` Heba Waly
2020-02-16 21:39   ` [PATCH v2 2/2] advice: extract vadvise() from advise() Heba Waly via GitGitGadget
2020-02-17  0:09   ` [PATCH v2 0/2] [RFC][Outreachy] advice: refactor advise API Junio C Hamano
2020-02-19 20:33   ` [PATCH v3 0/2] [Outreachy] advice: revamp " Heba Waly via GitGitGadget
2020-02-19 20:34     ` [PATCH v3 1/2] " Heba Waly via GitGitGadget
2020-02-20  1:37       ` Emily Shaffer
2020-02-21  0:31         ` Heba Waly
2020-02-19 20:34     ` [PATCH v3 2/2] advice: extract vadvise() from advise() Heba Waly via GitGitGadget
2020-02-20  1:42       ` Emily Shaffer
2020-02-21  0:34         ` Heba Waly
2020-02-24 15:13     ` [PATCH v4 0/3] [Outreachy] advice: revamp advise API Heba Waly via GitGitGadget
2020-02-24 15:13       ` [PATCH v4 1/3] advice: extract vadvise() from advise() Heba Waly via GitGitGadget
2020-02-24 22:04         ` Emily Shaffer
2020-02-24 15:13       ` [PATCH v4 2/3] advice: revamp advise API Heba Waly via GitGitGadget
2020-02-24 22:05         ` Junio C Hamano
2020-02-24 22:11           ` Eric Sunshine
2020-02-24 23:51             ` Heba Waly
2020-02-24 23:49           ` Heba Waly
2020-02-24 23:45         ` Emily Shaffer
2020-02-24 15:13       ` [PATCH v4 3/3] tag: use new advice API to check visibility Heba Waly via GitGitGadget
2020-02-24 22:07         ` Junio C Hamano
2020-02-24 23:46         ` Emily Shaffer
2020-02-25 10:55       ` [PATCH v5 0/3] [Outreachy] advice: revamp advise API Heba Waly via GitGitGadget
2020-02-25 10:55         ` [PATCH v5 1/3] advice: extract vadvise() from advise() Heba Waly via GitGitGadget
2020-02-25 10:55         ` [PATCH v5 2/3] advice: revamp advise API Heba Waly via GitGitGadget
2020-02-25 17:40           ` Junio C Hamano
2020-02-25 10:55         ` [PATCH v5 3/3] tag: use new advice API to check visibility Heba Waly via GitGitGadget
2020-02-25 17:48           ` Junio C Hamano

git@vger.kernel.org list mirror (unofficial, one of many)

Archives are clonable:
	git clone --mirror https://public-inbox.org/git
	git clone --mirror http://ou63pmih66umazou.onion/git
	git clone --mirror http://czquwvybam4bgbro.onion/git
	git clone --mirror http://hjrcffqmbrq6wope.onion/git

Example config snippet for mirrors

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.version-control.git
	nntp://ou63pmih66umazou.onion/inbox.comp.version-control.git
	nntp://czquwvybam4bgbro.onion/inbox.comp.version-control.git
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.version-control.git
	nntp://news.gmane.io/gmane.comp.version-control.git

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git