mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 20:34:19 +00:00
* fix(sync): replace copy-mode copies with links when switching back to merge Switching a target from copy back to merge left every copy whose entry name did not change as a real folder. Merge sync treated it as the user's own folder, so status and doctor kept saying "run skillshare sync" while sync changed nothing, and only `sync --force` turned it into a link. Copy mode already replaces merge-mode links with copies on its own, so merge sync now replaces a folder the manifest records with a checksum (a copy-mode copy) with a link, without --force. Folders the manifest does not record, or records as a link, stay untouched. The pre-sync target backup is unchanged. The CLI and dashboard diffs name these entries as copies sync replaces instead of suggesting --force, and the sync-modes docs describe the switch. * fix(sync): keep copy-mode copies the user edited when switching to merge The first version replaced any folder the manifest recorded as a copy-mode copy. The manifest checksum is only a record, so a copy edited since copy mode made it was deleted too, and `sync --json` and project-mode dashboard sync take no pre-sync backup to restore it from. Replace the folder only while it still hashes to the recorded checksum (Manifest.OwnsCopy). An edited copy is kept and `sync --force` still replaces it; diff keeps suggesting --force for it. A copy whose name: was rewritten by prefixed naming, or that was made with custom ignore patterns, no longer hashes to the source checksum and is kept the same way. * fix(ui): say a copy-mode copy in a merge target is replaced by a link The Sync page picks a row's text from the diff item's action alone. The copy-mode copy that merge sync now replaces comes from the server as an update, which the page worded as "Link is broken or points elsewhere. Will be relinked" even though no link exists yet. Match the server's reason and show "Copy made by copy mode. Will be replaced by a link" instead, in all eleven dashboard languages. * docs(sync): say only unedited copy-mode copies are replaced on merge sync The sync-modes paragraph promised that copies copy mode made are replaced with links without --force. Merge sync keeps a copy that no longer hashes to the checksum copy mode recorded, so an edited copy, or one whose name: copy mode rewrote for prefixed naming, still needs --force. State the three cases in sync-modes and qualify the diff Modified and Local override labels, in English and the ja, ko, zh-Hans and zh-Hant copies. * fix(sync): keep the copy until its replacement link exists Replacing a copy-mode copy removed the folder first and then created the link. If creating the link failed (no symlink or junction rights on a Windows or network filesystem), sync returned an error with the skill gone from the target. This path now also runs on a plain sync, not only on --force. Park the folder beside the entry, create the link, then delete the parked folder; on failure the folder is moved back. A parked folder left by a run that died mid-way may be the only copy, so the next sync stops with a message instead of deleting it. The manifest also forgets the copy-mode mtime and naming records of an entry that is now a link. * test(sourcefs): register replaceWithLink's target writes in the raw-write ratchet The previous commit moved the merge-mode folder replacement into replaceWithLink, which the ratchet reports as new raw os calls and the old os.RemoveAll allowance as stale. All of them act on the sync target, not the skills source, like the neighbouring entries. * fix(sync): bound the parked copy's name and document the naming exception The folder parked while a link is created was named after the entry, so an entry near the 255-byte filename limit (a long flattened nested skill) could not be moved aside and its replacement failed. The parked name is now a fixed-length hash of the entry name. sync-modes also says what happens when `target_naming` changes in the same sync: the copy gets a new entry name and the prune pass removes it as an orphan whether or not it was edited, as it did before this change.
115 lines
3.7 KiB
Go
115 lines
3.7 KiB
Go
package sync
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"skillshare/internal/utils"
|
|
)
|
|
|
|
// ManifestFile is the filename for the sync manifest.
|
|
const ManifestFile = ".skillshare-manifest.json"
|
|
|
|
// manifestSymlink is the Managed value merge mode records for a link it made.
|
|
const manifestSymlink = "symlink"
|
|
|
|
// Manifest tracks which skills are managed by skillshare in a target directory.
|
|
// Used by both merge mode (values: "symlink") and copy mode (values: SHA-256 checksum).
|
|
type Manifest struct {
|
|
Managed map[string]string `json:"managed"` // flatName → "symlink" (merge) or SHA-256 checksum (copy)
|
|
Mtimes map[string]int64 `json:"mtimes,omitempty"` // flatName → source dir max mtime (UnixNano), copy mode only
|
|
Sources map[string]string `json:"sources,omitempty"` // output name → SHA-256 of the source it was converted from, extension outputs only
|
|
Naming map[string]string `json:"naming,omitempty"` // entry name → target_naming that produced it, copy mode only
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
// ReadManifest reads the manifest from a target directory.
|
|
// Returns an empty manifest if the file does not exist.
|
|
func ReadManifest(targetPath string) (*Manifest, error) {
|
|
p := filepath.Join(targetPath, ManifestFile)
|
|
data, err := os.ReadFile(p)
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return &Manifest{Managed: make(map[string]string), Mtimes: make(map[string]int64), Naming: make(map[string]string)}, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
var m Manifest
|
|
if err := json.Unmarshal(data, &m); err != nil {
|
|
// Corrupt manifest — treat as empty so next sync rebuilds it.
|
|
return &Manifest{Managed: make(map[string]string), Mtimes: make(map[string]int64), Naming: make(map[string]string)}, nil
|
|
}
|
|
if m.Managed == nil {
|
|
m.Managed = make(map[string]string)
|
|
}
|
|
if m.Mtimes == nil {
|
|
m.Mtimes = make(map[string]int64)
|
|
}
|
|
if m.Naming == nil {
|
|
m.Naming = make(map[string]string)
|
|
}
|
|
return &m, nil
|
|
}
|
|
|
|
// Remove forgets every record of the entry name.
|
|
func (m *Manifest) Remove(name string) {
|
|
delete(m.Managed, name)
|
|
delete(m.Mtimes, name)
|
|
delete(m.Naming, name)
|
|
}
|
|
|
|
// CopyToLinkReason is the diff reason for a copy-mode copy that merge sync replaces with a link.
|
|
const CopyToLinkReason = "copy mode copy (sync replaces with link)"
|
|
|
|
// OwnsCopy reports whether dir is still the copy that copy mode made for name:
|
|
// the manifest records a checksum (not a merge-mode link) and the folder still
|
|
// hashes to it, so nothing the user edited is lost when it is replaced.
|
|
func (m *Manifest) OwnsCopy(name, dir string) bool {
|
|
if m == nil {
|
|
return false
|
|
}
|
|
recorded, ok := m.Managed[name]
|
|
if !ok || recorded == manifestSymlink {
|
|
return false
|
|
}
|
|
sum, err := DirChecksumWithIgnore(dir, DefaultFileIgnorePatterns())
|
|
return err == nil && sum == recorded
|
|
}
|
|
|
|
// SkipsHidden reports whether a target scan should skip name. Hidden entries
|
|
// are skipped unless the manifest records them as managed (e.g. a source skill
|
|
// under ".system/" synced as ".system__example").
|
|
func (m *Manifest) SkipsHidden(name string) bool {
|
|
if !utils.IsHidden(name) {
|
|
return false
|
|
}
|
|
if m == nil {
|
|
return true
|
|
}
|
|
_, managed := m.Managed[name]
|
|
return !managed
|
|
}
|
|
|
|
// WriteManifest writes the manifest to a target directory.
|
|
func WriteManifest(targetPath string, m *Manifest) error {
|
|
m.UpdatedAt = time.Now()
|
|
data, err := json.MarshalIndent(m, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.WriteFile(filepath.Join(targetPath, ManifestFile), data, 0644)
|
|
}
|
|
|
|
// RemoveManifest deletes the manifest file from a target directory.
|
|
// It is a no-op if the file does not exist.
|
|
func RemoveManifest(targetPath string) error {
|
|
err := os.Remove(filepath.Join(targetPath, ManifestFile))
|
|
if err != nil && !os.IsNotExist(err) {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|