feat(check): show what changed in a skill since install (#623)

* feat(check): show what changed in a skill since install

#622 made a hand-edited skill visible as modified and kept update from
overwriting it, but there was still no way to see the edits before
deciding to keep or discard them (#621).

LocalChanges compares the skill's files with the hashes recorded at
install, fetches the source at the recorded commit only when a recorded
file changed, and diffs each file against it, so the diff shows the
user's edits even after upstream moves on. A skill without a recorded
commit is refused rather than diffed against HEAD, which would show
upstream changes as the user's.

It backs `check <name> --diff`, GET /api/resources/{name}/local-changes,
and the dashboard: the Modified locally dialog on the skill page lists
the diffs, and the Updates tab links a modified skill to it. update's
skip hints now point at --diff. The route looks up the exact record and
refuses non-local names, so a nested or ../ path cannot borrow a
top-level skill's record or read outside the source. The file diff
helper moved to textdiff so the CLI and server share it.

* fix(install): diff local changes against the full installed commit

entry.Version holds the short SHA from `rev-parse --short`. A targeted
fetch needs the full SHA, so checkoutCommit fell back to fetching every
branch and tag with --unshallow: viewing one edited skill could download
the repository's whole history, and fail when the commit is only
reachable by its full SHA.

Use entry.Commit, the full SHA install records, when it is the same
commit as Version; a Commit left from an older install, or a legacy
record without one, falls back to Version.

* test(install): keep checkout line endings when editing in LocalChanges test

On Windows git checks the installed copy out with CRLF, so overwriting
SKILL.md with LF content made every line differ and the expected diff
failed. Edit the installed file in place, as an editor would, and compare
the diff without carriage returns.
This commit is contained in:
Willie
2026-10-24 17:32:34 +08:00
committed by GitHub
parent 7a7cceefab
commit 2d196c286b
53 changed files with 850 additions and 131 deletions
+58
View File
@@ -2,6 +2,7 @@
package textdiff
import (
"os"
"strings"
"github.com/sergi/go-diff/diffmatchpatch"
@@ -50,3 +51,60 @@ func format(diffs []diffmatchpatch.Diff) string {
return b.String()
}
// maxDiffFileSize is the maximum file size for generating unified diffs.
// Files larger than this are skipped to avoid performance issues.
const maxDiffFileSize = 1_000_000 // 1MB
// Files renders Lines for the files at oldPath and newPath. A missing file
// reads as empty. Returns "(binary file differs)" if either file contains null
// bytes in the first 8000 chars, and "(file too large for diff)" if either
// file exceeds maxDiffFileSize.
func Files(oldPath, newPath string) string {
if fileExceedsSize(oldPath, maxDiffFileSize) || fileExceedsSize(newPath, maxDiffFileSize) {
return "(file too large for diff)"
}
oldContent := readFileString(oldPath)
newContent := readFileString(newPath)
if isBinary(oldContent) || isBinary(newContent) {
return "(binary file differs)"
}
return Lines(oldContent, newContent)
}
// 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)
}
// fileExceedsSize returns true if the file at path is larger than limit bytes.
// Returns false if the file doesn't exist or can't be stat'd.
func fileExceedsSize(path string, limit int64) bool {
info, err := os.Stat(path)
if err != nil {
return false
}
return info.Size() > limit
}
// 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
}
+45
View File
@@ -0,0 +1,45 @@
package textdiff
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestFiles(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)
}
result := Files(oldPath, newPath)
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")
}
}