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>
This commit is contained in:
parent
8279ed033f
commit
4ddddc1f1d
72
worktree.c
72
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)
|
||||
{
|
||||
|
@ -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)
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user