mirror of
https://github.com/charmbracelet/crush.git
synced 2025-08-02 05:20:46 +03:00
chore: make diff config independent
This commit is contained in:
@@ -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 (
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user