Signed-off-by: Nguyễn Thái Ngọc Duy <pclo...@gmail.com>
---
 builtin/worktree.c                | 51 +++++++++++++++++++++++++++++++++++++++
 t/t2028-worktree-move.sh (new +x) | 41 +++++++++++++++++++++++++++++++
 2 files changed, 92 insertions(+)
 create mode 100755 t/t2028-worktree-move.sh

diff --git a/builtin/worktree.c b/builtin/worktree.c
index 36d942e..8d59199 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -445,6 +445,55 @@ static int list(int ac, const char **av, const char 
*prefix)
        return 0;
 }
 
+static int move(int ac, const char **av, const char *prefix)
+{
+       struct option options[] = {
+               OPT_END()
+       };
+       struct worktree **worktrees, *wt;
+       struct strbuf dst = STRBUF_INIT;
+       struct strbuf src = STRBUF_INIT;
+
+       ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
+       if (ac != 2)
+               usage_with_options(worktree_usage, options);
+
+       strbuf_addstr(&dst, prefix_filename(prefix,
+                                            strlen(prefix),
+                                            av[1]));
+       if (file_exists(dst.buf))
+               die(_("target '%s' already exists"), av[1]);
+
+       worktrees = get_worktrees();
+       strbuf_addstr(&src, prefix_filename(prefix,
+                                            strlen(prefix),
+                                            av[0]));
+       wt = find_worktree_by_path(worktrees, src.buf);
+       if (!wt)
+               die(_("'%s' is not a working directory"), av[0]);
+       if (is_main_worktree(wt))
+               die(_("'%s' is a main working directory"), av[0]);
+
+       /*
+        * First try. Atomically move, and probably cheaper, if both
+        * source and target are on the same file system.
+        */
+       if (rename(src.buf, dst.buf) == -1) {
+               if (errno != EXDEV)
+                       die_errno(_("failed to move '%s' to '%s'"),
+                                 src.buf, dst.buf);
+
+               /* second try.. */
+               if (copy_dir_recursively(src.buf, dst.buf))
+                       die(_("failed to copy '%s' to '%s'"),
+                           src.buf, dst.buf);
+               else
+                       (void)remove_dir_recursively(&src, 0);
+       }
+
+       return update_worktree_location(wt, dst.buf);
+}
+
 int cmd_worktree(int ac, const char **av, const char *prefix)
 {
        struct option options[] = {
@@ -461,5 +510,7 @@ int cmd_worktree(int ac, const char **av, const char 
*prefix)
                return prune(ac - 1, av + 1, prefix);
        if (!strcmp(av[1], "list"))
                return list(ac - 1, av + 1, prefix);
+       if (!strcmp(av[1], "move"))
+               return move(ac - 1, av + 1, prefix);
        usage_with_options(worktree_usage, options);
 }
diff --git a/t/t2028-worktree-move.sh b/t/t2028-worktree-move.sh
new file mode 100755
index 0000000..e8f6f0c
--- /dev/null
+++ b/t/t2028-worktree-move.sh
@@ -0,0 +1,41 @@
+#!/bin/sh
+
+test_description='test git worktree add'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+       test_commit init &&
+       git worktree add source &&
+       git worktree list --porcelain | grep "^worktree" >actual &&
+       cat <<-EOF >expected &&
+       worktree $TRASH_DIRECTORY
+       worktree $TRASH_DIRECTORY/source
+       EOF
+       test_cmp expected actual
+'
+
+test_expect_success 'move non-worktree' '
+       mkdir abc &&
+       test_must_fail git worktree move abc def
+'
+
+test_expect_success 'move worktree' '
+       git worktree move source destination &&
+       test_path_is_missing source &&
+       git worktree list --porcelain | grep "^worktree" >actual &&
+       cat <<-EOF >expected &&
+       worktree $TRASH_DIRECTORY
+       worktree $TRASH_DIRECTORY/destination
+       EOF
+       test_cmp expected actual &&
+       git -C destination log --format=%s >actual2 &&
+       echo init >expected2 &&
+       test_cmp expected2 actual2
+'
+
+test_expect_success 'move main worktree' '
+       test_must_fail git worktree move . def
+'
+
+test_done
-- 
2.7.0.377.g4cd97dd

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to