@matheusbsbs / matheus-bagliano / commits / b3b18d1621
Fork 0

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 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 advice_settings is added to cache the config variables names and values, it's intended to replace advice_config[] and the global variables once we migrate all the callers to use the new APIs. Signed-off-by: Heba Waly <heba.waly@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Heba Waly committed Mar 2, 2020 at 20:01 UTC b3b18d1621315fb187689ee6c759bdbbea214eb8
7 files changed +188 -4
Makefile
+1
@@ -695,6 +695,7 @@ X =
695
696 PROGRAMS += $(patsubst %.o,git-%$X,$(PROGRAM_OBJS))
697
698 +TEST_BUILTINS_OBJS += test-advise.o
699 TEST_BUILTINS_OBJS += test-chmtime.o
700 TEST_BUILTINS_OBJS += test-config.o
701 TEST_BUILTINS_OBJS += test-ctype.o
advice.c
+80 -4
@@ -96,13 +96,59 @@ static struct {
96 { "pushNonFastForward", &advice_push_update_rejected }
97 };
98
99 -static void vadvise(const char *advice, va_list params)
99 +static struct {
100 + const char *key;
101 + int enabled;
102 +} advice_setting[] = {
103 + [ADVICE_ADD_EMBEDDED_REPO] = { "addEmbeddedRepo", 1 },
104 + [ADVICE_AM_WORK_DIR] = { "amWorkDir", 1 },
105 + [ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME] = { "checkoutAmbiguousRemoteBranchName", 1 },
106 + [ADVICE_COMMIT_BEFORE_MERGE] = { "commitBeforeMerge", 1 },
107 + [ADVICE_DETACHED_HEAD] = { "detachedHead", 1 },
108 + [ADVICE_FETCH_SHOW_FORCED_UPDATES] = { "fetchShowForcedUpdates", 1 },
109 + [ADVICE_GRAFT_FILE_DEPRECATED] = { "graftFileDeprecated", 1 },
110 + [ADVICE_IGNORED_HOOK] = { "ignoredHook", 1 },
111 + [ADVICE_IMPLICIT_IDENTITY] = { "implicitIdentity", 1 },
112 + [ADVICE_NESTED_TAG] = { "nestedTag", 1 },
113 + [ADVICE_OBJECT_NAME_WARNING] = { "objectNameWarning", 1 },
114 + [ADVICE_PUSH_ALREADY_EXISTS] = { "pushAlreadyExists", 1 },
115 + [ADVICE_PUSH_FETCH_FIRST] = { "pushFetchFirst", 1 },
116 + [ADVICE_PUSH_NEEDS_FORCE] = { "pushNeedsForce", 1 },
117 +
118 + /* make this an alias for backward compatibility */
119 + [ADVICE_PUSH_UPDATE_REJECTED_ALIAS] = { "pushNonFastForward", 1 },
120 +
121 + [ADVICE_PUSH_NON_FF_CURRENT] = { "pushNonFFCurrent", 1 },
122 + [ADVICE_PUSH_NON_FF_MATCHING] = { "pushNonFFMatching", 1 },
123 + [ADVICE_PUSH_UNQUALIFIED_REF_NAME] = { "pushUnqualifiedRefName", 1 },
124 + [ADVICE_PUSH_UPDATE_REJECTED] = { "pushUpdateRejected", 1 },
125 + [ADVICE_RESET_QUIET_WARNING] = { "resetQuiet", 1 },
126 + [ADVICE_RESOLVE_CONFLICT] = { "resolveConflict", 1 },
127 + [ADVICE_RM_HINTS] = { "rmHints", 1 },
128 + [ADVICE_SEQUENCER_IN_USE] = { "sequencerInUse", 1 },
129 + [ADVICE_SET_UPSTREAM_FAILURE] = { "setUpstreamFailure", 1 },
130 + [ADVICE_STATUS_AHEAD_BEHIND_WARNING] = { "statusAheadBehindWarning", 1 },
131 + [ADVICE_STATUS_HINTS] = { "statusHints", 1 },
132 + [ADVICE_STATUS_U_OPTION] = { "statusUoption", 1 },
133 + [ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = { "submoduleAlternateErrorStrategyDie", 1 },
134 + [ADVICE_WAITING_FOR_EDITOR] = { "waitingForEditor", 1 },
135 +};
136 +
137 +static const char turn_off_instructions[] =
138 +N_("\n"
139 + "Disable this message with \"git config advice.%s false\"");
140 +
141 +static void vadvise(const char *advice, int display_instructions,
142 + const char *key, va_list params)
143 {
144 struct strbuf buf = STRBUF_INIT;
145 const char *cp, *np;
146
147 strbuf_vaddf(&buf, advice, params);
148
149 + if (display_instructions)
150 + strbuf_addf(&buf, turn_off_instructions, key);
151 +
152 for (cp = buf.buf; *cp; cp = np) {
153 np = strchrnul(cp, '\n');
154 fprintf(stderr, _("%shint: %.*s%s\n"),
@@ -119,7 +165,30 @@ void advise(const char *advice, ...)
165 {
166 va_list params;
167 va_start(params, advice);
122 - vadvise(advice, params);
168 + vadvise(advice, 0, "", params);
169 + va_end(params);
170 +}
171 +
172 +int advice_enabled(enum advice_type type)
173 +{
174 + switch(type) {
175 + case ADVICE_PUSH_UPDATE_REJECTED:
176 + return advice_setting[ADVICE_PUSH_UPDATE_REJECTED].enabled &&
177 + advice_setting[ADVICE_PUSH_UPDATE_REJECTED_ALIAS].enabled;
178 + default:
179 + return advice_setting[type].enabled;
180 + }
181 +}
182 +
183 +void advise_if_enabled(enum advice_type type, const char *advice, ...)
184 +{
185 + va_list params;
186 +
187 + if (!advice_enabled(type))
188 + return;
189 +
190 + va_start(params, advice);
191 + vadvise(advice, 1, advice_setting[type].key, params);
192 va_end(params);
193 }
194
@@ -149,6 +218,13 @@ int git_default_advice_config(const char *var, const char *value)
218 if (strcasecmp(k, advice_config[i].name))
219 continue;
220 *advice_config[i].preference = git_config_bool(var, value);
221 + break;
222 + }
223 +
224 + for (i = 0; i < ARRAY_SIZE(advice_setting); i++) {
225 + if (strcasecmp(k, advice_setting[i].key))
226 + continue;
227 + advice_setting[i].enabled = git_config_bool(var, value);
228 return 0;
229 }
230
@@ -159,8 +235,8 @@ void list_config_advices(struct string_list *list, const char *prefix)
235 {
236 int i;
237
162 - for (i = 0; i < ARRAY_SIZE(advice_config); i++)
163 - list_config_item(list, prefix, advice_config[i].name);
238 + for (i = 0; i < ARRAY_SIZE(advice_setting); i++)
239 + list_config_item(list, prefix, advice_setting[i].key);
240 }
241
242 int error_resolve_conflict(const char *me)
advice.h
+51
@@ -32,9 +32,60 @@ extern int advice_checkout_ambiguous_remote_branch_name;
32 extern int advice_nested_tag;
33 extern int advice_submodule_alternate_error_strategy_die;
34
35 +/*
36 + * To add a new advice, you need to:
37 + * Define a new advice_type.
38 + * Add a new entry to advice_setting array.
39 + * Add the new config variable to Documentation/config/advice.txt.
40 + * Call advise_if_enabled to print your advice.
41 + */
42 + enum advice_type {
43 + ADVICE_ADD_EMBEDDED_REPO,
44 + ADVICE_AM_WORK_DIR,
45 + ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME,
46 + ADVICE_COMMIT_BEFORE_MERGE,
47 + ADVICE_DETACHED_HEAD,
48 + ADVICE_FETCH_SHOW_FORCED_UPDATES,
49 + ADVICE_GRAFT_FILE_DEPRECATED,
50 + ADVICE_IGNORED_HOOK,
51 + ADVICE_IMPLICIT_IDENTITY,
52 + ADVICE_NESTED_TAG,
53 + ADVICE_OBJECT_NAME_WARNING,
54 + ADVICE_PUSH_ALREADY_EXISTS,
55 + ADVICE_PUSH_FETCH_FIRST,
56 + ADVICE_PUSH_NEEDS_FORCE,
57 + ADVICE_PUSH_NON_FF_CURRENT,
58 + ADVICE_PUSH_NON_FF_MATCHING,
59 + ADVICE_PUSH_UNQUALIFIED_REF_NAME,
60 + ADVICE_PUSH_UPDATE_REJECTED_ALIAS,
61 + ADVICE_PUSH_UPDATE_REJECTED,
62 + ADVICE_RESET_QUIET_WARNING,
63 + ADVICE_RESOLVE_CONFLICT,
64 + ADVICE_RM_HINTS,
65 + ADVICE_SEQUENCER_IN_USE,
66 + ADVICE_SET_UPSTREAM_FAILURE,
67 + ADVICE_STATUS_AHEAD_BEHIND_WARNING,
68 + ADVICE_STATUS_HINTS,
69 + ADVICE_STATUS_U_OPTION,
70 + ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE,
71 + ADVICE_WAITING_FOR_EDITOR,
72 +};
73 +
74 int git_default_advice_config(const char *var, const char *value);
75 __attribute__((format (printf, 1, 2)))
76 void advise(const char *advice, ...);
77 +
78 +/**
79 + * Checks if advice type is enabled (can be printed to the user).
80 + * Should be called before advise().
81 + */
82 +int advice_enabled(enum advice_type type);
83 +
84 +/**
85 + * Checks the visibility of the advice before printing.
86 + */
87 +void advise_if_enabled(enum advice_type type, const char *advice, ...);
88 +
89 int error_resolve_conflict(const char *me);
90 void NORETURN die_resolve_conflict(const char *me);
91 void NORETURN die_conclude_merge(void);
t/helper/test-advise.c new
+22
@@ -0,0 +1,22 @@
1 +#include "test-tool.h"
2 +#include "cache.h"
3 +#include "advice.h"
4 +#include "config.h"
5 +
6 +int cmd__advise_if_enabled(int argc, const char **argv)
7 +{
8 + if (!argv[1])
9 + die("usage: %s <advice>", argv[0]);
10 +
11 + setup_git_directory();
12 + git_config(git_default_config, NULL);
13 +
14 + /*
15 + * Any advice type can be used for testing, but NESTED_TAG was
16 + * selected here and in t0018 where this command is being
17 + * executed.
18 + */
19 + advise_if_enabled(ADVICE_NESTED_TAG, argv[1]);
20 +
21 + return 0;
22 +}
t/helper/test-tool.c
+1
@@ -14,6 +14,7 @@ struct test_cmd {
14 };
15
16 static struct test_cmd cmds[] = {
17 + { "advise", cmd__advise_if_enabled },
18 { "chmtime", cmd__chmtime },
19 { "config", cmd__config },
20 { "ctype", cmd__ctype },
t/helper/test-tool.h
+1
@@ -4,6 +4,7 @@
4 #define USE_THE_INDEX_COMPATIBILITY_MACROS
5 #include "git-compat-util.h"
6
7 +int cmd__advise_if_enabled(int argc, const char **argv);
8 int cmd__chmtime(int argc, const char **argv);
9 int cmd__config(int argc, const char **argv);
10 int cmd__ctype(int argc, const char **argv);
t/t0018-advice.sh new
+32
@@ -0,0 +1,32 @@
1 +#!/bin/sh
2 +
3 +test_description='Test advise_if_enabled functionality'
4 +
5 +. ./test-lib.sh
6 +
7 +test_expect_success 'advice should be printed when config variable is unset' '
8 + cat >expect <<-\EOF &&
9 + hint: This is a piece of advice
10 + hint: Disable this message with "git config advice.nestedTag false"
11 + EOF
12 + test-tool advise "This is a piece of advice" 2>actual &&
13 + test_i18ncmp expect actual
14 +'
15 +
16 +test_expect_success 'advice should be printed when config variable is set to true' '
17 + cat >expect <<-\EOF &&
18 + hint: This is a piece of advice
19 + hint: Disable this message with "git config advice.nestedTag false"
20 + EOF
21 + test_config advice.nestedTag true &&
22 + test-tool advise "This is a piece of advice" 2>actual &&
23 + test_i18ncmp expect actual
24 +'
25 +
26 +test_expect_success 'advice should not be printed when config variable is set to false' '
27 + test_config advice.nestedTag false &&
28 + test-tool advise "This is a piece of advice" 2>actual &&
29 + test_must_be_empty actual
30 +'
31 +
32 +test_done