worktree: extract copy_filtered_worktree_config()

This logic was introduced by 5325591 (worktree: copy sparse-checkout patterns and config on add, 2022-02-07), but some feedback came in that the add_worktree() method was already too complex. It is better to extract this logic into a helper method to reduce this complexity. Reported-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Feb 23, 2022 at 14:29 UTC 863970536525f071b29f2ee73ac9ac0a35b32a43
1 file changed +42 -39
builtin/worktree.c
+42 -39
@@ -236,6 +236,46 @@ static void check_candidate_path(const char *path,
236 die(_("'%s' is a missing but already registered worktree;\nuse '%s -f' to override, or 'prune' or 'remove' to clear"), path, cmd);
237 }
238
239 +static void copy_filtered_worktree_config(const char *worktree_git_dir)
240 +{
241 + char *from_file = git_pathdup("config.worktree");
242 + char *to_file = xstrfmt("%s/config.worktree", worktree_git_dir);
243 +
244 + if (file_exists(from_file)) {
245 + struct config_set cs = { { 0 } };
246 + const char *core_worktree;
247 + int bare;
248 +
249 + if (safe_create_leading_directories(to_file) ||
250 + copy_file(to_file, from_file, 0666)) {
251 + error(_("failed to copy worktree config from '%s' to '%s'"),
252 + from_file, to_file);
253 + goto worktree_copy_cleanup;
254 + }
255 +
256 + git_configset_init(&cs);
257 + git_configset_add_file(&cs, from_file);
258 +
259 + if (!git_configset_get_bool(&cs, "core.bare", &bare) &&
260 + bare &&
261 + git_config_set_multivar_in_file_gently(
262 + to_file, "core.bare", NULL, "true", 0))
263 + error(_("failed to unset '%s' in '%s'"),
264 + "core.bare", to_file);
265 + if (!git_configset_get_value(&cs, "core.worktree", &core_worktree) &&
266 + git_config_set_in_file_gently(to_file,
267 + "core.worktree", NULL))
268 + error(_("failed to unset '%s' in '%s'"),
269 + "core.worktree", to_file);
270 +
271 + git_configset_clear(&cs);
272 + }
273 +
274 +worktree_copy_cleanup:
275 + free(from_file);
276 + free(to_file);
277 +}
278 +
279 static int add_worktree(const char *path, const char *refname,
280 const struct add_opts *opts)
281 {
@@ -360,45 +400,8 @@ static int add_worktree(const char *path, const char *refname,
400 * values from the current worktree into the new one, that way the
401 * new worktree behaves the same as this one.
402 */
363 - if (repository_format_worktree_config) {
364 - char *from_file = git_pathdup("config.worktree");
365 - char *to_file = xstrfmt("%s/config.worktree",
366 - sb_repo.buf);
367 -
368 - if (file_exists(from_file)) {
369 - struct config_set cs = { { 0 } };
370 - const char *core_worktree;
371 - int bare;
372 -
373 - if (safe_create_leading_directories(to_file) ||
374 - copy_file(to_file, from_file, 0666)) {
375 - error(_("failed to copy worktree config from '%s' to '%s'"),
376 - from_file, to_file);
377 - goto worktree_copy_cleanup;
378 - }
379 -
380 - git_configset_init(&cs);
381 - git_configset_add_file(&cs, from_file);
382 -
383 - if (!git_configset_get_bool(&cs, "core.bare", &bare) &&
384 - bare &&
385 - git_config_set_multivar_in_file_gently(
386 - to_file, "core.bare", NULL, "true", 0))
387 - error(_("failed to unset '%s' in '%s'"),
388 - "core.bare", to_file);
389 - if (!git_configset_get_value(&cs, "core.worktree", &core_worktree) &&
390 - git_config_set_in_file_gently(to_file,
391 - "core.worktree", NULL))
392 - error(_("failed to unset '%s' in '%s'"),
393 - "core.worktree", to_file);
394 -
395 - git_configset_clear(&cs);
396 - }
397 -
398 -worktree_copy_cleanup:
399 - free(from_file);
400 - free(to_file);
401 - }
403 + if (repository_format_worktree_config)
404 + copy_filtered_worktree_config(sb_repo.buf);
405
406 strvec_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf);
407 strvec_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);