| 358 | } |
| 359 | |
| 360 | int validate_worktree(const struct worktree *wt, struct strbuf *errmsg, |
| 361 | unsigned flags) |
| 362 | { |
| 363 | struct strbuf wt_path = STRBUF_INIT; |
| 364 | struct strbuf realpath = STRBUF_INIT; |
| 365 | struct strbuf buf = STRBUF_INIT; |
| 366 | char *path = NULL; |
| 367 | int err, ret = -1; |
| 368 | |
| 369 | strbuf_addf(&wt_path, "%s/.git", wt->path); |
| 370 | |
| 371 | if (is_main_worktree(wt)) { |
| 372 | if (is_directory(wt_path.buf)) { |
| 373 | ret = 0; |
| 374 | goto done; |
| 375 | } |
| 376 | /* |
| 377 | * Main worktree using .git file to point to the |
| 378 | * repository would make it impossible to know where |
| 379 | * the actual worktree is if this function is executed |
| 380 | * from another worktree. No .git file support for now. |
| 381 | */ |
| 382 | strbuf_addf_gently(errmsg, |
| 383 | _("'%s' at main working tree is not the repository directory"), |
| 384 | wt_path.buf); |
| 385 | goto done; |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | * Make sure "gitdir" file points to a real .git file and that |
| 390 | * file points back here. |
| 391 | */ |
| 392 | if (!is_absolute_path(wt->path)) { |
| 393 | strbuf_addf_gently(errmsg, |
| 394 | _("'%s' file does not contain absolute path to the working tree location"), |
| 395 | repo_common_path_replace(the_repository, &buf, "worktrees/%s/gitdir", wt->id)); |
| 396 | goto done; |
| 397 | } |
| 398 | |
| 399 | if (flags & WT_VALIDATE_WORKTREE_MISSING_OK && |
| 400 | !file_exists(wt->path)) { |
| 401 | ret = 0; |
| 402 | goto done; |
| 403 | } |
| 404 | |
| 405 | if (!file_exists(wt_path.buf)) { |
| 406 | strbuf_addf_gently(errmsg, _("'%s' does not exist"), wt_path.buf); |
| 407 | goto done; |
| 408 | } |
| 409 | |
| 410 | path = xstrdup_or_null(read_gitfile_gently(wt_path.buf, &err)); |
| 411 | if (!path) { |
| 412 | strbuf_addf_gently(errmsg, _("'%s' is not a .git file, error code %d"), |
| 413 | wt_path.buf, err); |
| 414 | goto done; |
| 415 | } |
| 416 | |
| 417 | strbuf_realpath(&realpath, repo_common_path_replace(the_repository, &buf, "worktrees/%s", wt->id), 1); |
no test coverage detected