on main help sign in
chore: make diff config independent
commit97e5e8714b16b964fbdf1b59696abfb5c197bf19 authorKujtim Hoxha <kujtimii.h@gmail.com> dateSat 5 Jul 2025, 17:33 (1 year ago) parent2a0f007
4 files changed+9−10
internal/diff/diff.go−5 internal/llm/tools/edit.go+3−3 internal/llm/tools/write.go+1−1 internal/tui/components/chat/sidebar/sidebar.go+5−1
internal/diff/diff.go−5
@@ -4,15 +4,10 @@ import (
44 "strings"
55
66 "github.com/aymanbagabas/go-udiff"
7− "github.com/charmbracelet/crush/internal/config"
87)
98
109// GenerateDiff creates a unified diff from two file contents
1110func GenerateDiff(beforeContent, afterContent, fileName string) (string, int, int) {
12− // remove the cwd prefix and ensure consistent path format
13− // this prevents issues with absolute paths in different environments
14− cwd := config.Get().WorkingDir()
15− fileName = strings.TrimPrefix(fileName, cwd)
1611 fileName = strings.TrimPrefix(fileName, "/")
1712
1813 var (
internal/llm/tools/edit.go+3−3
@@ -206,7 +206,7 @@ func (e *editTool) createNewFile(ctx context.Context, filePath, content string)
206206 _, additions, removals := diff.GenerateDiff(
207207 "",
208208 content,
209− filePath,
209+ strings.TrimPrefix(filePath, config.Get().WorkingDir()),
210210 )
211211 rootDir := config.Get().WorkingDir()
212212 permissionPath := filepath.Dir(filePath)
@@ -318,7 +318,7 @@ func (e *editTool) deleteContent(ctx context.Context, filePath, oldString string
318318 _, additions, removals := diff.GenerateDiff(
319319 oldContent,
320320 newContent,
321− filePath,
321+ strings.TrimPrefix(filePath, config.Get().WorkingDir()),
322322 )
323323
324324 rootDir := config.Get().WorkingDir()
@@ -441,7 +441,7 @@ func (e *editTool) replaceContent(ctx context.Context, filePath, oldString, newS
441441 _, additions, removals := diff.GenerateDiff(
442442 oldContent,
443443 newContent,
444− filePath,
444+ strings.TrimPrefix(filePath, config.Get().WorkingDir()),
445445 )
446446 rootDir := config.Get().WorkingDir()
447447 permissionPath := filepath.Dir(filePath)
internal/llm/tools/write.go+1−1
@@ -168,7 +168,7 @@ func (w *writeTool) Run(ctx context.Context, call ToolCall) (ToolResponse, error
168168 diff, additions, removals := diff.GenerateDiff(
169169 oldContent,
170170 params.Content,
171− filePath,
171+ strings.TrimPrefix(filePath, config.Get().WorkingDir()),
172172 )
173173
174174 rootDir := config.Get().WorkingDir()
internal/tui/components/chat/sidebar/sidebar.go+5−1
@@ -160,6 +160,8 @@ func (m *sidebarCmp) handleFileHistoryEvent(event pubsub.Event[history.File]) te
160160 before := existing.History.initialVersion.Content
161161 after := existing.History.latestVersion.Content
162162 path := existing.History.initialVersion.Path
163+ cwd := config.Get().WorkingDir()
164+ path = strings.TrimPrefix(path, cwd)
163165 _, additions, deletions := diff.GenerateDiff(before, after, path)
164166 existing.Additions = additions
165167 existing.Deletions = deletions
@@ -213,7 +215,9 @@ func (m *sidebarCmp) loadSessionFiles() tea.Msg {
213215
214216 sessionFiles := make([]SessionFile, 0, len(fileMap))
215217 for path, fh := range fileMap {
216− _, additions, deletions := diff.GenerateDiff(fh.initialVersion.Content, fh.latestVersion.Content, fh.initialVersion.Path)
218+ cwd := config.Get().WorkingDir()
219+ path = strings.TrimPrefix(path, cwd)
220+ _, additions, deletions := diff.GenerateDiff(fh.initialVersion.Content, fh.latestVersion.Content, path)
217221 sessionFiles = append(sessionFiles, SessionFile{
218222 History: fh,
219223 FilePath: path,