on main help sign in
chore: make diff config independent
97e5e87Kujtim Hoxha
internal/diff/diff.go−5
@@ -4,15 +4,10 @@ import (
4 "strings"
4 "strings"
5
5
6 "github.com/aymanbagabas/go-udiff"
6 "github.com/aymanbagabas/go-udiff"
7− "github.com/charmbracelet/crush/internal/config"
8)
7)
9
8
10// GenerateDiff creates a unified diff from two file contents
9// GenerateDiff creates a unified diff from two file contents
11func GenerateDiff(beforeContent, afterContent, fileName string) (string, int, int) {
10func 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)
16 fileName = strings.TrimPrefix(fileName, "/")
11 fileName = strings.TrimPrefix(fileName, "/")
17
12
18 var (
13 var (
internal/llm/tools/edit.go+3−3
@@ -206,7 +206,7 @@ func (e *editTool) createNewFile(ctx context.Context, filePath, content string)
206 _, additions, removals := diff.GenerateDiff(
206 _, additions, removals := diff.GenerateDiff(
207 "",
207 "",
208 content,
208 content,
209− filePath,
209+ strings.TrimPrefix(filePath, config.Get().WorkingDir()),
210 )
210 )
211 rootDir := config.Get().WorkingDir()
211 rootDir := config.Get().WorkingDir()
212 permissionPath := filepath.Dir(filePath)
212 permissionPath := filepath.Dir(filePath)
@@ -318,7 +318,7 @@ func (e *editTool) deleteContent(ctx context.Context, filePath, oldString string
318 _, additions, removals := diff.GenerateDiff(
318 _, additions, removals := diff.GenerateDiff(
319 oldContent,
319 oldContent,
320 newContent,
320 newContent,
321− filePath,
321+ strings.TrimPrefix(filePath, config.Get().WorkingDir()),
322 )
322 )
323
323
324 rootDir := config.Get().WorkingDir()
324 rootDir := config.Get().WorkingDir()
@@ -441,7 +441,7 @@ func (e *editTool) replaceContent(ctx context.Context, filePath, oldString, newS
441 _, additions, removals := diff.GenerateDiff(
441 _, additions, removals := diff.GenerateDiff(
442 oldContent,
442 oldContent,
443 newContent,
443 newContent,
444− filePath,
444+ strings.TrimPrefix(filePath, config.Get().WorkingDir()),
445 )
445 )
446 rootDir := config.Get().WorkingDir()
446 rootDir := config.Get().WorkingDir()
447 permissionPath := filepath.Dir(filePath)
447 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
168 diff, additions, removals := diff.GenerateDiff(
168 diff, additions, removals := diff.GenerateDiff(
169 oldContent,
169 oldContent,
170 params.Content,
170 params.Content,
171− filePath,
171+ strings.TrimPrefix(filePath, config.Get().WorkingDir()),
172 )
172 )
173
173
174 rootDir := config.Get().WorkingDir()
174 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
160 before := existing.History.initialVersion.Content
160 before := existing.History.initialVersion.Content
161 after := existing.History.latestVersion.Content
161 after := existing.History.latestVersion.Content
162 path := existing.History.initialVersion.Path
162 path := existing.History.initialVersion.Path
163+ cwd := config.Get().WorkingDir()
164+ path = strings.TrimPrefix(path, cwd)
163 _, additions, deletions := diff.GenerateDiff(before, after, path)
165 _, additions, deletions := diff.GenerateDiff(before, after, path)
164 existing.Additions = additions
166 existing.Additions = additions
165 existing.Deletions = deletions
167 existing.Deletions = deletions
@@ -213,7 +215,9 @@ func (m *sidebarCmp) loadSessionFiles() tea.Msg {
213
215
214 sessionFiles := make([]SessionFile, 0, len(fileMap))
216 sessionFiles := make([]SessionFile, 0, len(fileMap))
215 for path, fh := range fileMap {
217 for path, fh := range fileMap {
218+ cwd := config.Get().WorkingDir()
219+ path = strings.TrimPrefix(path, cwd)
216− _, additions, deletions := diff.GenerateDiff(fh.initialVersion.Content, fh.latestVersion.Content, fh.initialVersion.Path)
220+ _, additions, deletions := diff.GenerateDiff(fh.initialVersion.Content, fh.latestVersion.Content, path)
217 sessionFiles = append(sessionFiles, SessionFile{
221 sessionFiles = append(sessionFiles, SessionFile{
218 History: fh,
222 History: fh,
219 FilePath: path,
223 FilePath: path,