worktree: prepare for more checks of whether path can become worktree

Certain conditions must be met for a path to be a valid candidate as the location of a new worktree; for instance, the path must not exist or must be an empty directory. Although the number of conditions is small, new conditions will soon be added so factor out the existing checks into a separate function to avoid further bloating add_worktree(). Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Aug 28, 2018 at 17:20 UTC 45059e6468ab5710748c8827d5bf1f4c7c69d6d1
1 file changed +7 -2
builtin/worktree.c
+7 -2
@@ -219,6 +219,12 @@ static const char *worktree_basename(const char *path, int *olen)
219 return name;
220 }
221
222 +static void validate_worktree_add(const char *path, const struct add_opts *opts)
223 +{
224 + if (file_exists(path) && !is_empty_dir(path))
225 + die(_("'%s' already exists"), path);
226 +}
227 +
228 static int add_worktree(const char *path, const char *refname,
229 const struct add_opts *opts)
230 {
@@ -233,8 +239,7 @@ static int add_worktree(const char *path, const char *refname,
239 struct commit *commit = NULL;
240 int is_branch = 0;
241
236 - if (file_exists(path) && !is_empty_dir(path))
237 - die(_("'%s' already exists"), path);
242 + validate_worktree_add(path, opts);
243
244 /* is 'refname' a branch or commit? */
245 if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&