diff --git a/cmd/bd/purge.go b/cmd/bd/purge.go index cead0ae98..1c95d99be 100644 --- a/cmd/bd/purge.go +++ b/cmd/bd/purge.go @@ -210,7 +210,7 @@ func runPurgeOrPrune(cmd *cobra.Command, scope purgeScope) error { case dryRun: return emitSweepDryRun(scope, limit, result) case !force: - return emitSweepConfirm(scope, olderThan, pattern, result) + return emitSweepConfirm(scope, olderThan, pattern, limit, result) } commandDidWrite.Store(true) @@ -354,7 +354,7 @@ func emitSweepDryRun(scope purgeScope, limit int, result issueops.SweepResult) e return nil } -func emitSweepConfirm(scope purgeScope, olderThan, pattern string, result issueops.SweepResult) error { +func emitSweepConfirm(scope purgeScope, olderThan, pattern string, limit int, result issueops.SweepResult) error { fmt.Printf("Found %d %s(s) to %s\n", result.Swept, scope.subjectNoun, scope.cmdName) if result.Skipped.Pinned > 0 { fmt.Printf("Skipping %d pinned bead(s)\n", result.Skipped.Pinned) @@ -365,6 +365,16 @@ func emitSweepConfirm(scope purgeScope, olderThan, pattern string, result issueo if result.Skipped.Referenced > 0 { fmt.Println(ui.MutedStyle.Render(fmt.Sprintf("Skipping %d referenced bead(s)", result.Skipped.Referenced))) } + return HandleErrorWithHint( + fmt.Sprintf("would %s %d bead(s)", scope.cmdName, result.Swept), + fmt.Sprintf("Use --force to confirm or --dry-run to preview.\n %s", sweepConfirmHint(scope, olderThan, pattern, limit))) +} + +// sweepConfirmHint is the command the confirmation suggests: the caller's own +// selection with --force added. Every selector the caller gave is carried, +// --limit among them — a hint that dropped the limit previewed one deletion +// and, followed, deleted every eligible row, no-history beads included. +func sweepConfirmHint(scope purgeScope, olderThan, pattern string, limit int) string { hint := fmt.Sprintf("bd %s --force", scope.cmdName) if scope.tier == issueops.SweepWispsPlane { hint += " --wisps-plane" @@ -375,9 +385,10 @@ func emitSweepConfirm(scope purgeScope, olderThan, pattern string, result issueo if pattern != "" { hint += " --pattern " + pattern } - return HandleErrorWithHint( - fmt.Sprintf("would %s %d bead(s)", scope.cmdName, result.Swept), - fmt.Sprintf("Use --force to confirm or --dry-run to preview.\n %s", hint)) + if limit > 0 { + hint += fmt.Sprintf(" --limit %d", limit) + } + return hint } func emitSweepResult(scope purgeScope, limit int, result issueops.SweepResult) error { @@ -472,6 +483,13 @@ const maxOlderThanDays = math.MaxInt64 / int64(24*time.Hour) var errOlderThanRange = fmt.Errorf("duration out of range (at most %d days)", maxOlderThanDays) func daysAge(days int64) (time.Duration, error) { + // Only the upper bound was checked here; a day count negative enough + // overflows the int64-nanosecond multiplication into a small positive + // duration, so positiveAge's own check below never sees the sign flip. + // Reject non-positive counts before the multiplication can run. + if days <= 0 { + return 0, fmt.Errorf("duration must be positive") + } if days > maxOlderThanDays { return 0, errOlderThanRange } diff --git a/cmd/bd/purge_test.go b/cmd/bd/purge_test.go index d44cf8221..dfcc5ff10 100644 --- a/cmd/bd/purge_test.go +++ b/cmd/bd/purge_test.go @@ -3,6 +3,8 @@ package main import ( "testing" "time" + + "github.com/steveyegge/beads/issueops" ) // TestParseOlderThan pins --older-than on both `bd purge` and `bd prune`. @@ -51,6 +53,13 @@ func TestParseOlderThan(t *testing.T) { {"106752d", 0, true}, {"9223372036854775807d", 0, true}, {"99999999999h", 0, true}, + // A negative count far enough past the floor must be refused too, + // never wrapped: -213503d used to overflow into a small positive + // duration (~23h35m) and purge almost everything instead of being + // rejected outright. + {"-213503d", 0, true}, + {"-213503", 0, true}, + {"-9223372036854775808", 0, true}, // The largest representable day count still parses. {"106751d", 106751 * day, false}, } @@ -67,3 +76,33 @@ func TestParseOlderThan(t *testing.T) { }) } } + +// TestSweepConfirmHintCarriesEverySelector pins the no-force confirmation's +// suggested command against dropping a selector the caller gave: a hint that +// dropped --limit previewed one deletion and, followed literally, deleted +// every eligible row instead of the one the preview showed. +func TestSweepConfirmHintCarriesEverySelector(t *testing.T) { + purge := purgeScope{cmdName: "purge", tier: issueops.SweepWispsPlane} + prune := purgeScope{cmdName: "prune", tier: issueops.SweepDurable} + tests := []struct { + name string + scope purgeScope + olderThan string + pattern string + limit int + want string + }{ + {"the gate's command", purge, "168h", "", 1, "bd purge --force --wisps-plane --older-than 168h --limit 1"}, + {"a larger limit", purge, "7d", "", 500, "bd purge --force --wisps-plane --older-than 7d --limit 500"}, + {"no limit asked", purge, "168h", "", 0, "bd purge --force --wisps-plane --older-than 168h"}, + {"pattern and limit", purge, "", "wi-*", 3, "bd purge --force --wisps-plane --pattern wi-* --limit 3"}, + {"prune defines no limit", prune, "30d", "", 0, "bd prune --force --older-than 30d"}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := sweepConfirmHint(tt.scope, tt.olderThan, tt.pattern, tt.limit); got != tt.want { + t.Errorf("sweepConfirmHint() = %q, want %q", got, tt.want) + } + }) + } +}