Skip to content

Commit f0e2183

Browse files
committed
Merge branch 'jl/git-no-advice'
A new global "--no-advice" option can be used to disable all advice messages, which is meant to be used only in scripts. * jl/git-no-advice: t0018: two small fixes advice: add --no-advice global option doc: add spacing around paginate options doc: clean up usage documentation for --no-* opts
2 parents db271e7 + cbdc83f commit f0e2183

File tree

5 files changed

+104
-10
lines changed

5 files changed

+104
-10
lines changed

Documentation/git.txt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ SYNOPSIS
1111
[verse]
1212
'git' [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]
1313
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
14-
[-p|--paginate|-P|--no-pager] [--no-replace-objects] [--bare]
15-
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
16-
[--config-env=<name>=<envvar>] <command> [<args>]
14+
[-p | --paginate | -P | --no-pager] [--no-replace-objects] [--no-lazy-fetch]
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
-----------
@@ -186,6 +187,13 @@ If you just want to run git as if it was started in `<path>` then use
186187
This is equivalent to setting the `GIT_NO_LAZY_FETCH`
187188
environment variable to `1`.
188189

190+
--no-optional-locks::
191+
Do not perform optional operations that require locks. This is
192+
equivalent to setting the `GIT_OPTIONAL_LOCKS` to `0`.
193+
194+
--no-advice::
195+
Disable all advice hints from being printed.
196+
189197
--literal-pathspecs::
190198
Treat pathspecs literally (i.e. no globbing, no pathspec magic).
191199
This is equivalent to setting the `GIT_LITERAL_PATHSPECS` environment
@@ -207,10 +215,6 @@ If you just want to run git as if it was started in `<path>` then use
207215
Add "icase" magic to all pathspec. This is equivalent to setting
208216
the `GIT_ICASE_PATHSPECS` environment variable to `1`.
209217

210-
--no-optional-locks::
211-
Do not perform optional operations that require locks. This is
212-
equivalent to setting the `GIT_OPTIONAL_LOCKS` to `0`.
213-
214218
--list-cmds=<group>[,<group>...]::
215219
List commands by group. This is an internal/experimental
216220
option and may change or be removed in the future. Supported

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: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ struct cmd_struct {
3636
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"
39-
" [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]\n"
40-
" [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]\n"
41-
" [--config-env=<name>=<envvar>] <command> [<args>]");
39+
" [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--no-lazy-fetch]\n"
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: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
test_description='Test advise_if_enabled functionality'
44

5+
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=trunk
6+
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7+
58
TEST_PASSES_SANITIZE_LEAK=true
69
. ./test-lib.sh
710

@@ -29,4 +32,72 @@ test_expect_success 'advice should not be printed when config variable is set to
2932
test_must_be_empty actual
3033
'
3134

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

0 commit comments

Comments
 (0)