fix(version): recognize brew --HEAD version stamps — never route HEAD witnesses into pre-v56 recovery (#5625 carry, #5603) (#6079)
* fix(version): never route a non-semver predecessor into the pre-0.56 .dolt recovery CompareVersions scans each dot-separated part with %d and leaves whatever it cannot read at 0, so every non-semver string writeLocalVersion has ever recorded compares as "older than 0.56.0" and hands a live workspace to RecoverPreV56DoltDir, which deletes .dolt when the .bd-dolt-ok marker is absent. Two such stamps reach this call today: - "HEAD-<shortsha>", written by a Homebrew --HEAD install (#5603). Because CompareVersions also reads the stamp as 0.0.0 on the other side, such a workspace already registers an upgrade when the user installs a release, so the recovery fires on the very next command. - "v1.1.1-0.20260805093327-bf97b73749ac", the v-prefixed Go pseudo-version a `go install`-built bd stamps, which is the shape that bricked five production cities in #5650. Gate the recovery on IsValidSemver before the comparison. It rejects exactly the shapes CompareVersions misreads, so the gate can only remove predecessors from the recovery set, never add one: every stamp that reaches the recovery today and still parses keeps its recovery unchanged. Extracting recoverPreV56IfNeeded makes the destructive edge testable against the real fixture — a .dolt/ with no .bd-dolt-ok marker — instead of only through a database open. Carried from #5625 by @anisoptera and recomposed against release/1.3.0 after #5650 rewrote the witness classifiers; see that PR for the original analysis. Refs #5603, #5625, #5650 Co-authored-by: Isis Anisoptera <github@lotuswind.net> Agent-Signature: claude-code-claude-opus-5[1m]-unknown-reasoning on behalf of Julian Knutsen * feat(doctor): add IsBrewHeadVersion, the canonical brew --HEAD stamp recognizer A Homebrew --HEAD install stamps HEAD-<shortsha> into main.Version, or a bare HEAD when the head spec resolves no commit, either shape optionally carrying Homebrew's _<revision> suffix. That string is not semver, so every consumer that compares or validates a version stamp misreads it — but it is a healthy current-era marker, not a malformed one: bd rewrites it identically on every run, so nothing the user does can turn it into semver. Put one recognizer in the doctor package rather than a predicate per consumer. It is bounded to git's 7-40 character abbreviation range so arbitrary hex cannot stand in for a commit, and the revision suffix is checked digits-only so Atoi's tolerance of signed forms ("+1", "-0") cannot admit shapes brew never emits. The dirty-suffix and signed-revision rejections are pinned by the table. Carried verbatim from #5625 by @anisoptera, including the test table; only the doc comment is adapted, since the legacy upgrade guard's use of this recognizer is deferred to main (#5650 gave the guard its own unknown-era policy on this branch). Refs #5603, #5625 Co-authored-by: Isis Anisoptera <github@lotuswind.net> Agent-Signature: claude-code-claude-opus-5[1m]-unknown-reasoning on behalf of Julian Knutsen * fix(doctor): stop nagging Homebrew --HEAD builds about a version they cannot change Both version checks read a HEAD stamp as broken, and both offered a remedy that does nothing: - CheckCLIVersion compares through CompareVersions, which reads the stamp as 0.0.0, so every published release looks newer and the check warned "update: brew upgrade beads" on every run. Taking that advice would replace the --HEAD build with a release, i.e. undo what the user asked for. - CheckMetadataVersionTracking rejected the stamp as an "Invalid version format" and suggested running any bd command to reset tracking — which writes the identical stamp back. The warning was permanent by construction. Report a --HEAD build as healthy in both, with no Fix line, and skip the release fetch entirely when there is nothing to compare against. The staleness heuristic below is untouched for semver stamps; hoisting its repeated OK result into trackingActive is the /simplify cleanup from the original PR. Carried from #5625 by @anisoptera. Refs #5603, #5625 Co-authored-by: Isis Anisoptera <github@lotuswind.net> Agent-Signature: claude-code-claude-opus-5[1m]-unknown-reasoning on behalf of Julian Knutsen * fix(version): treat a changed brew --HEAD stamp as an upgrade CompareVersions reads every HEAD stamp as 0.0.0, so the two directions a --HEAD user actually travels were invisible: reinstalling onto a newer HEAD compared equal, and moving a release workspace onto HEAD compared as a downgrade. Both left versionUpgradeDetected false, so the one-shot post-upgrade reconciliation in autoMigrateOnVersionBump never ran for them. Treat a changed stamp with a HEAD build on either side as an upgrade: brew reinstalls only move forward. The misfire is bounded if that is ever wrong — the preceding commit stops a non-semver predecessor from reaching the pre-0.56 recovery, and the workspace's own version markers still refuse a genuine downgrade. Those markers cannot arbitrate the direction here either; they compare with the same dotted scan (workapi.compareDottedVersions) and read a HEAD stamp as 0.0.0 too. TestTrackBdVersion_HeadStampNeverReachesPreV56Recovery walks the whole #5603 shape end to end, because the two halves have to compose: widening detection is exactly what puts a HEAD stamp into previousVersion, where the recovery gate then has to refuse it. Carried from #5625 by @anisoptera; the workspace setup shared by the two tests is factored out here. Refs #5603, #5625 Co-authored-by: Isis Anisoptera <github@lotuswind.net> Agent-Signature: claude-code-claude-opus-5[1m]-unknown-reasoning on behalf of Julian Knutsen * fix(upgrade): keep --HEAD stamps out of the changelog delta and out of banners getVersionsSince matches its argument against the hardcoded changelog table and falls back to "return everything" when it finds no entry — a sensible default for a very old release, wrong for a stamp that can never appear there. After a --HEAD reinstall, `bd upgrade status` and `bd upgrade review` dumped the entire release history as new. A HEAD stamp now yields no delta. The banners then labelled that stamp "vHEAD-f925f3f", which reads as a typo. displayVersion adds the conventional "v" to semver and renders a HEAD stamp verbatim, applied at the upgrade-path sites that prefixed a stamp with "v": the ones #5625 changed, plus the three in `bd upgrade ack` and the pending- migration notice. That last one is newly reachable for a --HEAD user because of the preceding commit — detecting the upgrade is what lets autoMigrateOnVersionBump run far enough to print it. Left alone: the `bd doctor` headers, `bd info --whats-new`, and the hook section markers in hooks.go, whose format is parsed back out of user files. Carried from #5625 by @anisoptera. Refs #5603, #5625 Co-authored-by: Isis Anisoptera <github@lotuswind.net> Agent-Signature: claude-code-claude-opus-5[1m]-unknown-reasoning on behalf of Julian Knutsen --------- Co-authored-by: Isis Anisoptera <github@lotuswind.net>
This commit is contained in:
parent
bcc9052cab
commit
251ae6b6e0
5 changed files with 441 additions and 35 deletions
|
|
@ -16,6 +16,17 @@ var latestGitHubReleaseFetcher = fetchLatestGitHubRelease
|
|||
// CheckCLIVersion checks if the CLI version is up to date.
|
||||
// Takes cliVersion parameter since it can't access the Version variable from main package.
|
||||
func CheckCLIVersion(cliVersion string) DoctorCheck {
|
||||
// A Homebrew --HEAD build carries no orderable version — CompareVersions
|
||||
// would read it as 0.0.0 and warn "update available" forever, and the
|
||||
// suggested upgrade would actually move the user off HEAD.
|
||||
if IsBrewHeadVersion(cliVersion) {
|
||||
return DoctorCheck{
|
||||
Name: "CLI Version",
|
||||
Status: StatusOK,
|
||||
Message: fmt.Sprintf("%s (Homebrew --HEAD build; release comparison skipped)", cliVersion),
|
||||
}
|
||||
}
|
||||
|
||||
latestVersion, err := latestGitHubReleaseFetcher()
|
||||
if err != nil {
|
||||
// Network error or API issue - don't fail, just warn
|
||||
|
|
@ -151,6 +162,20 @@ func CheckMetadataVersionTracking(path string, currentVersion string) DoctorChec
|
|||
}
|
||||
}
|
||||
|
||||
trackingActive := DoctorCheck{
|
||||
Name: "Version Tracking",
|
||||
Status: StatusOK,
|
||||
Message: fmt.Sprintf("Version tracking active (last: %s, current: %s)", lastVersion, currentVersion),
|
||||
}
|
||||
|
||||
// A Homebrew --HEAD stamp carries no ordering, so the staleness heuristic
|
||||
// below cannot apply — but it is a healthy marker, not a malformed one.
|
||||
// Warning about it would be permanent: the offered fix rewrites the same
|
||||
// stamp back.
|
||||
if IsBrewHeadVersion(lastVersion) {
|
||||
return trackingActive
|
||||
}
|
||||
|
||||
// Validate that version is a valid semver-like string
|
||||
if !IsValidSemver(lastVersion) {
|
||||
return DoctorCheck{
|
||||
|
|
@ -171,11 +196,7 @@ func CheckMetadataVersionTracking(path string, currentVersion string) DoctorChec
|
|||
|
||||
// Guard against short version strings (e.g., "5" → [5] has no [1])
|
||||
if len(currentParts) < 2 || len(lastParts) < 2 {
|
||||
return DoctorCheck{
|
||||
Name: "Version Tracking",
|
||||
Status: StatusOK,
|
||||
Message: fmt.Sprintf("Version tracking active (last: %s, current: %s)", lastVersion, currentVersion),
|
||||
}
|
||||
return trackingActive
|
||||
}
|
||||
|
||||
// Simple heuristic: warn if minor version is 10+ behind or major version differs by 1+
|
||||
|
|
@ -193,11 +214,7 @@ func CheckMetadataVersionTracking(path string, currentVersion string) DoctorChec
|
|||
}
|
||||
|
||||
// Version is behind but not too old - this is normal after upgrade
|
||||
return DoctorCheck{
|
||||
Name: "Version Tracking",
|
||||
Status: StatusOK,
|
||||
Message: fmt.Sprintf("Version tracking active (last: %s, current: %s)", lastVersion, currentVersion),
|
||||
}
|
||||
return trackingActive
|
||||
}
|
||||
|
||||
// Version is current or ahead
|
||||
|
|
@ -289,6 +306,49 @@ func CompareVersions(v1, v2 string) int {
|
|||
return 0
|
||||
}
|
||||
|
||||
// IsBrewHeadVersion recognizes the version Homebrew stamps into --HEAD
|
||||
// installs of the core beads formula: HEAD-<shortsha>, a bare HEAD when the
|
||||
// head spec resolves no commit, and either shape carrying Homebrew's
|
||||
// _<revision> suffix. Such a stamp is a healthy current-era version marker,
|
||||
// not a malformed one — the binary rewrites it identically on every run, so
|
||||
// nothing the user does can "fix" it into semver.
|
||||
//
|
||||
// This is the canonical definition; every consumer of a version stamp that
|
||||
// needs to tell a --HEAD build apart from a release delegates here so the
|
||||
// ends cannot drift.
|
||||
func IsBrewHeadVersion(version string) bool {
|
||||
rest, ok := strings.CutPrefix(version, "HEAD")
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
// Homebrew's pkg_version appends _<revision> once the formula carries a
|
||||
// revision, so a revision bump must not read as malformed. The revision is
|
||||
// an unsigned decimal; a digits-only check keeps Atoi's tolerance of
|
||||
// signed forms ("+1", "-0") from admitting shapes brew never emits.
|
||||
if cut := strings.IndexByte(rest, '_'); cut >= 0 {
|
||||
revision := rest[cut+1:]
|
||||
if revision == "" || strings.Trim(revision, "0123456789") != "" {
|
||||
return false
|
||||
}
|
||||
rest = rest[:cut]
|
||||
}
|
||||
// Homebrew leaves the version as a bare "HEAD" whenever the head spec
|
||||
// cannot resolve a commit.
|
||||
if rest == "" {
|
||||
return true
|
||||
}
|
||||
sha, ok := strings.CutPrefix(rest, "-")
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
// Bound the tail to a plausible git abbreviation so that arbitrary hex
|
||||
// cannot stand in for a commit; git never abbreviates below 7 characters.
|
||||
if len(sha) < 7 || len(sha) > 40 {
|
||||
return false
|
||||
}
|
||||
return strings.Trim(sha, "0123456789abcdefABCDEF") == ""
|
||||
}
|
||||
|
||||
// IsValidSemver checks if a version string is valid semver-like format (X.Y.Z)
|
||||
func IsValidSemver(version string) bool {
|
||||
if version == "" {
|
||||
|
|
|
|||
|
|
@ -69,6 +69,41 @@ func TestIsValidSemver(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestIsBrewHeadVersion(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
version string
|
||||
want bool
|
||||
}{
|
||||
{name: "short sha", version: "HEAD-f925f3f", want: true},
|
||||
{name: "full sha", version: "HEAD-" + strings.Repeat("a", 40), want: true},
|
||||
{name: "uppercase sha", version: "HEAD-F925F3F", want: true},
|
||||
{name: "bare HEAD", version: "HEAD", want: true},
|
||||
{name: "revision suffix", version: "HEAD-f925f3f_1", want: true},
|
||||
{name: "bare HEAD with revision suffix", version: "HEAD_2", want: true},
|
||||
{name: "empty sha", version: "HEAD-"},
|
||||
{name: "non-hex sha", version: "HEAD-zzzzzzz"},
|
||||
{name: "sha below git abbreviation floor", version: "HEAD-abc"},
|
||||
{name: "sha beyond object id length", version: "HEAD-" + strings.Repeat("a", 41)},
|
||||
{name: "dirty suffix", version: "HEAD-f925f3f-dirty"},
|
||||
{name: "non-numeric revision", version: "HEAD-f925f3f_x"},
|
||||
{name: "signed revision", version: "HEAD-f925f3f_+1"},
|
||||
{name: "negative zero revision", version: "HEAD_-0"},
|
||||
{name: "empty revision", version: "HEAD-f925f3f_"},
|
||||
{name: "unrelated prefix", version: "HEADLESS-f925f3f"},
|
||||
{name: "semver", version: "1.1.2"},
|
||||
{name: "empty", version: ""},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := IsBrewHeadVersion(tt.version); got != tt.want {
|
||||
t.Fatalf("IsBrewHeadVersion(%q) = %v, want %v", tt.version, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpgradeCommandForPath(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
|
@ -119,6 +154,39 @@ func TestCheckCLIVersionUsesFetcher(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestCheckCLIVersionBrewHeadBuild verifies that a --HEAD build is not told to
|
||||
// run `brew upgrade beads`. CompareVersions reads the stamp as 0.0.0, so every
|
||||
// release looks newer forever, and taking the advice would move the user off
|
||||
// HEAD entirely.
|
||||
func TestCheckCLIVersionBrewHeadBuild(t *testing.T) {
|
||||
orig := latestGitHubReleaseFetcher
|
||||
fetched := false
|
||||
latestGitHubReleaseFetcher = func() (string, error) {
|
||||
fetched = true
|
||||
return "1.3.0", nil
|
||||
}
|
||||
t.Cleanup(func() { latestGitHubReleaseFetcher = orig })
|
||||
|
||||
for _, stamp := range []string{"HEAD-f925f3f", "HEAD", "HEAD-f925f3f_1"} {
|
||||
t.Run(stamp, func(t *testing.T) {
|
||||
check := CheckCLIVersion(stamp)
|
||||
if check.Status != StatusOK {
|
||||
t.Fatalf("Status = %q, want %q (message: %q)", check.Status, StatusOK, check.Message)
|
||||
}
|
||||
if check.Fix != "" {
|
||||
t.Fatalf("Fix = %q, want no upgrade advice for a --HEAD build", check.Fix)
|
||||
}
|
||||
if !strings.Contains(check.Message, stamp) {
|
||||
t.Fatalf("Message = %q, want it to name the stamp", check.Message)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if fetched {
|
||||
t.Error("CheckCLIVersion fetched the latest release for a --HEAD build; there is nothing to compare against")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseVersionParts(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
|
@ -191,6 +259,34 @@ func TestCheckMetadataVersionTracking_EmptyFile(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestCheckMetadataVersionTracking_BrewHeadStamp verifies that the version
|
||||
// stamp a Homebrew --HEAD install writes is reported as healthy rather than
|
||||
// permanently warning as an invalid format the user cannot fix: the offered
|
||||
// fix, running any bd command, writes the same stamp straight back.
|
||||
func TestCheckMetadataVersionTracking_BrewHeadStamp(t *testing.T) {
|
||||
for _, stamp := range []string{"HEAD-f925f3f", "HEAD", "HEAD-f925f3f_1"} {
|
||||
t.Run(stamp, func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, ".local_version"), []byte(stamp+"\n"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
check := CheckMetadataVersionTracking(tmpDir, "HEAD-aaaaaaa")
|
||||
|
||||
if check.Status != StatusOK {
|
||||
t.Errorf("Status = %q, want %q (message: %q)", check.Status, StatusOK, check.Message)
|
||||
}
|
||||
if check.Fix != "" {
|
||||
t.Errorf("Fix = %q, want no fix advice for a --HEAD stamp", check.Fix)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestCheckMetadataVersionTracking_InvalidVersion verifies handling of
|
||||
// malformed version strings in .local_version.
|
||||
func TestCheckMetadataVersionTracking_InvalidVersion(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ Examples:
|
|||
}
|
||||
|
||||
if versionUpgradeDetected {
|
||||
fmt.Printf("✨ bd upgraded from v%s to v%s\n", previousVersion, Version)
|
||||
fmt.Printf("✨ bd upgraded from %s to %s\n", displayVersion(previousVersion), displayVersion(Version))
|
||||
newVersions := getVersionsSince(previousVersion)
|
||||
if len(newVersions) > 0 {
|
||||
fmt.Printf(" %d version%s with changes available\n",
|
||||
|
|
@ -68,9 +68,9 @@ Examples:
|
|||
fmt.Println("Run 'bd upgrade review' to see what changed")
|
||||
}
|
||||
} else if previousVersion == "" {
|
||||
fmt.Printf("bd version: v%s (first run or version tracking just enabled)\n", Version)
|
||||
fmt.Printf("bd version: %s (first run or version tracking just enabled)\n", displayVersion(Version))
|
||||
} else {
|
||||
fmt.Printf("bd version: v%s (no upgrade detected)\n", Version)
|
||||
fmt.Printf("bd version: %s (no upgrade detected)\n", displayVersion(Version))
|
||||
}
|
||||
return nil
|
||||
},
|
||||
|
|
@ -109,7 +109,7 @@ Examples:
|
|||
}
|
||||
|
||||
if !versionUpgradeDetected {
|
||||
fmt.Printf("You're already on v%s (no upgrade detected)\n", Version)
|
||||
fmt.Printf("You're already on %s (no upgrade detected)\n", displayVersion(Version))
|
||||
fmt.Println("Run 'bd info --whats-new' to see recent changes")
|
||||
return nil
|
||||
}
|
||||
|
|
@ -124,12 +124,12 @@ Examples:
|
|||
})
|
||||
}
|
||||
|
||||
fmt.Printf("\n🔄 Upgraded from v%s to v%s\n", lastVersion, Version)
|
||||
fmt.Printf("\n🔄 Upgraded from %s to %s\n", displayVersion(lastVersion), displayVersion(Version))
|
||||
fmt.Println(strings.Repeat("=", 60))
|
||||
fmt.Println()
|
||||
|
||||
if len(newVersions) == 0 {
|
||||
fmt.Printf("v%s is newer than v%s but not in changelog\n", Version, lastVersion)
|
||||
fmt.Printf("%s is newer than %s but not in changelog\n", displayVersion(Version), displayVersion(lastVersion))
|
||||
fmt.Println("Run 'bd info --whats-new' to see recent documented changes")
|
||||
return nil
|
||||
}
|
||||
|
|
@ -211,11 +211,11 @@ Examples:
|
|||
}
|
||||
|
||||
if lastSeenVersion == Version {
|
||||
fmt.Printf("✓ Already on v%s\n", Version)
|
||||
fmt.Printf("✓ Already on %s\n", displayVersion(Version))
|
||||
} else if lastSeenVersion == "" {
|
||||
fmt.Printf("✓ Acknowledged bd v%s\n", Version)
|
||||
fmt.Printf("✓ Acknowledged bd %s\n", displayVersion(Version))
|
||||
} else {
|
||||
fmt.Printf("✓ Acknowledged upgrade from v%s to v%s\n", lastSeenVersion, Version)
|
||||
fmt.Printf("✓ Acknowledged upgrade from %s to %s\n", displayVersion(lastSeenVersion), displayVersion(Version))
|
||||
}
|
||||
return nil
|
||||
},
|
||||
|
|
|
|||
|
|
@ -61,9 +61,21 @@ func trackBdVersionFile(persist bool) {
|
|||
localVersionPath := filepath.Join(beadsDir, localVersionFile)
|
||||
lastVersion := readLocalVersion(localVersionPath)
|
||||
|
||||
// Check if version changed (only flag actual upgrades, not downgrades)
|
||||
// Check if version changed (only flag actual upgrades, not downgrades).
|
||||
//
|
||||
// Homebrew --HEAD stamps (HEAD-<sha>) carry no ordering, so CompareVersions
|
||||
// reads them as 0.0.0: a reinstall onto a newer HEAD registers as no change
|
||||
// at all, and moving a release workspace onto HEAD registers as a
|
||||
// downgrade. Treat a changed stamp with a HEAD build on either side as an
|
||||
// upgrade, so the one-shot post-upgrade reconciliation actually runs; brew
|
||||
// reinstalls only move forward. If that assumption is ever wrong the
|
||||
// misfire is bounded — recoverPreV56IfNeeded refuses a non-semver
|
||||
// predecessor, and the workspace's own version markers still refuse a
|
||||
// genuine downgrade. (Those markers cannot arbitrate direction here: they
|
||||
// compare with the same dotted scan and read HEAD stamps as 0.0.0 too.)
|
||||
if lastVersion != "" && lastVersion != Version {
|
||||
if doctor.CompareVersions(Version, lastVersion) > 0 {
|
||||
if doctor.CompareVersions(Version, lastVersion) > 0 ||
|
||||
doctor.IsBrewHeadVersion(Version) || doctor.IsBrewHeadVersion(lastVersion) {
|
||||
// Version upgrade detected!
|
||||
versionUpgradeDetected = true
|
||||
previousVersion = lastVersion
|
||||
|
|
@ -109,6 +121,13 @@ func getVersionsSince(sinceVersion string) []VersionChange {
|
|||
return versionChanges
|
||||
}
|
||||
|
||||
// A brew --HEAD stamp names no changelog entry; without this the
|
||||
// unknown-version fallback below would report the entire release history
|
||||
// as new after every --HEAD reinstall.
|
||||
if doctor.IsBrewHeadVersion(sinceVersion) {
|
||||
return []VersionChange{}
|
||||
}
|
||||
|
||||
// Find the index of sinceVersion
|
||||
// versionChanges is ordered newest-first: [0.23.0, 0.22.1, 0.22.0, 0.21.0]
|
||||
startIdx := -1
|
||||
|
|
@ -143,6 +162,16 @@ func getVersionsSince(sinceVersion string) []VersionChange {
|
|||
return result
|
||||
}
|
||||
|
||||
// displayVersion formats a version stamp for user-facing messages: semver
|
||||
// stamps get the conventional "v" prefix, while a brew --HEAD stamp is shown
|
||||
// verbatim ("vHEAD-f925f3f" reads as a typo).
|
||||
func displayVersion(version string) string {
|
||||
if doctor.IsBrewHeadVersion(version) {
|
||||
return version
|
||||
}
|
||||
return "v" + version
|
||||
}
|
||||
|
||||
// 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.
|
||||
func maybeShowUpgradeNotification() {
|
||||
|
|
@ -155,7 +184,7 @@ func maybeShowUpgradeNotification() {
|
|||
upgradeAcknowledged = true
|
||||
|
||||
// Display notification
|
||||
fmt.Printf("🔄 bd upgraded from v%s to v%s since last use\n", previousVersion, Version)
|
||||
fmt.Printf("🔄 bd upgraded from %s to %s since last use\n", displayVersion(previousVersion), displayVersion(Version))
|
||||
fmt.Println("💡 Run 'bd upgrade review' to see what changed")
|
||||
if usesSQLServer() {
|
||||
fmt.Println("💊 Run 'bd doctor' to verify upgrade completed cleanly")
|
||||
|
|
@ -210,17 +239,7 @@ func autoMigrateOnVersionBump(beadsDir string) {
|
|||
return
|
||||
}
|
||||
|
||||
// GH#2137: If upgrading from pre-0.56, the dolt database may have been
|
||||
// created by the old embedded Dolt mode. Recover by reinitializing.
|
||||
if previousVersion != "" && doctor.CompareVersions(previousVersion, "0.56.0") < 0 {
|
||||
recovered, recErr := doltserver.RecoverPreV56DoltDir(dbPath)
|
||||
if recErr != nil {
|
||||
debug.Logf("auto-migrate: pre-v56 recovery failed: %v", recErr)
|
||||
}
|
||||
if recovered {
|
||||
debug.Logf("auto-migrate: rebuilt pre-v56 dolt database at %s", dbPath)
|
||||
}
|
||||
}
|
||||
recoverPreV56IfNeeded(previousVersion, dbPath)
|
||||
|
||||
// Open database using factory (respects backend config from metadata.json)
|
||||
// Use rootCtx if available and not canceled, otherwise use Background
|
||||
|
|
@ -288,6 +307,39 @@ func autoMigrateOnVersionBump(beadsDir string) {
|
|||
}
|
||||
}
|
||||
|
||||
// recoverPreV56IfNeeded rebuilds a Dolt database left behind by the pre-0.56
|
||||
// embedded mode (GH#2137) — which means deleting .dolt — but only when the
|
||||
// recorded predecessor is an actual semantic version older than 0.56.0.
|
||||
//
|
||||
// The validity check is not redundant with the comparison. CompareVersions
|
||||
// scans every dot-separated part with %d and leaves whatever it cannot read at
|
||||
// 0, so any non-semver string writeLocalVersion has recorded compares as
|
||||
// pre-0.56 and routes a current workspace into this destructive path:
|
||||
// Homebrew's "HEAD-<shortsha>" from a --HEAD install (#5603), and the
|
||||
// v-prefixed Go pseudo-version a `go install`-built bd stamps (#5650). Neither
|
||||
// is a pre-0.56 workspace; both would lose their database.
|
||||
//
|
||||
// IsValidSemver rejects exactly the shapes CompareVersions misreads, so this
|
||||
// gate can only remove predecessors from the recovery set, never add one: a
|
||||
// stamp that reaches RecoverPreV56DoltDir today and still parses keeps its
|
||||
// recovery unchanged.
|
||||
func recoverPreV56IfNeeded(previousVersion, dbPath string) {
|
||||
if !doctor.IsValidSemver(previousVersion) {
|
||||
return
|
||||
}
|
||||
if doctor.CompareVersions(previousVersion, "0.56.0") >= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
recovered, recErr := doltserver.RecoverPreV56DoltDir(dbPath)
|
||||
if recErr != nil {
|
||||
debug.Logf("auto-migrate: pre-v56 recovery failed: %v", recErr)
|
||||
}
|
||||
if recovered {
|
||||
debug.Logf("auto-migrate: rebuilt pre-v56 dolt database at %s", dbPath)
|
||||
}
|
||||
}
|
||||
|
||||
// noticeSharedMigrateRefusal turns the one failure autoMigrateOnVersionBump
|
||||
// must not swallow into a one-line stderr notice.
|
||||
//
|
||||
|
|
@ -340,8 +392,8 @@ func noticeSharedMigrateRefusal(err error) {
|
|||
remedy = "This database has a remote — run 'bd migrate' to see the migrate-or-adopt options before choosing; reads keep working meanwhile."
|
||||
}
|
||||
fmt.Fprintf(os.Stderr,
|
||||
"bd upgraded to v%s: %d schema migration(s) pending on this database — not auto-applying (#5920).\n%s\n",
|
||||
Version, gateErr.Pending, remedy)
|
||||
"bd upgraded to %s: %d schema migration(s) pending on this database — not auto-applying (#5920).\n%s\n",
|
||||
displayVersion(Version), gateErr.Pending, remedy)
|
||||
}
|
||||
|
||||
// recordedWorkspaceVersion reads the marker through the role's accessor on a
|
||||
|
|
|
|||
|
|
@ -46,6 +46,18 @@ func TestGetVersionsSince(t *testing.T) {
|
|||
expectedCount: 0,
|
||||
description: "Should return empty slice when already on latest in changelog",
|
||||
},
|
||||
{
|
||||
name: "brew HEAD stamp returns empty",
|
||||
sinceVersion: "HEAD-f925f3f",
|
||||
expectedCount: 0,
|
||||
description: "A --HEAD stamp names no changelog entry and must not dump the full history",
|
||||
},
|
||||
{
|
||||
name: "bare brew HEAD stamp returns empty",
|
||||
sinceVersion: "HEAD",
|
||||
expectedCount: 0,
|
||||
description: "A bare HEAD stamp names no changelog entry and must not dump the full history",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
|
@ -59,6 +71,28 @@ func TestGetVersionsSince(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestDisplayVersion(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
version string
|
||||
want string
|
||||
}{
|
||||
{name: "release", version: "1.3.0", want: "v1.3.0"},
|
||||
{name: "pre-release", version: "1.3.0-rc.1", want: "v1.3.0-rc.1"},
|
||||
{name: "brew HEAD stamp", version: "HEAD-f925f3f", want: "HEAD-f925f3f"},
|
||||
{name: "bare brew HEAD stamp", version: "HEAD", want: "HEAD"},
|
||||
{name: "brew HEAD stamp with revision", version: "HEAD-f925f3f_1", want: "HEAD-f925f3f_1"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := displayVersion(tt.version); got != tt.want {
|
||||
t.Fatalf("displayVersion(%q) = %q, want %q", tt.version, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetVersionsSinceOrder(t *testing.T) {
|
||||
// Test that versions are returned in chronological order (oldest first)
|
||||
// versionChanges array is newest-first, but getVersionsSince returns oldest-first
|
||||
|
|
@ -304,6 +338,107 @@ func TestTrackBdVersion_DowngradeIgnored(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// newTrackingWorkspace prepares the minimal .beads a trackBdVersion call needs
|
||||
// and pins the globals it mutates, returning the .local_version path.
|
||||
func newTrackingWorkspace(t *testing.T, lastVersion, binVersion string) string {
|
||||
t.Helper()
|
||||
ensureCleanGlobalState(t)
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatalf("Failed to create .beads: %v", err)
|
||||
}
|
||||
t.Setenv("BEADS_DIR", beadsDir)
|
||||
t.Chdir(tmpDir)
|
||||
|
||||
metadataPath := filepath.Join(beadsDir, "metadata.json")
|
||||
if err := os.WriteFile(metadataPath, []byte(`{"database":"beads.db"}`), 0600); err != nil {
|
||||
t.Fatalf("Failed to create metadata.json: %v", err)
|
||||
}
|
||||
|
||||
localVersionPath := filepath.Join(beadsDir, localVersionFile)
|
||||
if err := writeLocalVersion(localVersionPath, lastVersion); err != nil {
|
||||
t.Fatalf("Failed to write local version: %v", err)
|
||||
}
|
||||
|
||||
origVersion, origDetected, origPrevious := Version, versionUpgradeDetected, previousVersion
|
||||
t.Cleanup(func() {
|
||||
Version, versionUpgradeDetected, previousVersion = origVersion, origDetected, origPrevious
|
||||
})
|
||||
Version = binVersion
|
||||
versionUpgradeDetected = false
|
||||
previousVersion = ""
|
||||
|
||||
return localVersionPath
|
||||
}
|
||||
|
||||
// TestTrackBdVersion_HeadStampChangeDetectedAsUpgrade covers the stamp changes
|
||||
// CompareVersions cannot see: it reads every HEAD stamp as 0.0.0, so a HEAD
|
||||
// reinstall looked like no change and a release-to-HEAD move looked like a
|
||||
// downgrade. Both skipped the one-shot post-upgrade reconciliation entirely.
|
||||
func TestTrackBdVersion_HeadStampChangeDetectedAsUpgrade(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
lastVersion string
|
||||
binVersion string
|
||||
wantDetected bool
|
||||
}{
|
||||
{name: "HEAD stamp to different HEAD stamp", lastVersion: "HEAD-423afdc", binVersion: "HEAD-f925f3f", wantDetected: true},
|
||||
{name: "release to HEAD stamp", lastVersion: "1.1.2", binVersion: "HEAD-f925f3f", wantDetected: true},
|
||||
{name: "HEAD stamp to release", lastVersion: "HEAD-423afdc", binVersion: "1.3.0", wantDetected: true},
|
||||
{name: "same HEAD stamp", lastVersion: "HEAD-f925f3f", binVersion: "HEAD-f925f3f", wantDetected: false},
|
||||
{name: "non-HEAD garbage change stays undetected", lastVersion: "not-a-version", binVersion: "also-not-one", wantDetected: false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
localVersionPath := newTrackingWorkspace(t, tt.lastVersion, tt.binVersion)
|
||||
|
||||
trackBdVersion()
|
||||
|
||||
if versionUpgradeDetected != tt.wantDetected {
|
||||
t.Errorf("versionUpgradeDetected = %v, want %v", versionUpgradeDetected, tt.wantDetected)
|
||||
}
|
||||
wantPrevious := ""
|
||||
if tt.wantDetected {
|
||||
wantPrevious = tt.lastVersion
|
||||
}
|
||||
if previousVersion != wantPrevious {
|
||||
t.Errorf("previousVersion = %q, want %q", previousVersion, wantPrevious)
|
||||
}
|
||||
if got := readLocalVersion(localVersionPath); got != tt.binVersion {
|
||||
t.Errorf(".local_version = %q, want %q", got, tt.binVersion)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestTrackBdVersion_HeadStampNeverReachesPreV56Recovery walks the whole
|
||||
// #5603 shape end to end: a workspace last touched by a Homebrew --HEAD build,
|
||||
// a .dolt with no .bd-dolt-ok marker, and a release install on top. Detection
|
||||
// and the recovery gate have to compose — widening detection is what puts a
|
||||
// HEAD stamp into previousVersion in the first place.
|
||||
func TestTrackBdVersion_HeadStampNeverReachesPreV56Recovery(t *testing.T) {
|
||||
newTrackingWorkspace(t, "HEAD-f925f3f", "1.3.0")
|
||||
doltDir, sentinel := writePreV56DoltFixture(t)
|
||||
|
||||
trackBdVersion()
|
||||
|
||||
if !versionUpgradeDetected {
|
||||
t.Fatal("installing a release over a --HEAD build should register as an upgrade")
|
||||
}
|
||||
if previousVersion != "HEAD-f925f3f" {
|
||||
t.Fatalf("previousVersion = %q, want the HEAD stamp", previousVersion)
|
||||
}
|
||||
|
||||
recoverPreV56IfNeeded(previousVersion, doltDir)
|
||||
|
||||
if _, err := os.Stat(sentinel); err != nil {
|
||||
t.Fatalf("a --HEAD predecessor routed a live workspace into the pre-v56 recovery: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrackBdVersion_SameVersion(t *testing.T) {
|
||||
// Create temp .beads directory
|
||||
tmpDir := t.TempDir()
|
||||
|
|
@ -395,6 +530,69 @@ func TestMaybeShowUpgradeNotification(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// writePreV56DoltFixture builds the workspace shape RecoverPreV56DoltDir
|
||||
// destroys: a .dolt/ directory with no .bd-dolt-ok compatibility marker. It
|
||||
// returns a path inside .dolt/ whose survival tells the caller whether the
|
||||
// recovery ran.
|
||||
func writePreV56DoltFixture(t *testing.T) (doltDir, sentinel string) {
|
||||
t.Helper()
|
||||
doltDir = t.TempDir()
|
||||
sentinel = filepath.Join(doltDir, ".dolt", "sentinel.txt")
|
||||
if err := os.MkdirAll(filepath.Dir(sentinel), 0o750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(sentinel, []byte("live workspace data"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return doltDir, sentinel
|
||||
}
|
||||
|
||||
// TestRecoverPreV56IfNeeded_OnlySemverPredecessorsAreRecovered pins the
|
||||
// destructive edge from #5603/#5625: CompareVersions reads any unparsable
|
||||
// version part as 0, so a non-semver predecessor compares as pre-0.56 and
|
||||
// hands a live workspace to a path that deletes .dolt. The Homebrew --HEAD
|
||||
// stamp and the v-prefixed Go pseudo-version from #5650 are both such
|
||||
// predecessors and both reach this call today.
|
||||
func TestRecoverPreV56IfNeeded_OnlySemverPredecessorsAreRecovered(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
previous string
|
||||
wantRecovery bool
|
||||
}{
|
||||
{name: "brew HEAD stamp", previous: "HEAD-f925f3f"},
|
||||
{name: "bare brew HEAD stamp", previous: "HEAD"},
|
||||
{name: "brew HEAD stamp with revision", previous: "HEAD-f925f3f_1"},
|
||||
{name: "go pseudo-version", previous: "v1.1.1-0.20260805093327-bf97b73749ac"},
|
||||
{name: "unreadable witness", previous: "not-a-version"},
|
||||
{name: "no predecessor", previous: ""},
|
||||
{name: "current release", previous: "1.1.2"},
|
||||
{name: "0.56.0 itself", previous: "0.56.0"},
|
||||
{name: "pre-0.56 release", previous: "0.55.4", wantRecovery: true},
|
||||
{name: "pre-0.56 pre-release", previous: "0.55.4-rc.1", wantRecovery: true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
doltDir, sentinel := writePreV56DoltFixture(t)
|
||||
|
||||
recoverPreV56IfNeeded(tt.previous, doltDir)
|
||||
|
||||
_, err := os.Stat(sentinel)
|
||||
if tt.wantRecovery {
|
||||
// The reinitializing `dolt init` may fail in a bare
|
||||
// environment; the removal that precedes it is the assertion.
|
||||
if !os.IsNotExist(err) {
|
||||
t.Fatalf("predecessor %q: expected pre-v56 recovery to rebuild .dolt, but %s survived", tt.previous, sentinel)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("predecessor %q: pre-v56 recovery deleted a live .dolt: %v", tt.previous, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutoMigrateOnVersionBump_NoUpgrade(t *testing.T) {
|
||||
// Save original state
|
||||
origUpgradeDetected := versionUpgradeDetected
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue