feat(diff): add file-level diff engine for skill content comparison

Implement diffSkillFiles to walk source/destination directories and
detect add/modify/delete changes at the file level. Uses SHA-256 for
content comparison and sergi/go-diff for unified text diffs. Includes
binary detection, git-style stat rendering, and 9 unit tests.
This commit is contained in:
Willie
2026-03-02 02:23:50 +08:00
parent 7bdb9a4762
commit 398e0bf40c
3 changed files with 426 additions and 1 deletions
+235
View File
@@ -0,0 +1,235 @@
package main
import (
"crypto/sha256"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/sergi/go-diff/diffmatchpatch"
)
// fileDiffEntry represents a single file-level difference between source and destination.
type fileDiffEntry struct {
RelPath string
Action string // "add", "modify", "delete"
SrcSize int64
DstSize int64
SrcMtime time.Time
DstMtime time.Time
}
// diffSkillFiles walks both directories and returns file-level diffs.
// src-only = add, dst-only = delete, both but different content = modify.
// Results are sorted by RelPath. If dstDir doesn't exist, all src files are "add".
func diffSkillFiles(srcDir, dstDir string) []fileDiffEntry {
srcFiles := walkFiles(srcDir)
dstFiles := walkFiles(dstDir)
var entries []fileDiffEntry
// Files in source
for rel, srcInfo := range srcFiles {
dstInfo, inDst := dstFiles[rel]
if !inDst {
entries = append(entries, fileDiffEntry{
RelPath: rel,
Action: "add",
SrcSize: srcInfo.Size(),
SrcMtime: srcInfo.ModTime(),
})
continue
}
// Both exist — check if content differs
if srcInfo.Size() != dstInfo.Size() || !fileContentsEqual(filepath.Join(srcDir, filepath.FromSlash(rel)), filepath.Join(dstDir, filepath.FromSlash(rel))) {
entries = append(entries, fileDiffEntry{
RelPath: rel,
Action: "modify",
SrcSize: srcInfo.Size(),
DstSize: dstInfo.Size(),
SrcMtime: srcInfo.ModTime(),
DstMtime: dstInfo.ModTime(),
})
}
}
// Files only in destination
for rel, dstInfo := range dstFiles {
if _, inSrc := srcFiles[rel]; !inSrc {
entries = append(entries, fileDiffEntry{
RelPath: rel,
Action: "delete",
DstSize: dstInfo.Size(),
DstMtime: dstInfo.ModTime(),
})
}
}
sort.Slice(entries, func(i, j int) bool {
return entries[i].RelPath < entries[j].RelPath
})
return entries
}
// walkFiles walks a directory and returns a map of relPath -> FileInfo.
// Skips .git directories. Returns empty map if root doesn't exist.
func walkFiles(root string) map[string]os.FileInfo {
result := make(map[string]os.FileInfo)
if root == "" {
return result
}
if _, err := os.Stat(root); err != nil {
return result
}
_ = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info.IsDir() {
if info.Name() == ".git" {
return filepath.SkipDir
}
return nil
}
rel, relErr := filepath.Rel(root, path)
if relErr != nil {
return nil
}
// Normalize path separators to forward slash
rel = strings.ReplaceAll(rel, "\\", "/")
result[rel] = info
return nil
})
return result
}
// fileContentsEqual compares two files by SHA-256 hash.
// Returns false if either file is unreadable.
func fileContentsEqual(pathA, pathB string) bool {
hashA := fileHash(pathA)
hashB := fileHash(pathB)
if hashA == "" || hashB == "" {
return false
}
return hashA == hashB
}
// fileHash returns the SHA-256 hex hash of a file. Returns "" on error.
func fileHash(path string) string {
f, err := os.Open(path)
if err != nil {
return ""
}
defer f.Close()
h := sha256.New()
if _, err := io.Copy(h, f); err != nil {
return ""
}
return fmt.Sprintf("%x", h.Sum(nil))
}
// generateUnifiedDiff produces a unified diff between two files.
// dstContent is treated as "old", srcContent as "new".
// Returns "(binary file differs)" if either file contains null bytes in the first 8000 chars.
func generateUnifiedDiff(srcPath, dstPath string) string {
srcContent := readFileString(srcPath)
dstContent := readFileString(dstPath)
if isBinary(srcContent) || isBinary(dstContent) {
return "(binary file differs)"
}
dmp := diffmatchpatch.New()
// dstContent is "old", srcContent is "new"
a, b, lineArray := dmp.DiffLinesToChars(dstContent, srcContent)
diffs := dmp.DiffMain(a, b, false)
diffs = dmp.DiffCharsToLines(diffs, lineArray)
diffs = dmp.DiffCleanupSemantic(diffs)
return formatUnifiedDiff(diffs)
}
// readFileString reads a file and returns its content as a string.
// Returns "" on error.
func readFileString(path string) string {
data, err := os.ReadFile(path)
if err != nil {
return ""
}
return string(data)
}
// isBinary checks whether a string contains null bytes in the first 8000 characters.
func isBinary(content string) bool {
limit := len(content)
if limit > 8000 {
limit = 8000
}
for i := 0; i < limit; i++ {
if content[i] == 0 {
return true
}
}
return false
}
// formatUnifiedDiff formats diffmatchpatch.Diff slices as a unified diff string.
// Insert lines are prefixed with "+ ", Delete lines with "- ", Equal lines with " ".
func formatUnifiedDiff(diffs []diffmatchpatch.Diff) string {
var b strings.Builder
for _, d := range diffs {
lines := strings.Split(d.Text, "\n")
// If the text ends with \n, Split produces a trailing empty string.
// Don't emit a line for that trailing empty element.
if len(lines) > 0 && lines[len(lines)-1] == "" {
lines = lines[:len(lines)-1]
}
var prefix string
switch d.Type {
case diffmatchpatch.DiffInsert:
prefix = "+ "
case diffmatchpatch.DiffDelete:
prefix = "- "
case diffmatchpatch.DiffEqual:
prefix = " "
}
for _, line := range lines {
b.WriteString(prefix)
b.WriteString(line)
b.WriteString("\n")
}
}
return b.String()
}
// renderFileStat returns a git-style stat summary for file diffs.
//
// add: " + relpath (N bytes)"
// delete: " - relpath (N bytes)"
// modify: " ~ relpath (old -> new bytes)"
func renderFileStat(files []fileDiffEntry) string {
var b strings.Builder
for _, f := range files {
switch f.Action {
case "add":
fmt.Fprintf(&b, " + %s (%d bytes)\n", f.RelPath, f.SrcSize)
case "delete":
fmt.Fprintf(&b, " - %s (%d bytes)\n", f.RelPath, f.DstSize)
case "modify":
fmt.Fprintf(&b, " ~ %s (%d → %d bytes)\n", f.RelPath, f.DstSize, f.SrcSize)
}
}
return b.String()
}
+190
View File
@@ -0,0 +1,190 @@
package main
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestDiffSkillFiles_IdenticalDirs(t *testing.T) {
src := t.TempDir()
dst := t.TempDir()
writeTestFile(t, src, "SKILL.md", "# Hello")
writeTestFile(t, dst, "SKILL.md", "# Hello")
diffs := diffSkillFiles(src, dst)
if len(diffs) != 0 {
t.Fatalf("expected 0 diffs for identical dirs, got %d: %+v", len(diffs), diffs)
}
}
func TestDiffSkillFiles_NewFile(t *testing.T) {
src := t.TempDir()
dst := t.TempDir()
writeTestFile(t, src, "SKILL.md", "# Hello")
writeTestFile(t, src, "extra.md", "extra content")
writeTestFile(t, dst, "SKILL.md", "# Hello")
diffs := diffSkillFiles(src, dst)
if len(diffs) != 1 {
t.Fatalf("expected 1 diff, got %d: %+v", len(diffs), diffs)
}
if diffs[0].Action != "add" {
t.Errorf("expected action 'add', got %q", diffs[0].Action)
}
if diffs[0].RelPath != "extra.md" {
t.Errorf("expected RelPath 'extra.md', got %q", diffs[0].RelPath)
}
}
func TestDiffSkillFiles_DeletedFile(t *testing.T) {
src := t.TempDir()
dst := t.TempDir()
writeTestFile(t, src, "SKILL.md", "# Hello")
writeTestFile(t, dst, "SKILL.md", "# Hello")
writeTestFile(t, dst, "old.md", "old content")
diffs := diffSkillFiles(src, dst)
if len(diffs) != 1 {
t.Fatalf("expected 1 diff, got %d: %+v", len(diffs), diffs)
}
if diffs[0].Action != "delete" {
t.Errorf("expected action 'delete', got %q", diffs[0].Action)
}
if diffs[0].RelPath != "old.md" {
t.Errorf("expected RelPath 'old.md', got %q", diffs[0].RelPath)
}
}
func TestDiffSkillFiles_ModifiedFile(t *testing.T) {
src := t.TempDir()
dst := t.TempDir()
writeTestFile(t, src, "SKILL.md", "# Hello v2")
writeTestFile(t, dst, "SKILL.md", "# Hello v1")
diffs := diffSkillFiles(src, dst)
if len(diffs) != 1 {
t.Fatalf("expected 1 diff, got %d: %+v", len(diffs), diffs)
}
if diffs[0].Action != "modify" {
t.Errorf("expected action 'modify', got %q", diffs[0].Action)
}
}
func TestDiffSkillFiles_SrcOnly(t *testing.T) {
src := t.TempDir()
writeTestFile(t, src, "a.md", "aaa")
writeTestFile(t, src, "b.md", "bbb")
diffs := diffSkillFiles(src, "/nonexistent_dir_that_does_not_exist")
if len(diffs) != 2 {
t.Fatalf("expected 2 diffs, got %d: %+v", len(diffs), diffs)
}
for _, d := range diffs {
if d.Action != "add" {
t.Errorf("expected action 'add', got %q for %s", d.Action, d.RelPath)
}
}
}
func TestDiffSkillFiles_NestedDir(t *testing.T) {
src := t.TempDir()
dst := t.TempDir()
writeTestFile(t, src, "SKILL.md", "# Hello")
writeTestFile(t, dst, "SKILL.md", "# Hello")
// Add a nested file only in source
subDir := filepath.Join(src, "sub")
if err := os.MkdirAll(subDir, 0755); err != nil {
t.Fatal(err)
}
writeTestFile(t, src, filepath.Join("sub", "extra.md"), "nested content")
diffs := diffSkillFiles(src, dst)
if len(diffs) != 1 {
t.Fatalf("expected 1 diff, got %d: %+v", len(diffs), diffs)
}
if diffs[0].RelPath != "sub/extra.md" {
t.Errorf("expected RelPath 'sub/extra.md', got %q", diffs[0].RelPath)
}
if diffs[0].Action != "add" {
t.Errorf("expected action 'add', got %q", diffs[0].Action)
}
}
func TestGenerateUnifiedDiff(t *testing.T) {
dir := t.TempDir()
oldPath := filepath.Join(dir, "old.txt")
newPath := filepath.Join(dir, "new.txt")
if err := os.WriteFile(oldPath, []byte("line1\nline2\nline3\n"), 0644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(newPath, []byte("line1\nmodified\nline3\nextra\n"), 0644); err != nil {
t.Fatal(err)
}
// newPath is src (new), oldPath is dst (old)
result := generateUnifiedDiff(newPath, oldPath)
if !strings.Contains(result, "+") && !strings.Contains(result, "-") {
t.Errorf("expected diff output to contain '+' and '-' lines, got:\n%s", result)
}
if !strings.Contains(result, "modified") {
t.Errorf("expected diff to mention 'modified', got:\n%s", result)
}
if !strings.Contains(result, "extra") {
t.Errorf("expected diff to mention 'extra', got:\n%s", result)
}
}
func TestIsBinary(t *testing.T) {
if !isBinary("hello\x00world") {
t.Error("expected isBinary to return true for string with null byte")
}
if isBinary("hello world, this is normal text") {
t.Error("expected isBinary to return false for normal text")
}
if isBinary("") {
t.Error("expected isBinary to return false for empty string")
}
}
func TestRenderFileStat(t *testing.T) {
files := []fileDiffEntry{
{RelPath: "new.md", Action: "add", SrcSize: 100},
{RelPath: "changed.md", Action: "modify", DstSize: 200, SrcSize: 250},
{RelPath: "removed.md", Action: "delete", DstSize: 50},
}
result := renderFileStat(files)
if !strings.Contains(result, "+ new.md (100 bytes)") {
t.Errorf("expected add line for new.md, got:\n%s", result)
}
if !strings.Contains(result, "~ changed.md (200") {
t.Errorf("expected modify line for changed.md, got:\n%s", result)
}
if !strings.Contains(result, "- removed.md (50 bytes)") {
t.Errorf("expected delete line for removed.md, got:\n%s", result)
}
}
// writeTestFile creates a file relative to dir with the given content.
func writeTestFile(t *testing.T, dir, relPath, content string) {
t.Helper()
full := filepath.Join(dir, relPath)
parent := filepath.Dir(full)
if err := os.MkdirAll(parent, 0755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(full, []byte(content), 0644); err != nil {
t.Fatal(err)
}
}
+1 -1
View File
@@ -10,6 +10,7 @@ require (
github.com/charmbracelet/x/ansi v0.11.6
github.com/mattn/go-runewidth v0.0.19
github.com/pterm/pterm v0.12.82
github.com/sergi/go-diff v1.4.0
golang.org/x/term v0.32.0
gopkg.in/yaml.v3 v3.0.1
)
@@ -45,7 +46,6 @@ require (
github.com/muesli/termenv v0.16.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/sahilm/fuzzy v0.1.1 // indirect
github.com/sergi/go-diff v1.4.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
github.com/yuin/goldmark v1.7.8 // indirect
github.com/yuin/goldmark-emoji v1.0.5 // indirect