beads/cmd/bd/history.go
seanmartinsmith b5d8c46dc0
fix(history): respect --json flag when issue has no history (#3503)
* fix(history): respect --json flag when issue has no history

The empty-history early-return at cmd/bd/history.go:35-38 fired before
the `if jsonOutput` check, so `bd history <id> --json` printed prose
("No history found for issue X") instead of valid JSON when history
was empty. Every other input shape honored --json correctly; only the
empty case slipped through.

Reorder so the json branch runs above the empty-check. Result: --json
always returns valid JSON ([] when empty, full array otherwise),
consistent with `bd list --json` / `bd show --json`. No behavior change
for any non-empty case, no behavior change for the human-readable path.

Add a test case (nonexistent_issue_json_returns_empty_array) covering
the empty-history --json contract — strict JSON parse on stdout, not
the lenient bracket-index parse the existing helper uses.

Fixes #3502

* fix(history): reorder empty-check before --limit; add --json+--limit empty test

Address review suggestions on #3503:

- Move empty-history short-circuit ahead of the --limit slice. Slicing
  an empty slice is a no-op, but the new ordering makes the intent
  obvious: bail early on empty (with the right format), only apply
  --limit when there is data to limit.

- Add subtest covering 'bd history --json --limit 2 hi-nonexistent999'.
  Trivially correct from inspection of the new ordering, but guards
  against future reordering that might re-introduce the original bug
  in the limit+json+empty corner.
2026-04-28 06:34:45 -07:00

91 lines
2.2 KiB
Go

package main
import (
"fmt"
"github.com/spf13/cobra"
"github.com/steveyegge/beads/internal/ui"
)
var (
historyLimit int
)
var historyCmd = &cobra.Command{
Use: "history <id>",
GroupID: "views",
Short: "Show version history for an issue",
Long: `Show the complete version history of an issue, including all commits
where the issue was modified.
Examples:
bd history bd-123 # Show all history for issue bd-123
bd history bd-123 --limit 5 # Show last 5 changes`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ctx := rootCtx
issueID := args[0]
// Get issue history
history, err := store.History(ctx, issueID)
if err != nil {
FatalErrorRespectJSON("failed to get history: %v", err)
}
// Empty-history short-circuit handles both formats: --json gets []
// (so consumers piping to jq don't break), human format gets prose.
if len(history) == 0 {
if jsonOutput {
outputJSON(history)
return
}
fmt.Printf("No history found for issue %s\n", issueID)
return
}
// Apply limit only to non-empty history; slicing an empty slice is a no-op.
if historyLimit > 0 && historyLimit < len(history) {
history = history[:historyLimit]
}
if jsonOutput {
outputJSON(history)
return
}
// Display history in human-readable format
fmt.Printf("\n%s History for %s (%d entries)\n\n",
ui.RenderAccent("📜"), issueID, len(history))
for i, entry := range history {
// Commit info line
fmt.Printf("%s %s\n",
ui.RenderMuted(entry.CommitHash[:8]),
ui.RenderMuted(entry.CommitDate.Format("2006-01-02 15:04:05")))
fmt.Printf(" Author: %s\n", entry.Committer)
if entry.Issue != nil {
// Show issue state at this commit
statusIcon := ui.GetStatusIcon(string(entry.Issue.Status))
fmt.Printf(" %s %s: %s [P%d - %s]\n",
statusIcon,
entry.Issue.ID,
entry.Issue.Title,
entry.Issue.Priority,
entry.Issue.Status)
}
// Separator between entries
if i < len(history)-1 {
fmt.Println()
}
}
fmt.Println()
},
}
func init() {
historyCmd.Flags().IntVar(&historyLimit, "limit", 0, "Limit number of history entries (0 = all)")
historyCmd.ValidArgsFunction = issueIDCompletion
rootCmd.AddCommand(historyCmd)
}