diff --git a/file.go b/file.go index 0106e45..03750ee 100644 --- a/file.go +++ b/file.go @@ -9,7 +9,6 @@ package gocodewalker import ( "bytes" "errors" - "fmt" "github.com/boyter/gocodewalker/go-gitignore" "golang.org/x/sync/errgroup" "os" @@ -189,14 +188,14 @@ func (f *FileWalker) Start() error { for _, directory := range f.directories { d := directory // capture var eg.Go(func() error { - return f.walkDirectoryRecursive(0, d, []gitignore.GitIgnore{}, []gitignore.GitIgnore{}) + return f.walkDirectoryRecursive(0, d, []gitignore.GitIgnore{}, []gitignore.GitIgnore{}, []gitignore.GitIgnore{}) }) } err = eg.Wait() } else { if f.directory != "" { - err = f.walkDirectoryRecursive(0, f.directory, []gitignore.GitIgnore{}, []gitignore.GitIgnore{}) + err = f.walkDirectoryRecursive(0, f.directory, []gitignore.GitIgnore{}, []gitignore.GitIgnore{}, []gitignore.GitIgnore{}) } } @@ -209,7 +208,11 @@ func (f *FileWalker) Start() error { return err } -func (f *FileWalker) walkDirectoryRecursive(iteration int, directory string, gitignores []gitignore.GitIgnore, ignores []gitignore.GitIgnore) error { +func (f *FileWalker) walkDirectoryRecursive(iteration int, + directory string, + gitignores []gitignore.GitIgnore, + ignores []gitignore.GitIgnore, + moduleIgnores []gitignore.GitIgnore) error { if iteration == 1 { f.countingSemaphore <- true defer func() { @@ -259,7 +262,7 @@ func (f *FileWalker) walkDirectoryRecursive(iteration int, directory string, git } } - // Pull out all ignore and gitignore files and add them + // Pull out all ignore, gitignore and gitmodule files and add them // to out collection of gitignores to be applied for this pass // and any subdirectories // Since they can apply to the current list of files we need to ensure @@ -308,15 +311,14 @@ func (f *FileWalker) walkDirectoryRecursive(iteration int, directory string, git gitIgnore := gitignore.New(bytes.NewReader(c), abs, nil) ignores = append(ignores, gitIgnore) - fmt.Println(">>>>>>", gitIgnore) } } // this should only happen on the first iteration - // because there can only be one .gitmodules file per repository + // because there should be one .gitmodules file per repository // however we also need to support someone running in a directory of // projects that have multiple repositories or in a go vendor - // repository etc... + // repository etc... hence check every time if !f.IgnoreGitModules { if file.Name() == GitModules { // now we need to open and parse the file @@ -328,10 +330,18 @@ func (f *FileWalker) walkDirectoryRecursive(iteration int, directory string, git return err } - fmt.Println("//////") - fmt.Println(filepath.Join(directory, file.Name())) - fmt.Println(extractGitModuleFolders(string(c))) - fmt.Println("//////") + abs, err := filepath.Abs(directory) + if err != nil { + if f.errorsHandler(err) { + continue // if asked to ignore it lets continue + } + return err + } + + for _, gm := range extractGitModuleFolders(string(c)) { + gitIgnore := gitignore.New(strings.NewReader(gm), abs, nil) + moduleIgnores = append(moduleIgnores, gitIgnore) + } } } } @@ -495,6 +505,12 @@ func (f *FileWalker) walkDirectoryRecursive(iteration int, directory string, git shouldIgnore = ignore.Ignore(joined) } } + for _, ignore := range moduleIgnores { + // same rules as above + if ignore.MatchIsDir(joined, true) != nil { + shouldIgnore = ignore.Ignore(joined) + } + } // start by saying we didn't find it then check each possible // choice to see if we did find it @@ -561,11 +577,11 @@ func (f *FileWalker) walkDirectoryRecursive(iteration int, directory string, git if iteration == 0 { wg.Add(1) go func(iteration int, directory string, gitignores []gitignore.GitIgnore, ignores []gitignore.GitIgnore) { - _ = f.walkDirectoryRecursive(iteration+1, joined, gitignores, ignores) + _ = f.walkDirectoryRecursive(iteration+1, joined, gitignores, ignores, moduleIgnores) wg.Done() }(iteration, joined, gitignores, ignores) } else { - err = f.walkDirectoryRecursive(iteration+1, joined, gitignores, ignores) + err = f.walkDirectoryRecursive(iteration+1, joined, gitignores, ignores, moduleIgnores) if err != nil { return err }