* fix(init): verify refs/dolt/data before remote-history refusal bd init --reinit-local refused solely because sync.remote was set, claiming the remote had Dolt history without checking refs/dolt/data. Git remotes with only ordinary branches tripped a false positive and stranded workspaces with no way to set issue-prefix (GH#4861). Probe gitRemoteHasDoltDataRef for configured sync.remote in both early and late safety checks; re-probe on confirmation callbacks. Fixes #4861 * fix(init): tri-state the refs/dolt/data probe so a failed check refuses gitRemoteHasDoltDataRef collapsed "no refs/dolt/data" and "could not ask" (timeout, DNS, credential refusal) into the same false, so a probe failure silently fell through ADR-0002's refusal gate instead of tripping it. Add gitRemoteHasDoltDataRefStatus / gitOriginHasDoltDataRefStatus returning (bool, error), and route all four init.go call sites (both the new sync.remote probe from GH#4861 and the two pre-existing git-origin probes) through resolveRemoteHasDoltDataProbe, which treats a probe error as UNKNOWN and fails closed (refuse) rather than open. The confirmation re-probe in handleRemoteSafetyDecision now treats a probe error as "unchanged" too, so only a genuine true->false transition aborts — a transient network blip no longer spuriously kills a confirmed --discard-remote flow. Replace TestCheckRemoteSafety_ConfiguredRemoteWithoutDoltData, which called the pure CheckRemoteSafety directly and passed whether or not init.go actually probed anything, with a caller-level regression test: bd init --reinit-local against a real bare-repo sync.remote, with and without a pushed refs/dolt/data ref. Also restore the "Remote has no Dolt data yet; initialized a fresh local database" message on the configured-remote/no-history path, which the GH#4861 fix silently dropped by skipping cloneFromRemoteWithMode. Addresses maphew's review on #5136. * test(init): make the sync.remote regression test hermetic BD_SYNC_REMOTE works (viper AutomaticEnv maps BD_-prefixed vars to config keys, config.go:165-169 — confirmed empirically via `bd config get sync.remote`), but the test relied on the inherited environment: any ambient ~/.beads/config.yaml, ~/.config/bd/config.yaml, or stray BD_*/ BEADS_* var on the machine running the test could leak in unnoticed. Add hermeticInitEnv: strips inherited BD_*/BEADS_* vars and points HOME at an isolated tempdir (distinct from workDir — metrics.go caches to $HOME/.beads/eventsData, which collides with the project .beads/ this test asserts on if HOME == workDir). Verified under `env -i` with an isolated HOME/XDG_CONFIG_HOME and no inherited env: without BD_SYNC_REMOTE, init succeeds; with it pointed at a Dolt-data-free bare repo, only the pre-#4861-fix binary refuses (the actual bug); with it pointed at a repo with pushed refs/dolt/data, both pre- and post-fix binaries correctly refuse.
158 lines
5.1 KiB
Go
158 lines
5.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/steveyegge/beads/internal/beads"
|
|
"github.com/steveyegge/beads/internal/doltremote"
|
|
)
|
|
|
|
// isGitRepo checks if the current working directory is in a git repository.
|
|
// NOTE: This intentionally checks CWD, not the beads repo. It's used as a guard
|
|
// before calling other git functions to prevent hangs on Windows (GH#727).
|
|
// Does not use RepoContext because it's a prerequisite check for git availability.
|
|
func isGitRepo() bool {
|
|
cmd := exec.Command("git", "rev-parse", "--git-dir")
|
|
return cmd.Run() == nil
|
|
}
|
|
|
|
// isBareGitRepo checks if the current git repository is bare.
|
|
// Returns false when not in a git repository.
|
|
func isBareGitRepo() bool {
|
|
cmd := exec.Command("git", "rev-parse", "--is-bare-repository")
|
|
output, err := cmd.Output()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return strings.TrimSpace(string(output)) == "true"
|
|
}
|
|
|
|
// gitHasUpstream checks if the current branch has an upstream configured in the beads repository.
|
|
// Uses RepoContext to ensure git commands run in the correct repository.
|
|
// Uses git config directly for compatibility with Git for Windows.
|
|
func gitHasUpstream() bool {
|
|
rc, err := beads.GetRepoContext()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
ctx := context.Background()
|
|
// Get current branch name
|
|
branchCmd := rc.GitCmd(ctx, "symbolic-ref", "--short", "HEAD")
|
|
branchOutput, err := branchCmd.Output()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
branch := strings.TrimSpace(string(branchOutput))
|
|
|
|
return gitBranchHasUpstream(branch)
|
|
}
|
|
|
|
// gitHasAnyRemotes returns true if the git repository has any remotes configured.
|
|
// Used to distinguish between "new repo with no remotes" and "repo with origin but no upstream".
|
|
func gitHasAnyRemotes() bool {
|
|
rc, err := beads.GetRepoContext()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
ctx := context.Background()
|
|
remoteCmd := rc.GitCmd(ctx, "remote")
|
|
output, err := remoteCmd.Output()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return strings.TrimSpace(string(output)) != ""
|
|
}
|
|
|
|
// gitOriginGetURL returns the URL for the origin git remote.
|
|
func gitOriginGetURL() (string, error) {
|
|
cmd := exec.Command("git", "remote", "get-url", "origin")
|
|
output, err := cmd.Output()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return strings.TrimSpace(string(output)), nil
|
|
}
|
|
|
|
func gitOriginGetURLForActiveRepo(ctx context.Context) (string, error) {
|
|
rc, err := beads.GetRepoContext()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
cmd := rc.GitCmd(ctx, "remote", "get-url", "origin")
|
|
output, err := cmd.Output()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return strings.TrimSpace(string(output)), nil
|
|
}
|
|
|
|
// gitOriginHasDoltDataRef checks if origin has refs/dolt/data.
|
|
// Returns false on any error (network, no remote, timeout, etc).
|
|
// Uses a 10s timeout since this is a network call used for auto-detection,
|
|
// and suppresses credential prompts to avoid blocking on SSH remotes.
|
|
func gitOriginHasDoltDataRef() bool {
|
|
return gitRemoteHasDoltDataRef("origin")
|
|
}
|
|
|
|
func gitRemoteHasDoltDataRef(remote string) bool {
|
|
hasData, err := gitRemoteHasDoltDataRefStatus(remote)
|
|
return err == nil && hasData
|
|
}
|
|
|
|
// gitOriginHasDoltDataRefStatus is the tri-state form: no data vs. unknown.
|
|
func gitOriginHasDoltDataRefStatus() (bool, error) {
|
|
return gitRemoteHasDoltDataRefStatus("origin")
|
|
}
|
|
|
|
// A non-nil error means UNKNOWN, not "no data" — the bool is meaningless.
|
|
func gitRemoteHasDoltDataRefStatus(remote string) (bool, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
cmd := exec.CommandContext(ctx, "git", "ls-remote", gitRemoteURLForLsRemote(remote), "refs/dolt/data")
|
|
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
|
|
output, err := cmd.Output()
|
|
if err != nil {
|
|
return false, fmt.Errorf("probe refs/dolt/data on %s: %w", remote, err)
|
|
}
|
|
return strings.TrimSpace(string(output)) != "", nil
|
|
}
|
|
|
|
func gitRemoteURLForLsRemote(remote string) string {
|
|
return strings.TrimPrefix(remote, "git+")
|
|
}
|
|
|
|
// gitURLToDoltRemote converts a git remote URL to dolt's remote format.
|
|
// HTTPS URLs get "git+" prefix: https://... → git+https://...
|
|
// SCP-style SSH URLs are converted: git@host:path → git+ssh://git@host/path
|
|
// SSH URLs get "git+" prefix: ssh://... → git+ssh://...
|
|
// URLs that already have "git+" prefix are returned as-is.
|
|
func gitURLToDoltRemote(url string) string {
|
|
return doltremote.FromGitURL(url)
|
|
}
|
|
|
|
// gitBranchHasUpstream checks if a specific branch has an upstream configured.
|
|
// Unlike gitHasUpstream(), this works even when HEAD is detached (e.g., jj/jujutsu).
|
|
// Uses RepoContext to ensure git commands run in the correct repository.
|
|
func gitBranchHasUpstream(branch string) bool {
|
|
rc, err := beads.GetRepoContext()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
|
|
ctx := context.Background()
|
|
// Check if remote and merge refs are configured for the branch
|
|
remoteCmd := rc.GitCmd(ctx, "config", "--get", fmt.Sprintf("branch.%s.remote", branch)) //nolint:gosec // G204: branch from caller
|
|
mergeCmd := rc.GitCmd(ctx, "config", "--get", fmt.Sprintf("branch.%s.merge", branch)) //nolint:gosec // G204: branch from caller
|
|
|
|
remoteErr := remoteCmd.Run()
|
|
mergeErr := mergeCmd.Run()
|
|
|
|
return remoteErr == nil && mergeErr == nil
|
|
}
|