* perf(git): locate the repository in-process instead of running git rev-parse Every bd invocation resolved the process-wide git context (beads directory discovery reads it) with `git rev-parse --git-dir --git-common-dir --show-toplevel`: one git subprocess per bd call before any work. Tools that call bd hundreds of times (gascity's doctor issues ~250 bd calls per run) paid it on every call. On Linux, initGitContext now first runs discoverGitInProcess, which mirrors Git's discovery walk (physical cwd, .git as directory or gitfile, bare-dir check, filesystem-boundary stop) and produces byte-for-byte what rev-parse prints for ordinary repositories (top and subdirectories), linked worktrees (absolute or relative gitfile, symlinked targets), submodules (core.worktree pointing back at the gitfile's directory), nested repositories and no repository. Both answers feed one shared normalization (gitContextFromRevParse), so downstream values are unchanged. It declines, and git runs exactly as before, whenever it cannot prove the answer: GIT_DIR / GIT_WORK_TREE / GIT_COMMON_DIR / GIT_CEILING_DIRECTORIES / GIT_DISCOVERY_ACROSS_FILESYSTEM / GIT_OBJECT_DIRECTORY / GIT_CONFIG_PARAMETERS / GIT_CONFIG_COUNT in the environment; a symlinked .git, an invalid gitfile or git directory, a cwd inside a git directory; repository config with core.worktree (other than the submodule shape), core.bare, extensions, include directives, a format version above 1 or unmodeled syntax; any path not owned by the effective uid (safe.directory is git's to evaluate), or root under sudo. darwin and windows keep the subprocess (case folding and ownership semantics). Tests compare discoverGitFrom with real `git rev-parse` output across those layouts, pin every decline case, and check the resulting cached context equals the subprocess-derived one. Agent-Signature: claude-code-claude-opus-5-5-medium on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * perf(cli): resolve the git user.name actor fallback only when needed PersistentPreRunE assigned `actor = getActorWithGit()` for every command, which, with no --actor / BEADS_ACTOR / BD_ACTOR / config actor, runs `git config user.name`: the second git subprocess of every bd call, paid by read-only commands that never consult the actor. The pre-run now calls deferActorGitFallback, which marks an empty actor as pending; currentActor (and getActorWithGit, getActor) resolve it on first use with the unchanged priority order and store it in the global and the CommandContext copy, as the eager assignment did. Command code reads the actor through currentActor() instead of the raw global (a mechanical rewrite of every read site the compiler reported; writes, the flag binding and the pre-run config binding keep the raw variable). setActor cancels a pending fallback. The telemetry span's bd.actor attribute is still set, but only for a recording span, so a noop span does not force the lookup. With the in-process repository discovery from the previous commit, a read-only `bd list` / `bd show` now starts no git process at startup, in the main checkout and in a linked worktree; a write asks git for user.name once (bd create previously asked once, now still once) and records the same value. Measured on 50 sequential `bd list` calls: 100 -> 0 git execs (embedded and proxied-server workspaces, main checkout and worktree). TestEmbeddedReadOnlyCommandsSpawnNoGit counts git execs through a PATH shim; TestDeferredActorGitFallback pins the deferral semantics. Agent-Signature: claude-code-claude-opus-5-5-medium on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * fix(git): decline in-process discovery on any config line git might read differently Review follow-ups to the in-process repository discovery. - The repository config scanner was looser than git: a config starting with a UTF-8 BOM followed by `[core] worktree = /other` made git report /other while discovery returned the cwd repository, and `[ core ]` or `!!! = x` (which git refuses) were accepted. scanGitConfig (gitconfig_linux.go) now skips a leading BOM exactly as git does and accepts only a narrow grammar: blank/comment lines, `[section]` / `[section "sub"]` headers at column 0, and `key` / `key = value` lines inside a section, with values free of backslashes and unbalanced quotes. Any other line declines to git. - Walk from getcwd(2) (syscall.Getwd), as git does, instead of os.Getwd, which trusts $PWD when it names the same inode (bind-mount aliases). - Resolve relative gitfile and commondir targets without filepath.Join's lexical Clean, so "link/.." resolves physically like git's realpath. - A mount-boundary stop reports its own reason in the not-a-repository error. - Tests: every decline case (BOM + core.worktree, malformed headers and keys, keys before a section, escapes, continuations, bare repositories, a commondir inside .git, ...) now also checks that what startup derives equals git's answer; BOM and CRLF/comment configs are equivalence cases; the no-repository case must be answered in-process instead of skipping; a $PWD alias test pins the getcwd walk. - The exec-count test drops every GIT_* variable except the config-file selectors, so a runner exporting GIT_CONFIG_PARAMETERS & co. cannot make it fail spuriously. - Comment the two places that touch the raw actor before the deferral. Agent-Signature: claude-code-claude-opus-5-5-medium on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * perf(backup): decide embedded auto-backup's git-remote probe without git In an embedded workspace whose backup.enabled is unset, maybeAutoBackup asks isBackupAutoEnabled -> primeHasGitRemote after every command, reads included. That built a RepoContext (`git -C .beads rev-parse --git-common-dir`) and ran `git remote`: two git processes per read even after the startup calls were gone. The throttle cannot short-circuit it: without a remote no backup ever runs, so there is no backup state to throttle on. Both probes are now answered in-process when provable, and the subprocess remains the fallback, so the decision and every warning/notice downstream of it are unchanged: - internal/beads getGitCommonDirForPath uses git.CommonDirInProcess (the discovery walk from the given directory, git's spelling). - primeHasGitRemote uses git.HasRemoteInProcess in the same directory primeGitCmd would run git in (primeGitDir, factored out). It answers only when every config file `git remote` reads -- system (GIT_CONFIG_SYSTEM, or /etc/gitconfig and <prefix>/etc/gitconfig of the git on PATH), global (GIT_CONFIG_GLOBAL, or XDG and ~/.gitconfig) and the repository's own -- is absent or within scanGitConfig's grammar and include-free, no GIT_CONFIG_PARAMETERS/COUNT/GIT_CONFIG or discovery override is set, and no legacy .git/remotes or .git/branches entries exist. A remote seen only in a system-config candidate declines. Remote names outside [A-Za-z0-9._-] decline. Tests compare HasRemoteInProcess with `git remote` (no remote, local, worktree, submodule, remote.pushDefault only, global remote, no repo) and pin the declines (global include, malformed global, command-line config); CommonDirInProcess is compared with `git -C dir rev-parse --git-common-dir`. TestEmbeddedReadOnlyCommandsSpawnNoGit now runs with backup.enabled at its default, with and without a git remote, and still expects zero git processes for list/show. Agent-Signature: claude-code-claude-opus-5-5-medium on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
217 lines
6 KiB
Go
217 lines
6 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/steveyegge/beads/internal/metrics"
|
|
"github.com/steveyegge/beads/internal/storage"
|
|
"github.com/steveyegge/beads/internal/types"
|
|
"github.com/steveyegge/beads/internal/ui"
|
|
"github.com/steveyegge/beads/internal/utils"
|
|
)
|
|
|
|
var relateCmd = &cobra.Command{
|
|
Use: "relate <id1> <id2>",
|
|
Short: "Create a bidirectional relates_to link between issues",
|
|
Long: `Create a loose 'see also' relationship between two issues.
|
|
|
|
The relates_to link is bidirectional - both issues will reference each other.
|
|
This enables knowledge graph connections without blocking or hierarchy.
|
|
|
|
Examples:
|
|
bd relate bd-abc bd-xyz # Link two related issues
|
|
bd relate bd-123 bd-456 # Create see-also connection`,
|
|
Args: cobra.ExactArgs(2),
|
|
RunE: runRelate,
|
|
}
|
|
|
|
var unrelateCmd = &cobra.Command{
|
|
Use: "unrelate <id1> <id2>",
|
|
Short: "Remove a relates_to link between issues",
|
|
Long: `Remove a relates_to relationship between two issues.
|
|
|
|
Removes the link in both directions.
|
|
|
|
Example:
|
|
bd unrelate bd-abc bd-xyz`,
|
|
Args: cobra.ExactArgs(2),
|
|
RunE: runUnrelate,
|
|
}
|
|
|
|
func init() {
|
|
// Issue ID completions
|
|
relateCmd.ValidArgsFunction = issueIDCompletion
|
|
unrelateCmd.ValidArgsFunction = issueIDCompletion
|
|
|
|
// Add as subcommands of dep
|
|
depCmd.AddCommand(relateCmd)
|
|
depCmd.AddCommand(unrelateCmd)
|
|
}
|
|
|
|
func runRelate(cmd *cobra.Command, args []string) error {
|
|
CheckReadonly("relate")
|
|
|
|
evt := metrics.NewCommandEvent("relate")
|
|
defer func() {
|
|
if c := metrics.Global(); c != nil {
|
|
c.CloseEventAndAdd(evt)
|
|
}
|
|
}()
|
|
|
|
ctx := rootCtx
|
|
|
|
if usesProxiedServer() {
|
|
return runRelateProxiedServer(ctx, args)
|
|
}
|
|
|
|
// Resolve partial IDs
|
|
var id1, id2 string
|
|
var err error
|
|
id1, err = utils.ResolvePartialID(ctx, store, args[0])
|
|
if err != nil {
|
|
return fmt.Errorf("failed to resolve %s: %w", args[0], err)
|
|
}
|
|
id2, err = utils.ResolvePartialID(ctx, store, args[1])
|
|
if err != nil {
|
|
return fmt.Errorf("failed to resolve %s: %w", args[1], err)
|
|
}
|
|
|
|
if id1 == id2 {
|
|
return fmt.Errorf("cannot relate an issue to itself")
|
|
}
|
|
|
|
// Get both issues
|
|
var issue1, issue2 *types.Issue
|
|
issue1, err = store.GetIssue(ctx, id1)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get issue %s: %w", id1, err)
|
|
}
|
|
issue2, err = store.GetIssue(ctx, id2)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get issue %s: %w", id2, err)
|
|
}
|
|
|
|
if issue1 == nil {
|
|
return fmt.Errorf("issue not found: %s", id1)
|
|
}
|
|
if issue2 == nil {
|
|
return fmt.Errorf("issue not found: %s", id2)
|
|
}
|
|
|
|
// Add relates-to dependency: id1 -> id2 (bidirectional, so also id2 -> id1)
|
|
// Per Decision 004, relates-to links are now stored in dependencies table
|
|
// Add id1 -> id2
|
|
dep1 := &types.Dependency{
|
|
IssueID: id1,
|
|
DependsOnID: id2,
|
|
Type: types.DepRelatesTo,
|
|
}
|
|
// bd relate is an explicit dependency verb, so it records history like
|
|
// bd link / bd dep add (EmitEvent); only structural edge wiring stays silent.
|
|
if err := store.AddDependencyWithOptions(ctx, dep1, currentActor(), storage.DependencyAddOptions{EmitEvent: true}); err != nil {
|
|
return fmt.Errorf("failed to add relates-to %s -> %s: %w", id1, id2, err)
|
|
}
|
|
// Add id2 -> id1 (bidirectional)
|
|
dep2 := &types.Dependency{
|
|
IssueID: id2,
|
|
DependsOnID: id1,
|
|
Type: types.DepRelatesTo,
|
|
}
|
|
if err := store.AddDependencyWithOptions(ctx, dep2, currentActor(), storage.DependencyAddOptions{EmitEvent: true}); err != nil {
|
|
return fmt.Errorf("failed to add relates-to %s -> %s: %w", id2, id1, err)
|
|
}
|
|
|
|
if jsonOutput {
|
|
result := map[string]interface{}{
|
|
"id1": id1,
|
|
"id2": id2,
|
|
"related": true,
|
|
}
|
|
encoder := json.NewEncoder(os.Stdout)
|
|
encoder.SetIndent("", " ")
|
|
return encoder.Encode(result)
|
|
}
|
|
|
|
fmt.Printf("%s Linked %s ↔ %s\n", ui.RenderPass("✓"), id1, id2)
|
|
return nil
|
|
}
|
|
|
|
func runUnrelate(cmd *cobra.Command, args []string) error {
|
|
CheckReadonly("unrelate")
|
|
|
|
evt := metrics.NewCommandEvent("unrelate")
|
|
defer func() {
|
|
if c := metrics.Global(); c != nil {
|
|
c.CloseEventAndAdd(evt)
|
|
}
|
|
}()
|
|
|
|
ctx := rootCtx
|
|
|
|
if usesProxiedServer() {
|
|
return runUnrelateProxiedServer(ctx, args)
|
|
}
|
|
|
|
// Resolve partial IDs
|
|
var id1, id2 string
|
|
var err error
|
|
id1, err = utils.ResolvePartialID(ctx, store, args[0])
|
|
if err != nil {
|
|
return fmt.Errorf("failed to resolve %s: %w", args[0], err)
|
|
}
|
|
id2, err = utils.ResolvePartialID(ctx, store, args[1])
|
|
if err != nil {
|
|
return fmt.Errorf("failed to resolve %s: %w", args[1], err)
|
|
}
|
|
|
|
// Get both issues
|
|
var issue1, issue2 *types.Issue
|
|
issue1, err = store.GetIssue(ctx, id1)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get issue %s: %w", id1, err)
|
|
}
|
|
issue2, err = store.GetIssue(ctx, id2)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get issue %s: %w", id2, err)
|
|
}
|
|
|
|
if issue1 == nil {
|
|
return fmt.Errorf("issue not found: %s", id1)
|
|
}
|
|
if issue2 == nil {
|
|
return fmt.Errorf("issue not found: %s", id2)
|
|
}
|
|
|
|
// Remove relates-to dependency in both directions
|
|
// Per Decision 004, relates-to links are now stored in dependencies table.
|
|
// bd unrelate is an explicit dependency verb, so it records history
|
|
// (EmitEvent) like bd dep remove; only structural teardown stays silent.
|
|
// Remove id1 -> id2
|
|
if err := store.RemoveDependencyWithOptions(ctx, id1, id2, currentActor(), storage.DependencyRemoveOptions{EmitEvent: true}); err != nil {
|
|
return fmt.Errorf("failed to remove relates-to %s -> %s: %w", id1, id2, err)
|
|
}
|
|
// Remove id2 -> id1 (bidirectional)
|
|
if err := store.RemoveDependencyWithOptions(ctx, id2, id1, currentActor(), storage.DependencyRemoveOptions{EmitEvent: true}); err != nil {
|
|
return fmt.Errorf("failed to remove relates-to %s -> %s: %w", id2, id1, err)
|
|
}
|
|
|
|
if jsonOutput {
|
|
result := map[string]interface{}{
|
|
"id1": id1,
|
|
"id2": id2,
|
|
"unrelated": true,
|
|
}
|
|
encoder := json.NewEncoder(os.Stdout)
|
|
encoder.SetIndent("", " ")
|
|
return encoder.Encode(result)
|
|
}
|
|
|
|
fmt.Printf("%s Unlinked %s ↔ %s\n", ui.RenderPass("✓"), id1, id2)
|
|
return nil
|
|
}
|
|
|
|
// Note: contains, remove, formatRelatesTo functions removed per Decision 004
|
|
// relates-to links now use dependencies API instead of Issue.RelatesTo field
|