fix(io): address Copilot review feedback

- Fix MockMedium.Rename: collect keys before mutating maps during iteration
- Fix .git checks to use Exists instead of List (handles worktrees/submodules)
- Fix cmd_sync.go: use DeleteAll for recursive directory removal

Files updated:
- pkg/io/io.go: safe map iteration in Rename
- internal/cmd/setup/cmd_bootstrap.go: Exists for .git checks
- internal/cmd/setup/cmd_registry.go: Exists for .git checks
- internal/cmd/pkgcmd/cmd_install.go: Exists for .git checks
- internal/cmd/pkgcmd/cmd_manage.go: Exists for .git checks
- internal/cmd/docs/cmd_sync.go: DeleteAll for recursive delete

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Snider 2026-02-02 02:03:47 +00:00
parent fd8a4a03fc
commit e6d1bd22d2
6 changed files with 27 additions and 18 deletions

View file

@ -126,8 +126,8 @@ func runDocsSync(registryPath string, outputDir string, dryRun bool) error {
outName := packageOutputName(info.Name)
repoOutDir := filepath.Join(outputDir, outName)
// Clear existing directory
io.Local.Delete(repoOutDir) // Recursive delete
// Clear existing directory (recursively)
_ = io.Local.DeleteAll(repoOutDir)
if err := io.Local.EnsureDir(repoOutDir); err != nil {
cli.Print(" %s %s: %s\n", errorStyle.Render("✗"), info.Name, err)

View file

@ -74,7 +74,7 @@ func runPkgInstall(repoArg, targetDir string, addToRegistry bool) error {
repoPath := filepath.Join(targetDir, repoName)
if _, err := coreio.Local.List(filepath.Join(repoPath, ".git")); err == nil {
if coreio.Local.Exists(filepath.Join(repoPath, ".git")) {
fmt.Printf("%s %s\n", dimStyle.Render(i18n.Label("skip")), i18n.T("cmd.pkg.install.already_exists", map[string]string{"Name": repoName, "Path": repoPath}))
return nil
}

View file

@ -57,9 +57,8 @@ func runPkgList() error {
var installed, missing int
for _, r := range allRepos {
repoPath := filepath.Join(basePath, r.Name)
exists := false
if _, err := coreio.Local.List(filepath.Join(repoPath, ".git")); err == nil {
exists = true
exists := coreio.Local.Exists(filepath.Join(repoPath, ".git"))
if exists {
installed++
} else {
missing++
@ -219,7 +218,7 @@ func runPkgOutdated() error {
for _, r := range reg.List() {
repoPath := filepath.Join(basePath, r.Name)
if _, err := coreio.Local.List(filepath.Join(repoPath, ".git")); err != nil {
if !coreio.Local.Exists(filepath.Join(repoPath, ".git")) {
notInstalled++
continue
}

View file

@ -105,7 +105,7 @@ func runBootstrap(ctx context.Context, only string, dryRun, all bool, projectNam
// Clone core-devops first
devopsPath := filepath.Join(targetDir, devopsRepo)
if _, err := coreio.Local.List(filepath.Join(devopsPath, ".git")); err != nil {
if !coreio.Local.Exists(filepath.Join(devopsPath, ".git")) {
fmt.Printf("%s %s %s...\n", dimStyle.Render(">>"), i18n.T("common.status.cloning"), devopsRepo)
if !dryRun {
@ -148,9 +148,9 @@ func runBootstrap(ctx context.Context, only string, dryRun, all bool, projectNam
}
// isGitRepoRoot returns true if the directory is a git repository root.
// Handles both regular repos (.git is a directory) and worktrees (.git is a file).
func isGitRepoRoot(path string) bool {
_, err := coreio.Local.List(filepath.Join(path, ".git"))
return err == nil
return coreio.Local.Exists(filepath.Join(path, ".git"))
}
// isDirEmpty returns true if the directory is empty or contains only hidden files.

View file

@ -118,7 +118,7 @@ func runRegistrySetupWithReg(ctx context.Context, reg *repos.Registry, registryP
// Check if already exists
repoPath := filepath.Join(basePath, repo.Name)
// Check .git dir existence via List
if _, err := coreio.Local.List(filepath.Join(repoPath, ".git")); err == nil {
if coreio.Local.Exists(filepath.Join(repoPath, ".git")) {
exists++
continue
}
@ -147,7 +147,7 @@ func runRegistrySetupWithReg(ctx context.Context, reg *repos.Registry, registryP
// Check if already exists
repoPath := filepath.Join(basePath, repo.Name)
if _, err := coreio.Local.List(filepath.Join(repoPath, ".git")); err == nil {
if coreio.Local.Exists(filepath.Join(repoPath, ".git")) {
exists++
continue
}

View file

@ -276,22 +276,32 @@ func (m *MockMedium) Rename(oldPath, newPath string) error {
newPrefix += "/"
}
// Move files under this directory
// Collect files to move first (don't mutate during iteration)
filesToMove := make(map[string]string)
for f, content := range m.Files {
if strings.HasPrefix(f, oldPrefix) {
newF := newPrefix + strings.TrimPrefix(f, oldPrefix)
m.Files[newF] = content
delete(m.Files, f)
filesToMove[f] = newF
_ = content // content will be copied in next loop
}
}
// Move subdirectories
for oldF, newF := range filesToMove {
m.Files[newF] = m.Files[oldF]
delete(m.Files, oldF)
}
// Collect directories to move first
dirsToMove := make(map[string]string)
for d := range m.Dirs {
if strings.HasPrefix(d, oldPrefix) {
newD := newPrefix + strings.TrimPrefix(d, oldPrefix)
m.Dirs[newD] = true
delete(m.Dirs, d)
dirsToMove[d] = newD
}
}
for oldD, newD := range dirsToMove {
m.Dirs[newD] = true
delete(m.Dirs, oldD)
}
return nil
}
return coreerr.E("io.MockMedium.Rename", "path not found: "+oldPath, os.ErrNotExist)