Files
skillshare/internal/update/tracked.go
T
Willie 3479c42420 fix(update): share the tracked checkout rule and tighten the repair
Simplify and code-review passes over #474 found the fix spread over
several definitions of "tracked repo" and three holes in the repair.

One install.IsTrackedCheckout (a _-prefixed git checkout) now backs
GetTrackedRepos, GetUpdatableSkills, the dashboard routing and the repair.
GetUpdatableSkills skipping such entries replaces the two CLI resolver
filters, so `skillshare check` and the dashboard Updates tab no longer list
a rewritten repo a second time as a regular skill. The repair checks that
one path instead of walking the whole source tree.

- Reconcile sets tracked on a checkout again without dropping type or
  repo_url, so a rewritten entry that went through it was never cleaned.
  The repair now also runs when those regular-install fields remain.
- The repair used GetByPath, whose basename fallback can return another
  top-level item's entry; it now uses the repo's own key.
- A tracked entry whose checkout is gone fell through to the basename
  fallback of resolveTrackedRepo and pulled a different repo with the same
  basename. It now reports the repo as missing on disk.
2026-10-07 22:33:54 +08:00

194 lines
5.9 KiB
Go

// Package update holds update operations shared by the CLI and the dashboard.
package update
import (
"fmt"
"path/filepath"
"skillshare/internal/audit"
"skillshare/internal/git"
"skillshare/internal/install"
"skillshare/internal/sourcewalk"
)
// Status is how far TrackedRepo got when it returned without an error.
type Status string
const (
StatusUpdated Status = "updated"
StatusUpToDate Status = "up-to-date"
// StatusDirty means uncommitted changes stopped the update before the pull.
StatusDirty Status = "dirty"
// StatusDryRun means the pull was not run.
StatusDryRun Status = "dry-run"
)
// Stage names the step of TrackedRepo that failed before the audit gate.
type Stage string
const (
StageStatus Stage = "status"
StageDiscard Stage = "discard"
StagePull Stage = "pull"
)
// Error is a failure before the audit gate. A blocked audit is reported as
// *install.AuditGateError instead.
type Error struct {
Stage Stage
Err error
}
func (e *Error) Error() string {
switch e.Stage {
case StageStatus:
return fmt.Sprintf("failed to check git status: %v", e.Err)
case StageDiscard:
return fmt.Sprintf("failed to discard changes: %v", e.Err)
}
return fmt.Sprintf("git pull failed: %v", e.Err)
}
func (e *Error) Unwrap() error { return e.Err }
// TrackedRepoOptions configures one tracked-repo update.
type TrackedRepoOptions struct {
// SourceDir is the source root that holds the repo's metadata and accepted
// findings. Empty skips the metadata refresh.
SourceDir string
// Force discards uncommitted changes, pulls with a hard reset and lets
// audit findings through. It never overrides a scan that could not run.
Force bool
DryRun bool
SkipAudit bool
Threshold string
ProjectRoot string // non-empty audits with the project's rules
Follow *sourcewalk.Follow
// AcceptedFindings makes the gate ignore findings an earlier override
// recorded under SourceDir, and records the ones this update lets through.
// The CLI sets it; the dashboard does not.
AcceptedFindings bool
// Confirm is asked once when findings would block and Force is unset. It
// receives the result so far (Info, Audit, Threshold, AcceptedSkipped).
// Nil, false or an error blocks and rolls back.
Confirm func(*TrackedRepoResult) (bool, error)
OnProgress func(string)
}
// TrackedRepoResult is always returned, also next to an error, filled as far
// as the update got.
type TrackedRepoResult struct {
Status Status
Discarded bool // Force discarded uncommitted changes (in a dry run: would)
Info *git.UpdateInfo // set once the pull succeeded
Audit *audit.Result // nil when the audit was skipped or could not scan
Threshold string // normalized block threshold of the audit
AcceptedSkipped int // findings ignored because they were accepted earlier
Overridden bool // blocking findings were let through by Force or Confirm
Recorded int // findings newly recorded as accepted
RecordErr error // recording accepted findings failed; the update stays applied
MetadataChanged bool // the repo's entry under SourceDir was rewritten
MetadataErr error // refreshing metadata failed; the update stays applied
Warnings []string // git submodules the pulled checkout leaves empty
}
// TrackedRepo updates one tracked repo: it refuses or discards uncommitted
// changes, pulls, audits what was pulled, resets to the pre-pull commit when
// the audit blocks, and refreshes the repo's metadata.
func TrackedRepo(repoPath string, opts TrackedRepoOptions) (*TrackedRepoResult, error) {
res := &TrackedRepoResult{}
// Force resets the checkout anyway, so it does not need a readable status.
dirty, err := git.IsDirty(repoPath)
if err != nil && !opts.Force {
return res, &Error{Stage: StageStatus, Err: err}
}
if dirty {
if !opts.Force {
res.Status = StatusDirty
return res, nil
}
res.Discarded = true
if !opts.DryRun {
if err := git.Restore(repoPath); err != nil {
return res, &Error{Stage: StageDiscard, Err: err}
}
}
}
if opts.DryRun {
res.Status = StatusDryRun
return res, nil
}
pull := git.PullWithProgress
if opts.Force {
pull = git.ForcePullWithProgress
}
info, err := pull(repoPath, git.AuthEnvForRepo(repoPath), opts.OnProgress)
if err != nil {
return res, &Error{Stage: StagePull, Err: err}
}
res.Info = info
if !info.UpToDate && !opts.SkipAudit {
if err := auditPulled(repoPath, res, opts); err != nil {
return res, err
}
}
if !info.UpToDate {
res.Warnings = install.SubmoduleWarnings(repoPath, git.AuthEnvForRepo(repoPath))
}
if opts.SourceDir != "" {
res.MetadataChanged, res.MetadataErr = refreshMetadata(repoPath, opts)
}
res.Status = StatusUpdated
if info.UpToDate {
res.Status = StatusUpToDate
}
return res, nil
}
func auditPulled(repoPath string, res *TrackedRepoResult, opts TrackedRepoOptions) error {
fill := func(g *install.AuditGateResult) {
res.Audit, res.Threshold, res.AcceptedSkipped, res.Overridden = g.Audit, g.Threshold, g.AcceptedSkipped, g.Overridden
}
gate := install.AuditGate{
SourceDir: opts.SourceDir,
RepoPath: repoPath,
BeforeHash: res.Info.BeforeHash,
Threshold: opts.Threshold,
ProjectRoot: opts.ProjectRoot,
Follow: opts.Follow,
Override: opts.Force,
HonorAccepted: opts.AcceptedFindings,
}
if opts.Confirm != nil {
gate.Confirm = func(g *install.AuditGateResult) (bool, error) {
fill(g)
return opts.Confirm(res)
}
}
g, err := gate.Run()
if g != nil {
fill(g)
}
if err != nil {
return err
}
if g.Overridden && opts.AcceptedFindings {
res.Recorded, res.RecordErr = install.RecordAcceptedFindings(opts.SourceDir, repoPath, g.Audit, g.Threshold)
}
return nil
}
func refreshMetadata(repoPath string, opts TrackedRepoOptions) (bool, error) {
rel, err := filepath.Rel(opts.SourceDir, repoPath)
if err != nil {
return false, err
}
return install.RefreshTrackedRepoMetadata(opts.SourceDir, rel, repoPath, opts.Follow)
}