@@ -428,41 +428,45 @@ func (t *FilesystemTool) initGitignoreMatchers() {
428428 slog .Warn ("Failed to get absolute path for allowed directory" , "dir" , allowedDir , "error" , err )
429429 continue
430430 }
431+ t .loadMatcherForDirectory (absDir )
432+ }
433+ }
431434
432- // PlainOpen automatically searches up the directory tree for .git
433- repo , err := git .PlainOpen (absDir )
434- if err != nil {
435- slog .Debug ("No git repository found" , "directory" , absDir )
436- continue
437- }
438-
439- // Get the worktree
440- worktree , err := repo .Worktree ()
441- if err != nil {
442- slog .Warn ("Failed to get worktree" , "path" , absDir , "error" , err )
443- continue
444- }
435+ // loadMatcherForDirectory loads a gitignore matcher for a specific directory
436+ func (t * FilesystemTool ) loadMatcherForDirectory (absDir string ) {
437+ // PlainOpen automatically searches up the directory tree for .git
438+ repo , err := git .PlainOpen (absDir )
439+ if err != nil {
440+ slog .Debug ("No git repository found" , "directory" , absDir )
441+ return
442+ }
445443
446- // Get the repository root path
447- repoRoot := worktree .Filesystem .Root ()
444+ // Get the worktree
445+ worktree , err := repo .Worktree ()
446+ if err != nil {
447+ slog .Warn ("Failed to get worktree" , "path" , absDir , "error" , err )
448+ return
449+ }
448450
449- // Skip if we already have a matcher for this repository
450- if _ , exists := t .repoMatchers [repoRoot ]; exists {
451- continue
452- }
451+ // Get the repository root path
452+ repoRoot := worktree .Filesystem .Root ()
453453
454- // Read gitignore patterns from the repository
455- patterns , err := gitignore .ReadPatterns (worktree .Filesystem , nil )
456- if err != nil {
457- slog .Warn ("Failed to read gitignore patterns" , "path" , repoRoot , "error" , err )
458- continue
459- }
454+ // Skip if we already have a matcher for this repository
455+ if _ , exists := t .repoMatchers [repoRoot ]; exists {
456+ return
457+ }
460458
461- // Create a matcher from the patterns
462- matcher := gitignore .NewMatcher (patterns )
463- t .repoMatchers [repoRoot ] = matcher
464- slog .Debug ("Loaded gitignore patterns" , "repository" , repoRoot )
459+ // Read gitignore patterns from the repository
460+ patterns , err := gitignore .ReadPatterns (worktree .Filesystem , nil )
461+ if err != nil {
462+ slog .Warn ("Failed to read gitignore patterns" , "path" , repoRoot , "error" , err )
463+ return
465464 }
465+
466+ // Create a matcher from the patterns
467+ matcher := gitignore .NewMatcher (patterns )
468+ t .repoMatchers [repoRoot ] = matcher
469+ slog .Debug ("Loaded gitignore patterns" , "repository" , repoRoot )
466470}
467471
468472// shouldIgnorePath checks if a path should be ignored based on VCS rules
@@ -716,6 +720,11 @@ func (t *FilesystemTool) addAllowedDirectory(absPath string) (*tools.ToolCallRes
716720 // Add the directory to the allowed list
717721 t .allowedDirectories = append (t .allowedDirectories , absPath )
718722
723+ // Load gitignore matcher for the new directory if VCS ignoring is enabled
724+ if t .ignoreVCS {
725+ t .loadMatcherForDirectory (absPath )
726+ }
727+
719728 successMsg := fmt .Sprintf (`Directory successfully added to allowed directories list.
720729
721730Added: %s
0 commit comments