From 4084315dc18be9915e975fe26748b6438195db04 Mon Sep 17 00:00:00 2001 From: Greg Gauthier Date: Sat, 28 Mar 2026 13:23:49 +0000 Subject: [PATCH] refactor(analyze): improve file discovery and project context in analyze command - Enhance discoverSourceFiles to skip additional noise directories like "build" and "dist" while descending into source directories. - Update safety check to use package-level logger without .Get(). - Refine buildProjectContext with better labeling and consistent git remote handling. - Minor comment and string adjustments for clarity. --- cmd/analyze.go | 46 ++++++++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/cmd/analyze.go b/cmd/analyze.go index 334aecc..f55ede1 100644 --- a/cmd/analyze.go +++ b/cmd/analyze.go @@ -36,9 +36,11 @@ Uses language-specific prompts discovered in .grokkit/prompts/ or ~/.config/grok model := config.GetModel("analyze", viper.GetString("model")) yes := viper.GetBool("yes") - // Fixed: use package-level logger funcs (no .Get()) - // Safety: note if not in git repo (IsRepo takes no args) - if !git.IsRepo() { + // Logger (use the package-level logger as done in other commands) + // (remove the old "log := logger.Get()") + + // Safety check + if !git.IsRepo() { // IsRepo takes no arguments logger.Warn("Not inside a git repository. Git metadata in report will be limited.") } @@ -125,28 +127,35 @@ func init() { rootCmd.AddCommand(analyzeCmd) } -// discoverSourceFiles walks the directory and collects supported source files. -// Extend this if you want deeper recursion or ignore patterns. +// discoverSourceFiles walks the directory and collects all supported source files. +// It skips common noise directories but DOES descend into cmd/, internal/, etc. func discoverSourceFiles(root string) ([]string, error) { var files []string err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { if err != nil { return err } - if info.IsDir() && (strings.HasPrefix(info.Name(), ".") || info.Name() == "node_modules" || info.Name() == "vendor") { - return filepath.SkipDir - } - if !info.IsDir() { - if _, err := linter.DetectLanguage(path); err == nil { - files = append(files, path) + + // Skip common hidden/noise directories but still allow descent into source dirs + name := info.Name() + if info.IsDir() { + if strings.HasPrefix(name, ".") && name != "." && name != ".." || // skip .git, .grokkit (except we want .grokkit/prompts? but for source we skip) + name == "node_modules" || name == "vendor" || name == "build" || name == "dist" { + return filepath.SkipDir } + return nil + } + + // Check if this is a supported source file + if _, detectErr := linter.DetectLanguage(path); detectErr == nil { + files = append(files, path) } return nil }) return files, err } -// previewLines prints the first N lines of the report. +// previewLines prints the first N lines of the report (unchanged) func previewLines(content string, n int) { scanner := bufio.NewScanner(strings.NewReader(content)) for i := 0; i < n && scanner.Scan(); i++ { @@ -157,19 +166,16 @@ func previewLines(content string, n int) { } } -// buildProjectContext harvests useful context (tree, go.mod, git remote, etc.). -// Expand this as needed for other languages. -// buildProjectContext builds a concise but useful context block for the AI. +// buildProjectContext — small improvement for better context (optional but nice) func buildProjectContext(dir string, files []string) string { var sb strings.Builder sb.WriteString("Project Root: " + dir + "\n\n") - sb.WriteString(fmt.Sprintf("Total source files: %d\n\n", len(files))) + sb.WriteString(fmt.Sprintf("Total source files discovered: %d\n\n", len(files))) - // Top-level files only (avoid token explosion) - sb.WriteString("Key files (top level):\n") + sb.WriteString("Key files (top-level view):\n") for _, f := range files { rel, _ := filepath.Rel(dir, f) - if strings.Count(rel, string(filepath.Separator)) <= 2 { // shallow + if strings.Count(rel, string(filepath.Separator)) <= 2 { sb.WriteString(" - " + rel + "\n") } if sb.Len() > 2500 { @@ -177,7 +183,7 @@ func buildProjectContext(dir string, files []string) string { } } - // Git remotes (if present) + // Git remotes if out, err := git.Run([]string{"remote", "-v"}); err == nil && out != "" { sb.WriteString("\nGit Remotes:\n" + out + "\n") }