Skip to content

Commit 080d12b

Browse files
committed
advice: refactor 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 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 <[email protected]>
1 parent c7a6207 commit 080d12b

File tree

9 files changed

+191
-7
lines changed

9 files changed

+191
-7
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,7 @@ X =
695695

696696
PROGRAMS += $(patsubst %.o,git-%$X,$(PROGRAM_OBJS))
697697

698+
TEST_BUILTINS_OBJS += test-advise.o
698699
TEST_BUILTINS_OBJS += test-chmtime.o
699700
TEST_BUILTINS_OBJS += test-config.o
700701
TEST_BUILTINS_OBJS += test-ctype.o

advice.c

Lines changed: 84 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ int advice_ignored_hook = 1;
2929
int advice_waiting_for_editor = 1;
3030
int advice_graft_file_deprecated = 1;
3131
int advice_checkout_ambiguous_remote_branch_name = 1;
32-
int advice_nested_tag = 1;
3332
int advice_submodule_alternate_error_strategy_die = 1;
3433

3534
static int advice_use_color = -1;
@@ -89,13 +88,46 @@ static struct {
8988
{ "waitingForEditor", &advice_waiting_for_editor },
9089
{ "graftFileDeprecated", &advice_graft_file_deprecated },
9190
{ "checkoutAmbiguousRemoteBranchName", &advice_checkout_ambiguous_remote_branch_name },
92-
{ "nestedTag", &advice_nested_tag },
9391
{ "submoduleAlternateErrorStrategyDie", &advice_submodule_alternate_error_strategy_die },
9492

9593
/* make this an alias for backward compatibility */
9694
{ "pushNonFastForward", &advice_push_update_rejected }
9795
};
9896

