Skip to content

Open source AI coding agent. Designed for large projects and real world tasks.

working on 'apply' git and conflicts handling

Dane Schneider92a1528492da
diff --git a/cli/cmd/apply.go b/cli/cmd/apply.go
index 40685d2a..d343bcaa 100644
--- a/cli/cmd/apply.go
+++ b/cli/cmd/apply.go
@@ -50,6 +50,10 @@ func apply(cmd *cobra.Command, args []string) error {
appliedAny := false
currentPlanFiles, planResByPath, _, err := lib.GetCurrentPlanStateWithContext()
+ if err != nil {
+ return fmt.Errorf("error getting current plan files: %w", err)
+ }
+
// check if any files have been modified since the plan result was generated
pathsRemoved := []string{}
@@ -122,10 +126,6 @@ func apply(cmd *cobra.Command, args []string) error {
}
}
- if err != nil {
- return fmt.Errorf("error getting current plan files: %w", err)
- }
-
for path, content := range currentPlanFiles.Files {
// Compute destination path
dstPath := filepath.Join(lib.ProjectRoot, path)
diff --git a/cli/go.mod b/cli/go.mod
index 27f92948..cd0bcbc2 100644
--- a/cli/go.mod
+++ b/cli/go.mod
@@ -4,6 +4,7 @@ go 1.21.3
require (
github.com/charmbracelet/lipgloss v0.7.1
+ github.com/davecgh/go-spew v1.1.1
github.com/fatih/color v1.13.0
github.com/looplab/fsm v1.0.1
github.com/olekukonko/tablewriter v0.0.5
diff --git a/cli/lib/git.go b/cli/lib/git.go
index 5b692d90..d9817f4e 100644
--- a/cli/lib/git.go
+++ b/cli/lib/git.go
@@ -134,18 +134,51 @@ func GitStashPopNoConflict() error {
gitMutex.Lock()
defer gitMutex.Unlock()
- res, err := exec.Command("git", "stash", "pop", "--quiet", "--theirs").CombinedOutput()
+ res, err := exec.Command("git", "stash", "pop").CombinedOutput()
+
+ fmt.Println("'git stash pop' output:")
+ fmt.Println(string(res))
+
if err != nil {
- if strings.Contains(string(res), "Merge conflict") {
- // We have a merge conflict but we are choosing to favor committed changes over stash
+ if strings.Contains(string(res), "CONFLICT") {
+ // Parse the output to find which files have conflicts
+ conflictFiles := parseConflictFiles(string(res))
+ for _, file := range conflictFiles {
+ // Reset each conflicting file individually
+ err = exec.Command("git", "checkout", "--ours", file).Run()
+ if err != nil {
+ return fmt.Errorf("error resetting file %s: %v", file, err)
+ }
+ }
+ err = exec.Command("git", "stash", "drop").Run()
+ if err != nil {
+ return fmt.Errorf("error dropping git stash: %v", err)
+ }
return nil
+ } else {
+ return fmt.Errorf("error popping git stash: %v, output: %s", err, string(res))
}
- return fmt.Errorf("error popping git stash: %v, output: %s", err, string(res))
}
return nil
}
+func parseConflictFiles(gitOutput string) []string {
+ var conflictFiles []string
+ lines := strings.Split(gitOutput, "\n")
+ for _, line := range lines {
+ if strings.Contains(line, "CONFLICT") {
+ // Extract the filename from the line
+ parts := strings.Fields(line)
+ if len(parts) > 1 {
+ filename := parts[len(parts)-1]
+ conflictFiles = append(conflictFiles, filename)
+ }
+ }
+ }
+ return conflictFiles
+}
+
func CwdIsGitRepo() bool {
isGitRepo := false
if IsCommandAvailable("git") {
diff --git a/cli/lib/plan_files.go b/cli/lib/plan_files.go
index 4cc1c9d2..316cea28 100644
--- a/cli/lib/plan_files.go
+++ b/cli/lib/plan_files.go
@@ -136,6 +136,51 @@ func GetCurrentPlanStateWithContext() (*shared.CurrentPlanFiles, shared.PlanResu
return &shared.CurrentPlanFiles{Files: files, ContextShas: shas}, planResByPath, modelContext, nil
}
+func SetPendingResultsApplied(planResByPath shared.PlanResultsByPath) error {
+ ts := shared.StringTs()
+ numPending := planResByPath.NumPending()
+ planResByPath.SetApplied(ts)
+
+ errCh := make(chan error, numPending)
+
+ for _, planResults := range planResByPath {
+ for _, planResult := range planResults {
+ // only write results that were just applied
+ if planResult.AppliedAt != ts {
+ continue
+ }
+
+ go func(planResult *shared.PlanResult) {
+ bytes, err := json.Marshal(planResult)
+ if err != nil {
+ errCh <- fmt.Errorf("error marshalling plan result: %v", err)
+ return
+ }
+
+ path := filepath.Join(ResultsSubdir, planResult.Path, planResult.Ts+".json")
+
+ err = os.WriteFile(path, bytes, 0644)
+ if err != nil {
+ errCh <- fmt.Errorf("error writing plan result: %v", err)
+ return
+ }
+
+ errCh <- nil
+
+ }(planResult)
+ }
+ }
+
+ for i := 0; i < numPending; i++ {
+ err := <-errCh
+ if err != nil {
+ return fmt.Errorf("error wrting plan result file after SetApplied: %v", err)
+ }
+ }
+
+ return nil
+}
+
func getPlanResultsByPath() (shared.PlanResultsByPath, error) {
resByPath := make(shared.PlanResultsByPath)
@@ -185,45 +230,3 @@ func getPlanResultsByPath() (shared.PlanResultsByPath, error) {
return resByPath, nil
}
-
-func SetPendingResultsApplied(planResByPath shared.PlanResultsByPath) error {
- ts := shared.StringTs()
- planResByPath.SetApplied(ts)
- numPending := planResByPath.NumPending()
-
- errCh := make(chan error, numPending)
-
- for _, planResults := range planResByPath {
- for _, planResult := range planResults {
- if !planResult.IsPending() {
- continue
- }
-
- go func(planResult *shared.PlanResult) {
- bytes, err := json.Marshal(planResult)
- if err != nil {
- errCh <- fmt.Errorf("error marshalling plan result: %v", err)
- return
- }
-
- err = os.WriteFile(filepath.Join(ResultsSubdir, planResult.Path, planResult.Ts), bytes, 0644)
- if err != nil {
- errCh <- fmt.Errorf("error writing plan result: %v", err)
- return
- }
-
- errCh <- nil
-
- }(planResult)
- }
- }
-
- for i := 0; i < numPending; i++ {
- err := <-errCh
- if err != nil {
- return fmt.Errorf("error wrting plan result file after SetApplied: %v", err)
- }
- }
-
- return nil
-}