* fix(cli): stop eleven subcommands shadowing the root --json flag
--json is a persistent flag on rootCmd bound to jsonOutput. Ten
subcommands (restore, admin compact, repo add/remove/list/sync,
migrate, migrate sync/hooks/schema) also registered a local --json bound
to the same variable. pflag's AddFlagSet keeps the local flag and drops
the inherited one, so rootCmd.PersistentFlags().Changed("json") stayed
false for those commands; PersistentPreRunE and
refreshBoundCommandConfig then treated the flag as unset and reset
jsonOutput from config. `bd restore <id> --json` and friends printed text
unless config said json: true. restore_embedded_test.go recorded the
symptom as a "known issue" and skipped its JSON assertion.
bd preflight registered an unbound local Bool("json") instead; it read
its own flag and was not affected by the reset, but it shadowed the root
flag the same way and ignored a configured json default. It now inherits
the root flag like status, update and the proxied close/reopen routes.
Remove the eleven local registrations. Add
TestNoSubcommandShadowsRootJSONFlag, which walks the command tree and
fails on any command that registers its own --json (local or
persistent). Restore the restore_json assertion (parse the output, check
ID and description). Flip the compact and migrate schema registration
tests to assert the flag is inherited, matching stale_test.go.
Visible change: --json moves from each command's Flags section to Global
Flags in --help, with the root description. docs/cli-reference is pinned
to the release tag and regenerates at release time.
* fix(cli): make the --json shadow guard side-effect free
TestNoSubcommandShadowsRootJSONFlag walked every command through
LocalNonPersistentFlags(), and the --json assertions in compact_test.go and
migrate_test.go used InheritedFlags(). Both call cobra's
mergePersistentFlags(), which copies every root persistent flag into the
command's Flags() for the rest of the test process. TestServeFlags pins the
exact flag set of `bd serve`, so `go test ./cmd/bd` failed whenever it ran
after the guard; CI only stayed green because the two tests land in
different name-hash shards.
Compare the command's Flags().Lookup("json") against the root flag pointer
instead. A local registration is a different *pflag.Flag; an inherited one
is the same pointer, and a command that has not merged yet returns nil.
Verified: the four tests pass together, and re-adding a local --json to
`bd restore` still fails the guard.
bd preflight also drops its `jsonOutput, _ := cmd.Flags().GetBool("json")`
shadow of the package-level variable. The root flag is bound to that
variable and PersistentPreRun already resolves the flag/config precedence,
so the local copy was redundant and swallowed the lookup error.
* review: re-key the preflight --json test to the root flag
Base #6293 added TestPreflightJSONFlagBindsGlobal, which opens by requiring
preflightCmd to own a local --json. This PR's guard forbids exactly that, so
the two tests were mutually exclusive and the adopted head was red. The base
test arrived with a base advance after this PR was written, so nothing in the
contributed work is wrong.
Adapt the test rather than restore the local flag: every property it pins
survives re-keyed to the root persistent flag, which is BoolVar'd to the same
package global. The opening assertion becomes the shadow-aware compare this PR
already introduced in compact_test.go, the Changed==true branch is driven
through rootCmd.PersistentFlags().Set (flag.Value.Set never sets Changed), and
the config half is unchanged. Renamed to TestPreflightJSONInheritsRootFlag so
the name stops asserting the very thing the PR removes.
Also correct three CHANGELOG clauses that were true against the contributor's
older base and false at this one: #6293's commandJSONFlagChanged already
consults a subcommand's own Changed bit before the root's, preflight's local
flag was bound rather than unbound, and #6293 already announced that preflight
honors the json config default. The bullet now claims what this PR delivers
here -- deleting the shadowing registrations instead of compensating for them.
Compile the new guard into the Bazel test target. cmd/bd/json_flag_shadow_test.go
was absent from the explicit srcs of go_test(name = "bd_test"), so the invariant
this PR exists to create never ran in the BAZEL* lanes -- and those lanes are
required for merge: CI_GATE_REQUIRED on origin/main lists BAZEL, BAZEL_TEST,
BAZEL_PURE, BAZEL_EMBEDDED, BAZEL_DOLTSERVER, BAZEL_PROXIED and
BAZEL_SERVER_STORAGE. Added in sorted position between jira_test.go and
kv_embedded_test.go; the file is untagged package main, so bd_test is the right
target under the root gazelle:build_tags gms_pure_go. make bazel-sync-check
cannot catch this -- it maintains only the go_srcs filegroup globs, not go_test
srcs.
Finally, reconcile the neighbouring #6293 bullet, which this PR invalidates. It
still described preflight's --json as "bound to the same global every sibling
command binds" and closed on "because an unbound flag reports as unset" -- both
describing the local registration deleted here, fourteen lines from the bullet
announcing the deletion and shipping in the same [Unreleased] section. Trimmed
to the user-visible outcome #6293 still owns; the new bullet carries the
registration story.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
---------
Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
482 lines
14 KiB
Go
482 lines
14 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/steveyegge/beads/internal/config"
|
|
"github.com/steveyegge/beads/internal/metrics"
|
|
"github.com/steveyegge/beads/internal/remotecache"
|
|
"github.com/steveyegge/beads/internal/storage"
|
|
"github.com/steveyegge/beads/internal/types"
|
|
)
|
|
|
|
var repoCmd = &cobra.Command{
|
|
Use: "repo",
|
|
GroupID: "advanced",
|
|
Short: "Manage multiple repository configuration",
|
|
Long: `Configure and manage multiple repository support for multi-repo hydration.
|
|
|
|
Multi-repo support allows hydrating issues from multiple beads repositories
|
|
into a single database for unified cross-repo issue tracking.
|
|
|
|
Configuration is stored in .beads/config.yaml under the 'repos' section:
|
|
|
|
repos:
|
|
primary: "."
|
|
additional:
|
|
- ~/beads-planning
|
|
- ~/work-repo
|
|
|
|
Examples:
|
|
bd repo add ~/beads-planning # Add planning repo
|
|
bd repo add ../other-repo # Add relative path repo
|
|
bd repo list # Show all configured repos
|
|
bd repo remove ~/beads-planning # Remove by path
|
|
bd repo sync # Sync from all configured repos`,
|
|
}
|
|
|
|
var repoAddCmd = &cobra.Command{
|
|
Use: "add <path>",
|
|
Short: "Add an additional repository to sync",
|
|
Long: `Add a repository path to the repos.additional list in config.yaml.
|
|
|
|
The path should point to a directory containing a .beads folder.
|
|
Paths can be absolute or relative (they are stored as-is).
|
|
|
|
This modifies .beads/config.yaml, which is version-controlled and
|
|
shared across all clones of this repository.`,
|
|
Args: cobra.ExactArgs(1),
|
|
SilenceUsage: true,
|
|
SilenceErrors: true,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
if usesProxiedServer() {
|
|
return HandleErrorRespectJSON("repo add is not supported in proxied-server mode")
|
|
}
|
|
evt := metrics.NewCommandEvent("repo-add")
|
|
defer func() {
|
|
if c := metrics.Global(); c != nil {
|
|
c.CloseEventAndAdd(evt)
|
|
}
|
|
}()
|
|
|
|
repoPath := args[0]
|
|
|
|
if remotecache.IsRemoteURL(repoPath) {
|
|
fmt.Fprintf(os.Stderr, "Adding remote repository: %s\n", repoPath)
|
|
} else {
|
|
expandedPath := repoPath
|
|
if len(repoPath) > 0 && repoPath[0] == '~' {
|
|
home, err := os.UserHomeDir()
|
|
if err == nil {
|
|
expandedPath = filepath.Join(home, repoPath[1:])
|
|
}
|
|
}
|
|
|
|
beadsDir := filepath.Join(expandedPath, ".beads")
|
|
if _, err := os.Stat(beadsDir); os.IsNotExist(err) {
|
|
return HandleError("no beads workspace found at %s", expandedPath)
|
|
}
|
|
}
|
|
|
|
configPath, err := config.FindConfigYAMLPath()
|
|
if err != nil {
|
|
return HandleError("failed to find config.yaml: %v", err)
|
|
}
|
|
|
|
if err := config.AddRepo(configPath, repoPath); err != nil {
|
|
return HandleError("failed to add repository: %v", err)
|
|
}
|
|
|
|
if jsonOutput {
|
|
result := map[string]interface{}{
|
|
"added": true,
|
|
"path": repoPath,
|
|
}
|
|
return json.NewEncoder(os.Stdout).Encode(result)
|
|
}
|
|
|
|
fmt.Printf("Added repository: %s\n", repoPath)
|
|
fmt.Printf("Run 'bd repo sync' to hydrate issues from this repository.\n")
|
|
return nil
|
|
},
|
|
}
|
|
|
|
var repoRemoveCmd = &cobra.Command{
|
|
Use: "remove <path>",
|
|
Short: "Remove a repository from sync configuration",
|
|
Long: `Remove a repository path from the repos.additional list in config.yaml.
|
|
|
|
The path must exactly match what was added (e.g., if you added "~/foo",
|
|
you must remove "~/foo", not "/home/user/foo").
|
|
|
|
This command also removes any previously-hydrated issues from the database
|
|
that came from the removed repository.`,
|
|
Args: cobra.ExactArgs(1),
|
|
SilenceUsage: true,
|
|
SilenceErrors: true,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
if usesProxiedServer() {
|
|
return HandleErrorRespectJSON("repo remove is not supported in proxied-server mode")
|
|
}
|
|
evt := metrics.NewCommandEvent("repo-remove")
|
|
defer func() {
|
|
if c := metrics.Global(); c != nil {
|
|
c.CloseEventAndAdd(evt)
|
|
}
|
|
}()
|
|
|
|
repoPath := args[0]
|
|
|
|
if err := ensureDirectMode("repo remove requires direct database access"); err != nil {
|
|
return HandleError("%v", err)
|
|
}
|
|
|
|
ctx := rootCtx
|
|
|
|
deletedCount, err := store.DeleteIssuesBySourceRepo(ctx, repoPath)
|
|
if err != nil {
|
|
return HandleError("failed to delete issues from repo: %v", err)
|
|
}
|
|
|
|
if err := store.ClearRepoMtime(ctx, repoPath); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Warning: failed to clear mtime cache: %v\n", err)
|
|
}
|
|
|
|
configPath, err := config.FindConfigYAMLPath()
|
|
if err != nil {
|
|
return HandleError("failed to find config.yaml: %v", err)
|
|
}
|
|
|
|
if err := config.RemoveRepo(configPath, repoPath); err != nil {
|
|
return HandleError("failed to remove repository: %v", err)
|
|
}
|
|
|
|
// Evict remote cache if applicable
|
|
if remotecache.IsRemoteURL(repoPath) {
|
|
if cache, err := remotecache.DefaultCache(); err == nil {
|
|
_ = cache.Evict(repoPath)
|
|
}
|
|
}
|
|
|
|
commandDidWrite.Store(true)
|
|
|
|
if jsonOutput {
|
|
result := map[string]interface{}{
|
|
"removed": true,
|
|
"path": repoPath,
|
|
"issues_deleted": deletedCount,
|
|
}
|
|
return json.NewEncoder(os.Stdout).Encode(result)
|
|
}
|
|
|
|
fmt.Printf("Removed repository: %s\n", repoPath)
|
|
if deletedCount > 0 {
|
|
fmt.Printf("Deleted %d issue(s) from the database\n", deletedCount)
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
|
|
var repoListCmd = &cobra.Command{
|
|
Use: "list",
|
|
Short: "List all configured repositories",
|
|
Long: `List all repositories configured in .beads/config.yaml.
|
|
|
|
Shows the primary repository (always ".") and any additional
|
|
repositories configured for hydration.`,
|
|
SilenceUsage: true,
|
|
SilenceErrors: true,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
if usesProxiedServer() {
|
|
return HandleErrorRespectJSON("repo list is not supported in proxied-server mode")
|
|
}
|
|
evt := metrics.NewCommandEvent("repo-list")
|
|
defer func() {
|
|
if c := metrics.Global(); c != nil {
|
|
c.CloseEventAndAdd(evt)
|
|
}
|
|
}()
|
|
|
|
configPath, err := config.FindConfigYAMLPath()
|
|
if err != nil {
|
|
return HandleError("failed to find config.yaml: %v", err)
|
|
}
|
|
|
|
repos, err := config.ListRepos(configPath)
|
|
if err != nil {
|
|
return HandleError("failed to load config: %v", err)
|
|
}
|
|
|
|
if jsonOutput {
|
|
primary := repos.Primary
|
|
if primary == "" {
|
|
primary = "."
|
|
}
|
|
result := map[string]interface{}{
|
|
"primary": primary,
|
|
"additional": repos.Additional,
|
|
}
|
|
return json.NewEncoder(os.Stdout).Encode(result)
|
|
}
|
|
|
|
primary := repos.Primary
|
|
if primary == "" {
|
|
primary = "."
|
|
}
|
|
fmt.Printf("Primary repository: %s\n", primary)
|
|
if len(repos.Additional) == 0 {
|
|
fmt.Println("No additional repositories configured")
|
|
} else {
|
|
fmt.Println("\nAdditional repositories:")
|
|
for _, path := range repos.Additional {
|
|
fmt.Printf(" - %s\n", path)
|
|
}
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
|
|
var repoSyncCmd = &cobra.Command{
|
|
Use: "sync",
|
|
Short: "Manually trigger multi-repo sync",
|
|
Long: `Synchronize issues from all configured additional repositories.
|
|
|
|
Reads issues.jsonl from each additional repository and imports them into
|
|
the primary database with their original prefixes and source_repo set.
|
|
Uses mtime caching to skip repos whose JSONL hasn't changed.
|
|
|
|
Also triggers Dolt push/pull if a remote is configured.`,
|
|
SilenceUsage: true,
|
|
SilenceErrors: true,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
if usesProxiedServer() {
|
|
return HandleErrorRespectJSON("repo sync is not supported in proxied-server mode")
|
|
}
|
|
evt := metrics.NewCommandEvent("repo-sync")
|
|
defer func() {
|
|
if c := metrics.Global(); c != nil {
|
|
c.CloseEventAndAdd(evt)
|
|
}
|
|
}()
|
|
|
|
if err := ensureDirectMode("repo sync requires direct database access"); err != nil {
|
|
return HandleError("%v", err)
|
|
}
|
|
|
|
ctx := rootCtx
|
|
verbose, _ := cmd.Flags().GetBool("verbose")
|
|
|
|
configPath, err := config.FindConfigYAMLPath()
|
|
if err != nil {
|
|
return HandleError("failed to find config.yaml: %v", err)
|
|
}
|
|
|
|
repos, err := config.ListRepos(configPath)
|
|
if err != nil {
|
|
return HandleError("failed to load repo config: %v", err)
|
|
}
|
|
|
|
totalImported := 0
|
|
totalSkipped := 0
|
|
|
|
// Hydrate issues from each additional repository
|
|
for _, repoPath := range repos.Additional {
|
|
// Remote URL: pull into cache, read issues from SQL store
|
|
if remotecache.IsRemoteURL(repoPath) {
|
|
cache, err := remotecache.DefaultCache()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Warning: failed to init cache for %s: %v\n", repoPath, err)
|
|
continue
|
|
}
|
|
if _, err = cache.Ensure(ctx, repoPath); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Warning: failed to sync remote %s: %v\n", repoPath, err)
|
|
continue
|
|
}
|
|
remoteStore, err := cache.OpenStore(ctx, repoPath, newDoltStoreFromConfig)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Warning: failed to open remote store %s: %v\n", repoPath, err)
|
|
continue
|
|
}
|
|
|
|
issues, err := remoteStore.SearchIssues(ctx, "", types.IssueFilter{})
|
|
_ = remoteStore.Close() // close eagerly — defer in a loop would leak connections
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Warning: failed to read issues from %s: %v\n", repoPath, err)
|
|
continue
|
|
}
|
|
|
|
for _, issue := range issues {
|
|
issue.SourceRepo = repoPath
|
|
}
|
|
if len(issues) > 0 {
|
|
if importErr := store.CreateIssuesWithFullOptions(ctx, issues, "repo-sync", storage.BatchCreateOptions{
|
|
SkipPrefixValidation: true,
|
|
}); importErr != nil {
|
|
fmt.Fprintf(os.Stderr, "Warning: failed to import from %s: %v\n", repoPath, importErr)
|
|
continue
|
|
}
|
|
totalImported += len(issues)
|
|
if verbose {
|
|
fmt.Fprintf(os.Stderr, "Imported %d issue(s) from remote %s\n", len(issues), repoPath)
|
|
}
|
|
}
|
|
continue
|
|
}
|
|
|
|
// Local path: expand tilde
|
|
expandedPath := repoPath
|
|
if len(repoPath) > 0 && repoPath[0] == '~' {
|
|
home, err := os.UserHomeDir()
|
|
if err == nil {
|
|
expandedPath = filepath.Join(home, repoPath[1:])
|
|
}
|
|
}
|
|
|
|
// Resolve to absolute path for consistent mtime caching
|
|
absPath, err := filepath.Abs(expandedPath)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Warning: failed to resolve path %s: %v\n", repoPath, err)
|
|
continue
|
|
}
|
|
|
|
jsonlPath := filepath.Join(absPath, ".beads", "issues.jsonl")
|
|
info, err := os.Stat(jsonlPath)
|
|
if err != nil {
|
|
if verbose {
|
|
fmt.Fprintf(os.Stderr, "Skipping %s: no issues.jsonl found\n", repoPath)
|
|
}
|
|
continue
|
|
}
|
|
|
|
// Check mtime cache — skip if JSONL hasn't changed
|
|
currentMtime := info.ModTime().UnixNano()
|
|
cachedMtime, _ := store.GetRepoMtime(ctx, absPath)
|
|
if cachedMtime == currentMtime {
|
|
if verbose {
|
|
fmt.Fprintf(os.Stderr, "Skipping %s: JSONL unchanged\n", repoPath)
|
|
}
|
|
totalSkipped++
|
|
continue
|
|
}
|
|
|
|
// Parse issues from JSONL
|
|
issues, err := parseIssuesFromJSONL(jsonlPath)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Warning: failed to parse %s: %v\n", jsonlPath, err)
|
|
continue
|
|
}
|
|
|
|
if len(issues) == 0 {
|
|
if verbose {
|
|
fmt.Fprintf(os.Stderr, "Skipping %s: no issues in JSONL\n", repoPath)
|
|
}
|
|
continue
|
|
}
|
|
|
|
// Set source_repo on all imported issues
|
|
for _, issue := range issues {
|
|
issue.SourceRepo = repoPath
|
|
}
|
|
|
|
// Import with prefix validation skipped (cross-prefix hydration)
|
|
if err := store.CreateIssuesWithFullOptions(ctx, issues, "repo-sync", storage.BatchCreateOptions{
|
|
SkipPrefixValidation: true,
|
|
}); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Warning: failed to import from %s: %v\n", repoPath, err)
|
|
continue
|
|
}
|
|
|
|
// Update mtime cache
|
|
if err := store.SetRepoMtime(ctx, absPath, jsonlPath, currentMtime); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Warning: failed to update mtime cache for %s: %v\n", repoPath, err)
|
|
}
|
|
|
|
totalImported += len(issues)
|
|
if verbose {
|
|
fmt.Fprintf(os.Stderr, "Imported %d issue(s) from %s\n", len(issues), repoPath)
|
|
}
|
|
}
|
|
|
|
// Push is handled by periodic sync, not per-operation.
|
|
// Manual push available via: bd dolt push
|
|
|
|
if totalImported > 0 {
|
|
commandDidWrite.Store(true)
|
|
}
|
|
|
|
if jsonOutput {
|
|
result := map[string]interface{}{
|
|
"synced": true,
|
|
"repos_synced": len(repos.Additional) - totalSkipped,
|
|
"repos_skipped": totalSkipped,
|
|
"issues_imported": totalImported,
|
|
}
|
|
return json.NewEncoder(os.Stdout).Encode(result)
|
|
}
|
|
|
|
if totalImported > 0 {
|
|
fmt.Printf("Multi-repo sync complete: imported %d issue(s) from %d repo(s)\n",
|
|
totalImported, len(repos.Additional)-totalSkipped)
|
|
} else if totalSkipped == len(repos.Additional) {
|
|
fmt.Println("Multi-repo sync complete: all repos up to date")
|
|
} else {
|
|
fmt.Println("Multi-repo sync complete")
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
|
|
// parseIssuesFromJSONL reads and parses issues from a JSONL file.
|
|
func parseIssuesFromJSONL(path string) ([]*types.Issue, error) {
|
|
// #nosec G304 -- path comes from user-configured repos.additional in config.yaml
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to open JSONL: %w", err)
|
|
}
|
|
defer f.Close()
|
|
|
|
var issues []*types.Issue
|
|
scanner := bufio.NewScanner(f)
|
|
// Allow up to 10MB per line (large issues with embedded content)
|
|
scanner.Buffer(make([]byte, 0, 64*1024), 10*1024*1024)
|
|
|
|
lineNum := 0
|
|
for scanner.Scan() {
|
|
lineNum++
|
|
line := scanner.Bytes()
|
|
if len(line) == 0 {
|
|
continue
|
|
}
|
|
|
|
var issue types.Issue
|
|
if err := json.Unmarshal(line, &issue); err != nil {
|
|
return nil, fmt.Errorf("failed to parse issue at line %d: %w", lineNum, err)
|
|
}
|
|
if issue.ID == "" {
|
|
continue // Skip malformed entries
|
|
}
|
|
issues = append(issues, &issue)
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
return nil, fmt.Errorf("failed to read JSONL: %w", err)
|
|
}
|
|
|
|
return issues, nil
|
|
}
|
|
|
|
func init() {
|
|
repoCmd.AddCommand(repoAddCmd)
|
|
repoCmd.AddCommand(repoRemoveCmd)
|
|
repoCmd.AddCommand(repoListCmd)
|
|
repoCmd.AddCommand(repoSyncCmd)
|
|
|
|
repoSyncCmd.Flags().Bool("verbose", false, "Show detailed sync progress")
|
|
|
|
rootCmd.AddCommand(repoCmd)
|
|
}
|