Commit

382f88157754097baad9fb0b45513d9e9135a63e

worktree add: reject separator-only path

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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe <l.s.r@web.de> · 2026-08-25 18:03 UTC · committed by Junio C Hamano

Browse files
2 files changed, +6 −0
modified builtin/worktree.c
@@ -491,6 +491,8 @@ static int add_worktree(const char *path, const char *refname,
491 491
492 492 name = worktree_basename(path, &len);
493 493 strbuf_add(&sb, name, path + len - name);
494 + if (!sb.len)
495 + die(_("invalid path '%s'"), path);
494 496 sanitize_refname_component(sb.buf, &sb_name);
495 497 if (!sb_name.len)
496 498 BUG("How come '%s' becomes empty after sanitization?", sb.buf);
modified t/t2400-worktree-add.sh
@@ -46,6 +46,10 @@ test_expect_success '"add" refuses to checkout locked branch' '
46 46 test_path_is_missing .git/worktrees/zere
47 47 '
48 48
49 + test_expect_success '"add" rejects an empty path' '
50 + test_must_fail git worktree add "" HEAD
51 + '
52 +
49 53 test_expect_success 'checking out paths not complaining about linked checkouts' '
50 54 (
51 55 cd existing_empty &&