Files
skillshare/internal/install/local_changes.go
T
Willie 2d196c286b 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.
2026-10-24 17:32:34 +08:00

111 lines
3.6 KiB
Go

package install
import (
"errors"
"fmt"
"os"
"path/filepath"
"slices"
"sort"
"strings"
"skillshare/internal/sourcewalk"
"skillshare/internal/textdiff"
)
// FileChange is a file of an installed skill that differs from the version
// the skill was installed at.
type FileChange struct {
Path string `json:"path"`
Status string `json:"status"` // "modified", "added" or "deleted"
Diff string `json:"diff"` // textdiff.Files from the installed version to the local file
}
// ErrNoInstallRecord means the skill was not installed from a repository with
// recorded file hashes, so there is nothing to compare it with.
var ErrNoInstallRecord = errors.New("no install record to compare with")
// LocalChanges lists the files of the skill at dir whose hashes differ from
// the ones entry recorded, and diffs each against the skill's source checked
// out at the recorded commit. It fetches only when a recorded file changed.
func LocalChanges(entry *MetadataEntry, parseOpts ParseOptions, dir string, follow ...*sourcewalk.Follow) ([]FileChange, error) {
if entry == nil || entry.Tracked || entry.RepoURL == "" || len(entry.FileHashes) == 0 {
return nil, ErrNoInstallRecord
}
local, err := ComputeFileHashes(dir, follow...)
if err != nil {
return nil, err
}
var changes []FileChange
for path, hash := range local {
if recorded, ok := entry.FileHashes[path]; !ok {
changes = append(changes, FileChange{Path: path, Status: "added"})
} else if recorded != hash {
changes = append(changes, FileChange{Path: path, Status: "modified"})
}
}
for path := range entry.FileHashes {
if _, ok := local[path]; !ok {
changes = append(changes, FileChange{Path: path, Status: "deleted"})
}
}
if len(changes) == 0 {
return nil, nil
}
sort.Slice(changes, func(i, j int) bool { return changes[i].Path < changes[j].Path })
// An added file diffs against nothing, so the installed version is only
// fetched for files it had.
installed := ""
if slices.ContainsFunc(changes, func(c FileChange) bool { return c.Status != "added" }) {
// Upstream HEAD would show its own changes as the user's.
commit := installedCommit(entry)
if commit == "" {
return nil, errors.New("no recorded commit to compare with")
}
source, err := ParseSourceWithOptions(entry.Source, parseOpts)
if err != nil {
return nil, fmt.Errorf("invalid source: %w", err)
}
source.ApplyRecordedBranch(entry.Branch)
source.Commit = commit
tmp, err := os.MkdirTemp("", "skillshare-changes-")
if err != nil {
return nil, err
}
defer os.RemoveAll(tmp)
installed = filepath.Join(tmp, "skill")
// Only read, never used, so the audit gate does not apply.
if _, err := Install(source, installed, InstallOptions{SkipAudit: true}); err != nil {
return nil, fmt.Errorf("fetch the installed version: %w", err)
}
}
if len(follow) > 0 {
if resolved, ok := follow[0].Resolve(dir); ok {
dir = resolved
}
}
for i, c := range changes {
rel := filepath.FromSlash(c.Path)
old := "" // reads as empty
if c.Status != "added" {
old = filepath.Join(installed, rel)
}
changes[i].Diff = textdiff.Files(old, filepath.Join(dir, rel))
}
return changes, nil
}
// installedCommit is the commit the skill was installed at: the full SHA when
// it is the same commit as the short Version, since fetching a short SHA falls
// back to downloading the whole history. "" when none was recorded.
func installedCommit(entry *MetadataEntry) string {
if !IsCommitSHA(entry.Version) {
return ""
}
if IsCommitSHA(entry.Commit) && strings.HasPrefix(entry.Commit, entry.Version) {
return entry.Commit
}
return entry.Version
}