perf(git): cache git rev-parse results within sessions

Multiple gt commands call git rev-parse --show-toplevel, adding ~50ms
each invocation. Results rarely change within a session, and multiple
agents calling git concurrently contend on .git/index.lock.

Add cached RepoRoot() and RepoRootFrom() functions to the git package
and update all callers to use them. This ensures a single git subprocess
call per process for the common case of checking the current directory's
repo root.

Files updated:
- internal/git/git.go: Add RepoRoot() and RepoRootFrom()
- internal/cmd/prime.go: Use cached git.RepoRoot()
- internal/cmd/molecule_status.go: Use cached git.RepoRoot()
- internal/cmd/sling_helpers.go: Use cached git.RepoRoot()
- internal/cmd/rig_quick_add.go: Use git.RepoRootFrom() for path arg
- internal/version/stale.go: Use cached git.RepoRoot()

Closes: bd-2zd.5
This commit is contained in:
onyx
2026-01-24 17:22:17 -08:00
committed by John Ogle
parent 04ddb2c3bd
commit 6202d783c2
6 changed files with 57 additions and 26 deletions

View File

@@ -4,13 +4,13 @@ import (
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/spf13/cobra"
"github.com/steveyegge/gastown/internal/beads"
"github.com/steveyegge/gastown/internal/config"
"github.com/steveyegge/gastown/internal/git"
"github.com/steveyegge/gastown/internal/style"
"github.com/steveyegge/gastown/internal/workspace"
)
@@ -963,13 +963,9 @@ func outputMoleculeCurrent(info MoleculeCurrentInfo) error {
}
// getGitRootForMolStatus returns the git root for hook file lookup.
// Uses cached value to avoid repeated git subprocess calls.
func getGitRootForMolStatus() (string, error) {
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
out, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(out)), nil
return git.RepoRoot()
}
// isTownLevelRole returns true if the agent ID is a town-level role.

View File

@@ -11,6 +11,7 @@ import (
"github.com/spf13/cobra"
"github.com/steveyegge/gastown/internal/beads"
"github.com/steveyegge/gastown/internal/git"
"github.com/steveyegge/gastown/internal/lock"
"github.com/steveyegge/gastown/internal/state"
"github.com/steveyegge/gastown/internal/style"
@@ -544,13 +545,9 @@ func buildRoleAnnouncement(ctx RoleContext) string {
}
// getGitRoot returns the root of the current git repository.
// Uses cached value to avoid repeated git subprocess calls.
func getGitRoot() (string, error) {
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
out, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(out)), nil
return git.RepoRoot()
}
// getAgentIdentity returns the agent identity string for hook lookup.

View File

@@ -11,6 +11,7 @@ import (
"strings"
"github.com/spf13/cobra"
"github.com/steveyegge/gastown/internal/git"
"github.com/steveyegge/gastown/internal/style"
"github.com/steveyegge/gastown/internal/workspace"
)
@@ -138,13 +139,7 @@ func runRigQuickAdd(cmd *cobra.Command, args []string) error {
}
func findGitRoot(path string) (string, error) {
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
cmd.Dir = path
out, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(out)), nil
return git.RepoRootFrom(path)
}
func findGitRemoteURL(gitRoot string) (string, error) {

View File

@@ -10,6 +10,7 @@ import (
"github.com/steveyegge/gastown/internal/beads"
"github.com/steveyegge/gastown/internal/constants"
"github.com/steveyegge/gastown/internal/git"
"github.com/steveyegge/gastown/internal/tmux"
"github.com/steveyegge/gastown/internal/workspace"
)
@@ -378,13 +379,13 @@ func ensureAgentReady(sessionName string) error {
}
// detectCloneRoot finds the root of the current git clone.
// Uses cached value to avoid repeated git subprocess calls.
func detectCloneRoot() (string, error) {
cmd := exec.Command("git", "rev-parse", "--show-toplevel")
out, err := cmd.Output()
root, err := git.RepoRoot()
if err != nil {
return "", fmt.Errorf("not in a git repository")
}
return strings.TrimSpace(string(out)), nil
return root, nil
}
// detectActor returns the current agent's actor string for event logging.