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.
This commit is contained in:
Greg Gauthier 2026-03-28 13:23:49 +00:00
parent fd033b03c7
commit 4084315dc1

View File

@ -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")
}