validateName rejects names that would escape the worktrees directory or produce an invalid git branch. Names must be a single path segment made of safe characters, which also keeps the derived "worktree- " branch valid.
(name string)
| 316 | // produce an invalid git branch. Names must be a single path segment made of |
| 317 | // safe characters, which also keeps the derived "worktree-<name>" branch valid. |
| 318 | func validateName(name string) error { |
| 319 | if name != strings.TrimSpace(name) { |
| 320 | return fmt.Errorf("%w: %q has surrounding whitespace", ErrInvalidName, name) |
| 321 | } |
| 322 | if strings.ContainsAny(name, `/\`) { |
| 323 | return fmt.Errorf("%w: %q must not contain path separators", ErrInvalidName, name) |
| 324 | } |
| 325 | if name == "." || name == ".." { |
| 326 | return fmt.Errorf("%w: %q is not allowed", ErrInvalidName, name) |
| 327 | } |
| 328 | // filepath.Base collapses separators and ".."; if the cleaned segment |
| 329 | // differs, the input was not a plain single path component. |
| 330 | if filepath.Base(name) != name { |
| 331 | return fmt.Errorf("%w: %q must be a single path segment", ErrInvalidName, name) |
| 332 | } |
| 333 | return nil |
| 334 | } |
| 335 | |
| 336 | // repoRoot returns the worktree root of the git repository containing dir, |
| 337 | // or [ErrNotGitRepository] when dir is not inside one. |