cmd/bd/docs.md
Path: @/cmd/bd
The cmd/bd directory contains the complete CLI application for the Beads issue tracker. It implements the bd command-line tool, which users interact with to create, query, manage, and synchronize issues across distributed systems.
The CLI is built on the Cobra framework and consists of command implementations for core operations (create, list, delete, import, export, sync, etc.), Dolt server management for background operations, and version reporting that includes git commit and branch information from the build.
Entry Point: The CLI defined here (cmd/bd/main.go) is the user-facing interface to the entire beads system. All user interactions flow through this package.
Integration with Core Libraries: The CLI commands call into libraries at @/internal/beads (database discovery, version detection), @/internal/storage (database operations), @/internal/rpc (Dolt server communication), and other internal packages.
Server Communication: Commands use RPC client logic to communicate with the Dolt server (PersistentPreRun hook), allowing the CLI to operate either in server mode (delegating to the Dolt server) or embedded mode (local database operations).
Version Reporting: The version command (@/cmd/bd/version.go) reports full build information - it resolves git commit and branch from ldflags set at build time via the Makefile (@/Makefile) and goreleaser config (@/.goreleaser.yml). This enables users to identify exactly what code their binary was built from.
Release Pipeline Integration: The version infrastructure here integrates directly with the release pipeline documented in @/RELEASING.md. Version information is injected during builds by build automation, ensuring consistency across all distribution channels (GitHub releases, Homebrew, npm, direct go install).
Version Information Pipeline (GitHub issue #503):
Version Variables (lines 15-23 in @/cmd/bd/version.go):
Version: The semantic version (e.g., "0.29.0")Build: The build type ("dev" for local builds, typically set to short commit hash by goreleaser)Commit: Git commit hash (optional, set via ldflag -X main.Commit=...)Branch: Git branch name (optional, set via ldflag -X main.Branch=...)Resolution Strategy (resolveCommitHash() and resolveBranch() functions):
runtime/debug.ReadBuildInfo() to extract VCS info automatically embedded by Go when using go build directly from sourcegit symbolic-ref --short HEAD as a runtime resolutionBuild-Time Injection:
@/Makefile, lines 37-41): The make install target extracts git info at build time and passes it to go install via ldflags@/.goreleaser.yml): All 5 build configurations (linux-amd64, linux-arm64, darwin-amd64, darwin-arm64, windows-amd64) set the same ldflags using goreleaser's template variables {{.Commit}} and {{.Branch}}@/scripts/install.sh, lines 13-14): Helper script for users who want to build from source with explicit git info passed to go installOutput Formatting:
version.go): Shows format like bd version 0.29.0 (dev: main@7e70940) when both commit and branch are availablecommit and branch fields when availableServer Version Checking (lines 63-109): The --server flag shows server/client compatibility by calling health RPC endpoints
Command Structure:
Command structs and run functionsinit() functions that add them to rootCmdKey Data Paths:
Why Both Commit and Branch Are Needed:
Commit variable allows tracing the exact code versionBranch variable provides context for CI/CD systems, build automation, and helps users understand which development line they're runningThe Problem This Solves (Issue #503):
go install from source doesn't automatically embed VCS info like go build does (Go 1.18+ feature)bd version would only see semantic version and build type, not which commit they hadmake install and raw go install produce binaries with full version info by setting ldflags explicitlyFallback Resolution Chain (important for development):
resolveCommitHash() function first checks the ldflag, then checks runtime build info, returning empty if neither is availableTesting Coverage (@/cmd/bd/version_test.go):
TestResolveCommitHash: Verifies ldflag values are used when setTestResolveBranch: Verifies ldflag values are used when setTestVersionOutputWithCommitAndBranch: Verifies text and JSON output formats correctly include commit and branch informationTestVersionCommand and TestVersionFlag tests verify basic version outputBuild System Dependencies:
Platform-Specific Considerations:
symbolic-ref fallback in resolveBranch works reliably even in fresh repos with no commitsCreated and maintained by Nori.