git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] advice: refactor advise API

From
Heba Waly via GitGitGadget <gitgitgadget@gmail.com>
Date
Feb 10, 2020, 05:04 UTC
Message-ID
<pull.548.git.1581311049547.gitgitgadget@gmail.com>
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
Next: Derrick Stolee
Message 1 of 76 in “advice: refactor advise API”
  1. advice: refactor advise APIHeba Waly via GitGitGadget, Feb 10, 2020
  2. Derrick StoleeFeb 10, 2020
  3. Junio C HamanoFeb 10, 2020
  4. Taylor BlauFeb 10, 2020
  5. Emily ShafferFeb 10, 2020
  6. Heba WalyFeb 11, 2020
  7. Taylor BlauFeb 12, 2020
  8. Heba WalyFeb 10, 2020
  9. Derrick StoleeFeb 11, 2020
  10. Jeff KingFeb 10, 2020
  11. Emily ShafferFeb 10, 2020
  12. Heba WalyFeb 11, 2020
  13. Jeff KingFeb 11, 2020
  14. Jeff KingFeb 11, 2020
  15. Heba WalyFeb 11, 2020
  16. Junio C HamanoFeb 10, 2020
  17. Heba WalyFeb 11, 2020
  18. Junio C HamanoFeb 11, 2020
  19. 0/2 [RFC][Outreachy] advice: refactor advise APIHeba Waly via GitGitGadget, Feb 16, 2020
  20. 2/2 advice: extract vadvise() from advise()Heba Waly via GitGitGadget, Feb 16, 2020
  21. 1/2 advice: refactor advise APIHeba Waly via GitGitGadget, Feb 16, 2020
  22. Junio C HamanoFeb 17, 2020
  23. Heba WalyFeb 17, 2020
  24. Heba WalyFeb 19, 2020
  25. Junio C HamanoFeb 17, 2020
  26. 0/2 [Outreachy] advice: revamp advise APIHeba Waly via GitGitGadget, Feb 19, 2020
  27. 2/2 advice: extract vadvise() from advise()Heba Waly via GitGitGadget, Feb 19, 2020
  28. Emily ShafferFeb 20, 2020
  29. Heba WalyFeb 21, 2020
  30. 1/2 advice: revamp advise APIHeba Waly via GitGitGadget, Feb 19, 2020
  31. Emily ShafferFeb 20, 2020
  32. Heba WalyFeb 21, 2020
  33. 0/3 [Outreachy] advice: revamp advise APIHeba Waly via GitGitGadget, Feb 24, 2020
  34. 1/3 advice: extract vadvise() from advise()Heba Waly via GitGitGadget, Feb 24, 2020
  35. Emily ShafferFeb 24, 2020
  36. 3/3 tag: use new advice API to check visibilityHeba Waly via GitGitGadget, Feb 24, 2020
  37. Junio C HamanoFeb 24, 2020
  38. Emily ShafferFeb 24, 2020
  39. 2/3 advice: revamp advise APIHeba Waly via GitGitGadget, Feb 24, 2020
  40. Junio C HamanoFeb 24, 2020
  41. Eric SunshineFeb 24, 2020
  42. Heba WalyFeb 24, 2020
  43. Heba WalyFeb 24, 2020
  44. Emily ShafferFeb 24, 2020
  45. 0/3 [Outreachy] advice: revamp advise APIHeba Waly via GitGitGadget, Feb 25, 2020
  46. 1/3 advice: extract vadvise() from advise()Heba Waly via GitGitGadget, Feb 25, 2020
  47. 2/3 advice: revamp advise APIHeba Waly via GitGitGadget, Feb 25, 2020
  48. Junio C HamanoFeb 25, 2020
  49. Emily ShafferFeb 25, 2020
  50. Junio C HamanoFeb 25, 2020
  51. Junio C HamanoFeb 25, 2020
  52. Heba WalyFeb 25, 2020
  53. Junio C HamanoFeb 25, 2020
  54. Heba WalyFeb 26, 2020
  55. Junio C HamanoFeb 26, 2020
  56. Heba WalyFeb 26, 2020
  57. Junio C HamanoFeb 26, 2020
  58. Jonathan TanFeb 26, 2020
  59. 3/3 tag: use new advice API to check visibilityHeba Waly via GitGitGadget, Feb 25, 2020
  60. Junio C HamanoFeb 25, 2020
  61. 0/4 [Outreachy] advice: revamp advise APIHeba Waly via GitGitGadget, Feb 27, 2020
  62. 1/4 advice: extract vadvise() from advise()Heba Waly via GitGitGadget, Feb 27, 2020
  63. 2/4 advice: change "setupStreamFailure" to "setUpstreamFailure"Heba Waly via GitGitGadget, Feb 27, 2020
  64. Junio C HamanoFeb 27, 2020
  65. 3/4 advice: revamp advise APIHeba Waly via GitGitGadget, Feb 27, 2020
  66. Junio C HamanoFeb 27, 2020
  67. Heba WalyFeb 29, 2020
  68. 4/4 tag: use new advice API to check visibilityHeba Waly via GitGitGadget, Feb 27, 2020
  69. 0/4 [Outreachy] advice: revamp advise APIHeba Waly via GitGitGadget, Mar 2, 2020
  70. 1/4 advice: extract vadvise() from advise()Heba Waly via GitGitGadget, Mar 2, 2020
  71. 2/4 advice: change "setupStreamFailure" to "setUpstreamFailure"Heba Waly via GitGitGadget, Mar 2, 2020
  72. 4/4 tag: use new advice API to check visibilityHeba Waly via GitGitGadget, Mar 2, 2020
  73. 3/4 advice: revamp advise APIHeba Waly via GitGitGadget, Mar 2, 2020
  74. Junio C HamanoMar 2, 2020
  75. Junio C HamanoMar 3, 2020
  76. Heba WalyMar 4, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.