summaryrefslogtreecommitdiff
path: root/worktree.c
diff options
context:
space:
mode:
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>2018-01-24 09:53:51 (GMT)
committerJunio C Hamano <gitster@pobox.com>2018-01-24 19:04:26 (GMT)
commit4ddddc1f1d763d3c4e0e57af1153c6d48ca4db9b (patch)
treef11fca1c2cedc65196fb297d172c1832e69ea35b /worktree.c
parent8279ed033f703d4115bee620dccd32a9ec94d9aa (diff)
downloadgit-4ddddc1f1d763d3c4e0e57af1153c6d48ca4db9b.zip
git-4ddddc1f1d763d3c4e0e57af1153c6d48ca4db9b.tar.gz
git-4ddddc1f1d763d3c4e0e57af1153c6d48ca4db9b.tar.bz2
worktree.c: add validate_worktree()
This function is later used by "worktree move" and "worktree remove" to ensure that we have a good connection between the repository and the worktree. For example, if a worktree is moved manually, the worktree location recorded in $GIT_DIR/worktrees/.../gitdir is incorrect and we should not move that one. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'worktree.c')
-rw-r--r--worktree.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/worktree.c b/worktree.c
index f5da7d2..b238d87 100644
--- a/worktree.c
+++ b/worktree.c
@@ -254,6 +254,78 @@ const char *is_worktree_locked(struct worktree *wt)
return wt->lock_reason;
}
+/* convenient wrapper to deal with NULL strbuf */
+static void strbuf_addf_gently(struct strbuf *buf, const char *fmt, ...)
+{
+ va_list params;
+
+ if (!buf)
+ return;
+
+ va_start(params, fmt);
+ strbuf_vaddf(buf, fmt, params);
+ va_end(params);
+}
+
+int validate_worktree(const struct worktree *wt, struct strbuf *errmsg)
+{
+ struct strbuf wt_path = STRBUF_INIT;
+ char *path = NULL;
+ int err, ret = -1;
+
+ strbuf_addf(&wt_path, "%s/.git", wt->path);
+
+ if (is_main_worktree(wt)) {
+ if (is_directory(wt_path.buf)) {
+ ret = 0;
+ goto done;
+ }
+ /*
+ * Main worktree using .git file to point to the
+ * repository would make it impossible to know where
+ * the actual worktree is if this function is executed
+ * from another worktree. No .git file support for now.
+ */
+ strbuf_addf_gently(errmsg,
+ _("'%s' at main working tree is not the repository directory"),
+ wt_path.buf);
+ goto done;
+ }
+
+ /*
+ * Make sure "gitdir" file points to a real .git file and that
+ * file points back here.
+ */
+ if (!is_absolute_path(wt->path)) {
+ strbuf_addf_gently(errmsg,
+ _("'%s' file does not contain absolute path to the working tree location"),
+ git_common_path("worktrees/%s/gitdir", wt->id));
+ goto done;
+ }
+
+ if (!file_exists(wt_path.buf)) {
+ strbuf_addf_gently(errmsg, _("'%s' does not exist"), wt_path.buf);
+ goto done;
+ }
+
+ path = xstrdup_or_null(read_gitfile_gently(wt_path.buf, &err));
+ if (!path) {
+ strbuf_addf_gently(errmsg, _("'%s' is not a .git file, error code %d"),
+ wt_path.buf, err);
+ goto done;
+ }
+
+ ret = fspathcmp(path, real_path(git_common_path("worktrees/%s", wt->id)));
+
+ if (ret)
+ strbuf_addf_gently(errmsg, _("'%s' does not point back to '%s'"),
+ wt->path, git_common_path("worktrees/%s", wt->id));
+done:
+ free(path);
+ strbuf_release(&wt_path);
+ return ret;
+}
+
int is_worktree_being_rebased(const struct worktree *wt,
const char *target)
{