Subject: [PATCH 4/7] worktree move: accept destination as directory

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Wed, 24 Jan 2018 16:53:54 +0700

Similar to "mv a b/", which is actually "mv a b/a", we extract basename
of source worktree and create a directory of the same name at
destination if dst path is a directory.

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 builtin/worktree.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/builtin/worktree.c b/builtin/worktree.c
index 2faa95430a..89398e67e4 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -624,8 +624,6 @@ static int move_worktree(int ac, const char **av, const char *prefix)
 	path = prefix_filename(prefix, av[1]);
 	strbuf_addstr(&dst, path);
 	free(path);
-	if (file_exists(dst.buf))
-		die(_("target '%s' already exists"), av[1]);
 
 	worktrees = get_worktrees(0);
 	wt = find_worktree(worktrees, prefix, av[0]);
@@ -633,6 +631,20 @@ static int move_worktree(int ac, const char **av, const char *prefix)
 		die(_("'%s' is not a working tree"), av[0]);
 	if (is_main_worktree(wt))
 		die(_("'%s' is a main working tree"), av[0]);
+
+	if (is_directory(dst.buf)) {
+		const char *sep = find_last_dir_sep(wt->path);
+
+		if (!sep)
+			die(_("could not figure out destination name from '%s'"),
+			    wt->path);
+		strbuf_addstr(&dst, sep);
+		if (file_exists(dst.buf))
+			die(_("target '%s' already exists"), dst.buf);
+	} else if (file_exists(dst.buf)) {
+		die(_("target '%s' already exists"), av[1]);
+	}
+
 	reason = is_worktree_locked(wt);
 	if (reason) {
 		if (*reason)
-- 
2.16.0.47.g3d9b0fac3a