Commit 4ddddc1f authored by Duy Nguyen's avatar Duy Nguyen Committed by Junio C Hamano

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: Duy Nguyen's avatarNguyễn Thái Ngọc Duy <>
Signed-off-by: 's avatarJunio C Hamano <>
parent 8279ed03
......@@ -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)
va_start(params, fmt);
strbuf_vaddf(buf, fmt, 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.
_("'%s' at main working tree is not the repository directory"),
goto done;
* Make sure "gitdir" file points to a real .git file and that
* file points back here.
if (!is_absolute_path(wt->path)) {
_("'%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));
return ret;
int is_worktree_being_rebased(const struct worktree *wt,
const char *target)
......@@ -3,6 +3,8 @@
#include "refs.h"
struct strbuf;
struct worktree {
char *path;
char *id;
......@@ -59,6 +61,13 @@ extern int is_main_worktree(const struct worktree *wt);
extern const char *is_worktree_locked(struct worktree *wt);
* Return zero if the worktree is in good condition. Error message is
* returned if "errmsg" is not NULL.
extern int validate_worktree(const struct worktree *wt,
struct strbuf *errmsg);
* Free up the memory for worktree(s)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment