submodule--helper: move "config" to a test-tool

As with other moves to "test-tool" in f322e9f51b5 (Merge branch 'ab/submodule-helper-prep', 2022-09-13) the "config" sub-command was only used by our own tests. It was last used by "git submodule" itself in code that went away with a6226fd772b (submodule--helper: convert the bulk of cmd_add() to C, 2021-08-10). Let's move it over, and while doing so make it easier to reason about by splitting up the various uses for it into separate sub-commands, so that we don't need to count arguments to see what it does. This also has the advantage that we stop wasting future translator time on this command, currently the usage information for this internal-only tool has been translated into several languages. The use of the "_" function has also been removed from the "please make sure..." message. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Taylor Blau <me@ttaylorr.com>

Ævar Arnfjörð Bjarmason committed Nov 8, 2022 at 15:10 UTC cc74a4ac7255510b4a928d4973dcfcc746d74e8f
4 files changed +104 -66
builtin/submodule--helper.c
-46
@@ -2861,51 +2861,6 @@ cleanup:
2861 return ret;
2862 }
2863
2864 -static int module_config(int argc, const char **argv, const char *prefix)
2865 -{
2866 - enum {
2867 - CHECK_WRITEABLE = 1,
2868 - DO_UNSET = 2
2869 - } command = 0;
2870 - struct option module_config_options[] = {
2871 - OPT_CMDMODE(0, "check-writeable", &command,
2872 - N_("check if it is safe to write to the .gitmodules file"),
2873 - CHECK_WRITEABLE),
2874 - OPT_CMDMODE(0, "unset", &command,
2875 - N_("unset the config in the .gitmodules file"),
2876 - DO_UNSET),
2877 - OPT_END()
2878 - };
2879 - const char *const git_submodule_helper_usage[] = {
2880 - N_("git submodule--helper config <name> [<value>]"),
2881 - N_("git submodule--helper config --unset <name>"),
2882 - "git submodule--helper config --check-writeable",
2883 - NULL
2884 - };
2885 -
2886 - argc = parse_options(argc, argv, prefix, module_config_options,
2887 - git_submodule_helper_usage, PARSE_OPT_KEEP_ARGV0);
2888 -
2889 - if (argc == 1 && command == CHECK_WRITEABLE)
2890 - return is_writing_gitmodules_ok() ? 0 : -1;
2891 -
2892 - /* Equivalent to ACTION_GET in builtin/config.c */
2893 - if (argc == 2 && command != DO_UNSET)
2894 - return print_config_from_gitmodules(the_repository, argv[1]);
2895 -
2896 - /* Equivalent to ACTION_SET in builtin/config.c */
2897 - if (argc == 3 || (argc == 2 && command == DO_UNSET)) {
2898 - const char *value = (argc == 3) ? argv[2] : NULL;
2899 -
2900 - if (!is_writing_gitmodules_ok())
2901 - die(_("please make sure that the .gitmodules file is in the working tree"));
2902 -
2903 - return config_set_in_gitmodules_file_gently(argv[1], value);
2904 - }
2905 -
2906 - usage_with_options(git_submodule_helper_usage, module_config_options);
2907 -}
2908 -
2864 static int module_set_url(int argc, const char **argv, const char *prefix)
2865 {
2866 int quiet = 0;
@@ -3424,7 +3379,6 @@ static struct cmd_struct commands[] = {
3379 {"summary", module_summary, 0},
3380 {"push-check", push_check, 0},
3381 {"absorbgitdirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
3427 - {"config", module_config, 0},
3382 {"set-url", module_set_url, 0},
3383 {"set-branch", module_set_branch, 0},
3384 {"create-branch", module_create_branch, 0},
t/helper/test-submodule.c
+84
@@ -111,10 +111,94 @@ static int cmd__submodule_resolve_relative_url(int argc, const char **argv)
111 return 0;
112 }
113
114 +static int cmd__submodule_config_list(int argc, const char **argv)
115 +{
116 + struct option options[] = {
117 + OPT_END()
118 + };
119 + const char *const usage[] = {
120 + "test-tool submodule config-list <key>",
121 + NULL
122 + };
123 + argc = parse_options(argc, argv, "test-tools", options, usage,
124 + PARSE_OPT_KEEP_ARGV0);
125 +
126 + setup_git_directory();
127 +
128 + if (argc == 2)
129 + return print_config_from_gitmodules(the_repository, argv[1]);
130 + usage_with_options(usage, options);
131 +}
132 +
133 +static int cmd__submodule_config_set(int argc, const char **argv)
134 +{
135 + struct option options[] = {
136 + OPT_END()
137 + };
138 + const char *const usage[] = {
139 + "test-tool submodule config-set <key> <value>",
140 + NULL
141 + };
142 + argc = parse_options(argc, argv, "test-tools", options, usage,
143 + PARSE_OPT_KEEP_ARGV0);
144 +
145 + setup_git_directory();
146 +
147 + /* Equivalent to ACTION_SET in builtin/config.c */
148 + if (argc == 3) {
149 + if (!is_writing_gitmodules_ok())
150 + die("please make sure that the .gitmodules file is in the working tree");
151 +
152 + return config_set_in_gitmodules_file_gently(argv[1], argv[2]);
153 + }
154 + usage_with_options(usage, options);
155 +}
156 +
157 +static int cmd__submodule_config_unset(int argc, const char **argv)
158 +{
159 + struct option options[] = {
160 + OPT_END()
161 + };
162 + const char *const usage[] = {
163 + "test-tool submodule config-unset <key>",
164 + NULL
165 + };
166 +
167 + setup_git_directory();
168 +
169 + if (argc == 2) {
170 + if (!is_writing_gitmodules_ok())
171 + die("please make sure that the .gitmodules file is in the working tree");
172 + return config_set_in_gitmodules_file_gently(argv[1], NULL);
173 + }
174 + usage_with_options(usage, options);
175 +}
176 +
177 +static int cmd__submodule_config_writeable(int argc, const char **argv)
178 +{
179 + struct option options[] = {
180 + OPT_END()
181 + };
182 + const char *const usage[] = {
183 + "test-tool submodule config-writeable",
184 + NULL
185 + };
186 + setup_git_directory();
187 +
188 + if (argc == 1)
189 + return is_writing_gitmodules_ok() ? 0 : -1;
190 +
191 + usage_with_options(usage, options);
192 +}
193 +
194 static struct test_cmd cmds[] = {
195 { "check-name", cmd__submodule_check_name },
196 { "is-active", cmd__submodule_is_active },
197 { "resolve-relative-url", cmd__submodule_resolve_relative_url},
198 + { "config-list", cmd__submodule_config_list },
199 + { "config-set", cmd__submodule_config_set },
200 + { "config-unset", cmd__submodule_config_unset },
201 + { "config-writeable", cmd__submodule_config_writeable },
202 };
203
204 int cmd__submodule(int argc, const char **argv)
t/t7411-submodule-config.sh
+18 -18
@@ -137,44 +137,44 @@ test_expect_success 'error in history in fetchrecursesubmodule lets continue' '
137 )
138 '
139
140 -test_expect_success 'reading submodules config from the working tree with "submodule--helper config"' '
140 +test_expect_success 'reading submodules config from the working tree' '
141 (cd super &&
142 echo "../submodule" >expect &&
143 - git submodule--helper config submodule.submodule.url >actual &&
143 + test-tool submodule config-list submodule.submodule.url >actual &&
144 test_cmp expect actual
145 )
146 '
147
148 -test_expect_success 'unsetting submodules config from the working tree with "submodule--helper config --unset"' '
148 +test_expect_success 'unsetting submodules config from the working tree' '
149 (cd super &&
150 - git submodule--helper config --unset submodule.submodule.url &&
151 - git submodule--helper config submodule.submodule.url >actual &&
150 + test-tool submodule config-unset submodule.submodule.url &&
151 + test-tool submodule config-list submodule.submodule.url >actual &&
152 test_must_be_empty actual
153 )
154 '
155
156
157 -test_expect_success 'writing submodules config with "submodule--helper config"' '
157 +test_expect_success 'writing submodules config' '
158 (cd super &&
159 echo "new_url" >expect &&
160 - git submodule--helper config submodule.submodule.url "new_url" &&
161 - git submodule--helper config submodule.submodule.url >actual &&
160 + test-tool submodule config-set submodule.submodule.url "new_url" &&
161 + test-tool submodule config-list submodule.submodule.url >actual &&
162 test_cmp expect actual
163 )
164 '
165
166 -test_expect_success 'overwriting unstaged submodules config with "submodule--helper config"' '
166 +test_expect_success 'overwriting unstaged submodules config' '
167 test_when_finished "git -C super checkout .gitmodules" &&
168 (cd super &&
169 echo "newer_url" >expect &&
170 - git submodule--helper config submodule.submodule.url "newer_url" &&
171 - git submodule--helper config submodule.submodule.url >actual &&
170 + test-tool submodule config-set submodule.submodule.url "newer_url" &&
171 + test-tool submodule config-list submodule.submodule.url >actual &&
172 test_cmp expect actual
173 )
174 '
175
176 test_expect_success 'writeable .gitmodules when it is in the working tree' '
177 - git -C super submodule--helper config --check-writeable
177 + test-tool -C super submodule config-writeable
178 '
179
180 test_expect_success 'writeable .gitmodules when it is nowhere in the repository' '
@@ -183,7 +183,7 @@ test_expect_success 'writeable .gitmodules when it is nowhere in the repository'
183 (cd super &&
184 git rm .gitmodules &&
185 git commit -m "remove .gitmodules from the current branch" &&
186 - git submodule--helper config --check-writeable
186 + test-tool submodule config-writeable
187 )
188 '
189
@@ -191,7 +191,7 @@ test_expect_success 'non-writeable .gitmodules when it is in the index but not i
191 test_when_finished "git -C super checkout .gitmodules" &&
192 (cd super &&
193 rm -f .gitmodules &&
194 - test_must_fail git submodule--helper config --check-writeable
194 + test_must_fail test-tool submodule config-writeable
195 )
196 '
197
@@ -200,7 +200,7 @@ test_expect_success 'non-writeable .gitmodules when it is in the current branch
200 test_when_finished "git -C super reset --hard $ORIG" &&
201 (cd super &&
202 git rm .gitmodules &&
203 - test_must_fail git submodule--helper config --check-writeable
203 + test_must_fail test-tool submodule config-writeable
204 )
205 '
206
@@ -208,11 +208,11 @@ test_expect_success 'reading submodules config from the index when .gitmodules i
208 ORIG=$(git -C super rev-parse HEAD) &&
209 test_when_finished "git -C super reset --hard $ORIG" &&
210 (cd super &&
211 - git submodule--helper config submodule.submodule.url "staged_url" &&
211 + test-tool submodule config-set submodule.submodule.url "staged_url" &&
212 git add .gitmodules &&
213 rm -f .gitmodules &&
214 echo "staged_url" >expect &&
215 - git submodule--helper config submodule.submodule.url >actual &&
215 + test-tool submodule config-list submodule.submodule.url >actual &&
216 test_cmp expect actual
217 )
218 '
@@ -223,7 +223,7 @@ test_expect_success 'reading submodules config from the current branch when .git
223 (cd super &&
224 git rm .gitmodules &&
225 echo "../submodule" >expect &&
226 - git submodule--helper config submodule.submodule.url >actual &&
226 + test-tool submodule config-list submodule.submodule.url >actual &&
227 test_cmp expect actual
228 )
229 '
t/t7418-submodule-sparse-gitmodules.sh
+2 -2
@@ -50,12 +50,12 @@ test_expect_success 'sparse checkout setup which hides .gitmodules' '
50
51 test_expect_success 'reading gitmodules config file when it is not checked out' '
52 echo "../submodule" >expect &&
53 - git -C super submodule--helper config submodule.submodule.url >actual &&
53 + test-tool -C super submodule config-list submodule.submodule.url >actual &&
54 test_cmp expect actual
55 '
56
57 test_expect_success 'not writing gitmodules config file when it is not checked out' '
58 - test_must_fail git -C super submodule--helper config submodule.submodule.url newurl &&
58 + test_must_fail test-tool -C super submodule config-set submodule.submodule.url newurl &&
59 test_path_is_missing super/.gitmodules
60 '
61