Subject: [PATCH 19/20] worktree: move repo, simple case

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Wed, 3 Feb 2016 16:35:49 +0700

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

diff --git a/builtin/worktree.c b/builtin/worktree.c
index 3ebb9e9..80a8b80 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -477,9 +477,52 @@ static int list(int ac, const char **av, const char *prefix)
 	return 0;
 }
 
+static int move_gitdir(int ac, const char **av, const char *prefix)
+{
+	struct string_list paths = STRING_LIST_INIT_NODUP;
+	struct strbuf dst = STRBUF_INIT;
+	struct strbuf src = STRBUF_INIT;
+	struct worktree **worktrees, *mwt = NULL;
+	int i, ret = 0;
+
+	if (ac != 1)
+		die(_("--repository requires one argument"));
+
+	strbuf_addstr(&src, real_path(get_git_common_dir()));
+	strbuf_addstr(&dst, real_path(prefix_filename(prefix, strlen(prefix), av[0])));
+
+	worktrees = get_worktrees();
+	for (i = 0; worktrees[i]; i++) {
+		if (is_main_worktree(worktrees[i]))
+			mwt = worktrees[i];
+		string_list_append(&paths,
+				   get_worktree_git_dir(worktrees[i]));
+	}
+
+	if (mwt)
+		die(_("converting main worktree is not supported"));
+
+	ret = copy_dir_recursively(src.buf, dst.buf);
+	if (ret)
+		die(_("failed to copy '%s' to '%s'"), src.buf, dst.buf);
+
+	for (i = 0; paths.nr; i++)
+		if (update_worktree_gitfile(paths.items[i].string, dst.buf,
+					    worktrees[i]->id))
+			ret = -1;
+
+	if (!ret)
+		ret = remove_dir_recursively(&src, 0);
+
+	string_list_clear(&paths, 0);
+	return ret;
+}
+
 static int move(int ac, const char **av, const char *prefix)
 {
+	int move_repo = 0;
 	struct option options[] = {
+		OPT_BOOL(0, "repository", &move_repo, N_("move repository")),
 		OPT_END()
 	};
 	struct worktree **worktrees, *wt;
@@ -487,6 +530,8 @@ static int move(int ac, const char **av, const char *prefix)
 	struct strbuf src = STRBUF_INIT;
 
 	ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
+	if (move_repo)
+		return move_gitdir(ac, av, prefix);
 	if (ac != 2)
 		usage_with_options(worktree_usage, options);
 
-- 
2.7.0.377.g4cd97dd