@@ -27,6 +27,17 @@ sanitize_branch_name() {
2727 printf " %s" " $branch " | sed -e ' s/[\/\\ :*?"<>|]/-/g' -e ' s/^-*//' -e ' s/-*$//'
2828}
2929
30+ # Canonicalize a path to its absolute form, resolving symlinks
31+ # Usage: canonicalize_path path
32+ # Returns: canonical path or empty string on failure
33+ canonicalize_path () {
34+ local path=" $1 "
35+ # Unset CDPATH to prevent unexpected directory changes
36+ # Suppress stderr to hide errors for non-existent directories
37+ # Use subshell to avoid changing current working directory
38+ ( unset CDPATH && cd -P -- " $path " 2> /dev/null && pwd -P )
39+ }
40+
3041# Resolve the base directory for worktrees
3142# Usage: resolve_base_dir repo_root
3243resolve_base_dir () {
@@ -57,12 +68,31 @@ resolve_base_dir() {
5768 # Absolute paths (starting with /) are used as-is
5869 fi
5970
71+ # Canonicalize base_dir if it exists
72+ if [ -d " $base_dir " ]; then
73+ local canonical_base
74+ canonical_base=$( canonicalize_path " $base_dir " )
75+ if [ -n " $canonical_base " ]; then
76+ base_dir=" $canonical_base "
77+ fi
78+ # If canonicalization fails (empty result), base_dir keeps its absolute form
79+ fi
80+
81+ # Canonicalize repo_root before comparison
82+ local canonical_repo_root
83+ canonical_repo_root=$( canonicalize_path " $repo_root " )
84+ # Warn if canonicalization fails (indicates repository issue)
85+ if [ -z " $canonical_repo_root " ]; then
86+ log_warn " Unable to canonicalize repository path: $repo_root "
87+ canonical_repo_root=" $repo_root "
88+ fi
89+
6090 # Warn if worktree dir is inside repo (but not a sibling)
61- if [[ " $base_dir " == " $repo_root " /* ]]; then
62- local rel_path=" ${base_dir# $repo_root / } "
91+ if [[ " $base_dir " == " $canonical_repo_root " /* ]]; then
92+ local rel_path=" ${base_dir# $canonical_repo_root / } "
6393 # Check if .gitignore exists and whether it includes the worktree directory
64- if [ -f " $repo_root /.gitignore" ]; then
65- if ! grep -qE " ^/?${rel_path} /?\$ |^/?${rel_path} /\*?\$ " " $repo_root /.gitignore" 2> /dev/null; then
94+ if [ -f " $canonical_repo_root /.gitignore" ]; then
95+ if ! grep -qE " ^/?${rel_path} /?\$ |^/?${rel_path} /\*?\$ " " $canonical_repo_root /.gitignore" 2> /dev/null; then
6696 log_warn " Worktrees are inside repository at: $rel_path "
6797 log_warn " Consider adding '/$rel_path /' to .gitignore to avoid committing worktrees"
6898 fi
0 commit comments