chore: make diff config independent

This commit is contained in:
Kujtim Hoxha
2025-07-05 17:33:57 +02:00
parent 2a0f007d41
commit 97e5e8714b
4 changed files with 9 additions and 10 deletions

View File

@@ -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 (

View File

@@ -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)

View File

@@ -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()

View File

@@ -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,