Skip to content

Commit 74e1219

Browse files
LemmingAvalanchegitster
authored andcommitted
maintenance: use XDG config if it exists
`git maintenance register` registers the repository in the user's global config. `$XDG_CONFIG_HOME/git/config` is supposed to be used if `~/.gitconfig` does not exist. However, this command creates a `~/.gitconfig` file and writes to that one even though the XDG variant exists. This used to work correctly until 50a044f (gc: replace config subprocesses with API calls, 2022-09-27), when the command started calling the config API instead of git-config(1). Also change `unregister` accordingly. Signed-off-by: Kristoffer Haugsbakk <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c15129b commit 74e1219

File tree

2 files changed

+58
-14
lines changed

2 files changed

+58
-14
lines changed

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_paths(&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_paths(&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))

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)