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 <james@jamesliu.io> Signed-off-by: Junio C Hamano <gitster@pobox.com>

James Liu committed May 3, 2024 at 17:17 UTC b79deeb55444f9e01a3f6335d7e0507e23e90190
5 files changed +95 -4
Documentation/git.txt
+6 -2
@@ -12,8 +12,9 @@ SYNOPSIS
12 'git' [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]
13 [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
14 [-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>]
18
19 DESCRIPTION
20 -----------
@@ -190,6 +191,9 @@ If you just want to run git as if it was started in `<path>` then use
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 +
197 --literal-pathspecs::
198 Treat pathspecs literally (i.e. no globbing, no pathspec magic).
199 This is equivalent to setting the `GIT_LITERAL_PATHSPECS` environment
advice.c
+7
@@ -2,6 +2,7 @@
2 #include "advice.h"
3 #include "config.h"
4 #include "color.h"
5 +#include "environment.h"
6 #include "gettext.h"
7 #include "help.h"
8 #include "string-list.h"
@@ -127,6 +128,12 @@ void advise(const char *advice, ...)
128 int advice_enabled(enum advice_type type)
129 {
130 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;
137
138 if (type == ADVICE_PUSH_UPDATE_REJECTED)
139 return enabled &&
environment.h
+7
@@ -57,6 +57,13 @@ const char *getenv_safe(struct strvec *argv, const char *name);
57 #define GIT_TEXT_DOMAIN_DIR_ENVIRONMENT "GIT_TEXTDOMAINDIR"
58 #define GIT_ATTR_SOURCE_ENVIRONMENT "GIT_ATTR_SOURCE"
59
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 +
67 /*
68 * Environment variable used in handshaking the wire protocol.
69 * Contains a colon ':' separated list of keys with optional values
git.c
+7 -2
@@ -37,8 +37,9 @@ const char git_usage_string[] =
37 N_("git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]\n"
38 " [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\n"
39 " [-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>]");
43
44 const char git_more_info_string[] =
45 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)
338 setenv(GIT_ATTR_SOURCE_ENVIRONMENT, cmd, 1);
339 if (envchanged)
340 *envchanged = 1;
341 + } else if (!strcmp(cmd, "--no-advice")) {
342 + setenv(GIT_ADVICE_ENVIRONMENT, "0", 1);
343 + if (envchanged)
344 + *envchanged = 1;
345 } else {
346 fprintf(stderr, _("unknown option: %s\n"), cmd);
347 usage(git_usage_string);
t/t0018-advice.sh
+68
@@ -29,4 +29,72 @@ test_expect_success 'advice should not be printed when config variable is set to
29 test_must_be_empty actual
30 '
31
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 +
100 test_done