set_work_tree: use chdir_notify
[gitweb.git] / worktree.c
index b238d87bf1355f2b0234db5b057a1e8bd01a81f2..28989cf06ef4bd15b3b97f86ae9fd32c4b78401f 100644 (file)
@@ -267,7 +267,8 @@ static void strbuf_addf_gently(struct strbuf *buf, const char *fmt, ...)
        va_end(params);
 }
 
-int validate_worktree(const struct worktree *wt, struct strbuf *errmsg)
+int validate_worktree(const struct worktree *wt, struct strbuf *errmsg,
+                     unsigned flags)
 {
        struct strbuf wt_path = STRBUF_INIT;
        char *path = NULL;
@@ -303,6 +304,12 @@ int validate_worktree(const struct worktree *wt, struct strbuf *errmsg)
                goto done;
        }
 
+       if (flags & WT_VALIDATE_WORKTREE_MISSING_OK &&
+           !file_exists(wt->path)) {
+               ret = 0;
+               goto done;
+       }
+
        if (!file_exists(wt_path.buf)) {
                strbuf_addf_gently(errmsg, _("'%s' does not exist"), wt_path.buf);
                goto done;
@@ -326,6 +333,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)
 {