[PATCH 2/4] worktree add: reject separator-only path
- From
René Scharfe <l.s.r@web.de>
- Date
- Aug 25, 2026, 18:03 UTC
- Message-ID
- <20260825180350.2099-3-l.s.r@web.de>
- In-Reply-To
- <20260825180350.2099-1-l.s.r@web.de>
worktree_basename() extracts an empty basename from a path consisting only of zero or more path separators. We can't use that as a worktree name. Properly report such a path as invalid instead of triggering a BUG that asks the user what just happened.
Original-patch-by: Matthias Aßhauer <mha1993@live.de> Signed-off-by: René Scharfe <l.s.r@web.de> --- builtin/worktree.c | 2 ++ t/t2400-worktree-add.sh | 4 ++++ 2 files changed, 6 insertions(+)
diff --git a/builtin/worktree.c b/builtin/worktree.c index a770dd5ead..a53e815cc9 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -491,6 +491,8 @@ static int add_worktree(const char *path, const char *refname, name = worktree_basename(path, &len); strbuf_add(&sb, name, path + len - name); + if (!sb.len) + die(_("invalid path '%s'"), path); sanitize_refname_component(sb.buf, &sb_name); if (!sb_name.len) BUG("How come '%s' becomes empty after sanitization?", sb.buf); diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh index 87b926728a..280d2e2c07 100755 --- a/t/t2400-worktree-add.sh +++ b/t/t2400-worktree-add.sh @@ -46,6 +46,10 @@ test_expect_success '"add" refuses to checkout locked branch' ' test_path_is_missing .git/worktrees/zere ' +test_expect_success '"add" rejects an empty path' ' + test_must_fail git worktree add "" HEAD +' + test_expect_success 'checking out paths not complaining about linked checkouts' ' ( cd existing_empty &&
-- 2.55.0