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 50a044f1e4 (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 <code@khaugsbakk.name> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Kristoffer Haugsbakk committed Jan 18, 2024 at 17:12 UTC 74e12192e6d2c7747c411261aee247a7479d5703
2 files changed +58 -14
builtin/gc.c
+13 -14
@@ -1543,19 +1543,18 @@ static int maintenance_register(int argc, const char **argv, const char *prefix)
1543
1544 if (!found) {
1545 int rc;
1546 - char *user_config = NULL, *xdg_config = NULL;
1546 + char *global_config_file = NULL;
1547
1548 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;
1551 }
1552 + if (!config_file)
1553 + die(_("$HOME not set"));
1554 rc = git_config_set_multivar_in_file_gently(
1555 config_file, "maintenance.repo", maintpath,
1556 CONFIG_REGEX_NONE, 0);
1557 - free(user_config);
1558 - free(xdg_config);
1557 + free(global_config_file);
1558
1559 if (rc)
1560 die(_("unable to add '%s' value of '%s'"),
@@ -1612,18 +1611,18 @@ static int maintenance_unregister(int argc, const char **argv, const char *prefi
1611
1612 if (found) {
1613 int rc;
1615 - char *user_config = NULL, *xdg_config = NULL;
1614 + char *global_config_file = NULL;
1615 +
1616 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;
1619 }
1620 + if (!config_file)
1621 + die(_("$HOME not set"));
1622 rc = git_config_set_multivar_in_file_gently(
1623 config_file, key, NULL, maintpath,
1624 CONFIG_FLAGS_MULTI_REPLACE | CONFIG_FLAGS_FIXED_VALUE);
1625 - free(user_config);
1626 - free(xdg_config);
1625 + free(global_config_file);
1626
1627 if (rc &&
1628 (!force || rc == CONFIG_NOTHING_SET))
t/t7900-maintenance.sh
+45
@@ -67,6 +67,51 @@ test_expect_success 'maintenance.auto config option' '
67 test_subcommand ! git maintenance run --auto --quiet <false
68 '
69
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 +
115 test_expect_success 'maintenance.<task>.enabled' '
116 git config maintenance.gc.enabled false &&
117 git config maintenance.commit-graph.enabled true &&