From 97e5e8714b16b964fbdf1b59696abfb5c197bf19 Mon Sep 17 00:00:00 2001 From: Kujtim Hoxha Date: Sat, 5 Jul 2025 17:33:57 +0200 Subject: [PATCH] chore: make diff config independent --- internal/diff/diff.go | 5 ----- internal/llm/tools/edit.go | 6 +++--- internal/llm/tools/write.go | 2 +- internal/tui/components/chat/sidebar/sidebar.go | 6 +++++- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/internal/diff/diff.go b/internal/diff/diff.go index 92878533..9e00c871 100644 --- a/internal/diff/diff.go +++ b/internal/diff/diff.go @@ -4,15 +4,10 @@ import ( "strings" "github.com/aymanbagabas/go-udiff" - "github.com/charmbracelet/crush/internal/config" ) // GenerateDiff creates a unified diff from two file contents func GenerateDiff(beforeContent, afterContent, fileName string) (string, int, int) { - // remove the cwd prefix and ensure consistent path format - // this prevents issues with absolute paths in different environments - cwd := config.Get().WorkingDir() - fileName = strings.TrimPrefix(fileName, cwd) fileName = strings.TrimPrefix(fileName, "/") var ( diff --git a/internal/llm/tools/edit.go b/internal/llm/tools/edit.go index d50dfa66..5b27223d 100644 --- a/internal/llm/tools/edit.go +++ b/internal/llm/tools/edit.go @@ -206,7 +206,7 @@ func (e *editTool) createNewFile(ctx context.Context, filePath, content string) _, additions, removals := diff.GenerateDiff( "", content, - filePath, + strings.TrimPrefix(filePath, config.Get().WorkingDir()), ) rootDir := config.Get().WorkingDir() permissionPath := filepath.Dir(filePath) @@ -318,7 +318,7 @@ func (e *editTool) deleteContent(ctx context.Context, filePath, oldString string _, additions, removals := diff.GenerateDiff( oldContent, newContent, - filePath, + strings.TrimPrefix(filePath, config.Get().WorkingDir()), ) rootDir := config.Get().WorkingDir() @@ -441,7 +441,7 @@ func (e *editTool) replaceContent(ctx context.Context, filePath, oldString, newS _, additions, removals := diff.GenerateDiff( oldContent, newContent, - filePath, + strings.TrimPrefix(filePath, config.Get().WorkingDir()), ) rootDir := config.Get().WorkingDir() permissionPath := filepath.Dir(filePath) diff --git a/internal/llm/tools/write.go b/internal/llm/tools/write.go index 432964e1..9c64f5bf 100644 --- a/internal/llm/tools/write.go +++ b/internal/llm/tools/write.go @@ -168,7 +168,7 @@ func (w *writeTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error diff, additions, removals := diff.GenerateDiff( oldContent, params.Content, - filePath, + strings.TrimPrefix(filePath, config.Get().WorkingDir()), ) rootDir := config.Get().WorkingDir() diff --git a/internal/tui/components/chat/sidebar/sidebar.go b/internal/tui/components/chat/sidebar/sidebar.go index 9983eb27..609b196c 100644 --- a/internal/tui/components/chat/sidebar/sidebar.go +++ b/internal/tui/components/chat/sidebar/sidebar.go @@ -160,6 +160,8 @@ func (m *sidebarCmp) handleFileHistoryEvent(event pubsub.Event[history.File]) te before := existing.History.initialVersion.Content after := existing.History.latestVersion.Content path := existing.History.initialVersion.Path + cwd := config.Get().WorkingDir() + path = strings.TrimPrefix(path, cwd) _, additions, deletions := diff.GenerateDiff(before, after, path) existing.Additions = additions existing.Deletions = deletions @@ -213,7 +215,9 @@ func (m *sidebarCmp) loadSessionFiles() tea.Msg { sessionFiles := make([]SessionFile, 0, len(fileMap)) for path, fh := range fileMap { - _, additions, deletions := diff.GenerateDiff(fh.initialVersion.Content, fh.latestVersion.Content, fh.initialVersion.Path) + cwd := config.Get().WorkingDir() + path = strings.TrimPrefix(path, cwd) + _, additions, deletions := diff.GenerateDiff(fh.initialVersion.Content, fh.latestVersion.Content, path) sessionFiles = append(sessionFiles, SessionFile{ History: fh, FilePath: path,