Files
beads/cmd/bd/doctor/fix/untracked.go
Steve Yegge d5f2d91d04 fix(ci): more changes to fix failing CI (#415)
Fixes from maphew including:
- Remove test for deleted isPathWithinDir function
- Add gosec nolint directives for safe file operations
- Add rm -rf .beads before init in CI workflow
- Simplify panic handling and file operations

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: maphew <maphew@users.noreply.github.com>
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-29 22:24:29 -08:00

83 lines
2.1 KiB
Go

package fix
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
// UntrackedJSONL stages and commits untracked .beads/*.jsonl files.
// This fixes the issue where bd cleanup -f creates deletions.jsonl but
// leaves it untracked. (bd-pbj)
func UntrackedJSONL(path string) error {
if err := validateBeadsWorkspace(path); err != nil {
return err
}
beadsDir := filepath.Join(path, ".beads")
// Find untracked JSONL files
cmd := exec.Command("git", "status", "--porcelain", ".beads/")
cmd.Dir = path
output, err := cmd.Output()
if err != nil {
return fmt.Errorf("failed to check git status: %w", err)
}
// Parse output for untracked JSONL files
var untrackedFiles []string
for _, line := range strings.Split(string(output), "\n") {
line = strings.TrimSpace(line)
if line == "" {
continue
}
// Untracked files start with "?? "
if strings.HasPrefix(line, "?? ") {
file := strings.TrimPrefix(line, "?? ")
if strings.HasSuffix(file, ".jsonl") {
untrackedFiles = append(untrackedFiles, file)
}
}
}
if len(untrackedFiles) == 0 {
fmt.Println(" No untracked JSONL files found")
return nil
}
// Stage the untracked files
for _, file := range untrackedFiles {
fullPath := filepath.Join(path, file)
// Verify file exists in .beads directory (security check)
if !strings.HasPrefix(fullPath, beadsDir) {
continue
}
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
continue
}
// #nosec G204 -- file is validated against a whitelist of JSONL files
addCmd := exec.Command("git", "add", file)
addCmd.Dir = path
if err := addCmd.Run(); err != nil {
return fmt.Errorf("failed to stage %s: %w", file, err)
}
fmt.Printf(" Staged %s\n", filepath.Base(file))
}
// Commit the staged files
commitMsg := "chore(beads): commit untracked JSONL files\n\nAuto-committed by bd doctor --fix (bd-pbj)"
commitCmd := exec.Command("git", "commit", "-m", commitMsg)
commitCmd.Dir = path
commitCmd.Stdout = os.Stdout
commitCmd.Stderr = os.Stderr
if err := commitCmd.Run(); err != nil {
return fmt.Errorf("failed to commit: %w", err)
}
return nil
}