mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
* 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.
127 lines
4.3 KiB
Go
127 lines
4.3 KiB
Go
package install
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestLocalChanges_DiffsAgainstInstalledCommit(t *testing.T) {
|
|
repo := initTestRepo(t)
|
|
if err := os.WriteFile(filepath.Join(repo, "SKILL.md"), []byte("---\nname: alpha\n---\nv1\n"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
gitAdd(t, repo, ".")
|
|
gitCommit(t, repo, "v1")
|
|
|
|
sourceDir := t.TempDir()
|
|
dest := filepath.Join(sourceDir, "alpha")
|
|
source, err := ParseSource("file://" + repo)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := Install(source, dest, InstallOptions{SourceDir: sourceDir, SkipAudit: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
entry := LoadMetadataOrNew(sourceDir).Get("alpha")
|
|
|
|
// Upstream moves on; the diff must still be against the installed v1.
|
|
if err := os.WriteFile(filepath.Join(repo, "SKILL.md"), []byte("---\nname: alpha\n---\nv2\n"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
gitAdd(t, repo, ".")
|
|
gitCommit(t, repo, "v2")
|
|
|
|
// Edit in place like an editor would: git may have checked out CRLF on Windows.
|
|
installed, err := os.ReadFile(filepath.Join(dest, "SKILL.md"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dest, "SKILL.md"), []byte(strings.Replace(string(installed), "v1", "mine", 1)), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dest, "notes.md"), []byte("note\n"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
changes, err := LocalChanges(entry, ParseOptions{}, dest)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var got []string
|
|
for _, c := range changes {
|
|
got = append(got, c.Status+" "+c.Path+"\n"+strings.ReplaceAll(c.Diff, "\r", ""))
|
|
}
|
|
want := []string{
|
|
"modified SKILL.md\n ---\n name: alpha\n ---\n- v1\n+ mine\n",
|
|
"added notes.md\n+ note\n",
|
|
}
|
|
if strings.Join(got, "|") != strings.Join(want, "|") {
|
|
t.Errorf("changes =\n%q\nwant\n%q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestLocalChanges_UnchangedSkillDoesNotFetch(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte("# a\n"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
hashes, err := ComputeFileHashes(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
changes, err := LocalChanges(&MetadataEntry{Source: "file:///nonexistent/repo", RepoURL: "file:///nonexistent/repo", FileHashes: hashes}, ParseOptions{}, dir)
|
|
if err != nil || changes != nil {
|
|
t.Errorf("LocalChanges = %v, %v; want nil, nil", changes, err)
|
|
}
|
|
}
|
|
|
|
// Without the installed commit the diff would be against upstream HEAD and
|
|
// show upstream changes as the user's.
|
|
func TestLocalChanges_NoRecordedCommit(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte("# a\n"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err := LocalChanges(&MetadataEntry{Source: "file:///nonexistent/repo", RepoURL: "file:///nonexistent/repo", FileHashes: map[string]string{"SKILL.md": "sha256:old"}}, ParseOptions{}, dir)
|
|
if err == nil || !strings.Contains(err.Error(), "no recorded commit") {
|
|
t.Errorf("err = %v, want no recorded commit", err)
|
|
}
|
|
}
|
|
|
|
// Added files diff against nothing, so they need neither a commit nor a fetch.
|
|
func TestLocalChanges_OnlyAddedFilesDoNotFetch(t *testing.T) {
|
|
dir := t.TempDir()
|
|
if err := os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte("# a\n"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
hashes, err := ComputeFileHashes(dir)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "notes.md"), []byte("mine\n"), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
changes, err := LocalChanges(&MetadataEntry{Source: "file:///nonexistent/repo", RepoURL: "file:///nonexistent/repo", FileHashes: hashes}, ParseOptions{}, dir)
|
|
if err != nil || len(changes) != 1 || changes[0] != (FileChange{Path: "notes.md", Status: "added", Diff: "+ mine\n"}) {
|
|
t.Errorf("LocalChanges = %+v, %v", changes, err)
|
|
}
|
|
}
|
|
|
|
// A short SHA makes git fetch the whole history, so the full recorded commit
|
|
// is used when it is the same commit as Version.
|
|
func TestInstalledCommit(t *testing.T) {
|
|
full := "0123456789abcdef0123456789abcdef01234567"
|
|
for _, c := range []struct{ version, commit, want string }{
|
|
{"0123456", full, full},
|
|
{"0123456", "", "0123456"},
|
|
{"89abcde", full, "89abcde"}, // stale Commit from an older install
|
|
{"", full, ""},
|
|
} {
|
|
if got := installedCommit(&MetadataEntry{Version: c.version, Commit: c.commit}); got != c.want {
|
|
t.Errorf("installedCommit(%q, %q) = %q, want %q", c.version, c.commit, got, c.want)
|
|
}
|
|
}
|
|
}
|