Subject: [PATCH 12/20] worktree.c: add update_worktree_location()

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

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 worktree.c | 23 +++++++++++++++++++++++
 worktree.h |  6 ++++++
 2 files changed, 29 insertions(+)

diff --git a/worktree.c b/worktree.c
index 80c525b..a11c053 100644
--- a/worktree.c
+++ b/worktree.c
@@ -221,6 +221,29 @@ int is_main_worktree(const struct worktree *wt)
 	return wt && !wt->id;
 }
 
+int update_worktree_location(struct worktree *wt, const char *path_)
+{
+	struct strbuf path = STRBUF_INIT;
+	int ret;
+
+	if (is_main_worktree(wt))
+		return 0;
+
+	strbuf_add_absolute_path(&path, path_);
+	if (strcmp_icase(wt->path, path.buf)) {
+		if (!write_file_gently(git_common_path("worktrees/%s/gitdir", wt->id),
+				       "%s/.git", path.buf)) {
+			free(wt->path);
+			wt->path = strbuf_detach(&path, NULL);
+			ret = 0;
+		} else
+			ret = sys_error(_("failed to update '%s' for update"),
+					git_common_path("worktrees/%s/gitdir", wt->id));
+	}
+	strbuf_release(&path);
+	return ret;
+}
+
 char *find_shared_symref(const char *symref, const char *target)
 {
 	char *existing = NULL;
diff --git a/worktree.h b/worktree.h
index c7a4d20..4c0395a 100644
--- a/worktree.h
+++ b/worktree.h
@@ -39,6 +39,12 @@ extern struct worktree *find_worktree_by_path(struct worktree **list,
 extern int is_main_worktree(const struct worktree *wt);
 
 /*
+ * Update worktrees/xxx/gitdir with the new path.
+ */
+extern int update_worktree_location(struct worktree *wt,
+				    const char *path_);
+
+/*
  * Free up the memory for worktree
  */
 extern void clear_worktree(struct worktree *);
-- 
2.7.0.377.g4cd97dd