worktree add: be tolerant of corrupt worktrees

find_worktree() can die() unexpectedly because it uses real_path()
instead of the gentler version. When it's used in 'git worktree add' [1]
and there's a bad worktree, this die() could prevent people from adding
new worktrees.

The "bad" condition to trigger this is when a parent of the worktree's
location is deleted. Then real_path() will complain.

Use the other version so that bad worktrees won't affect 'worktree
add'. The bad ones will eventually be pruned, we just have to tolerate
them for a bit.

[1] added in cb56f55c16 (worktree: disallow adding same path multiple
    times, 2018-08-28), or since v2.20.0. Though the real bug in
    find_worktree() is much older.

Reported-by: Shaheed Haque <shaheedhaque@gmail.com>
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:
Nguyễn Thái Ngọc Duy
2019-05-13 17:49:44 +07:00
committed by Junio C Hamano
parent aeb582a983
commit 105df73e71
2 changed files with 17 additions and 2 deletions

View File

@ -222,9 +222,12 @@ struct worktree *find_worktree(struct worktree **list,
free(to_free);
return NULL;
}
for (; *list; list++)
if (!fspathcmp(path, real_path((*list)->path)))
for (; *list; list++) {
const char *wt_path = real_path_if_valid((*list)->path);
if (wt_path && !fspathcmp(path, wt_path))
break;
}
free(path);
free(to_free);
return *list;