Skip to content

Commit b79deeb

Browse files
disposedtrolleygitster
authored andcommitted
advice: add --no-advice global option
Advice hints must be disabled individually by setting the relevant advice.* variables to false in the Git configuration. For server-side and scripted usages of Git where hints can be a hindrance, it can be cumbersome to maintain configuration to ensure all advice hints are disabled in perpetuity. This is a particular concern in tests, where new or changed hints can result in failed assertions. Add a --no-advice global option to disable all advice hints from being displayed. This is independent of the toggles for individual advice hints. Use an internal environment variable (GIT_ADVICE) to ensure this configuration is propagated to the usage site, even if it executes in a subprocess. Signed-off-by: James Liu <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5bd8811 commit b79deeb

File tree

5 files changed

+95
-4
lines changed

5 files changed

+95
-4
lines changed

Documentation/git.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ SYNOPSIS
1212
'git' [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]
1313
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
1414
[-p | --paginate | -P | --no-pager] [--no-replace-objects] [--no-lazy-fetch]
15-
[--no-optional-locks] [--bare] [--git-dir=<path>] [--work-tree=<path>]
16-
[--namespace=<name>] [--config-env=<name>=<envvar>] <command> [<args>]
15+
[--no-optional-locks] [--no-advice] [--bare] [--git-dir=<path>]
16+
[--work-tree=<path>] [--namespace=<name>] [--config-env=<name>=<envvar>]
17+
<command> [<args>]
1718

1819
DESCRIPTION
1920
-----------
@@ -190,6 +191,9 @@ If you just want to run git as if it was started in `<path>` then use
190191
Do not perform optional operations that require locks. This is
191192
equivalent to setting the `GIT_OPTIONAL_LOCKS` to `0`.
192193

194+
--no-advice::
195+
Disable all advice hints from being printed.
196+
193197
--literal-pathspecs::
194198
Treat pathspecs literally (i.e. no globbing, no pathspec magic).
195199
This is equivalent to setting the `GIT_LITERAL_PATHSPECS` environment

advice.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "advice.h"
33
#include "config.h"
44
#include "color.h"
5+
#include "environment.h"
56
#include "gettext.h"
67
#include "help.h"
78
#include "string-list.h"
@@ -127,6 +128,12 @@ void advise(const char *advice, ...)
127128
int advice_enabled(enum advice_type type)
128129
{
129130
int enabled = advice_setting[type].level != ADVICE_LEVEL_DISABLED;
131+
static int globally_enabled = -1;
132+
133+
if (globally_enabled < 0)
134+
globally_enabled = git_env_bool(GIT_ADVICE_ENVIRONMENT, 1);
135+
if (!globally_enabled)
136+
return 0;
130137

131138
if (type == ADVICE_PUSH_UPDATE_REJECTED)
132139
return enabled &&

environment.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ const char *getenv_safe(struct strvec *argv, const char *name);
5757
#define GIT_TEXT_DOMAIN_DIR_ENVIRONMENT "GIT_TEXTDOMAINDIR"
5858
#define GIT_ATTR_SOURCE_ENVIRONMENT "GIT_ATTR_SOURCE"
5959

60+
/*
61+
* Environment variable used to propagate the --no-advice global option to the
62+
* advice_enabled() helper, even when run in a subprocess.
63+
* This is an internal variable that should not be set by the user.
64+
*/
65+
#define GIT_ADVICE_ENVIRONMENT "GIT_ADVICE"
66+
6067
/*
6168
* Environment variable used in handshaking the wire protocol.
6269
* Contains a colon ':' separated list of keys with optional values

git.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ const char git_usage_string[] =
3737
N_("git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]\n"
3838
" [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\n"
3939
" [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--no-lazy-fetch]\n"
40-
" [--no-optional-locks] [--bare] [--git-dir=<path>] [--work-tree=<path>]\n"
41-
" [--namespace=<name>] [--config-env=<name>=<envvar>] <command> [<args>]");
40+
" [--no-optional-locks] [--no-advice] [--bare] [--git-dir=<path>]\n"
41+
" [--work-tree=<path>] [--namespace=<name>] [--config-env=<name>=<envvar>]\n"
42+
" <command> [<args>]");
4243

4344
const char git_more_info_string[] =
4445
N_("'git help -a' and 'git help -g' list available subcommands and some\n"
@@ -337,6 +338,10 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
337338
setenv(GIT_ATTR_SOURCE_ENVIRONMENT, cmd, 1);
338339
if (envchanged)
339340
*envchanged = 1;
341+
} else if (!strcmp(cmd, "--no-advice")) {
342+
setenv(GIT_ADVICE_ENVIRONMENT, "0", 1);
343+
if (envchanged)
344+
*envchanged = 1;
340345
} else {
341346
fprintf(stderr, _("unknown option: %s\n"), cmd);
342347
usage(git_usage_string);

t/t0018-advice.sh

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,72 @@ test_expect_success 'advice should not be printed when config variable is set to
2929
test_must_be_empty actual
3030
'
3131

32+
test_expect_success 'advice should not be printed when --no-advice is used' '
33+
q_to_tab >expect <<-\EOF &&
34+
On branch master
35+
36+
No commits yet
37+
38+
Untracked files:
39+
QREADME
40+
41+
nothing added to commit but untracked files present
42+
EOF
43+
44+
test_when_finished "rm -fr advice-test" &&
45+
git init advice-test &&
46+
(
47+
cd advice-test &&
48+
>README &&
49+
git --no-advice status
50+
) >actual &&
51+
test_cmp expect actual
52+
'
53+
54+
test_expect_success 'advice should not be printed when GIT_ADVICE is set to false' '
55+
q_to_tab >expect <<-\EOF &&
56+
On branch master
57+
58+
No commits yet
59+
60+
Untracked files:
61+
QREADME
62+
63+
nothing added to commit but untracked files present
64+
EOF
65+
66+
test_when_finished "rm -fr advice-test" &&
67+
git init advice-test &&
68+
(
69+
cd advice-test &&
70+
>README &&
71+
GIT_ADVICE=false git status
72+
) >actual &&
73+
test_cmp expect actual
74+
'
75+
76+
test_expect_success 'advice should be printed when GIT_ADVICE is set to true' '
77+
q_to_tab >expect <<-\EOF &&
78+
On branch master
79+
80+
No commits yet
81+
82+
Untracked files:
83+
(use "git add <file>..." to include in what will be committed)
84+
QREADME
85+
86+
nothing added to commit but untracked files present (use "git add" to track)
87+
EOF
88+
89+
test_when_finished "rm -fr advice-test" &&
90+
git init advice-test &&
91+
(
92+
cd advice-test &&
93+
>README &&
94+
GIT_ADVICE=true git status
95+
) >actual &&
96+
cat actual > /tmp/actual &&
97+
test_cmp expect actual
98+
'
99+
32100
test_done

0 commit comments

Comments
 (0)