- Add `vendor rename <source> [upstream-id=name ...] [--reset ids]` to change deployed names after `vendor add`, without moving the pinned rev or selection - Persist renames in the lockfile so they survive `vendor update` and `vendor select` - Rewrite SKILL.md frontmatter `name:` to the folder name so OpenCode loads renamed skills - Validate rename targets: single path segment, no leading dot, and OpenCode skill naming rules for skills - Re-run collision checks before writing; failed renames leave lockfile and external area untouched - Add unit tests for frontmatter rewrite and name validation, plus e2e rename coverage - Document renames in README; sync beads issue export
181 lines
4.6 KiB
Go
181 lines
4.6 KiB
Go
package vendor
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
|
|
"github.com/m3tam3re/agent-lib/internal/discovery"
|
|
"github.com/m3tam3re/agent-lib/internal/gitsource"
|
|
"github.com/m3tam3re/agent-lib/internal/lockfile"
|
|
)
|
|
|
|
// RenameOptions parameterize vendor rename.
|
|
type RenameOptions struct {
|
|
// Set maps upstream ids to new deployed names. Mapping an id onto itself
|
|
// is the same as resetting it.
|
|
Set map[string]string
|
|
// Reset drops existing renames so the items deploy under their upstream id.
|
|
Reset []string
|
|
}
|
|
|
|
// Rename changes the deployed names of a vendored source's items after the
|
|
// fact — without advancing the pinned revision or touching the selection.
|
|
// Ids are validated against the pinned upstream, collisions re-run, and the
|
|
// external area is rewritten atomically. With no options it only lists the
|
|
// current renames.
|
|
func Rename(workDir, name string, opts RenameOptions, stdout io.Writer) error {
|
|
lockPath := filepath.Join(workDir, lockfile.FileName)
|
|
lf, err := lockfile.Load(lockPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
src, ok := lf.Sources[name]
|
|
if !ok {
|
|
return fmt.Errorf("source %q is not in the lockfile", name)
|
|
}
|
|
|
|
if len(opts.Set) == 0 && len(opts.Reset) == 0 {
|
|
printRenames(stdout, name, src.Renames)
|
|
return nil
|
|
}
|
|
|
|
newRenames := map[string]string{}
|
|
for k, v := range src.Renames {
|
|
newRenames[k] = v
|
|
}
|
|
for _, id := range opts.Reset {
|
|
if _, ok := newRenames[id]; !ok {
|
|
return fmt.Errorf("source %q: --reset %q has no rename to reset", name, id)
|
|
}
|
|
delete(newRenames, id)
|
|
}
|
|
for up, dep := range opts.Set {
|
|
if up == dep {
|
|
delete(newRenames, up)
|
|
continue
|
|
}
|
|
newRenames[up] = dep
|
|
}
|
|
|
|
repo, err := gitsource.Clone(src.URL, src.Ref)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
pinned, err := repo.Pin(src.Rev)
|
|
if err != nil {
|
|
return fmt.Errorf("%w — run vendor update to re-pin", err)
|
|
}
|
|
tree, err := pinned.Tree()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
items, err := discovery.Scan(tree, discovery.FromLockfileDiscovery(derefDiscovery(src)))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
ids := append(sortedKeys(boolSet(keysOf(opts.Set))), opts.Reset...)
|
|
if err := checkIDEntries(items, ids, name, "rename"); err != nil {
|
|
return err
|
|
}
|
|
types := map[string]string{}
|
|
for _, it := range items {
|
|
if !it.Skipped {
|
|
types[it.UpstreamID] = it.Type
|
|
}
|
|
}
|
|
for up, dep := range opts.Set {
|
|
if up == dep {
|
|
continue
|
|
}
|
|
if err := validateRenameTarget(types[up], up, dep); err != nil {
|
|
return fmt.Errorf("source %q: %w", name, err)
|
|
}
|
|
}
|
|
|
|
selected := applySelection(items, src.Selection)
|
|
if err := checkCollisions(workDir, lf, name, selected, newRenames); err != nil {
|
|
return err
|
|
}
|
|
|
|
cleanupStaleStaging(workDir)
|
|
staging := filepath.Join(workDir, externalDir, ".staging-"+name)
|
|
if err := os.RemoveAll(staging); err != nil {
|
|
return err
|
|
}
|
|
if err := materializeTree(tree, selected, newRenames, staging); err != nil {
|
|
os.RemoveAll(staging)
|
|
return err
|
|
}
|
|
oldDir := filepath.Join(workDir, externalDir, name)
|
|
if err := os.RemoveAll(oldDir); err != nil {
|
|
return fmt.Errorf("replacing %s: %w", oldDir, err)
|
|
}
|
|
if err := os.Rename(staging, oldDir); err != nil {
|
|
return fmt.Errorf("swapping in new external area: %w", err)
|
|
}
|
|
|
|
oldRenames := src.Renames
|
|
if len(newRenames) == 0 {
|
|
newRenames = nil
|
|
}
|
|
src.Renames = newRenames
|
|
src.Inventory = inventoryOf(selected)
|
|
src.Warnings = dedupe(warningsOf(items))
|
|
if err := lf.Save(lockPath); err != nil {
|
|
return err
|
|
}
|
|
|
|
inSelection := idSet(selected)
|
|
fmt.Fprintf(stdout, "source %s: renames updated at pinned rev %s\n", name, short(src.Rev))
|
|
for _, id := range ids {
|
|
before := deployedName(id, oldRenames)
|
|
after := deployedName(id, newRenames)
|
|
note := ""
|
|
if !inSelection[types[id]][id] {
|
|
note = " (not selected — applies once added)"
|
|
}
|
|
if before == after {
|
|
fmt.Fprintf(stdout, " %s %s: unchanged (%s)%s\n", types[id], id, after, note)
|
|
continue
|
|
}
|
|
fmt.Fprintf(stdout, " %s %s: %s → %s%s\n", types[id], id, before, after, note)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func printRenames(w io.Writer, name string, renames map[string]string) {
|
|
if len(renames) == 0 {
|
|
fmt.Fprintf(w, "source %s: no renames\n", name)
|
|
return
|
|
}
|
|
fmt.Fprintf(w, "source %s renames (upstream → deployed):\n", name)
|
|
ups := make([]string, 0, len(renames))
|
|
for up := range renames {
|
|
ups = append(ups, up)
|
|
}
|
|
sort.Strings(ups)
|
|
for _, up := range ups {
|
|
fmt.Fprintf(w, " %s → %s\n", up, renames[up])
|
|
}
|
|
}
|
|
|
|
func keysOf(m map[string]string) []string {
|
|
out := make([]string, 0, len(m))
|
|
for k := range m {
|
|
out = append(out, k)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func boolSet(ids []string) map[string]bool {
|
|
out := map[string]bool{}
|
|
for _, id := range ids {
|
|
out[id] = true
|
|
}
|
|
return out
|
|
}
|