Skip to content

Commit a423e65

Browse files
HebaWalygitster
authored andcommitted
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 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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent d8d0aff commit a423e65

File tree

7 files changed

+188
-4
lines changed

7 files changed

+188
-4
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: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,56 @@ static struct {
9696
{ "pushNonFastForward", &advice_push_update_rejected }
9797
};
9898

99-
static void vadvise(const char *advice, va_list params)
99+
static const char *advice_config_keys[] = {
100+
[ADD_EMBEDDED_REPO] = "addEmbeddedRepo",
101+
[AMWORKDIR] = "amWorkDir",
102+
[CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME] = "checkoutAmbiguousRemoteBranchName",
103+
[COMMIT_BEFORE_MERGE] = "commitBeforeMerge",
104+
[DETACHED_HEAD] = "detachedHead",
105+
[FETCH_SHOW_FORCED_UPDATES] = "fetchShowForcedUpdates",
106+
[GRAFT_FILE_DEPRECATED] = "graftFileDeprecated",
107+
[IGNORED_HOOK] = "ignoredHook",
108+
[IMPLICIT_IDENTITY] = "implicitIdentity",
109+
[NESTED_TAG] = "nestedTag",
110+
[OBJECT_NAME_WARNING] = "objectNameWarning",
111+
[PUSH_ALREADY_EXISTS] = "pushAlreadyExists",
112+
[PUSH_FETCH_FIRST] = "pushFetchFirst",
113+
[PUSH_NEEDS_FORCE] = "pushNeedsForce",
114+
115+
/* make this an alias for backward compatibility */
116+
[PUSH_UPDATE_REJECTED_ALIAS] = "pushNonFastForward",
117+
118+
[PUSH_NON_FF_CURRENT] = "pushNonFFCurrent",
119+
[PUSH_NON_FF_MATCHING] = "pushNonFFMatching",
120+
[PUSH_UNQUALIFIED_REF_NAME] = "pushUnqualifiedRefName",
121+
[PUSH_UPDATE_REJECTED] = "pushUpdateRejected",
122+
[RESET_QUIET_WARNING] = "resetQuiet",
123+
[RESOLVE_CONFLICT] = "resolveConflict",
124+
[RM_HINTS] = "rmHints",
125+
[SEQUENCER_IN_USE] = "sequencerInUse",
126+
[SET_UPSTREAM_FAILURE] = "setupStreamFailure",
127+
[STATUS_AHEAD_BEHIND_WARNING] = "statusAheadBehindWarning",
128+
[STATUS_HINTS] = "statusHints",
129+
[STATUS_U_OPTION] = "statusUoption",
130+
[SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE] = "submoduleAlternateErrorStrategyDie",
131+
[WAITING_FOR_EDITOR] = "waitingForEditor",
132+
};
133+
134+
static const char turn_off_instructions[] =
135+
N_("\n"
136+
"Disable this message with \"git config %s false\"");
137+
138+
static void vadvise(const char *advice, int display_instructions,
139+
char *key, va_list params)
100140
{
101141
struct strbuf buf = STRBUF_INIT;
102142
const char *cp, *np;
103143

104144
strbuf_vaddf(&buf, advice, params);
105145

146+
if (display_instructions)
147+
strbuf_addf(&buf, turn_off_instructions, key);
148+
106149
for (cp = buf.buf; *cp; cp = np) {
107150
np = strchrnul(cp, '\n');
108151
fprintf(stderr, _("%shint: %.*s%s\n"),
@@ -119,8 +162,43 @@ void advise(const char *advice, ...)
119162
{
120163
va_list params;
121164
va_start(params, advice);
122-
vadvise(advice, params);
165+
vadvise(advice, 0, "", params);
166+
va_end(params);
167+
}
168+
169+
static int get_config_value(enum advice_type type)
170+
{
171+
int value = 1;
172+
char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
173+
174+
git_config_get_bool(key, &value);
175+
free(key);
176+
return value;
177+
}
178+
179+
int advice_enabled(enum advice_type type)
180+
{
181+
switch (type) {
182+
case PUSH_UPDATE_REJECTED:
183+
return get_config_value(PUSH_UPDATE_REJECTED) &&
184+
get_config_value(PUSH_UPDATE_REJECTED_ALIAS);
185+
default:
186+
return get_config_value(type);
187+
}
188+
}
189+
190+
void advise_if_enabled(enum advice_type type, const char *advice, ...)
191+
{
192+
char *key = xstrfmt("%s.%s", "advice", advice_config_keys[type]);
193+
va_list params;
194+
195+
if (!advice_enabled(type))
196+
return;
197+
198+
va_start(params, advice);
199+
vadvise(advice, 1, key, params);
123200
va_end(params);
201+
free(key);
124202
}
125203

126204
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)
159237
{
160238
int i;
161239

162-
for (i = 0; i < ARRAY_SIZE(advice_config); i++)
163-
list_config_item(list, prefix, advice_config[i].name);
240+
for (i = 0; i < ARRAY_SIZE(advice_config_keys); i++)
241+
list_config_item(list, prefix, advice_config_keys[i]);
164242
}
165243

166244
int error_resolve_conflict(const char *me)

advice.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,61 @@ extern int advice_checkout_ambiguous_remote_branch_name;
3232
extern int advice_nested_tag;
3333
extern int advice_submodule_alternate_error_strategy_die;
3434

35+
/*
36+
* To add a new advice, you need to:
37+
* Define an advice_type.
38+
* Add a new entry to advice_config_keys list.
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+
ADD_EMBEDDED_REPO,
44+
AMWORKDIR,
45+
CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME,
46+
COMMIT_BEFORE_MERGE,
47+
DETACHED_HEAD,
48+
FETCH_SHOW_FORCED_UPDATES,
49+
GRAFT_FILE_DEPRECATED,
50+
IGNORED_HOOK,
51+
IMPLICIT_IDENTITY,
52+
NESTED_TAG,
53+
OBJECT_NAME_WARNING,
54+
PUSH_ALREADY_EXISTS,
55+
PUSH_FETCH_FIRST,
56+
PUSH_NEEDS_FORCE,
57+
PUSH_NON_FF_CURRENT,
58+
PUSH_NON_FF_MATCHING,
59+
PUSH_UNQUALIFIED_REF_NAME,
60+
PUSH_UPDATE_REJECTED_ALIAS,
61+
PUSH_UPDATE_REJECTED,
62+
RESET_QUIET_WARNING,
63+
RESOLVE_CONFLICT,
64+
RM_HINTS,
65+
SEQUENCER_IN_USE,
66+
SET_UPSTREAM_FAILURE,
67+
STATUS_AHEAD_BEHIND_WARNING,
68+
STATUS_HINTS,
69+
STATUS_U_OPTION,
70+
SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE,
71+
WAITING_FOR_EDITOR,
72+
};
73+
74+
3575
int git_default_advice_config(const char *var, const char *value);
3676
__attribute__((format (printf, 1, 2)))
3777
void advise(const char *advice, ...);
78+
79+
/**
80+
Checks if advice type is enabled (can be printed to the user).
81+
Should be called before advise().
82+
*/
83+
int advice_enabled(enum advice_type type);
84+
85+
/**
86+
Checks the visibility of the advice before printing.
87+
*/
88+
void advise_if_enabled(enum advice_type type, const char *advice, ...);
89+
3890
int error_resolve_conflict(const char *me);
3991
void NORETURN die_resolve_conflict(const char *me);
4092
void NORETURN die_conclude_merge(void);

t/helper/test-advise.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
/*
13+
Any advice type can be used for testing, but NESTED_TAG was selected
14+
here and in t0018 where this command is being executed.
15+
*/
16+
advise_if_enabled(NESTED_TAG, argv[1]);
17+
18+
return 0;
19+
}

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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)