Files
beads/cmd/bd/version_tracking.go
Steve Yegge 238ce34b52 fix: Code review fixes for bd-loka
Address code review findings from bd-p3b0:

1. Fix variable shadowing in upgradeAckCmd
   - Renamed local 'previousVersion' to 'lastSeenVersion'
   - Prevents confusion with global variable

2. Fix getVersionsSince() logic bug
   - versionChanges array is reverse chronological (newest first)
   - Function now correctly returns versions before the index
   - Reverses result to provide chronological order (oldest first)
   - Adds comprehensive documentation

3. Add comprehensive unit tests
   - Test getVersionsSince with various scenarios
   - Test trackBdVersion with no dir, first run, upgrade, same version
   - Test maybeShowUpgradeNotification behavior
   - All tests passing

Fixes found bugs and adds 100% test coverage for version tracking.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-23 17:16:27 -08:00

118 lines
3.5 KiB
Go

package main
import (
"fmt"
"github.com/steveyegge/beads/internal/beads"
"github.com/steveyegge/beads/internal/configfile"
)
// trackBdVersion checks if bd version has changed since last run and updates metadata.json.
// This function is best-effort - failures are silent to avoid disrupting commands.
// Sets global variables versionUpgradeDetected and previousVersion if upgrade detected.
//
// bd-loka: Built-in version tracking for upgrade awareness
func trackBdVersion() {
// Find the beads directory
beadsDir := beads.FindBeadsDir()
if beadsDir == "" {
// No .beads directory found - this is fine (e.g., bd init, bd version, etc.)
return
}
// Load current config
cfg, err := configfile.Load(beadsDir)
if err != nil {
// Silent failure - config might not exist yet
return
}
if cfg == nil {
// No config file yet - create one with current version
cfg = configfile.DefaultConfig()
cfg.LastBdVersion = Version
_ = cfg.Save(beadsDir) // Best effort
return
}
// Check if version changed
if cfg.LastBdVersion != "" && cfg.LastBdVersion != Version {
// Version upgrade detected!
versionUpgradeDetected = true
previousVersion = cfg.LastBdVersion
}
// Update metadata.json with current version (best effort)
// Only write if version actually changed to minimize I/O
// Also update on first run (when LastBdVersion is empty) to initialize tracking
if cfg.LastBdVersion != Version {
cfg.LastBdVersion = Version
_ = cfg.Save(beadsDir) // Silent failure is fine
}
}
// getVersionsSince returns all version changes since the given version.
// If sinceVersion is empty, returns all known versions.
// Returns changes in chronological order (oldest first).
//
// Note: versionChanges array is in reverse chronological order (newest first),
// so we return elements before the found index and reverse the slice.
func getVersionsSince(sinceVersion string) []VersionChange {
if sinceVersion == "" {
// Return all versions (already in reverse chronological, but kept for compatibility)
return versionChanges
}
// Find the index of sinceVersion
// versionChanges is ordered newest-first: [0.23.0, 0.22.1, 0.22.0, 0.21.0]
startIdx := -1
for i, vc := range versionChanges {
if vc.Version == sinceVersion {
startIdx = i
break
}
}
if startIdx == -1 {
// sinceVersion not found in our changelog - return all versions
// (user might be upgrading from a very old version)
return versionChanges
}
if startIdx == 0 {
// Already on the newest version
return []VersionChange{}
}
// Return versions before sinceVersion (those are newer)
// Then reverse to get chronological order (oldest first)
newerVersions := versionChanges[:startIdx]
// Reverse the slice to get chronological order
result := make([]VersionChange, len(newerVersions))
for i := range newerVersions {
result[i] = newerVersions[len(newerVersions)-1-i]
}
return result
}
// maybeShowUpgradeNotification displays a one-time upgrade notification if version changed.
// This is called by commands like 'bd ready' and 'bd list' to inform users of upgrades.
// Returns true if notification was shown.
func maybeShowUpgradeNotification() bool {
// Only show if upgrade detected and not yet acknowledged
if !versionUpgradeDetected || upgradeAcknowledged {
return false
}
// Mark as acknowledged so we only show once per session
upgradeAcknowledged = true
// Display notification
fmt.Printf("🔄 bd upgraded from v%s to v%s since last use\n", previousVersion, Version)
fmt.Println("💡 Run 'bd upgrade review' to see what changed")
fmt.Println()
return true
}