Skip to content

Commit 12ee4ed

Browse files
committed
Merge branch 'kh/maintenance-use-xdg-when-it-should'
When $HOME/.gitignore is missing but XDG config file available, we should write into the latter, not former. "git gc" and "git maintenance" wrote into a wrong "global config" file, which have been corrected. * kh/maintenance-use-xdg-when-it-should: maintenance: use XDG config if it exists config: factor out global config file retrieval config: rename global config function config: format newlines
2 parents 2be9ccf + 74e1219 commit 12ee4ed

File tree

6 files changed

+86
-43
lines changed

6 files changed

+86
-43
lines changed

builtin/config.c

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -708,30 +708,11 @@ int cmd_config(int argc, const char **argv, const char *prefix)
708708
}
709709

710710
if (use_global_config) {
711-
char *user_config, *xdg_config;
712-
713-
git_global_config(&user_config, &xdg_config);
714-
if (!user_config)
715-
/*
716-
* It is unknown if HOME/.gitconfig exists, so
717-
* we do not know if we should write to XDG
718-
* location; error out even if XDG_CONFIG_HOME
719-
* is set and points at a sane location.
720-
*/
711+
given_config_source.file = git_global_config();
712+
if (!given_config_source.file)
721713
die(_("$HOME not set"));
722-
723714
given_config_source.scope = CONFIG_SCOPE_GLOBAL;
724-
725-
if (access_or_warn(user_config, R_OK, 0) &&
726-
xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
727-
given_config_source.file = xdg_config;
728-
free(user_config);
729-
} else {
730-
given_config_source.file = user_config;
731-
free(xdg_config);
732-
}
733-
}
734-
else if (use_system_config) {
715+
} else if (use_system_config) {
735716
given_config_source.file = git_system_config();
736717
given_config_source.scope = CONFIG_SCOPE_SYSTEM;
737718
} else if (use_local_config) {
@@ -760,7 +741,6 @@ int cmd_config(int argc, const char **argv, const char *prefix)
760741
given_config_source.scope = CONFIG_SCOPE_COMMAND;
761742
}
762743

763-
764744
if (respect_includes_opt == -1)
765745
config_options.respect_includes = !given_config_source.file;
766746
else

builtin/gc.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,19 +1543,18 @@ static int maintenance_register(int argc, const char **argv, const char *prefix)
15431543

