fix(init): verify refs/dolt/data before remote-history refusal (#5136)
* 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.
This commit is contained in:
parent
9200e3d5d1
commit
5ff61baef3
3 changed files with 171 additions and 26 deletions
|
|
@ -874,6 +874,7 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1):
|
|||
var earlySyncURL string
|
||||
earlyRemoteSource := initSyncRemoteNone
|
||||
earlyRemoteHasDoltData := false
|
||||
var earlyProbeNote string
|
||||
earlySyncURL, earlyRemoteSource = resolveInitConfiguredSyncRemote(initRemote, initRemoteChanged, resolveSyncRemote)
|
||||
if earlyRemoteSource == initSyncRemoteExplicit {
|
||||
// An explicit --remote is intent to bootstrap or wire that URL,
|
||||
|
|
@ -882,17 +883,27 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1):
|
|||
// source and skips cloning, so we must probe now rather than
|
||||
// silently wiring a populated remote to orphan local history.
|
||||
if fromJSONL {
|
||||
earlyRemoteHasDoltData = gitRemoteHasDoltDataRef(earlySyncURL)
|
||||
hasData, err := gitRemoteHasDoltDataRefStatus(earlySyncURL)
|
||||
earlyRemoteHasDoltData, earlyProbeNote = resolveRemoteHasDoltDataProbe(earlySyncURL, hasData, err)
|
||||
}
|
||||
} else if earlyRemoteSource == initSyncRemoteConfigured {
|
||||
earlyRemoteHasDoltData = true // sync.remote configured = user intends bootstrap
|
||||
// Probe refs/dolt/data — do NOT treat mere presence of
|
||||
// sync.remote as proof of remote history (GH#4861). A git
|
||||
// remote with only ordinary branches must not refuse
|
||||
// --reinit-local / local init.
|
||||
hasData, err := gitRemoteHasDoltDataRefStatus(earlySyncURL)
|
||||
earlyRemoteHasDoltData, earlyProbeNote = resolveRemoteHasDoltDataProbe(earlySyncURL, hasData, err)
|
||||
} else if earlyRemoteSource == initSyncRemoteNone && !stealth && isGitRepo() && !isBareGitRepo() {
|
||||
if originURL, err := gitOriginGetURL(); err == nil && originURL != "" {
|
||||
earlySyncURL = normalizeRemoteURL(originURL)
|
||||
earlyRemoteHasDoltData = gitOriginHasDoltDataRef()
|
||||
hasData, probeErr := gitOriginHasDoltDataRefStatus()
|
||||
earlyRemoteHasDoltData, earlyProbeNote = resolveRemoteHasDoltDataProbe(earlySyncURL, hasData, probeErr)
|
||||
}
|
||||
}
|
||||
if earlySyncURL != "" {
|
||||
if earlyProbeNote != "" {
|
||||
fmt.Fprintf(os.Stderr, "%s %s\n", ui.RenderWarn("!"), earlyProbeNote)
|
||||
}
|
||||
earlyDecision := CheckRemoteSafety(RemoteSafetyInput{
|
||||
Force: force,
|
||||
ReinitLocal: reinitLocal,
|
||||
|
|
@ -903,14 +914,12 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1):
|
|||
RemoteHasDoltData: earlyRemoteHasDoltData,
|
||||
IsInteractive: term.IsTerminal(int(os.Stdin.Fd())),
|
||||
})
|
||||
if _, err := handleRemoteSafetyDecision(earlyDecision, prefix, earlySyncURL, destroyToken, func() bool {
|
||||
if _, err := handleRemoteSafetyDecision(earlyDecision, prefix, earlySyncURL, destroyToken, func() (bool, error) {
|
||||
switch earlyRemoteSource {
|
||||
case initSyncRemoteExplicit:
|
||||
return gitRemoteHasDoltDataRef(earlySyncURL)
|
||||
case initSyncRemoteConfigured:
|
||||
return earlyRemoteHasDoltData
|
||||
case initSyncRemoteExplicit, initSyncRemoteConfigured:
|
||||
return gitRemoteHasDoltDataRefStatus(earlySyncURL)
|
||||
default:
|
||||
return gitOriginHasDoltDataRef()
|
||||
return gitOriginHasDoltDataRefStatus()
|
||||
}
|
||||
}, earlyRemoteHasDoltData, &remoteDivergenceConfirmed); err != nil {
|
||||
// The early guard refuses and confirms only; bootstrap
|
||||
|
|
@ -1205,6 +1214,7 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1):
|
|||
syncFromRemote := false
|
||||
syncURLFromGitOrigin := false
|
||||
remoteHasDoltData := false
|
||||
var lateProbeNote string
|
||||
|
||||
if syncURL != "" {
|
||||
// sync.remote was explicitly configured. Treat it as bootstrap-
|
||||
|
|
@ -1216,10 +1226,14 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1):
|
|||
// configured explicitly by the user (GH#3339).
|
||||
if syncRemoteSource == initSyncRemoteExplicit && !fromJSONL {
|
||||
syncFromRemote = true
|
||||
} else if syncRemoteSource == initSyncRemoteConfigured {
|
||||
remoteHasDoltData = true
|
||||
} else if syncRemoteSource == initSyncRemoteExplicit {
|
||||
remoteHasDoltData = gitRemoteHasDoltDataRef(syncURL)
|
||||
} else if syncRemoteSource == initSyncRemoteConfigured || syncRemoteSource == initSyncRemoteExplicit {
|
||||
// Always verify refs/dolt/data rather than assuming history
|
||||
// from a configured URL alone (GH#4861).
|
||||
hasData, err := gitRemoteHasDoltDataRefStatus(syncURL)
|
||||
remoteHasDoltData, lateProbeNote = resolveRemoteHasDoltDataProbe(syncURL, hasData, err)
|
||||
}
|
||||
if lateProbeNote != "" {
|
||||
fmt.Fprintf(os.Stderr, "%s %s\n", ui.RenderWarn("!"), lateProbeNote)
|
||||
}
|
||||
if !syncFromRemote {
|
||||
decision := CheckRemoteSafety(RemoteSafetyInput{
|
||||
|
|
@ -1232,22 +1246,29 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1):
|
|||
RemoteHasDoltData: remoteHasDoltData,
|
||||
IsInteractive: term.IsTerminal(int(os.Stdin.Fd())),
|
||||
})
|
||||
bootstrap, err := handleRemoteSafetyDecision(decision, prefix, syncURL, destroyToken, func() bool {
|
||||
if syncRemoteSource == initSyncRemoteExplicit {
|
||||
return gitRemoteHasDoltDataRef(syncURL)
|
||||
}
|
||||
return remoteHasDoltData
|
||||
bootstrap, err := handleRemoteSafetyDecision(decision, prefix, syncURL, destroyToken, func() (bool, error) {
|
||||
return gitRemoteHasDoltDataRefStatus(syncURL)
|
||||
}, remoteHasDoltData, &remoteDivergenceConfirmed)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
syncFromRemote = bootstrap
|
||||
if !bootstrap && decision.Action == ActionNoRemoteData && !quiet {
|
||||
// Skipping the clone (because the probe came back clean)
|
||||
// loses the explanation cloneFromRemoteWithMode used to
|
||||
// print via isEmptyRemoteCloneError. Say it here instead.
|
||||
fmt.Printf(" %s Remote has no Dolt data yet; initialized a fresh local database\n", ui.RenderWarn("!"))
|
||||
}
|
||||
}
|
||||
} else if syncRemoteSource == initSyncRemoteNone && !stealth && isGitRepo() && !isBareGitRepo() {
|
||||
if originURL, err := gitOriginGetURL(); err == nil && originURL != "" {
|
||||
syncURL = normalizeRemoteURL(originURL)
|
||||
syncURLFromGitOrigin = true
|
||||
remoteHasDoltData = gitOriginHasDoltDataRef()
|
||||
hasData, probeErr := gitOriginHasDoltDataRefStatus()
|
||||
remoteHasDoltData, lateProbeNote = resolveRemoteHasDoltDataProbe(syncURL, hasData, probeErr)
|
||||
if lateProbeNote != "" {
|
||||
fmt.Fprintf(os.Stderr, "%s %s\n", ui.RenderWarn("!"), lateProbeNote)
|
||||
}
|
||||
|
||||
decision := CheckRemoteSafety(RemoteSafetyInput{
|
||||
Force: force,
|
||||
|
|
@ -1260,7 +1281,7 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1):
|
|||
IsInteractive: term.IsTerminal(int(os.Stdin.Fd())),
|
||||
})
|
||||
|
||||
bootstrap, err := handleRemoteSafetyDecision(decision, prefix, syncURL, destroyToken, gitOriginHasDoltDataRef, remoteHasDoltData, &remoteDivergenceConfirmed)
|
||||
bootstrap, err := handleRemoteSafetyDecision(decision, prefix, syncURL, destroyToken, gitOriginHasDoltDataRefStatus, remoteHasDoltData, &remoteDivergenceConfirmed)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -2953,6 +2974,17 @@ func shouldWriteInitDoltRemote(gateway bool, syncURL string, syncFromRemote, syn
|
|||
return !gateway && shouldConfigureInitDoltRemote(syncURL, syncFromRemote, syncURLFromConfig, syncURLFromGitOrigin, localOnly)
|
||||
}
|
||||
|
||||
// resolveRemoteHasDoltDataProbe fails closed: a probe error is UNKNOWN, and
|
||||
// ADR-0002 treats unknown as has-data (refuse) rather than no-data. Returns
|
||||
// the resolved bool for CheckRemoteSafety plus a note naming the probe
|
||||
// failure (empty on success) so the caller can tell the user why.
|
||||
func resolveRemoteHasDoltDataProbe(syncURL string, hasData bool, err error) (bool, string) {
|
||||
if err != nil {
|
||||
return true, fmt.Sprintf("could not verify refs/dolt/data on %s (%v); treating remote as having Dolt history", syncURL, err)
|
||||
}
|
||||
return hasData, ""
|
||||
}
|
||||
|
||||
// handleRemoteSafetyDecision applies a CheckRemoteSafety decision at an init
|
||||
// remote-divergence checkpoint. It returns (bootstrap, err): bootstrap is true
|
||||
// when the caller should clone/bootstrap from the remote, and err is a non-nil
|
||||
|
|
@ -2962,7 +2994,7 @@ func shouldWriteInitDoltRemote(gateway bool, syncURL string, syncFromRemote, syn
|
|||
// metrics CloseEventAndAdd only fires on a normal return. Every refusal path
|
||||
// returns an *exitError so the caller can propagate it up through RunE and keep
|
||||
// the usage-metrics close intact (see errors.go).
|
||||
func handleRemoteSafetyDecision(decision RemoteSafetyDecision, prefix, syncURL, destroyToken string, remoteHasDoltData func() bool, observedRemoteHasDoltData bool, confirmed *bool) (bool, error) {
|
||||
func handleRemoteSafetyDecision(decision RemoteSafetyDecision, prefix, syncURL, destroyToken string, remoteHasDoltData func() (bool, error), observedRemoteHasDoltData bool, confirmed *bool) (bool, error) {
|
||||
switch decision.Action {
|
||||
case ActionRefuseDivergence, ActionRequireDestroyToken:
|
||||
fmt.Fprintf(os.Stderr, "\n%s\n\n", decision.UserMessage)
|
||||
|
|
@ -2983,9 +3015,14 @@ func handleRemoteSafetyDecision(decision RemoteSafetyDecision, prefix, syncURL,
|
|||
}
|
||||
*confirmed = true
|
||||
}
|
||||
if remoteHasDoltData != nil && remoteHasDoltData() != observedRemoteHasDoltData {
|
||||
fmt.Fprintf(os.Stderr, "\nAborted: remote state changed during confirmation. Re-run to re-verify intent.\n")
|
||||
return false, &exitError{Code: ExitRemoteDivergenceRefused}
|
||||
if remoteHasDoltData != nil {
|
||||
// A probe error here is UNKNOWN, not "changed" — a transient
|
||||
// network blip on the confirmation probe must not abort a
|
||||
// destroy-token flow the user already confirmed.
|
||||
if current, err := remoteHasDoltData(); err == nil && current != observedRemoteHasDoltData {
|
||||
fmt.Fprintf(os.Stderr, "\nAborted: remote state changed during confirmation. Re-run to re-verify intent.\n")
|
||||
return false, &exitError{Code: ExitRemoteDivergenceRefused}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
|
|
|
|||
|
|
@ -19,6 +19,103 @@ import (
|
|||
"github.com/steveyegge/beads/internal/config"
|
||||
)
|
||||
|
||||
// hermeticInitEnv sets sync.remote via BD_SYNC_REMOTE (viper AutomaticEnv,
|
||||
// config.go:165-169), stripped of inherited BD_*/BEADS_* vars and
|
||||
// HOME-isolated so ambient ~/.beads or ~/.config/bd config can't leak in.
|
||||
func hermeticInitEnv(homeDir string, extra ...string) []string {
|
||||
var env []string
|
||||
for _, e := range os.Environ() {
|
||||
if strings.HasPrefix(e, "BD_") || strings.HasPrefix(e, "BEADS_") {
|
||||
continue
|
||||
}
|
||||
env = append(env, e)
|
||||
}
|
||||
env = append(env, "HOME="+homeDir, "GIT_CONFIG_GLOBAL=/dev/null", "GIT_CONFIG_SYSTEM=/dev/null")
|
||||
return append(env, extra...)
|
||||
}
|
||||
|
||||
// TestInitReinitLocalConfiguredRemoteWithoutDoltDataSucceeds is the
|
||||
// caller-level regression test for GH#4861: sync.remote (via BD_SYNC_REMOTE,
|
||||
// not --remote, to hit initSyncRemoteConfigured) has no refs/dolt/data, so
|
||||
// `bd init --reinit-local` must succeed rather than refuse. Supersedes
|
||||
// TestCheckRemoteSafety_ConfiguredRemoteWithoutDoltData, which called the
|
||||
// unmodified CheckRemoteSafety directly and tested nothing about this fix
|
||||
// (both its cases are already in TestCheckRemoteSafety_GuardMatrix).
|
||||
func TestInitReinitLocalConfiguredRemoteWithoutDoltDataSucceeds(t *testing.T) {
|
||||
bdBin := buildBDForInitTests(t)
|
||||
|
||||
bareDir := filepath.Join(t.TempDir(), "bare.git")
|
||||
runGitForBootstrapTest(t, "", "init", "--bare", bareDir)
|
||||
// bareDir has ordinary git plumbing but no refs/dolt/data (GH#4861 shape).
|
||||
|
||||
workDir := t.TempDir()
|
||||
runGitForBootstrapTest(t, workDir, "init", "-b", "main")
|
||||
runGitForBootstrapTest(t, workDir, "config", "core.hooksPath", ".git/hooks")
|
||||
|
||||
homeDir := t.TempDir() // must differ from workDir: metrics.go caches to $HOME/.beads
|
||||
|
||||
cmd := exec.Command(bdBin, "init", "--reinit-local", "--prefix", "cfg", "--quiet", "--non-interactive", "--skip-hooks", "--skip-agents")
|
||||
cmd.Dir = workDir
|
||||
cmd.Env = hermeticInitEnv(homeDir, "BD_SYNC_REMOTE="+bareDir)
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stderr = &stderr
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Fatalf("bd init --reinit-local with a Dolt-data-free sync.remote failed: %v\nstderr:\n%s", err, stderr.String())
|
||||
}
|
||||
|
||||
beadsDir := filepath.Join(workDir, ".beads")
|
||||
if _, err := os.Stat(beadsDir); err != nil {
|
||||
t.Fatalf(".beads should have been created by a successful init: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestInitReinitLocalConfiguredRemoteWithDoltDataRefuses is the sibling case:
|
||||
// sync.remote (again via BD_SYNC_REMOTE, not --remote) points at a bare repo
|
||||
// that DOES have refs/dolt/data pushed to it. `bd init --reinit-local` must
|
||||
// still refuse with ExitRemoteDivergenceRefused (10) — this is the ADR-0002
|
||||
// invariant the GH#4861 fix must not weaken while closing the false positive.
|
||||
func TestInitReinitLocalConfiguredRemoteWithDoltDataRefuses(t *testing.T) {
|
||||
bdBin := buildBDForInitTests(t)
|
||||
|
||||
bareDir := filepath.Join(t.TempDir(), "bare.git")
|
||||
runGitForBootstrapTest(t, "", "init", "--bare", bareDir)
|
||||
|
||||
sourceDir := t.TempDir()
|
||||
runGitForBootstrapTest(t, sourceDir, "init", "-b", "main")
|
||||
runGitForBootstrapTest(t, sourceDir, "config", "user.email", "test@test.com")
|
||||
runGitForBootstrapTest(t, sourceDir, "config", "user.name", "Test User")
|
||||
runGitForBootstrapTest(t, sourceDir, "commit", "--allow-empty", "-m", "init")
|
||||
runGitForBootstrapTest(t, sourceDir, "remote", "add", "origin", bareDir)
|
||||
runGitForBootstrapTest(t, sourceDir, "push", "origin", "HEAD:refs/dolt/data")
|
||||
|
||||
workDir := t.TempDir()
|
||||
runGitForBootstrapTest(t, workDir, "init", "-b", "main")
|
||||
runGitForBootstrapTest(t, workDir, "config", "core.hooksPath", ".git/hooks")
|
||||
homeDir := t.TempDir() // see collision note in the sibling test above
|
||||
|
||||
cmd := exec.Command(bdBin, "init", "--reinit-local", "--prefix", "cfg", "--quiet", "--non-interactive", "--skip-hooks", "--skip-agents")
|
||||
cmd.Dir = workDir
|
||||
cmd.Env = hermeticInitEnv(homeDir, "BD_SYNC_REMOTE="+bareDir)
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stderr = &stderr
|
||||
err := cmd.Run()
|
||||
if err == nil {
|
||||
t.Fatalf("bd init --reinit-local succeeded; expected refusal (sync.remote has Dolt history). stderr:\n%s", stderr.String())
|
||||
}
|
||||
exitErr, ok := err.(*exec.ExitError)
|
||||
if !ok {
|
||||
t.Fatalf("non-exec-exit error: %v\nstderr:\n%s", err, stderr.String())
|
||||
}
|
||||
if code := exitErr.ExitCode(); code != ExitRemoteDivergenceRefused {
|
||||
t.Errorf("exit code = %d, want %d (ExitRemoteDivergenceRefused)", code, ExitRemoteDivergenceRefused)
|
||||
}
|
||||
|
||||
beadsDir := filepath.Join(workDir, ".beads")
|
||||
if _, err := os.Stat(beadsDir); err == nil {
|
||||
t.Errorf("refusal should not have created %s", beadsDir)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckRemoteSafety_GuardMatrix(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
|
|
|
|||
|
|
@ -102,15 +102,26 @@ func gitOriginHasDoltDataRef() bool {
|
|||
}
|
||||
|
||||
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
|
||||
return false, fmt.Errorf("probe refs/dolt/data on %s: %w", remote, err)
|
||||
}
|
||||
return strings.TrimSpace(string(output)) != ""
|
||||
return strings.TrimSpace(string(output)) != "", nil
|
||||
}
|
||||
|
||||
func gitRemoteURLForLsRemote(remote string) string {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue