@@ -23,45 +23,7 @@ var removeGetwd = os.Getwd
2323
2424// isWorktreeManaged determines if a worktree is managed by wtp
2525func isWorktreeManaged (worktreePath string , cfg * config.Config , mainRepoPath string , isMain bool ) bool {
26- // Main worktree is always managed
27- if isMain {
28- return true
29- }
30-
31- // Get base directory - use default config if config is not available
32- if cfg == nil {
33- // Create default config when none is available
34- defaultCfg := & config.Config {
35- Defaults : config.Defaults {
36- BaseDir : "../worktrees" ,
37- },
38- }
39- cfg = defaultCfg
40- }
41-
42- baseDir := cfg .ResolveWorktreePath (mainRepoPath , "" )
43- // Remove trailing slash if it exists
44- baseDir = strings .TrimSuffix (baseDir , "/" )
45-
46- // Check if worktree path is under base directory
47- absWorktreePath , err := filepath .Abs (worktreePath )
48- if err != nil {
49- return false
50- }
51-
52- absBaseDir , err := filepath .Abs (baseDir )
53- if err != nil {
54- return false
55- }
56-
57- // Check if worktree is within base directory
58- relPath , err := filepath .Rel (absBaseDir , absWorktreePath )
59- if err != nil {
60- return false
61- }
62-
63- // If relative path starts with "..", it's outside base directory
64- return ! strings .HasPrefix (relPath , ".." )
26+ return isWorktreeManagedCommon (worktreePath , cfg , mainRepoPath , isMain )
6527}
6628
6729// NewRemoveCommand creates the remove command definition
@@ -134,7 +96,7 @@ func removeCommandWithCommandExecutor(
13496 _ * cli.Command ,
13597 w io.Writer ,
13698 executor command.Executor ,
137- _ string ,
99+ cwd string ,
138100 worktreeName string ,
139101 force , withBranch , forceBranch bool ,
140102) error {
@@ -154,6 +116,20 @@ func removeCommandWithCommandExecutor(
154116 return err
155117 }
156118
119+ absTargetPath , err := filepath .Abs (targetWorktree .Path )
120+ if err != nil {
121+ return errors .WorktreeRemovalFailed (targetWorktree .Path , err )
122+ }
123+
124+ absCwd , err := filepath .Abs (cwd )
125+ if err != nil {
126+ return errors .DirectoryAccessFailed ("access current" , cwd , err )
127+ }
128+
129+ if isPathWithin (absTargetPath , absCwd ) {
130+ return errors .CannotRemoveCurrentWorktree (worktreeName , absTargetPath )
131+ }
132+
157133 // Remove worktree using CommandExecutor
158134 removeCmd := command .GitWorktreeRemove (targetWorktree .Path , force )
159135 result , err = executor .Execute ([]command.Command {removeCmd })
@@ -190,6 +166,23 @@ func validateRemoveInput(worktreeName string, withBranch, forceBranch bool) erro
190166 return nil
191167}
192168
169+ func isPathWithin (basePath , targetPath string ) bool {
170+ rel , err := filepath .Rel (basePath , targetPath )
171+ if err != nil {
172+ return false
173+ }
174+
175+ if rel == "." || rel == "" {
176+ return true
177+ }
178+
179+ if rel == ".." || strings .HasPrefix (rel , ".." + string (os .PathSeparator )) {
180+ return false
181+ }
182+
183+ return true
184+ }
185+
193186func removeBranchWithCommandExecutor (
194187 w io.Writer ,
195188 executor command.Executor ,
0 commit comments