15441544
if (!found) {
15451545
int rc;
1546-
char *user_config = NULL, *xdg_config = NULL;
1546+
char *global_config_file = NULL;
15471547

15481548
if (!config_file) {
1549-
git_global_config(&user_config, &xdg_config);
1550-
config_file = user_config;
1551-
if (!user_config)
1552-
die(_("$HOME not set"));
1549+
global_config_file = git_global_config();
1550+
config_file = global_config_file;
15531551
}
1552+
if (!config_file)
1553+
die(_("$HOME not set"));
15541554
rc = git_config_set_multivar_in_file_gently(
15551555
config_file, "maintenance.repo", maintpath,
15561556
CONFIG_REGEX_NONE, 0);
1557-
free(user_config);
1558-
free(xdg_config);
1557+
free(global_config_file);
15591558

15601559
if (rc)
15611560
die(_("unable to add '%s' value of '%s'"),
@@ -1612,18 +1611,18 @@ static int maintenance_unregister(int argc, const char **argv, const char *prefi
16121611

16131612
if (found) {
16141613
int rc;
1615-
char *user_config = NULL, *xdg_config = NULL;
1614+
char *global_config_file = NULL;
1615+
16161616
if (!config_file) {
1617-
git_global_config(&user_config, &xdg_config);
1618-
config_file = user_config;
1619-
if (!user_config)
1620-
die(_("$HOME not set"));
1617+
global_config_file = git_global_config();
1618+
config_file = global_config_file;
16211619
}
1620+
if (!config_file)
1621+
die(_("$HOME not set"));
16221622
rc = git_config_set_multivar_in_file_gently(
16231623
config_file, key, NULL, maintpath,
16241624
CONFIG_FLAGS_MULTI_REPLACE | CONFIG_FLAGS_FIXED_VALUE);
1625-
free(user_config);
1626-
free(xdg_config);
1625+
free(global_config_file);
16271626

16281627
if (rc &&
16291628
(!force || rc == CONFIG_NOTHING_SET))

builtin/var.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static char *git_config_val_global(int ident_flag UNUSED)
9090
char *user, *xdg;
9191
size_t unused;
9292

93-
git_global_config(&user, &xdg);
93+
git_global_config_paths(&user, &xdg);
9494
if (xdg && *xdg) {
9595
normalize_path_copy(xdg, xdg);
9696
strbuf_addf(&buf, "%s\n", xdg);

config.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ static long config_file_ftell(struct config_source *conf)
9595
return ftell(conf->u.file);
9696
}
9797

98-
9998
static int config_buf_fgetc(struct config_source *conf)
10099
{
101100
if (conf->u.buf.pos < conf->u.buf.len)
@@ -1988,7 +1987,27 @@ char *git_system_config(void)
19881987
return system_config;
19891988
}
19901989

1991-
void git_global_config(char **user_out, char **xdg_out)
1990+
char *git_global_config(void)
1991+
{
1992+
char *user_config, *xdg_config;
1993+
1994+
git_global_config_paths(&user_config, &xdg_config);
1995+
if (!user_config) {
1996+
free(xdg_config);
1997+
return NULL;
1998+
}
1999+
2000+
if (access_or_warn(user_config, R_OK, 0) && xdg_config &&
2001+
!access_or_warn(xdg_config, R_OK, 0)) {
2002+
free(user_config);
2003+
return xdg_config;
2004+
} else {
2005+
free(xdg_config);
2006+
return user_config;
2007+
}
2008+
}
2009+
2010+
void git_global_config_paths(char **user_out, char **xdg_out)
19922011
{
19932012
char *user_config = xstrdup_or_null(getenv("GIT_CONFIG_GLOBAL"));
19942013
char *xdg_config = NULL;
@@ -2041,7 +2060,7 @@ static int do_git_config_sequence(const struct config_options *opts,
20412060
data, CONFIG_SCOPE_SYSTEM,
20422061
NULL);
20432062

2044-
git_global_config(&user_config, &xdg_config);
2063+
git_global_config_paths(&user_config, &xdg_config);
20452064

20462065
if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK))
20472066
ret += git_config_from_file_with_options(fn, xdg_config, data,
@@ -3418,7 +3437,6 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
34183437
write_err_out:
34193438
ret = write_error(get_lock_file_path(&lock));
34203439
goto out_free;
3421-
34223440
}
34233441

34243442
void git_config_set_multivar_in_file(const char *config_filename,

config.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,8 @@ int config_error_nonbool(const char *);
382382
#endif
383383

384384
char *git_system_config(void);
385-
void git_global_config(char **user, char **xdg);
385+
char *git_global_config(void);
386+
void git_global_config_paths(char **user, char **xdg);
386387

387388
int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
388389

t/t7900-maintenance.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,51 @@ test_expect_success 'maintenance.auto config option' '
6767
test_subcommand ! git maintenance run --auto --quiet <false
6868
'
6969

70+
test_expect_success 'register uses XDG_CONFIG_HOME config if it exists' '
71+
test_when_finished rm -r .config/git/config &&
72+
(
73+
XDG_CONFIG_HOME=.config &&
74+
export XDG_CONFIG_HOME &&
75+
mkdir -p $XDG_CONFIG_HOME/git &&
76+
>$XDG_CONFIG_HOME/git/config &&
77+
git maintenance register &&
78+
git config --file=$XDG_CONFIG_HOME/git/config --get maintenance.repo >actual &&
79+
pwd >expect &&
80+
test_cmp expect actual
81+
)
82+
'
83+
84+
test_expect_success 'register does not need XDG_CONFIG_HOME config to exist' '
85+
test_when_finished git maintenance unregister &&
86+
test_path_is_missing $XDG_CONFIG_HOME/git/config &&
87+
git maintenance register &&
88+
git config --global --get maintenance.repo >actual &&
89+
pwd >expect &&
90+
test_cmp expect actual
91+
'
92+
93+
test_expect_success 'unregister uses XDG_CONFIG_HOME config if it exists' '
94+
test_when_finished rm -r .config/git/config &&
95+
(
96+
XDG_CONFIG_HOME=.config &&
97+
export XDG_CONFIG_HOME &&
98+
mkdir -p $XDG_CONFIG_HOME/git &&
99+
>$XDG_CONFIG_HOME/git/config &&
100+
git maintenance register &&
101+
git maintenance unregister &&
102+
test_must_fail git config --file=$XDG_CONFIG_HOME/git/config --get maintenance.repo >actual &&
103+
test_must_be_empty actual
104+
)
105+
'
106+
107+
test_expect_success 'unregister does not need XDG_CONFIG_HOME config to exist' '
108+
test_path_is_missing $XDG_CONFIG_HOME/git/config &&
109+
git maintenance register &&
110+
git maintenance unregister &&
111+
test_must_fail git config --global --get maintenance.repo >actual &&
112+
test_must_be_empty actual
113+
'
114+
70115
test_expect_success 'maintenance.<task>.enabled' '
71116
git config maintenance.gc.enabled false &&
72117
git config maintenance.commit-graph.enabled true &&

0 commit comments

Comments
 (0)