This commit is contained in:
Steve Yegge
2025-11-20 20:23:28 -05:00
3 changed files with 226 additions and 2 deletions

View File

@@ -3,6 +3,9 @@ package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/merge"
@@ -45,10 +48,15 @@ Vendored into bd with permission.`,
leftPath := args[2]
rightPath := args[3]
// Ensure cleanup runs after merge completes
defer func() {
cleanupMergeArtifacts(outputPath, debugMerge)
}()
err := merge.Merge3Way(outputPath, basePath, leftPath, rightPath, debugMerge)
if err != nil {
// Check if error is due to conflicts
if err.Error() == fmt.Sprintf("merge completed with %d conflicts", 1) ||
if err.Error() == fmt.Sprintf("merge completed with %d conflicts", 1) ||
err.Error() == fmt.Sprintf("merge completed with %d conflicts", 2) ||
err.Error()[:len("merge completed with")] == "merge completed with" {
// Conflicts present - exit with 1 (standard for merge drivers)
@@ -63,6 +71,61 @@ Vendored into bd with permission.`,
},
}
func cleanupMergeArtifacts(outputPath string, debug bool) {
// Determine the .beads directory from the output path
// outputPath is typically .beads/beads.jsonl
beadsDir := filepath.Dir(outputPath)
if debug {
fmt.Fprintf(os.Stderr, "=== CLEANUP ===\n")
fmt.Fprintf(os.Stderr, "Cleaning up artifacts in: %s\n", beadsDir)
}
// 1. Find and remove any files with "backup" in the name
entries, err := os.ReadDir(beadsDir)
if err != nil {
if debug {
fmt.Fprintf(os.Stderr, "Warning: failed to read directory for cleanup: %v\n", err)
}
return
}
for _, entry := range entries {
if entry.IsDir() {
continue
}
if strings.Contains(strings.ToLower(entry.Name()), "backup") {
fullPath := filepath.Join(beadsDir, entry.Name())
// Try to git rm if tracked
gitRmCmd := exec.Command("git", "rm", "-f", "--quiet", fullPath)
gitRmCmd.Dir = filepath.Dir(beadsDir)
_ = gitRmCmd.Run() // Ignore errors, file may not be tracked
// Also remove from filesystem if git rm didn't work
if err := os.Remove(fullPath); err == nil {
if debug {
fmt.Fprintf(os.Stderr, "Removed backup file: %s\n", entry.Name())
}
}
}
}
// 2. Run git clean -f in .beads/ directory to remove untracked files
cleanCmd := exec.Command("git", "clean", "-f")
cleanCmd.Dir = beadsDir
if debug {
cleanCmd.Stderr = os.Stderr
cleanCmd.Stdout = os.Stderr
fmt.Fprintf(os.Stderr, "Running: git clean -f in %s\n", beadsDir)
}
_ = cleanCmd.Run() // Ignore errors, git clean may fail in some contexts
if debug {
fmt.Fprintf(os.Stderr, "Cleanup complete\n\n")
}
}
func init() {
mergeCmd.Flags().BoolVar(&debugMerge, "debug", false, "Enable debug output to stderr")
rootCmd.AddCommand(mergeCmd)