mirror of
https://github.com/charmbracelet/crush.git
synced 2025-08-02 05:20:46 +03:00
30 lines
692 B
Go
30 lines
692 B
Go
package diff
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/aymanbagabas/go-udiff"
|
|
)
|
|
|
|
// GenerateDiff creates a unified diff from two file contents
|
|
func GenerateDiff(beforeContent, afterContent, fileName string) (string, int, int) {
|
|
fileName = strings.TrimPrefix(fileName, "/")
|
|
|
|
var (
|
|
unified = udiff.Unified("a/"+fileName, "b/"+fileName, beforeContent, afterContent)
|
|
additions = 0
|
|
removals = 0
|
|
)
|
|
|
|
lines := strings.SplitSeq(unified, "\n")
|
|
for line := range lines {
|
|
if strings.HasPrefix(line, "+") && !strings.HasPrefix(line, "+++") {
|
|
additions++
|
|
} else if strings.HasPrefix(line, "-") && !strings.HasPrefix(line, "---") {
|
|
removals++
|
|
}
|
|
}
|
|
|
|
return unified, additions, removals
|
|
}
|