worktree.c: add update_worktree_location()
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Mon, 12 Feb 2018 09:49:35 +0000 (16:49 +0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 12 Feb 2018 21:13:35 +0000 (13:13 -0800)
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
worktree.c
worktree.h
index b238d87bf1355f2b0234db5b057a1e8bd01a81f2..0373faf0dc805b78581a92c46149a5bea5567dfe 100644 (file)
@@ -326,6 +326,23 @@ int validate_worktree(const struct worktree *wt, struct strbuf *errmsg)
        return ret;
 }
 
+void update_worktree_location(struct worktree *wt, const char *path_)
+{
+       struct strbuf path = STRBUF_INIT;
+
+       if (is_main_worktree(wt))
+               die("BUG: can't relocate main worktree");
+
+       strbuf_realpath(&path, path_, 1);
+       if (fspathcmp(wt->path, path.buf)) {
+               write_file(git_common_path("worktrees/%s/gitdir", wt->id),
+                          "%s/.git", path.buf);
+               free(wt->path);
+               wt->path = strbuf_detach(&path, NULL);
+       }
+       strbuf_release(&path);
+}
+
 int is_worktree_being_rebased(const struct worktree *wt,
                              const char *target)
 {
index cb577de8cd431b527be839e5de3c92c2d9963e37..a913428c3d62d5cfb94f47e9ae642f86af204087 100644 (file)
@@ -68,6 +68,12 @@ extern const char *is_worktree_locked(struct worktree *wt);
 extern int validate_worktree(const struct worktree *wt,
                             struct strbuf *errmsg);
 
+/*
+ * Update worktrees/xxx/gitdir with the new path.
+ */
+extern void update_worktree_location(struct worktree *wt,
+                                    const char *path_);
+
 /*
  * Free up the memory for worktree(s)
  */