Files
skillshare/internal/sync/extras.go
T
Willie e1c4761333 fix(extras): apply output_ext when computing diff target filenames
The /api/extras/diff path resolved the copy mode but ignored a transform
extension's output_ext, so buildExtrasDiffItems compared source relative
paths against the target directory. After a .md -> .toml transform sync,
the diff looked for the original .md name and reported false drift, even
though the status path already handled this.

Export ApplyOutputExt as the single source of truth for source->target
filename mapping (previously unexported applyOutputExt) and use it in the
diff path so sync, status, and diff all agree on the transformed name.
2026-05-30 19:23:12 +08:00

727 lines
21 KiB
Go

package sync
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"strings"
)
// ExtraResult holds the result of an extras sync operation.
type ExtraResult struct {
Synced int // Files synced (new + already correct)
Skipped int // Files skipped (local conflict, no --force)
Pruned int // Orphan files removed
Errors []string // Non-fatal error messages
Warnings []string // Non-fatal warnings (e.g. flatten collisions)
}
// reservedMetadataFile is skillshare's per-directory install-tracking store
// (mirrors install.MetadataFileName, duplicated here to avoid an import cycle).
// It is internal bookkeeping, not user content, so it must never be synced or
// transformed into a target directory.
const reservedMetadataFile = ".metadata.json"
// DiscoverExtraFiles recursively walks sourcePath and returns relative paths
// of all regular files. Directories named ".git" and skillshare's reserved
// .metadata.json bookkeeping file are skipped. Results are sorted for
// deterministic output.
func DiscoverExtraFiles(sourcePath string) ([]string, error) {
info, err := os.Stat(sourcePath)
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("extras source directory does not exist: %s", sourcePath)
}
return nil, fmt.Errorf("failed to stat extras source: %w", err)
}
if !info.IsDir() {
return nil, fmt.Errorf("extras source is not a directory: %s", sourcePath)
}
var files []string
err = filepath.Walk(sourcePath, func(path string, fi os.FileInfo, walkErr error) error {
if walkErr != nil {
return nil // skip inaccessible paths
}
if fi.IsDir() {
if fi.Name() == ".git" {
return filepath.SkipDir
}
return nil
}
if fi.Name() == reservedMetadataFile {
return nil // skillshare's internal tracking store, never sync it
}
rel, relErr := filepath.Rel(sourcePath, path)
if relErr != nil {
return nil
}
files = append(files, rel)
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to walk extras source: %w", err)
}
sort.Strings(files)
return files, nil
}
// SyncExtra synchronises extra files from sourcePath into targetPath.
//
// Supported modes:
// - "merge" (default): per-file symlink from target to source
// - "copy": per-file copy
// - "symlink": entire directory symlink
//
// When spec is non-nil, the sync is routed through syncExtraTransform
// (transform/extension mode). When spec is nil, behavior is unchanged.
// When dryRun is true the function counts what would happen but makes no
// filesystem changes.
func SyncExtra(sourcePath, targetPath, mode string, dryRun, force, flatten bool, projectRoot string, spec *ExtensionSpec) (*ExtraResult, error) {
if spec != nil {
// Transform extensions emit generated files into the target, so only
// copy semantics apply. Reject merge/symlink here so the API and the
// CLI (validateExtensionMode) enforce the same contract.
if mode != "" && mode != "copy" {
return nil, fmt.Errorf("extension %q requires copy mode, got %q", spec.Name, mode)
}
return syncExtraTransform(sourcePath, targetPath, spec, dryRun, force, flatten)
}
if mode == "" {
mode = "merge"
}
if flatten && mode == "symlink" {
return nil, fmt.Errorf("flatten cannot be used with symlink mode")
}
switch mode {
case "symlink":
return syncExtraSymlinkMode(sourcePath, targetPath, dryRun, force, projectRoot)
case "merge", "copy":
return syncExtraPerFile(sourcePath, targetPath, mode, dryRun, force, flatten, projectRoot)
default:
return nil, fmt.Errorf("unsupported extras sync mode: %q", mode)
}
}
// syncExtraTransform applies an extension to every source file and writes the
// transformed output into targetPath using copy semantics. Output files are
// renamed per spec.OutputExt. Orphan generated files are pruned.
func syncExtraTransform(sourcePath, targetPath string, spec *ExtensionSpec, dryRun, force, flatten bool) (*ExtraResult, error) {
result := &ExtraResult{}
files, err := DiscoverExtraFiles(sourcePath)
if err != nil {
return nil, err
}
absSrc, err := filepath.Abs(sourcePath)
if err != nil {
return nil, fmt.Errorf("failed to resolve source path: %w", err)
}
seen := make(map[string]string) // flatten: basename → original rel
sourceSet := make(map[string]bool)
for _, rel := range files {
srcFile := filepath.Join(absSrc, rel)
tgtRel := rel
if flatten {
base := filepath.Base(rel)
if prev, ok := seen[base]; ok {
result.Skipped++
result.Warnings = append(result.Warnings,
fmt.Sprintf("flatten conflict: %s skipped (%s already synced from %s)", rel, base, prev))
continue
}
seen[base] = rel
tgtRel = base
}
tgtRel = ApplyOutputExt(tgtRel, spec.OutputExt)
sourceSet[tgtRel] = true
tgtFile := filepath.Join(targetPath, tgtRel)
// Dry-run reports the outcome without spawning the extension and without
// touching the filesystem. The transformed output can only be produced by
// a subprocess, and a preview must never execute arbitrary extension code,
// so content can't be compared here. An existing real file or directory is
// therefore conservatively reported as a conflict requiring --force —
// matching the non-dry-run skip path below — while a missing target, a
// leftover symlink, or --force would be (re)written and counts as synced.
if dryRun {
if info, lstatErr := os.Lstat(tgtFile); lstatErr == nil &&
info.Mode()&os.ModeSymlink == 0 && !force {
result.Skipped++
} else {
result.Synced++
}
continue
}
env := map[string]string{
"SS_SRC_PATH": srcFile,
"SS_REL_PATH": rel,
"SS_TARGET_DIR": targetPath,
"SS_MODE": "sync",
}
out, runErr := runExtension(spec, srcFile, env)
if runErr != nil {
result.Errors = append(result.Errors, fmt.Sprintf("%s: %v", rel, runErr))
continue
}
// Conflict/force contract, mirroring syncOneExtraFile copy semantics:
// only leftover symlinks auto-replace; real files and directories are
// never destroyed without --force.
if info, lstatErr := os.Lstat(tgtFile); lstatErr == nil {
switch {
case info.Mode()&os.ModeSymlink != 0:
// Leftover symlink from a different mode: safe to replace.
// os.Remove drops the link itself without following it.
if rmErr := os.Remove(tgtFile); rmErr != nil {
result.Errors = append(result.Errors, fmt.Sprintf("%s: failed to remove conflicting symlink: %v", rel, rmErr))
continue
}
case info.IsDir():
// A real directory is never silently destroyed; require --force,
// then replace it wholesale with the generated file.
if !force {
result.Skipped++
continue
}
if rmErr := os.RemoveAll(tgtFile); rmErr != nil {
result.Errors = append(result.Errors, fmt.Sprintf("%s: failed to remove conflicting directory: %v", rel, rmErr))
continue
}
default:
// Regular file: idempotent if identical, otherwise needs --force.
if existing, readErr := os.ReadFile(tgtFile); readErr == nil && bytes.Equal(existing, out) {
result.Synced++
continue
}
if !force {
result.Skipped++
continue
}
// With --force, WriteFile overwrites the file in place below.
}
}
if mkErr := os.MkdirAll(filepath.Dir(tgtFile), 0755); mkErr != nil {
result.Errors = append(result.Errors, fmt.Sprintf("%s: create target dir: %v", rel, mkErr))
continue
}
if wErr := os.WriteFile(tgtFile, out, 0644); wErr != nil {
result.Errors = append(result.Errors, fmt.Sprintf("%s: write target: %v", rel, wErr))
continue
}
result.Synced++
}
if !dryRun {
pruned, pruneErrors := pruneExtraOrphans(targetPath, sourceSet, "copy")
result.Pruned = pruned
result.Errors = append(result.Errors, pruneErrors...)
}
return result, nil
}
// syncExtraSymlinkMode symlinks the entire source directory to the target path.
func syncExtraSymlinkMode(sourcePath, targetPath string, dryRun, force bool, projectRoot string) (*ExtraResult, error) {
result := &ExtraResult{}
absSrc, err := filepath.Abs(sourcePath)
if err != nil {
return nil, fmt.Errorf("failed to resolve source path: %w", err)
}
// Check existing target
info, lstatErr := os.Lstat(targetPath)
if lstatErr == nil {
// Something exists at targetPath
if info.Mode()&os.ModeSymlink != 0 {
// Already a symlink — check if correct
dest, readErr := os.Readlink(targetPath)
if readErr == nil {
absDest := resolveReadlink(dest, targetPath)
if absDest == absSrc {
relative := shouldUseRelative(projectRoot, absSrc, targetPath)
if !linkNeedsReformat(dest, relative) {
result.Synced = 1
return result, nil
}
// Correct target but wrong format (abs↔rel) — recreate
if dryRun {
result.Synced = 1
return result, nil
}
if err := reformatLink(targetPath, absSrc, relative); err != nil {
return nil, fmt.Errorf("failed to reformat directory symlink: %w", err)
}
result.Synced = 1
return result, nil
}
}
// Wrong symlink
if !force {
result.Skipped = 1
return result, nil
}
if !dryRun {
if err := os.Remove(targetPath); err != nil {
return nil, fmt.Errorf("failed to remove wrong symlink: %w", err)
}
}
} else {
// Real file/dir
if !force {
result.Skipped = 1
return result, nil
}
if !dryRun {
os.RemoveAll(targetPath)
}
}
}
if dryRun {
result.Synced = 1
return result, nil
}
if err := os.MkdirAll(filepath.Dir(targetPath), 0755); err != nil {
return nil, fmt.Errorf("failed to create parent directory: %w", err)
}
relative := shouldUseRelative(projectRoot, absSrc, targetPath)
if err := createLink(targetPath, absSrc, relative); err != nil {
return nil, fmt.Errorf("failed to create directory symlink: %w", err)
}
result.Synced = 1
return result, nil
}
// syncExtraPerFile handles merge (symlink) and copy modes on a per-file basis.
func syncExtraPerFile(sourcePath, targetPath, mode string, dryRun, force, flatten bool, projectRoot string) (*ExtraResult, error) {
result := &ExtraResult{}
files, err := DiscoverExtraFiles(sourcePath)
if err != nil {
return nil, err
}
absSrc, err := filepath.Abs(sourcePath)
if err != nil {
return nil, fmt.Errorf("failed to resolve source path: %w", err)
}
seen := make(map[string]string) // basename → original rel path (flatten only)
// Compute once for entire batch — all files share the same source/target root.
relative := shouldUseRelative(projectRoot, sourcePath, targetPath)
for _, rel := range files {
srcFile := filepath.Join(absSrc, rel)
tgtRel := rel
if flatten {
base := filepath.Base(rel)
if prev, exists := seen[base]; exists {
result.Skipped++
result.Warnings = append(result.Warnings,
fmt.Sprintf("flatten conflict: %s skipped (%s already synced from %s)", rel, base, prev))
continue
}
seen[base] = rel
tgtRel = base
}
tgtFile := filepath.Join(targetPath, tgtRel)
synced, skipped, syncErr := syncOneExtraFile(srcFile, tgtFile, mode, dryRun, force, relative)
if syncErr != nil {
result.Errors = append(result.Errors, fmt.Sprintf("%s: %v", rel, syncErr))
continue
}
result.Synced += synced
result.Skipped += skipped
}
// Prune orphans (only when not dry-run)
if !dryRun {
sourceSet := make(map[string]bool, len(files))
if flatten {
for base := range seen {
sourceSet[base] = true
}
} else {
for _, f := range files {
sourceSet[f] = true
}
}
pruned, pruneErrors := pruneExtraOrphans(targetPath, sourceSet, mode)
result.Pruned = pruned
result.Errors = append(result.Errors, pruneErrors...)
}
return result, nil
}
// syncOneExtraFile syncs a single file. Returns (synced, skipped, error).
func syncOneExtraFile(srcFile, tgtFile, mode string, dryRun, force, relative bool) (int, int, error) {
// Ensure parent directory exists
if !dryRun {
if err := os.MkdirAll(filepath.Dir(tgtFile), 0755); err != nil {
return 0, 0, fmt.Errorf("failed to create parent dir: %w", err)
}
}
// Check if target already exists
info, lstatErr := os.Lstat(tgtFile)
if lstatErr == nil {
isSymlink := info.Mode()&os.ModeSymlink != 0
// Already correct? → skip (idempotent)
if mode == "merge" && isSymlink {
dest, readErr := os.Readlink(tgtFile)
if readErr == nil {
absDest := resolveReadlink(dest, tgtFile)
if absDest == srcFile {
if !linkNeedsReformat(dest, relative) {
return 1, 0, nil
}
// Correct target but wrong format — recreate
if !dryRun {
if err := reformatLink(tgtFile, srcFile, relative); err != nil {
return 0, 0, fmt.Errorf("failed to reformat symlink: %w", err)
}
}
return 1, 0, nil
}
}
}
if mode == "copy" && !isSymlink && !info.IsDir() {
srcInfo, srcErr := os.Stat(srcFile)
if srcErr == nil && srcInfo.Size() == info.Size() && contentEqual(srcFile, tgtFile) {
return 1, 0, nil
}
}
// Symlinks left over from a different mode are safe to replace
autoReplace := isSymlink
if !autoReplace && !force {
return 0, 1, nil
}
if !dryRun {
if err := os.Remove(tgtFile); err != nil {
return 0, 0, fmt.Errorf("failed to remove conflicting file: %w", err)
}
}
}
if dryRun {
return 1, 0, nil
}
switch mode {
case "merge":
if err := createLink(tgtFile, srcFile, relative); err != nil {
return 0, 0, fmt.Errorf("failed to create symlink: %w", err)
}
case "copy":
if err := copyFile(srcFile, tgtFile); err != nil {
return 0, 0, fmt.Errorf("failed to copy file: %w", err)
}
}
return 1, 0, nil
}
// pruneExtraOrphans walks the target directory and removes files that have no
// corresponding source. In merge mode only symlinks are pruned; user-created
// local files are preserved. Empty parent directories are cleaned up.
// Hidden files (names starting with ".") are skipped.
func pruneExtraOrphans(targetPath string, sourceFiles map[string]bool, mode string) (pruned int, errors []string) {
// Collect paths to prune (walk first, delete after to avoid mutation during walk)
var toRemove []string
_ = filepath.Walk(targetPath, func(path string, info os.FileInfo, walkErr error) error {
if walkErr != nil {
return nil
}
if info.IsDir() {
if info.Name() == ".git" {
return filepath.SkipDir
}
// A directory occupying a claimed output path is a skipped conflict
// (the source wanted a file there but we refused to clobber it).
// Leave its contents entirely untouched — never prune inside it.
if rel, relErr := filepath.Rel(targetPath, path); relErr == nil && sourceFiles[rel] {
return filepath.SkipDir
}
return nil
}
// Skip hidden files
if strings.HasPrefix(info.Name(), ".") {
return nil
}
rel, relErr := filepath.Rel(targetPath, path)
if relErr != nil {
return nil
}
// Check if corresponding source file exists
if sourceFiles[rel] {
return nil // source exists, keep it
}
// Source doesn't exist — candidate for pruning
if mode == "merge" {
// In merge mode, only prune symlinks (don't delete user's local files)
if info.Mode()&os.ModeSymlink == 0 {
return nil
}
}
toRemove = append(toRemove, path)
return nil
})
for _, path := range toRemove {
if err := os.Remove(path); err != nil {
errors = append(errors, fmt.Sprintf("prune %s: %v", path, err))
continue
}
pruned++
// Clean empty parent directories up to targetPath
cleanEmptyParents(filepath.Dir(path), targetPath)
}
return pruned, errors
}
// contentEqual returns true if two files have identical content.
func contentEqual(a, b string) bool {
fa, err := os.Open(a)
if err != nil {
return false
}
defer fa.Close()
fb, err := os.Open(b)
if err != nil {
return false
}
defer fb.Close()
bufA := make([]byte, 4096)
bufB := make([]byte, 4096)
for {
nA, errA := fa.Read(bufA)
nB, errB := fb.Read(bufB)
if !bytes.Equal(bufA[:nA], bufB[:nB]) {
return false
}
if errA == io.EOF && errB == io.EOF {
return true
}
if errA != nil || errB != nil {
return false
}
}
}
// EffectiveMode returns the mode to use for sync, defaulting to "merge".
func EffectiveMode(mode string) string {
if mode == "" {
return "merge"
}
return mode
}
// FlattenRel returns the target-relative path for a source file under flatten mode.
// When flatten is true, it uses only the basename and tracks seen basenames to skip
// collisions (first-wins, matching sync behavior). Returns ("", false) for collisions.
func FlattenRel(rel string, flatten bool, seen map[string]bool) (tgtRel string, ok bool) {
if !flatten {
return rel, true
}
base := filepath.Base(rel)
if seen[base] {
return "", false
}
seen[base] = true
return base, true
}
// CheckSyncStatus compares source files against the target directory and
// returns a status string: "synced" or "drift". When outputExt is non-empty a
// transform extension is in effect, so the expected target file carries the
// transformed extension (e.g. foo.md → foo.toml) instead of the source name.
func CheckSyncStatus(sourceFiles []string, sourceDir, targetDir, mode string, flatten bool, outputExt string) string {
seen := make(map[string]bool)
for _, rel := range sourceFiles {
tgtRel, ok := FlattenRel(rel, flatten, seen)
if !ok {
continue
}
tgtRel = ApplyOutputExt(tgtRel, outputExt)
targetFile := filepath.Join(targetDir, tgtRel)
sourceFile := filepath.Join(sourceDir, rel)
tInfo, err := os.Lstat(targetFile)
if err != nil {
return "drift"
}
switch mode {
case "symlink", "merge":
if tInfo.Mode()&os.ModeSymlink != 0 {
link, readErr := os.Readlink(targetFile)
if readErr != nil || link != sourceFile {
return "drift"
}
} else {
return "drift"
}
case "copy":
if !tInfo.Mode().IsRegular() {
return "drift"
}
}
}
return "synced"
}
// cleanEmptyParents removes empty directories from dir up to (but not
// including) stopAt.
func cleanEmptyParents(dir, stopAt string) {
for dir != stopAt && dir != filepath.Dir(dir) {
entries, err := os.ReadDir(dir)
if err != nil || len(entries) > 0 {
break
}
os.Remove(dir)
dir = filepath.Dir(dir)
}
}
// ExtraCollectResult holds results from collecting extra files.
type ExtraCollectResult struct {
Collected int
Skipped int
Errors []string
}
// CollectExtraFiles scans targetDir for non-symlink local files,
// copies them to sourceDir, and replaces originals with symlinks.
// When flatten is true, collected files are placed in the source root
// (basename only) rather than preserving the target subdirectory structure.
func CollectExtraFiles(sourceDir, targetDir string, dryRun, flatten bool, projectRoot string) (*ExtraCollectResult, error) {
result := &ExtraCollectResult{}
if _, err := os.Stat(targetDir); os.IsNotExist(err) {
return nil, fmt.Errorf("target directory does not exist: %s", targetDir)
}
// Ensure source directory exists
if !dryRun {
if err := os.MkdirAll(sourceDir, 0755); err != nil {
return nil, fmt.Errorf("failed to create source directory: %w", err)
}
}
err := filepath.Walk(targetDir, func(path string, info os.FileInfo, walkErr error) error {
if walkErr != nil {
return nil
}
// Skip directories and .git
if info.IsDir() {
if info.Name() == ".git" {
return filepath.SkipDir
}
return nil
}
// filepath.Walk follows symlinks, so check via Lstat to detect them
linfo, err := os.Lstat(path)
if err != nil {
return nil
}
if linfo.Mode()&os.ModeSymlink != 0 {
result.Skipped++
return nil
}
rel, err := filepath.Rel(targetDir, path)
if err != nil {
result.Errors = append(result.Errors, fmt.Sprintf("cannot compute relative path: %s", path))
return nil
}
destRel := rel
if flatten {
destRel = filepath.Base(rel)
}
destPath := filepath.Join(sourceDir, destRel)
// Skip if already exists in source
if _, err := os.Stat(destPath); err == nil {
result.Skipped++
return nil
}
if dryRun {
result.Collected++
return nil
}
// Create parent directory
if err := os.MkdirAll(filepath.Dir(destPath), 0755); err != nil {
result.Errors = append(result.Errors, fmt.Sprintf("mkdir failed: %v", err))
return nil
}
// Read source content
content, err := os.ReadFile(path)
if err != nil {
result.Errors = append(result.Errors, fmt.Sprintf("read failed: %v", err))
return nil
}
// Write to source dir
if err := os.WriteFile(destPath, content, info.Mode().Perm()); err != nil {
result.Errors = append(result.Errors, fmt.Sprintf("write failed: %v", err))
return nil
}
// Remove original and create symlink
if err := os.Remove(path); err != nil {
result.Errors = append(result.Errors, fmt.Sprintf("remove original failed: %v", err))
return nil
}
relative := shouldUseRelative(projectRoot, destPath, path)
if err := createLink(path, destPath, relative); err != nil {
result.Errors = append(result.Errors, fmt.Sprintf("symlink failed: %v", err))
return nil
}
result.Collected++
return nil
})
if err != nil {
return result, fmt.Errorf("walk error: %w", err)
}
return result, nil
}