97+
static const char *advice_config_keys[] = {
98+
[FETCH_SHOW_FORCED_UPDATES] = "fetchShowForcedUpdates",
99+
[PUSH_UPDATE_REJECTED] = "pushUpdateRejected",
100+
/* make this an alias for backward compatibility */
101+
[PUSH_UPDATE_REJECTED_ALIAS] = "pushNonFastForward",
102+
103+
[PUSH_NON_FF_CURRENT] = "pushNonFFCurrent",
104+
[PUSH_NON_FF_MATCHING] = "pushNonFFMatching",
105+
[PUSH_ALREADY_EXISTS] = "pushAlreadyExists",
106+
[PUSH_FETCH_FIRST] = "pushFetchFirst",
107+
[PUSH_NEEDS_FORCE] = "pushNeedsForce",
108+
[PUSH_UNQUALIFIED_REF_NAME] = "pushUnqualifiedRefName",
109+
[STATUS_HINTS] = "statusHints",
110+
[STATUS_U_OPTION] = "statusUoption",
111+
[STATUS_AHEAD_BEHIND_WARNING] = "statusAheadBehindWarning",
112+
[COMMIT_BEFORE_MERGE] = "commitBeforeMerge",
113+
[RESET_QUIET_WARNING] = "resetQuiet",
114+
[RESOLVE_CONFLICT] = "resolveConflict",
115+
[SEQUENCER_IN_USE] = "sequencerInUse",
116+
[IMPLICIT_IDENTITY] = "implicitIdentity",
117+
[DETACHED_HEAD] = "detachedHead",
118+
[SET_UPSTREAM_FAILURE] = "setupStreamFailure",
119+
[OBJECT_NAME_WARNING] = "objectNameWarning",
120+
[AMWORKDIR] = "amWorkDir",
121+
[RM_HINTS] = "rmHints",
122+
[ADD_EMBEDDED_REPO] = "addEmbeddedRepo",
123+
[IGNORED_HOOK] = "ignoredHook",
124+
[WAITING_FOR_EDITOR] = "waitingForEditor",
125+
[GRAFT_FILE_DEPRECATED] = "graftFileDeprecated",
126+
[CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME] = "checkoutAmbiguousRemoteBranchName",
127+
[NESTED_TAG] = "nestedTag",
128+
[SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = "submoduleAlternateErrorStrategyDie"
129+
};
130+
99131
void advise(const char *advice, ...)
100132
{
101133
struct strbuf buf = STRBUF_INIT;
@@ -118,6 +150,54 @@ void advise(const char *advice, ...)
118150
strbuf_release(&buf);
119151
}
120152

153+
int advice_enabled(enum advice_type type)
154+
{
155+
int value = 1;
156+
char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
157+
git_config_get_bool(key, &value);
158+
return value;
159+
}
160+
161+
int advice_push_update_rejected_enabled(void)
162+
{
163+
return advice_enabled(PUSH_UPDATE_REJECTED) ||
164+
advice_enabled(PUSH_UPDATE_REJECTED_ALIAS);
165+
166+
}
167+
168+
static const char turn_off_instructions[] =
169+
N_("\n"
170+
"Disable this message with \"git config %s false\"");
171+
172+
void advise_if_enabled(enum advice_type type, const char *advice, ...)
173+
{
174+
struct strbuf buf = STRBUF_INIT;
175+
char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
176+
va_list params;
177+
const char *cp, *np;
178+
179+
if(!advice_enabled(type))
180+
return;
181+
182+
va_start(params, advice);
183+
strbuf_vaddf(&buf, advice, params);
184+
va_end(params);
185+
186+
strbuf_addf(&buf, turn_off_instructions, key);
187+
188+
for (cp = buf.buf; *cp; cp = np) {
189+
np = strchrnul(cp, '\n');
190+
fprintf(stderr, _("%shint: %.*s%s\n"),
191+
advise_get_color(ADVICE_COLOR_HINT),
192+
(int)(np - cp), cp,
193+
advise_get_color(ADVICE_COLOR_RESET));
194+
if (*np)
195+
np++;
196+
}
197+
strbuf_release(&buf);
198+
199+
}
200+
121201
int git_default_advice_config(const char *var, const char *value)
122202
{
123203
const char *k, *slot_name;
@@ -154,8 +234,8 @@ void list_config_advices(struct string_list *list, const char *prefix)
154234
{
155235
int i;
156236

157-
for (i = 0; i < ARRAY_SIZE(advice_config); i++)
158-
list_config_item(list, prefix, advice_config[i].name);
237+
for (i = 0; i < ARRAY_SIZE(advice_config_keys); i++)
238+
list_config_item(list, prefix, advice_config_keys[i]);
159239
}
160240

161241
int error_resolve_conflict(const char *me)

advice.h

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,68 @@ extern int advice_ignored_hook;
2929
extern int advice_waiting_for_editor;
3030
extern int advice_graft_file_deprecated;
3131
extern int advice_checkout_ambiguous_remote_branch_name;
32-
extern int advice_nested_tag;
3332
extern int advice_submodule_alternate_error_strategy_die;
3433

34+
/**
35+
To add a new advice, you need to:
36+
- Define an advice_type.
37+
- Add a new entry to advice_config_keys list.
38+
- Add the new config variable to Documentation/config/advice.txt.
39+
- Call advise_if_enabled to print your advice.
40+
*/
41+
enum advice_type {
42+
FETCH_SHOW_FORCED_UPDATES = 0,
43+
PUSH_UPDATE_REJECTED = 1,
44+
PUSH_UPDATE_REJECTED_ALIAS = 2,
45+
PUSH_NON_FF_CURRENT = 3,
46+
PUSH_NON_FF_MATCHING = 4,
47+
PUSH_ALREADY_EXISTS = 5,
48+
PUSH_FETCH_FIRST = 6,
49+
PUSH_NEEDS_FORCE = 7,
50+
PUSH_UNQUALIFIED_REF_NAME = 8,
51+
STATUS_HINTS = 9,
52+
STATUS_U_OPTION = 10,
53+
STATUS_AHEAD_BEHIND_WARNING = 11,
54+
COMMIT_BEFORE_MERGE = 12,
55+
RESET_QUIET_WARNING = 13,
56+
RESOLVE_CONFLICT = 14,
57+
SEQUENCER_IN_USE = 15,
58+
IMPLICIT_IDENTITY = 16,
59+
DETACHED_HEAD = 17,
60+
SET_UPSTREAM_FAILURE = 18,
61+
OBJECT_NAME_WARNING = 19,
62+
AMWORKDIR = 20,
63+
RM_HINTS = 21,
64+
ADD_EMBEDDED_REPO = 22,
65+
IGNORED_HOOK = 23,
66+
WAITING_FOR_EDITOR = 24,
67+
GRAFT_FILE_DEPRECATED = 25,
68+
CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME = 26,
69+
NESTED_TAG = 27,
70+
SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE = 28,
71+
};
72+
73+
3574
int git_default_advice_config(const char *var, const char *value);
3675
__attribute__((format (printf, 1, 2)))
3776
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 if PUSH_UPDATE_REJECTED advice type is enabled.
86+
*/
87+
int advice_push_update_rejected_enabled(void);
88+
89+
/**
90+
Checks the visibility of the advice before priniting.
91+
*/
92+
void advise_if_enabled(enum advice_type type, const char *advice, ...);
93+
3894
int error_resolve_conflict(const char *me);
3995
void NORETURN die_resolve_conflict(const char *me);
4096
void NORETURN die_conclude_merge(void);

builtin/tag.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ static void create_tag(const struct object_id *object, const char *object_ref,
231231
if (type <= OBJ_NONE)
232232
die(_("bad object type."));
233233

234-
if (type == OBJ_TAG && advice_nested_tag)
235-
advise(_(message_advice_nested_tag), tag, object_ref);
234+
if (type == OBJ_TAG)
235+
advise_if_enabled(NESTED_TAG, _(message_advice_nested_tag), tag, object_ref);
236236

237237
strbuf_addf(&header,
238238
"object %s\n"

t/helper/test-advise.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "test-tool.h"
2+
#include "cache.h"
3+
#include "advice.h"
4+
5+
int cmd__advise_if_enabled(int argc, const char **argv)
6+
{
7+
if (!argv[1])
8+
die("usage: %s <advice>", argv[0]);
9+
10+
setup_git_directory();
11+
12+
//use any advice type for testing
13+
advise_if_enabled(NESTED_TAG, argv[1]);
14+
15+
return 0;
16+
}

t/helper/test-tool.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct test_cmd {
1414
};
1515

1616
static struct test_cmd cmds[] = {
17+
{ "advise", cmd__advise_if_enabled },
1718
{ "chmtime", cmd__chmtime },
1819
{ "config", cmd__config },
1920
{ "ctype", cmd__ctype },

t/helper/test-tool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#define USE_THE_INDEX_COMPATIBILITY_MACROS
55
#include "git-compat-util.h"
66

7+
int cmd__advise_if_enabled(int argc, const char **argv);
78
int cmd__chmtime(int argc, const char **argv);
89
int cmd__config(int argc, const char **argv);
910
int cmd__ctype(int argc, const char **argv);

t/t0018-advice.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/sh
2+
3+
test_description='Test advise_if_enabled functionality'
4+
5+
. ./test-lib.sh
6+
7+
cat > expected <<EOF
8+
hint: This is a piece of advice
9+
hint: Disable this message with "git config advice.nestedTag false"
10+
EOF
11+
test_expect_success 'advise should be printed when config variable is unset' '
12+
test-tool advise "This is a piece of advice" 2>actual &&
13+
test_i18ncmp expected actual
14+
'
15+
16+
test_expect_success 'advise should be printed when config variable is set to true' '
17+
test_config advice.nestedTag true &&
18+
test-tool advise "This is a piece of advice" 2>actual &&
19+
test_i18ncmp expected actual
20+
'
21+
22+
test_expect_success 'advise should not be printed when config variable is set to false' '
23+
test_config advice.nestedTag false &&
24+
test-tool advise "This is a piece of advice" 2>actual &&
25+
test_must_be_empty actual
26+
'
27+
28+
test_done

t/t7004-tag.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,6 +1726,7 @@ test_expect_success 'recursive tagging should give advice' '
17261726
hint: already a tag. If you meant to tag the object that it points to, use:
17271727
hint: |
17281728
hint: git tag -f nested annotated-v4.0^{}
1729+
hint: Disable this message with "git config advice.nestedTag false"
17291730
EOF
17301731
git tag -m nested nested annotated-v4.0 2>actual &&
17311732
test_i18ncmp expect actual

0 commit comments

Comments
 (0)