mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
* fix(sync): close gaps found while simulating the prefixed naming switch Simulating the #497 reporter's switch from merge + flat to copy + prefixed turned up four problems: - `target X --mode copy --target-naming prefixed` applied only --mode, since the setter dispatch returned after the first flag. Both flags given together now check the final pair, then apply in an order that keeps the saved pair valid (copy mode first, any other mode last). - Skipped skills were only counted, so a prefixed name over 64 characters vanished with "1 skill(s) skipped". Sync now names the first five with their reason; the cap keeps huge sources from flooding the output. - In copy mode a merge-mode link to the skill was not a managed entry, so when a local folder held the new name the old link was pruned and the skill left the target, contrary to the documented "old managed entry is preserved". Copy mode now migrates such links like managed copies. - PATCH /api/targets validated the mode/naming pair against the current skills state, so disabling skills while changing mode was rejected. It now checks the state the request leaves the target in. * docs(target): drop stray space in the Japanese mode+naming sentence * fix(diff): save paired target settings once and report kept legacy entries - `target X --mode M --target-naming N` ran the two setters, each saving the config, so a failed second save left half the change behind (Codex on #504). Both fields are now set and saved once, after the final pair is checked. - When a local folder holds the name a skill's naming now gives it, sync keeps the skill under its old entry and leaves the folder alone, even with --force. diff still called the folder a "local copy" and suggested `sync --force`, which does nothing for it. CLI and Web API diff, copy and merge mode, now report the folder and the entry the skill stays at. * fix(sync): migrate only links the manifest records as merge-made Copy mode treated any link to the skill's source at an old naming's name as the entry merge mode left behind, so a user's own link was renamed and replaced with a copy (Codex on #504). A link now migrates only when the manifest records it as "symlink", which merge mode writes for links it makes; other links stay as the user left them. The value is now a named constant. * fix(diff): mark kept legacy entries as unaffected by force Two problems Codex found on #504: - The Web API diff sent a local folder on a skill's new name as a "skip", which the dashboard shows as an edited entry that Force replaces. Sync keeps that folder and the legacy entry even with force, so the preview promised a change that never happens. It is now its own "kept" action, shown with its reason and not counted. - `target X --mode M --target-naming N` printed the target's own naming as the old value, so a target inheriting standard or prefixed showed "flat -> ...". It now prints the naming the target had in effect. * fix(diff): keep kept-name conflicts out of the sync count Two problems Codex found on #504: - A local folder on a skill's new name counted toward "N to sync" and fell through to the plain `skillshare sync` hint, though sync changes nothing there until the folder is renamed or removed. Such rows now carry a kept flag, count as neither sync nor local, and print their own hint; a target whose only row is kept still shows that row. - The dashboard grouped targets by row name and icon only, so two targets keeping the same skill at different old entries merged and showed one target's old location for both. The row detail is now part of the key. * fix(diff): do not list a kept local folder's files as deletions A kept row (a local folder on a skill's new name) still pointed at that folder, so `diff --stat` and `--patch` listed each of its files as removed, though sync never touches the folder (Codex on #504). Kept rows no longer point at a target directory, so no file-level diff is built for them in the CLI or the TUI. * fix(diff): report kept skills consistently in every diff view An exhaustive review of the kept-row change (prompted by two Codex findings on #504) found the same gap in several places: - A target whose only rows were kept was summarized as "in sync", and mixed summaries left it out of every count. Such targets now count as "N kept under old name" with a warning mark, and the scan progress counts them. - Plain output and the TUI hid where the skill stays; rows now read "name (stays at old-name)". The TUI also lacked the kept Next hint and called kept-only targets "differs"; it now matches the plain output. - `diff --json` emitted kept rows as "remove", indistinguishable from local-only folders; it now emits "kept", as the Web API does. The diff docs' JSON example also showed actions the CLI never emits. - Renamed rows whose old entry is a merge-made link showed a bogus file stat, because the link was walked as a file; a link now stands for the folder it points to. - A dry-run migration printed "Would copy" to the old path; it now names the new one. * fix(sync): stop reporting kept folders and skipped skills as pending work (#509) Three reports told users to do something sync cannot do, or hid what it did: - Copy and merge sync kept a folder the user made on a skill's name but counted it as "up to date" with no hint. Sync now counts it as local and adds one capped line per target naming the folders and saying that `sync --force` replaces them. The line is added in SyncSkillTarget, so the dashboard gets it too. - status and doctor counted skills that sync skips on purpose (invalid names under standard or prefixed naming, name collisions) as "not synced — run skillshare sync". Running sync can never add them, so the advice looped. They now compare against the skills sync places. - diff marked a target whose only items were local-only folders as "to sync" and `synced: false`. Those items are still listed, but they no longer make the target pending. * fix(target): apply every setting flag, require flag values, report no-ops as unchanged (#507) * fix(target): apply every setting flag, require flag values, report no-ops as unchanged `skillshare target <name>` dispatched --mode, --agent-mode and --target-naming through an if-chain that returned after the first match, so `--agent-mode copy --target-naming prefixed` applied one setting and silently dropped the other. Filters, --skills and --dry-run had the same early-return problem. - Check --mode, --agent-mode and --target-naming as a whole and save once, in global and project mode (one targetSettingsUpdate replaces six per-flag functions). - Reject --mode/--agent-mode/--target-naming combined with filters or --skills, and --dry-run without --skills, instead of dropping them. - A value flag followed by another flag (or an empty string) now reports "<flag> requires a value"; `--target-naming -p` used to report an invalid naming '-p'. - A value the target already has is reported as unchanged and the config is not rewritten. * fix(target): leave the skills pair alone on agents-only changes A target with skills off may keep target_naming prefixed with a merge mode it cannot sync; validateGlobalTarget and validateProjectTarget only check that pair for enabled skills. The combined settings update ran the pair check for every setting, so `target claude --agent-mode copy` was refused on such a target although the old agent-mode handler never looked at skills settings. Check the pair only when --mode or --target-naming is given. * docs(target): show combined settings in --help and the built-in skill The combined --mode/--agent-mode/--target-naming form is now the way to move a target to copy + prefixed in one step, but neither the help examples nor the built-in skill's target reference showed it.
577 lines
19 KiB
Go
577 lines
19 KiB
Go
package sync
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
|
|
"skillshare/internal/config"
|
|
"skillshare/internal/skillignore"
|
|
"skillshare/internal/utils"
|
|
)
|
|
|
|
// CopyResult holds the result of a copy sync operation.
|
|
type CopyResult struct {
|
|
Copied []string // newly copied skills
|
|
Skipped []string // checksum unchanged, or a user's folder kept
|
|
KeptLocal []string // the Skipped that are a user's folder on a skill's name; --force replaces them
|
|
Updated []string // checksum changed, overwritten
|
|
DirCreated string // Non-empty if target directory was auto-created (or would be in dry-run)
|
|
// UnmatchedIncludes are the include patterns that select no skill.
|
|
UnmatchedIncludes []UnmatchedInclude
|
|
}
|
|
|
|
// CopyOptions controls copy-mode sync behavior.
|
|
type CopyOptions struct {
|
|
IgnorePatterns []string
|
|
}
|
|
|
|
// SyncTargetCopy performs copy mode sync — copies each skill individually
|
|
// while preserving target-specific (unmanaged) skills.
|
|
func SyncTargetCopy(name string, target config.TargetConfig, sourcePath string, dryRun, force bool) (*CopyResult, error) {
|
|
return SyncTargetCopyWithOptions(name, target, sourcePath, dryRun, force, CopyOptions{
|
|
IgnorePatterns: DefaultFileIgnorePatterns(),
|
|
})
|
|
}
|
|
|
|
// SyncTargetCopyWithOptions is SyncTargetCopy with explicit copy behavior options.
|
|
func SyncTargetCopyWithOptions(name string, target config.TargetConfig, sourcePath string, dryRun, force bool, opts CopyOptions) (*CopyResult, error) {
|
|
skills, err := DiscoverSourceSkills(sourcePath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to discover skills: %w", err)
|
|
}
|
|
return SyncTargetCopyWithSkillsOptions(name, target, skills, sourcePath, dryRun, force, nil, opts)
|
|
}
|
|
|
|
// SyncTargetCopyWithSkills is like SyncTargetCopy but accepts pre-discovered skills
|
|
// and an optional progress callback for per-skill UI updates.
|
|
// sourcePath is the skills source directory, used to detect symlink-mode targets.
|
|
func SyncTargetCopyWithSkills(name string, target config.TargetConfig, allSkills []DiscoveredSkill, sourcePath string, dryRun, force bool, onProgress func(current, total int, skill string)) (*CopyResult, error) {
|
|
return SyncTargetCopyWithSkillsOptions(name, target, allSkills, sourcePath, dryRun, force, onProgress, CopyOptions{
|
|
IgnorePatterns: DefaultFileIgnorePatterns(),
|
|
})
|
|
}
|
|
|
|
// SyncTargetCopyWithSkillsOptions is SyncTargetCopyWithSkills with explicit copy behavior options.
|
|
func SyncTargetCopyWithSkillsOptions(name string, target config.TargetConfig, allSkills []DiscoveredSkill, sourcePath string, dryRun, force bool, onProgress func(current, total int, skill string), opts CopyOptions) (*CopyResult, error) {
|
|
sc := target.SkillsConfig()
|
|
result := &CopyResult{}
|
|
if !sc.IsEnabled() {
|
|
return result, nil
|
|
}
|
|
ignorePatterns := opts.IgnorePatterns
|
|
if ignorePatterns == nil {
|
|
ignorePatterns = DefaultFileIgnorePatterns()
|
|
}
|
|
|
|
// Convert from symlink mode if needed, auto-create if missing.
|
|
dirCreated, err := ensureRealTargetDir(sc.Path, sourcePath, "copy", dryRun)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if dirCreated {
|
|
result.DirCreated = sc.Path
|
|
}
|
|
// When dry-run would create the directory, suppress per-skill diagnostic
|
|
// messages (they flood the terminal). Counts are still populated.
|
|
quietDryRun := dirCreated && dryRun
|
|
|
|
resolution, err := ResolveTargetSkillsForTarget(name, target.SkillsConfig(), allSkills)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
printResolutionSummary(resolution)
|
|
result.UnmatchedIncludes = resolution.UnmatchedIncludes
|
|
|
|
// Read existing manifest
|
|
manifest, err := ReadManifest(sc.Path)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read manifest: %w", err)
|
|
}
|
|
naming := resolution.Naming
|
|
|
|
taken := resolution.ValidTargetNames()
|
|
for i, resolved := range resolution.Skills {
|
|
skill := resolved.Skill
|
|
activeName, err := selectActiveTargetNameForSync("copy", sc.Path, resolved, taken, manifest, dryRun)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if onProgress != nil {
|
|
onProgress(i+1, len(resolution.Skills), activeName)
|
|
}
|
|
|
|
targetSkillPath := filepath.Join(sc.Path, activeName)
|
|
|
|
// Compute source mtime for fast-path skip
|
|
currentMtime, mtimeErr := DirMaxMtimeWithIgnore(skill.SourcePath, ignorePatterns)
|
|
|
|
// mtime fast-path: if source mtime is unchanged AND target is still a valid dir, skip checksum
|
|
oldChecksum, isManaged := manifest.Managed[activeName]
|
|
oldMtime := manifest.Mtimes[activeName] // 0 if missing
|
|
// A copy made under another naming carries another name:, so it is
|
|
// refreshed even when the source is unchanged. A legacy entry kept
|
|
// under its old name keeps its old naming.
|
|
onDesiredName := activeName == resolved.TargetName
|
|
recordedNaming := manifest.Naming[activeName]
|
|
namingChanged := onDesiredName && recordedNaming != "" && recordedNaming != naming
|
|
// Under prefixed naming the copy's name: becomes the entry name, so it
|
|
// matches its folder. A legacy entry kept under its old name is copied as is.
|
|
rewriteName := ""
|
|
if naming == "prefixed" && onDesiredName && resolved.TargetName != resolved.SkillName {
|
|
rewriteName = resolved.TargetName
|
|
}
|
|
recordNaming := func() {
|
|
if onDesiredName && !dryRun {
|
|
manifest.Naming[activeName] = naming
|
|
}
|
|
}
|
|
if mtimeErr == nil && isManaged && !force && !namingChanged && oldMtime > 0 && currentMtime == oldMtime {
|
|
// Verify target still exists as a directory (user may have replaced it)
|
|
if ti, err := os.Lstat(targetSkillPath); err == nil && ti.IsDir() {
|
|
recordNaming()
|
|
result.Skipped = append(result.Skipped, activeName)
|
|
continue
|
|
}
|
|
}
|
|
|
|
// mtime changed or no record — compute full checksum
|
|
srcChecksum, err := DirChecksumWithIgnore(skill.SourcePath, ignorePatterns)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to checksum source skill %s: %w", activeName, err)
|
|
}
|
|
|
|
// Check what exists at the target path
|
|
targetInfo, lstatErr := os.Lstat(targetSkillPath)
|
|
exists := lstatErr == nil
|
|
|
|
if exists {
|
|
// If it's a symlink (leftover from merge mode), remove it
|
|
if utils.IsSymlinkOrJunction(targetSkillPath) {
|
|
if dryRun {
|
|
if !quietDryRun {
|
|
fmt.Fprintf(DiagOutput, "[dry-run] Would replace symlink with copy: %s\n", activeName)
|
|
}
|
|
} else {
|
|
os.Remove(targetSkillPath)
|
|
}
|
|
// Fall through to copy below
|
|
} else {
|
|
// Non-directory entries are invalid for managed skills.
|
|
// Managed/forced entries should be replaced with a proper skill directory.
|
|
if !targetInfo.IsDir() {
|
|
if isManaged || force {
|
|
if dryRun {
|
|
if !quietDryRun {
|
|
fmt.Fprintf(DiagOutput, "[dry-run] Would replace non-directory entry with copy: %s\n", activeName)
|
|
}
|
|
} else {
|
|
if err := os.RemoveAll(targetSkillPath); err != nil {
|
|
return nil, fmt.Errorf("failed to remove invalid entry %s: %w", activeName, err)
|
|
}
|
|
if err := copySkillToTarget(skill.SourcePath, targetSkillPath, rewriteName, ignorePatterns); err != nil {
|
|
return nil, fmt.Errorf("failed to copy skill %s: %w", activeName, err)
|
|
}
|
|
manifest.Managed[activeName] = srcChecksum
|
|
recordNaming()
|
|
if mtimeErr == nil {
|
|
manifest.Mtimes[activeName] = currentMtime
|
|
}
|
|
}
|
|
result.Updated = append(result.Updated, activeName)
|
|
continue
|
|
}
|
|
|
|
// Local non-directory entry — preserve unless --force.
|
|
result.Skipped = append(result.Skipped, activeName)
|
|
result.KeptLocal = append(result.KeptLocal, activeName)
|
|
continue
|
|
}
|
|
|
|
if !force && isManaged && !namingChanged && oldChecksum == srcChecksum {
|
|
// Unchanged — skip (but update mtime record if it changed)
|
|
if mtimeErr == nil && currentMtime != oldMtime && !dryRun {
|
|
manifest.Mtimes[activeName] = currentMtime
|
|
}
|
|
recordNaming()
|
|
result.Skipped = append(result.Skipped, activeName)
|
|
continue
|
|
}
|
|
|
|
if isManaged || force {
|
|
// Managed or forced — overwrite
|
|
if dryRun {
|
|
if !quietDryRun {
|
|
fmt.Fprintf(DiagOutput, "[dry-run] Would update copy: %s\n", activeName)
|
|
}
|
|
} else {
|
|
if err := os.RemoveAll(targetSkillPath); err != nil {
|
|
return nil, fmt.Errorf("failed to remove old copy %s: %w", activeName, err)
|
|
}
|
|
}
|
|
if !dryRun {
|
|
if err := copySkillToTarget(skill.SourcePath, targetSkillPath, rewriteName, ignorePatterns); err != nil {
|
|
return nil, fmt.Errorf("failed to copy skill %s: %w", activeName, err)
|
|
}
|
|
manifest.Managed[activeName] = srcChecksum
|
|
recordNaming()
|
|
if mtimeErr == nil {
|
|
manifest.Mtimes[activeName] = currentMtime
|
|
}
|
|
}
|
|
result.Updated = append(result.Updated, activeName)
|
|
continue
|
|
}
|
|
|
|
// Not managed (local skill) — preserve
|
|
result.Skipped = append(result.Skipped, activeName)
|
|
result.KeptLocal = append(result.KeptLocal, activeName)
|
|
continue
|
|
}
|
|
}
|
|
|
|
// Copy skill to target
|
|
if dryRun {
|
|
if !quietDryRun {
|
|
dest := targetSkillPath
|
|
// A dry run leaves an entry it would rename at its old name.
|
|
if !onDesiredName {
|
|
newPath := filepath.Join(sc.Path, resolved.TargetName)
|
|
if _, err := os.Lstat(newPath); os.IsNotExist(err) {
|
|
dest = newPath
|
|
}
|
|
}
|
|
fmt.Fprintf(DiagOutput, "[dry-run] Would copy: %s -> %s\n", skill.SourcePath, dest)
|
|
}
|
|
} else {
|
|
if err := copySkillToTarget(skill.SourcePath, targetSkillPath, rewriteName, ignorePatterns); err != nil {
|
|
return nil, fmt.Errorf("failed to copy skill %s: %w", activeName, err)
|
|
}
|
|
manifest.Managed[activeName] = srcChecksum
|
|
recordNaming()
|
|
if mtimeErr == nil {
|
|
manifest.Mtimes[activeName] = currentMtime
|
|
}
|
|
}
|
|
result.Copied = append(result.Copied, activeName)
|
|
}
|
|
|
|
// Write updated manifest
|
|
if !dryRun {
|
|
if err := WriteManifest(sc.Path, manifest); err != nil {
|
|
return nil, fmt.Errorf("failed to write manifest: %w", err)
|
|
}
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
// copySkillToTarget copies a skill into dst and, when newName is set, rewrites
|
|
// name: in the copied SKILL.md to it. The source is never touched.
|
|
func copySkillToTarget(src, dst, newName string, ignorePatterns []string) error {
|
|
if err := copyDirectoryWithIgnore(src, dst, ignorePatterns); err != nil {
|
|
return err
|
|
}
|
|
if newName == "" {
|
|
return nil
|
|
}
|
|
skillFile := filepath.Join(dst, "SKILL.md")
|
|
// The copy keeps the source's mode; lift a read-only one for the rewrite only.
|
|
if info, err := os.Stat(skillFile); err == nil && info.Mode().Perm()&0o200 == 0 {
|
|
os.Chmod(skillFile, info.Mode().Perm()|0o200)
|
|
defer os.Chmod(skillFile, info.Mode().Perm())
|
|
}
|
|
if err := utils.SetFrontmatterValue(skillFile, "name", newName); err != nil {
|
|
// dst was just created by this copy; left behind without a manifest
|
|
// entry, it would pass for a user's folder and never be refreshed.
|
|
os.RemoveAll(dst)
|
|
return fmt.Errorf("failed to set prefixed name: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// PruneOrphanCopies removes managed copies that no longer exist in source.
|
|
func PruneOrphanCopies(targetPath, sourcePath string, include, exclude []string, targetName, targetNaming string, dryRun bool) (*PruneResult, error) {
|
|
allSourceSkills, err := DiscoverSourceSkills(sourcePath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to discover skills for pruning: %w", err)
|
|
}
|
|
return PruneOrphanCopiesWithSkills(targetPath, allSourceSkills, include, exclude, targetName, targetNaming, dryRun)
|
|
}
|
|
|
|
// PruneOrphanCopiesWithSkills is like PruneOrphanCopies but accepts pre-discovered skills.
|
|
func PruneOrphanCopiesWithSkills(targetPath string, allSourceSkills []DiscoveredSkill, include, exclude []string, targetName, targetNaming string, dryRun bool) (*PruneResult, error) {
|
|
result := &PruneResult{}
|
|
|
|
manifest, err := ReadManifest(targetPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to read manifest: %w", err)
|
|
}
|
|
|
|
resolution, err := ResolveTargetSkillsForTarget(targetName, config.ResourceTargetConfig{
|
|
Path: targetPath,
|
|
TargetNaming: targetNaming,
|
|
Include: include,
|
|
Exclude: exclude,
|
|
}, allSourceSkills)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
validTargetNames := resolution.ValidTargetNames()
|
|
legacyNames := resolution.LegacyNames("copy", targetPath, manifest)
|
|
|
|
// Remove manifest entries that are no longer in source
|
|
for entryName := range manifest.Managed {
|
|
if validTargetNames[entryName] {
|
|
continue
|
|
}
|
|
if _, keepLegacy := legacyNames[entryName]; keepLegacy {
|
|
continue
|
|
}
|
|
|
|
entryPath := filepath.Join(targetPath, entryName)
|
|
if dryRun {
|
|
fmt.Fprintf(DiagOutput, "[dry-run] Would remove orphan copy: %s\n", entryPath)
|
|
} else {
|
|
if err := os.RemoveAll(entryPath); err != nil {
|
|
result.Warnings = append(result.Warnings,
|
|
fmt.Sprintf("%s: failed to remove: %v", entryName, err))
|
|
continue
|
|
}
|
|
manifest.Remove(entryName)
|
|
}
|
|
result.Removed = append(result.Removed, entryName)
|
|
}
|
|
|
|
// Write updated manifest (only if we actually removed something)
|
|
if !dryRun && len(result.Removed) > 0 {
|
|
if err := WriteManifest(targetPath, manifest); err != nil {
|
|
return result, fmt.Errorf("failed to write manifest: %w", err)
|
|
}
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
// CheckStatusCopy checks the status of a target in copy mode.
|
|
// Returns: status, managed count, local count.
|
|
func CheckStatusCopy(targetPath string) (TargetStatus, int, int) {
|
|
info, err := os.Lstat(targetPath)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return StatusNotExist, 0, 0
|
|
}
|
|
return StatusUnknown, 0, 0
|
|
}
|
|
|
|
// If it's a symlink, it's using symlink mode, not copy
|
|
if utils.IsSymlinkOrJunction(targetPath) {
|
|
return StatusLinked, 0, 0
|
|
}
|
|
|
|
if !info.IsDir() {
|
|
return StatusUnknown, 0, 0
|
|
}
|
|
|
|
manifest, err := ReadManifest(targetPath)
|
|
if err != nil {
|
|
return StatusUnknown, 0, 0
|
|
}
|
|
|
|
// Count managed entries that actually exist on disk
|
|
managedCount := 0
|
|
for name := range manifest.Managed {
|
|
if info, err := os.Stat(filepath.Join(targetPath, name)); err == nil && info.IsDir() {
|
|
managedCount++
|
|
}
|
|
}
|
|
|
|
// Count local (non-managed) entries
|
|
localCount := 0
|
|
entries, _ := os.ReadDir(targetPath)
|
|
for _, entry := range entries {
|
|
if utils.IsHidden(entry.Name()) {
|
|
continue
|
|
}
|
|
if !entry.IsDir() {
|
|
continue
|
|
}
|
|
if _, isManaged := manifest.Managed[entry.Name()]; !isManaged {
|
|
localCount++
|
|
}
|
|
}
|
|
|
|
if managedCount > 0 || len(manifest.Managed) > 0 {
|
|
return StatusCopied, managedCount, localCount
|
|
}
|
|
|
|
return StatusHasFiles, 0, localCount
|
|
}
|
|
|
|
// DirMaxMtime returns the latest ModTime (UnixNano) among all files in dir.
|
|
// Skips .git directories. Only uses os.Stat (via filepath.Walk), never reads file content.
|
|
func DirMaxMtime(dir string) (int64, error) {
|
|
return DirMaxMtimeWithIgnore(dir, nil)
|
|
}
|
|
|
|
// DirMaxMtimeWithIgnore returns the latest ModTime among non-ignored files in dir.
|
|
func DirMaxMtimeWithIgnore(dir string, ignorePatterns []string) (int64, error) {
|
|
var maxMtime int64
|
|
hasSymlink := false
|
|
ignore := compileFileIgnore(ignorePatterns)
|
|
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
relPath, relErr := filepath.Rel(dir, path)
|
|
if relErr != nil {
|
|
return relErr
|
|
}
|
|
relPath = strings.ReplaceAll(relPath, "\\", "/")
|
|
if info.IsDir() {
|
|
if info.Name() == ".git" {
|
|
return filepath.SkipDir
|
|
}
|
|
if relPath != "." && isFileIgnored(ignore, relPath, true) {
|
|
return filepath.SkipDir
|
|
}
|
|
return nil
|
|
}
|
|
if isFileIgnored(ignore, relPath, false) {
|
|
return nil
|
|
}
|
|
// Symlink targets can change without updating the link mtime.
|
|
// Disable mtime fast-path for symlink-containing skills.
|
|
if utils.IsLinkMode(path, info.Mode()) {
|
|
hasSymlink = true
|
|
return nil
|
|
}
|
|
if mt := info.ModTime().UnixNano(); mt > maxMtime {
|
|
maxMtime = mt
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if hasSymlink {
|
|
return 0, fmt.Errorf("mtime fast-path not supported for directories containing symlinks")
|
|
}
|
|
return maxMtime, nil
|
|
}
|
|
|
|
// DirChecksum computes a deterministic SHA256 checksum of a directory.
|
|
// It hashes sorted relative paths and file contents.
|
|
func DirChecksum(dir string) (string, error) {
|
|
return DirChecksumWithIgnore(dir, nil)
|
|
}
|
|
|
|
// DirChecksumWithIgnore computes a deterministic checksum of non-ignored files in dir.
|
|
func DirChecksumWithIgnore(dir string, ignorePatterns []string) (string, error) {
|
|
var entries []checksumEntry
|
|
err := collectChecksumEntries(dir, "", &entries, map[string]bool{}, compileFileIgnore(ignorePatterns))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// Sort for deterministic output
|
|
sort.Slice(entries, func(i, j int) bool {
|
|
return entries[i].relPath < entries[j].relPath
|
|
})
|
|
|
|
h := sha256.New()
|
|
for _, e := range entries {
|
|
io.WriteString(h, e.relPath)
|
|
h.Write([]byte{0}) // separator
|
|
h.Write(e.content)
|
|
h.Write([]byte{0}) // separator
|
|
}
|
|
|
|
return fmt.Sprintf("%x", h.Sum(nil)), nil
|
|
}
|
|
|
|
type checksumEntry struct {
|
|
relPath string
|
|
content []byte
|
|
}
|
|
|
|
// collectChecksumEntries recursively collects file entries for checksumming.
|
|
// Directory symlinks are dereferenced to hash effective copied content.
|
|
func collectChecksumEntries(root, relPrefix string, entries *[]checksumEntry, active map[string]bool, ignore *skillignore.Matcher) error {
|
|
resolvedRoot, err := resolveLinkRoot(root)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to resolve checksum root %s: %w", root, err)
|
|
}
|
|
if active[resolvedRoot] {
|
|
return fmt.Errorf("detected symlink directory cycle while checksumming: %s", root)
|
|
}
|
|
active[resolvedRoot] = true
|
|
defer delete(active, resolvedRoot)
|
|
|
|
// The root link has already been resolved and entered in the cycle set.
|
|
root = resolvedRoot
|
|
return filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
relPath, err := filepath.Rel(root, path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// Normalize path separators for cross-platform consistency
|
|
relPath = strings.ReplaceAll(relPath, "\\", "/")
|
|
if relPath == "." {
|
|
relPath = ""
|
|
}
|
|
|
|
fullRelPath := relPath
|
|
if relPrefix != "" {
|
|
if fullRelPath == "" {
|
|
fullRelPath = relPrefix
|
|
} else {
|
|
fullRelPath = relPrefix + "/" + fullRelPath
|
|
}
|
|
}
|
|
if info.IsDir() {
|
|
if info.Name() == ".git" {
|
|
return filepath.SkipDir
|
|
}
|
|
if fullRelPath != "" && isFileIgnored(ignore, fullRelPath, true) {
|
|
return filepath.SkipDir
|
|
}
|
|
return nil
|
|
}
|
|
if isFileIgnored(ignore, fullRelPath, false) {
|
|
return nil
|
|
}
|
|
|
|
// A symlink can point to a directory. In copy mode we dereference
|
|
// directory symlinks, so checksum should include effective contents.
|
|
if utils.IsLinkMode(path, info.Mode()) {
|
|
targetInfo, statErr := os.Stat(path)
|
|
if statErr != nil {
|
|
return fmt.Errorf("failed to stat symlink target %s: %w", path, statErr)
|
|
}
|
|
if targetInfo.IsDir() {
|
|
resolvedDir, resolveErr := filepath.EvalSymlinks(path)
|
|
if resolveErr != nil {
|
|
return fmt.Errorf("failed to resolve symlink directory %s: %w", path, resolveErr)
|
|
}
|
|
return collectChecksumEntries(resolvedDir, fullRelPath, entries, active, ignore)
|
|
}
|
|
}
|
|
|
|
content, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
*entries = append(*entries, checksumEntry{relPath: fullRelPath, content: content})
|
|
return nil
|
|
})
|
|
}
|