Skip to content

Commit ace5ac5

Browse files
derrickstoleegitster
authored andcommitted
worktree: extract copy_sparse_checkout()
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 <[email protected]> Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8639705 commit ace5ac5

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

builtin/worktree.c

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,22 @@ static void check_candidate_path(const char *path,
236236
die(_("'%s' is a missing but already registered worktree;\nuse '%s -f' to override, or 'prune' or 'remove' to clear"), path, cmd);
237237
}
238238

239+
static void copy_sparse_checkout(const char *worktree_git_dir)
240+
{
241+
char *from_file = git_pathdup("info/sparse-checkout");
242+
char *to_file = xstrfmt("%s/info/sparse-checkout", worktree_git_dir);
243+
244+
if (file_exists(from_file)) {
245+
if (safe_create_leading_directories(to_file) ||
246+
copy_file(to_file, from_file, 0666))
247+
error(_("failed to copy '%s' to '%s'; sparse-checkout may not work correctly"),
248+
from_file, to_file);
249+
}
250+
251+
free(from_file);
252+
free(to_file);
253+
}
254+
239255
static void copy_filtered_worktree_config(const char *worktree_git_dir)
240256
{
241257
char *from_file = git_pathdup("config.worktree");
@@ -379,21 +395,8 @@ static int add_worktree(const char *path, const char *refname,
379395
* If the current worktree has sparse-checkout enabled, then copy
380396
* the sparse-checkout patterns from the current worktree.
381397
*/
382-
if (core_apply_sparse_checkout) {
383-
char *from_file = git_pathdup("info/sparse-checkout");
384-
char *to_file = xstrfmt("%s/info/sparse-checkout",
385-
sb_repo.buf);
386-
387-
if (file_exists(from_file)) {
388-
if (safe_create_leading_directories(to_file) ||
389-
copy_file(to_file, from_file, 0666))
390-
error(_("failed to copy '%s' to '%s'; sparse-checkout may not work correctly"),
391-
from_file, to_file);
392-
}
393-
394-
free(from_file);
395-
free(to_file);
396-
}
398+
if (core_apply_sparse_checkout)
399+
copy_sparse_checkout(sb_repo.buf);
397400

398401
/*
399402
* If we are using worktree config, then copy all current config

0 commit comments

Comments
 (0)