Skip to content

Commit 3ea3696

Browse files
phillipwoodgitster
authored andcommitted
config: warn on core.commentString=auto
As support for this setting was deprecated in the last commit print a warning (or die when WITH_BREAKING_CHANGES is enabled) if it is set. When printing a warning avoid bombarding the user by only printing it when running commands commands that run "git commit" and only only once per command. Some scaffolding is added to repo_read_config() to allow it to detect deprecated config settings and warn about them. As both "core.commentChar" and "core.commentString" set the comment character we record which one of them is used and tailor the warning message appropriately. Note the odd combination of die_message() followed by die(NULL) is to allow the next commit to insert a call to advise() in the middle. Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 65791c7 commit 3ea3696

File tree

11 files changed

+158
-4
lines changed

11 files changed

+158
-4
lines changed

builtin/commit.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,6 +1783,9 @@ int cmd_commit(int argc,
17831783
show_usage_with_options_if_asked(argc, argv,
17841784
builtin_commit_usage, builtin_commit_options);
17851785

1786+
#ifndef WITH_BREAKING_CHANGES
1787+
warn_on_auto_comment_char = true;
1788+
#endif /* !WITH_BREAKING_CHANGES */
17861789
prepare_repo_settings(the_repository);
17871790
the_repository->settings.command_requires_full_index = 0;
17881791

builtin/merge.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,9 @@ int cmd_merge(int argc,
13781378
show_usage_with_options_if_asked(argc, argv,
13791379
builtin_merge_usage, builtin_merge_options);
13801380

1381+
#ifndef WITH_BREAKING_CHANGES
1382+
warn_on_auto_comment_char = true;
1383+
#endif /* !WITH_BREAKING_CHANGES */
13811384
prepare_repo_settings(the_repository);
13821385
the_repository->settings.command_requires_full_index = 0;
13831386

builtin/rebase.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,9 @@ int cmd_rebase(int argc,
12421242
builtin_rebase_usage,
12431243
builtin_rebase_options);
12441244

1245+
#ifndef WITH_BREAKING_CHANGES
1246+
warn_on_auto_comment_char = true;
1247+
#endif /* !WITH_BREAKING_CHANGES */
12451248
prepare_repo_settings(the_repository);
12461249
the_repository->settings.command_requires_full_index = 0;
12471250

builtin/revert.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "builtin.h"
55
#include "parse-options.h"
66
#include "diff.h"
7+
#include "environment.h"
78
#include "gettext.h"
89
#include "revision.h"
910
#include "rerere.h"
@@ -285,6 +286,9 @@ int cmd_revert(int argc,
285286
struct replay_opts opts = REPLAY_OPTS_INIT;
286287
int res;
287288

289+
#ifndef WITH_BREAKING_CHANGES
290+
warn_on_auto_comment_char = true;
291+
#endif /* !WITH_BREAKING_CHANGES */
288292
opts.action = REPLAY_REVERT;
289293
sequencer_init_config(&opts);
290294
res = run_sequencer(argc, argv, prefix, &opts);
@@ -302,6 +306,9 @@ struct repository *repo UNUSED)
302306
struct replay_opts opts = REPLAY_OPTS_INIT;
303307
int res;
304308

309+
#ifndef WITH_BREAKING_CHANGES
310+
warn_on_auto_comment_char = true;
311+
#endif /* !WITH_BREAKING_CHANGES */
305312
opts.action = REPLAY_PICK;
306313
sequencer_init_config(&opts);
307314
res = run_sequencer(argc, argv, prefix, &opts);

config.c

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88

99
#include "git-compat-util.h"
1010
#include "abspath.h"
11+
#include "advice.h"
1112
#include "date.h"
1213
#include "branch.h"
1314
#include "config.h"
15+
#include "dir.h"
1416
#include "parse.h"
1517
#include "convert.h"
1618
#include "environment.h"
@@ -1951,10 +1953,110 @@ int git_configset_get_pathname(struct config_set *set, const char *key, char **d
19511953
return 1;
19521954
}
19531955

1956+
struct comment_char_config {
1957+
unsigned last_key_id;
1958+
bool auto_set;
1959+
};
1960+
1961+
#define COMMENT_CHAR_CFG_INIT { 0 }
1962+
1963+
static const char* comment_key_name(unsigned id)
1964+
{
1965+
static const char *name[] = {
1966+
"core.commentChar",
1967+
"core.commentString",
1968+
};
1969+
1970+
if (id >= ARRAY_SIZE(name))
1971+
BUG("invalid comment key id");
1972+
1973+
return name[id];
1974+
}
1975+
1976+
static void comment_char_callback(const char *key, const char *value,
1977+
const struct config_context *ctx UNUSED,
1978+
void *data)
1979+
{
1980+
struct comment_char_config *config = data;
1981+
unsigned key_id;
1982+
1983+
if (!strcmp(key, "core.commentchar"))
1984+
key_id = 0;
1985+
else if (!strcmp(key, "core.commentstring"))
1986+
key_id = 1;
1987+
else
1988+
return;
1989+
1990+
config->last_key_id = key_id;
1991+
config->auto_set = value && !strcmp(value, "auto");
1992+
}
1993+
1994+
struct repo_config {
1995+
struct repository *repo;
1996+
struct comment_char_config comment_char_config;
1997+
};
1998+
1999+
#define REPO_CONFIG_INIT(repo_) { \
2000+
.comment_char_config = COMMENT_CHAR_CFG_INIT, \
2001+
.repo = repo_, \
2002+
};
2003+
2004+
#ifdef WITH_BREAKING_CHANGES
2005+
static void check_auto_comment_char_config(struct comment_char_config *config)
2006+
{
2007+
if (!config->auto_set)
2008+
return;
2009+
2010+
die_message(_("Support for '%s=auto' has been removed in Git 3.0"),
2011+
comment_key_name(config->last_key_id));
2012+
die(NULL);
2013+
}
2014+
#else
2015+
static void check_auto_comment_char_config(struct comment_char_config *config)
2016+
{
2017+
extern bool warn_on_auto_comment_char;
2018+
const char *DEPRECATED_CONFIG_ENV =
2019+
"GIT_AUTO_COMMENT_CHAR_CONFIG_WARNING_GIVEN";
2020+
2021+
if (!config->auto_set || !warn_on_auto_comment_char)
2022+
return;
2023+
2024+
/*
2025+
* Use an environment variable to ensure that subprocesses do not repeat
2026+
* the warning.
2027+
*/
2028+
if (git_env_bool(DEPRECATED_CONFIG_ENV, false))
2029+
return;
2030+
2031+
setenv(DEPRECATED_CONFIG_ENV, "true", true);
2032+
2033+
warning(_("Support for '%s=auto' is deprecated and will be removed in "
2034+
"Git 3.0"), comment_key_name(config->last_key_id));
2035+
}
2036+
#endif /* WITH_BREAKING_CHANGES */
2037+
2038+
static void check_deprecated_config(struct repo_config *config)
2039+
{
2040+
if (!config->repo->check_deprecated_config)
2041+
return;
2042+
2043+
check_auto_comment_char_config(&config->comment_char_config);
2044+
}
2045+
2046+
static int repo_config_callback(const char *key, const char *value,
2047+
const struct config_context *ctx, void *data)
2048+
{
2049+
struct repo_config *config = data;
2050+
2051+
comment_char_callback(key, value, ctx, &config->comment_char_config);
2052+
return config_set_callback(key, value, ctx, config->repo->config);
2053+
}
2054+
19542055
/* Functions use to read configuration from a repository */
19552056
static void repo_read_config(struct repository *repo)
19562057
{
19572058
struct config_options opts = { 0 };
2059+
struct repo_config config = REPO_CONFIG_INIT(repo);
19582060

19592061
opts.respect_includes = 1;
19602062
opts.commondir = repo->commondir;
@@ -1966,8 +2068,8 @@ static void repo_read_config(struct repository *repo)
19662068
git_configset_clear(repo->config);
19672069

19682070
git_configset_init(repo->config);
1969-
if (config_with_options(config_set_callback, repo->config, NULL,
1970-
repo, &opts) < 0)
2071+
if (config_with_options(repo_config_callback, &config, NULL, repo,
2072+
&opts) < 0)
19712073
/*
19722074
* config_with_options() normally returns only
19732075
* zero, as most errors are fatal, and
@@ -1980,6 +2082,7 @@ static void repo_read_config(struct repository *repo)
19802082
* immediately.
19812083
*/
19822084
die(_("unknown error occurred while reading the configuration files"));
2085+
check_deprecated_config(&config);
19832086
}
19842087

19852088
static void git_config_check_init(struct repository *repo)
@@ -2667,6 +2770,14 @@ int repo_config_set_multivar_in_file_gently(struct repository *r,
26672770
char *contents = NULL;
26682771
size_t contents_sz;
26692772
struct config_store_data store = CONFIG_STORE_INIT;
2773+
bool saved_check_deprecated_config = r->check_deprecated_config;
2774+
2775+
/*
2776+
* Do not warn or die if there are deprecated config settings as
2777+
* we want the user to be able to change those settings by running
2778+
* "git config".
2779+
*/
2780+
r->check_deprecated_config = false;
26702781

26712782
validate_comment_string(comment);
26722783

@@ -2898,6 +3009,7 @@ int repo_config_set_multivar_in_file_gently(struct repository *r,
28983009
if (in_fd >= 0)
28993010
close(in_fd);
29003011
config_store_data_clear(&store);
3012+
r->check_deprecated_config = saved_check_deprecated_config;
29013013
return ret;
29023014

29033015
write_err_out:

environment.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ const char *comment_line_str = "#";
124124
char *comment_line_str_to_free;
125125
#ifndef WITH_BREAKING_CHANGES
126126
int auto_comment_line_char;
127+
bool warn_on_auto_comment_char;
127128
#endif /* !WITH_BREAKING_CHANGES */
128129

129130
/* This is set by setup_git_directory_gently() and/or git_default_config() */

environment.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ extern const char *comment_line_str;
210210
extern char *comment_line_str_to_free;
211211
#ifndef WITH_BREAKING_CHANGES
212212
extern int auto_comment_line_char;
213+
extern bool warn_on_auto_comment_char;
213214
#endif /* !WITH_BREAKING_CHANGES */
214215

215216
# endif /* USE_THE_REPOSITORY_VARIABLE */

repository.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ void initialize_repository(struct repository *repo)
5757
repo->parsed_objects = parsed_object_pool_new(repo);
5858
ALLOC_ARRAY(repo->index, 1);
5959
index_state_init(repo->index, repo);
60+
repo->check_deprecated_config = true;
6061

6162
/*
6263
* When a command runs inside a repository, it learns what

repository.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ struct repository {
161161

162162
/* Indicate if a repository has a different 'commondir' from 'gitdir' */
163163
unsigned different_commondir:1;
164+
165+
/* Should repo_config() check for deprecated settings */
166+
bool check_deprecated_config;
164167
};
165168

166169
#ifdef USE_THE_REPOSITORY_VARIABLE

t/t3404-rebase-interactive.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1184,8 +1184,13 @@ test_expect_success !WITH_BREAKING_CHANGES 'rebase -i respects core.commentchar=
11841184
test_when_finished "git rebase --abort || :" &&
11851185
(
11861186
test_set_editor "$(pwd)/copy-edit-script.sh" &&
1187-
git rebase -i HEAD^
1187+
git rebase -i HEAD^ 2>err
11881188
) &&
1189+
sed -n "s/^warning: //p" err >actual &&
1190+
cat >expect <<-EOF &&
1191+
Support for ${SQ}core.commentChar=auto${SQ} is deprecated and will be removed in Git 3.0
1192+
EOF
1193+
test_cmp expect actual &&
11891194
test -z "$(grep -ve "^#" -e "^\$" -e "^pick" edit-script)"
11901195
'
11911196

0 commit comments

Comments
 (0)