Open source AI coding agent. Designed for large projects and real world tasks.
Go aiai-agentsai-developer-toolsai-toolsclicommand-linedeveloper-toolsgitgolanggpt-4llmopenaipolyglot-programmingterminalterminal-basedterminal-ui
diffs command now working with temp dir, little bit of refactoring
Dane Schneiderf76bb630ed36
diff --git a/cli/cmd/apply.go b/cli/cmd/apply.goindex 6342c38e..324810c3 100644--- a/cli/cmd/apply.go+++ b/cli/cmd/apply.go@@ -2,7 +2,6 @@ package cmdimport ("fmt"- "io""log""os""path/filepath"@@ -73,7 +72,7 @@ func apply(cmd *cobra.Command, args []string) error {dstPath := filepath.Join(lib.ProjectRoot, relPath)// Copy the file- err = copyFile(srcPath, dstPath)+ err = lib.CopyFile(srcPath, dstPath)if err != nil {return fmt.Errorf("failed to copy %s to %s: %w", srcPath, dstPath, err)}@@ -93,21 +92,3 @@ func apply(cmd *cobra.Command, args []string) error {return nil}--// Utility function to copy files-func copyFile(src, dst string) error {- srcFile, err := os.Open(src)- if err != nil {- return err- }- defer srcFile.Close()-- dstFile, err := os.Create(dst)- if err != nil {- return err- }- defer dstFile.Close()-- _, err = io.Copy(dstFile, srcFile)- return err-}diff --git a/cli/cmd/diffs.go b/cli/cmd/diffs.goindex 5d2cafa4..f5384dcc 100644--- a/cli/cmd/diffs.go+++ b/cli/cmd/diffs.go@@ -10,6 +10,8 @@ import ("github.com/spf13/cobra")+const tempBranchName = "pdx_temp"+func init() {RootCmd.AddCommand(diffsCmd)}@@ -34,60 +36,82 @@ func showDiffs() error {return fmt.Errorf("git is required to generate diffs")}- // Save current branch name- cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")- currentBranch, err := cmd.Output()+ originalDir, err := os.Getwd()if err != nil {- return fmt.Errorf("error getting current branch: %v", err)+ return fmt.Errorf("error getting current directory: %v", err)}- // Create and checkout a temporary branch- cmd = exec.Command("git", "checkout", "-b", "temp_diff_branch")- err = cmd.Run()+ // Create a temporary directory+ tempDir, err := os.MkdirTemp(os.TempDir(), "plandex-diffs-*")if err != nil {- return fmt.Errorf("error creating temp branch: %v", err)+ return fmt.Errorf("error creating temp directory: %v", err)}+ defer os.RemoveAll(tempDir) // Cleanup temporary directory- defer func() {- // Switch back to the original branch and delete the temporary branch- cmd = exec.Command("git", "checkout", string(currentBranch))- err = cmd.Run()- if err != nil {- fmt.Println(fmt.Errorf("error switching back to original branch: %v", err))- }+ fmt.Println("Temp directory:", tempDir)++ // Copy relevant project files to the temporary directory+ paths, err := lib.GetCurrentPlanFilePaths()+ ch := make(chan error)+ for _, filePath := range paths {+ go func(filePath string) {+ ch <- lib.CopyFile(filepath.Join(lib.ProjectRoot, filePath), filepath.Join(tempDir, filePath))+ }(filePath)+ }- cmd = exec.Command("git", "branch", "-D", "temp_diff_branch")- err = cmd.Run()- if err != nil {- fmt.Println(fmt.Errorf("error deleting temp branch: %v", err))+ for range paths {+ select {+ case err := <-ch:+ if err != nil {+ if !os.IsNotExist(err) {+ return fmt.Errorf("error copying file: %v", err)+ }+ }}+ }- }()+ // Navigate to the temporary directory+ err = os.Chdir(tempDir)+ if err != nil {+ return fmt.Errorf("error changing to temp directory: %v", err)+ }+ defer os.Chdir(originalDir)- // Commit any outstanding changes- cmd = exec.Command("git", "add", ".")+ // Initialize a new git repo in the temporary directory+ cmd := exec.Command("git", "init")err = cmd.Run()if err != nil {- return fmt.Errorf("error adding changes: %v", err)+ return fmt.Errorf("error initializing git repo in temp directory: %v", err)}- cmd = exec.Command("git", "commit", "-m", "temp commit")- err = cmd.Run()++ // Add and commit changes in the temporary directory+ cmd = exec.Command("git", "add", ".")+ output, err := cmd.CombinedOutput()if err != nil {- return fmt.Errorf("error committing changes: %v", err)+ err = fmt.Errorf("error adding changes in temp directory: %v", err)+ fmt.Println(err)+ fmt.Println(string(output))}- // Overwrite project files with plan files- err = copyDirectory(lib.ProjectRoot, lib.PlanFilesDir)+ cmd = exec.Command("git", "commit", "-m", "temp commit", "--allow-empty")+ output, err = cmd.CombinedOutput()+ if err != nil {+ err = fmt.Errorf("error committing changes in temp directory: %v", err)+ fmt.Println(err)+ fmt.Println(string(output))+ }+ err = lib.CopyDir(lib.PlanFilesDir, tempDir)if err != nil {- return fmt.Errorf("error copying files: %v", err)+ return fmt.Errorf("error copying files to temp directory: %v", err)}- // Add changes to the staging areacmd = exec.Command("git", "add", ".")- err = cmd.Run()+ output, err = cmd.CombinedOutput()if err != nil {- return fmt.Errorf("error adding changes: %v", err)+ err = fmt.Errorf("error adding changes in temp directory: %v", err)+ fmt.Println(err)+ fmt.Println(string(output))}// Show the diff@@ -95,40 +119,9 @@ func showDiffs() error {cmd.Stdin = os.Stdincmd.Stdout = os.Stdoutcmd.Stderr = os.Stderr-err = cmd.Run()if err != nil {- return fmt.Errorf("error generating diff: %v", err)- }-- return nil-}--func copyDirectory(srcDir, dstDir string) error {- entries, err := os.ReadDir(srcDir)- if err != nil {- return err- }-- for _, entry := range entries {- srcPath := filepath.Join(srcDir, entry.Name())- dstPath := filepath.Join(dstDir, entry.Name())-- if entry.IsDir() {- err = copyDirectory(srcPath, dstPath)- if err != nil {- return err- }- } else {- data, err := os.ReadFile(srcPath)- if err != nil {- return err- }- err = os.WriteFile(dstPath, data, 0644)- if err != nil {- return err- }- }+ return fmt.Errorf("error showing diffs: %v", err)}return nildiff --git a/cli/lib/fs.go b/cli/lib/fs.goindex 06d207a1..0c459456 100644--- a/cli/lib/fs.go+++ b/cli/lib/fs.go@@ -3,6 +3,7 @@ package libimport ("encoding/json""fmt"+ "io""log""os""path/filepath"@@ -179,6 +180,55 @@ func FlattenPaths(fileOrDirPaths []string, params *types.LoadContextParams) []streturn resPaths}+func CopyFile(src, dst string) error {+ srcFile, err := os.Open(src)+ if err != nil {+ return err+ }+ defer srcFile.Close()++ // ensure parent directory exists+ err = os.MkdirAll(filepath.Dir(dst), os.ModePerm)+ if err != nil {+ return err+ }++ dstFile, err := os.Create(dst)+ if err != nil {+ return err+ }+ defer dstFile.Close()++ _, err = io.Copy(dstFile, srcFile)+ return err+}++func CopyDir(srcDir, dstDir string) error {+ entries, err := os.ReadDir(srcDir)+ if err != nil {+ return err+ }++ for _, entry := range entries {+ srcPath := filepath.Join(srcDir, entry.Name())+ dstPath := filepath.Join(dstDir, entry.Name())++ if entry.IsDir() {+ err = CopyDir(srcPath, dstPath)+ if err != nil {+ return err+ }+ } else {+ err = CopyFile(srcPath, dstPath)+ if err != nil {+ return err+ }+ }+ }++ return nil+}+func findPlandex() (string, string) {searchPath := Cwdfor searchPath != "/" {diff --git a/cli/lib/plan_files.go b/cli/lib/plan_files.goindex 08534241..68450000 100644--- a/cli/lib/plan_files.go+++ b/cli/lib/plan_files.go@@ -84,7 +84,7 @@ func getCurrentPlanFiles() (shared.CurrentPlanFiles, error) {return shared.CurrentPlanFiles{Files: planFiles.data}, nil}-func getCurrentPlanFilePaths() ([]string, error) {+func GetCurrentPlanFilePaths() ([]string, error) {_, err := os.Stat(PlanFilesDir)filesDirExists := !os.IsNotExist(err)@@ -101,7 +101,12 @@ func getCurrentPlanFilePaths() ([]string, error) {return nil}- filePaths = append(filePaths, srcPath)+ relPath, err := filepath.Rel(PlanFilesDir, srcPath)+ if err != nil {+ return err+ }++ filePaths = append(filePaths, relPath)return nil})@@ -115,7 +120,7 @@ func getCurrentPlanFilePaths() ([]string, error) {}func isFilePathInPlan(filePath string) bool {- filePaths, err := getCurrentPlanFilePaths()+ filePaths, err := GetCurrentPlanFilePaths()if err != nil {return false}