Skip to content

Commit 0dcab7b

Browse files
committed
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 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 <[email protected]>
1 parent c7a6207 commit 0dcab7b

File tree

9 files changed

+91
-5
lines changed

9 files changed

+91
-5
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: 35 additions & 2 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,7 +88,6 @@ 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 */
@@ -118,6 +116,41 @@ void advise(const char *advice, ...)
118116
strbuf_release(&buf);
119117
}
120118

119+
static const char turn_off_instructions[] =
120+
N_("\n"
121+
"Turn this message off by running\n"
122+
"\"git config %s false\"");
123+
124+
void advise_ng(const char *key, const char *advice, ...)
125+
{
126+
int value = 1;
127+
struct strbuf buf = STRBUF_INIT;
128+
va_list params;
129+
const char *cp, *np;
130+
131+
git_config_get_bool(key, &value);
132+
133+
if(value)
134+
{
135+
va_start(params, advice);
136+
strbuf_vaddf(&buf, advice, params);
137+
va_end(params);
138+
139+
strbuf_addf(&buf, turn_off_instructions, key);
140+
141+
for (cp = buf.buf; *cp; cp = np) {
142+
np = strchrnul(cp, '\n');
143+
fprintf(stderr, _("%shint: %.*s%s\n"),
144+
advise_get_color(ADVICE_COLOR_HINT),
145+
(int)(np - cp), cp,
146+
advise_get_color(ADVICE_COLOR_RESET));
147+
if (*np)
148+
np++;
149+
}
150+
strbuf_release(&buf);
151+
}
152+
}
153+
121154
int git_default_advice_config(const char *var, const char *value)
122155
{
123156
const char *k, *slot_name;

advice.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@ 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

3534
int git_default_advice_config(const char *var, const char *value);
3635
__attribute__((format (printf, 1, 2)))
3736
void advise(const char *advice, ...);
37+
38+
/**
39+
Checks the visibility of the advice before priniting.
40+
*/
41+
void advise_ng(const char *key, const char *advice, ...);
3842
int error_resolve_conflict(const char *me);
3943
void NORETURN die_resolve_conflict(const char *me);
4044
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_ng("advice.nestedTag", _(message_advice_nested_tag), tag, object_ref);
236236

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

t/helper/test-advise.c

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

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

t/t7004-tag.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,6 +1726,8 @@ 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: Turn this message off by running
1730+
hint: "git config advice.nestedTag false"
17291731
EOF
17301732
git tag -m nested nested annotated-v4.0 2>actual &&
17311733
test_i18ncmp expect actual

0 commit comments

Comments
 (0)