* fix(bootstrap): probe git remotes for dolt data instead of rejecting them `bd init` derives sync.remote from the git origin and persists it to the git-tracked .beads/config.yaml — by design, because the first `bd dolt push` creates refs/dolt/data on that same git remote. `bd bootstrap` then rejected exactly that value: isGitCodeRepoURL classified any .git-suffixed or known-forge URL as "not a Dolt remote" and returned Action=none *before* the refs/dolt/data origin auto-detect that would have hydrated the clone. Every fresh clone of such a repo got no database, and deleting the sync.remote line by hand was the only way out (#5743, #5663). The #4421 CPU storm this guard was added for is not a property of "the URL points at a forge". Dolt's dbfactory routes by scheme: a raw http(s):// URL goes to the remotesapi client, which speaks its wire protocol at github.com and retries forever; a git+https/git+ssh URL goes to the git remote factory, which shells out to git and fails cleanly. So the classifier is a good router and a bad rejector. Route on it instead: when sync.remote classifies as a forge URL, normalize it to its git+ form and probe refs/dolt/data with the existing 10s-bounded `git ls-remote` helper, then - data present: Action=sync from the git+ URL, never the raw form - definitively absent: note it and fall through to origin auto-detect, backup, issues.jsonl and fresh init, matching `bd init`'s own "remote has no Dolt data yet" behavior - probe error (UNKNOWN): Action=none with a new Blocked flag; never clone something we could not verify Non-forge remotes (dolthub://, s3://, remotesapi http(s) endpoints, self-hosted git+ssh) keep the trusted-as-is path untouched, so GH#3339 still holds: normalization now happens only inside the forge branch, where a remotesapi endpoint cannot appear. Blocked/BlockedRemote are additive JSON fields; the exit-code half of the fix follows in the next commit. Refs #5743, #5663 * fix(bootstrap): exit non-zero when bootstrap declines to set up a database Action=="none" was two different outcomes wearing one name: "a database already exists, nothing to do" and "bootstrap refused and left you with no database". printBootstrapPlan printed "✓ Database already exists" for both, and RunE returned nil for both — so a rejected sync.remote reported success, exited 0, and left the user with `bd list` saying "no beads database found" (#5743, #5663). The split was already encoded in the plan: every genuine no-op sets HasExisting. Use it. - printBootstrapPlan reserves the success tick for HasExisting plans and otherwise reports the reason on stderr. Blocked plans additionally get a runnable `git ls-remote <url> refs/dolt/data` hint (the git+ prefix is stripped, as the probe itself strips it) and a pointer at the sync.remote line in config.yaml. - bootstrapPlanOutcome maps the plan to an exit status in one place, so text, --json and --dry-run all agree. JSON mode returns SilentExit, since the plan (carrying "blocked": true) is already on stdout. - The "Could not verify existing server database" branch is marked Blocked for the same reason: it also declined without confirming a database, and it also false-ticked. Refs #5743, #5663 * fix(init): normalize git-forge sync.remote URLs before cloning Init never had bootstrap's forge guard: a configured `sync.remote: https://github.com/org/repo.git` was passed to DOLT_CLONE verbatim, which Dolt routes to the remotesapi client — the same retry storm (#4421) the bootstrap guard exists to prevent. Route it the same way bootstrap now does: forge URLs are converted to their git+ form, which selects the git remote factory, and everything else is left byte-identical so a user-configured remotesapi endpoint (http://myserver:7007/mydb) is never rewritten (GH#3339). Also documents, at the two places someone would go looking after a bootstrap failure, why init deliberately persists a plain git origin as sync.remote: bootstrap probes refs/dolt/data to resolve it (#5743), so un-persisting here would be the wrong fix and would strand every already-committed .beads/config.yaml. This commit is independent of the bootstrap fix and can be dropped from a release cherry-pick if the minimum diff is wanted. Refs #5743, #5663, #4421 * fix(sync): thread git's stderr into the refs/dolt/data probe error The probe wrapped only the exec error, and exec.ExitError.Error() is just "exit status 128" — git's actual complaint sits unread in ExitError.Stderr, which cmd.Output() had already captured. A context timeout was worse still: "signal: killed". Since bootstrap's blocked message is built from this error, the fix's primary failure path told users nothing that distinguished bad credentials from DNS from a deleted repository. Now the error carries git's own first stderr line ("Permission denied (publickey)", "Repository not found", "Invalid username or token") and names a timeout as a timeout. Also drops the remote and ref name from the wrapper. Every caller already names the remote it probed, so wrapping here too produced messages that said both twice: `... on sync.remote "X": probe refs/dolt/data on X: exit status 128`. Refs #5743 * fix(bootstrap): derive the blocked verdict once; keep local recovery reachable Review round on #6037. Every change below fixes a confirmed defect in this PR's own new code. State leak (the #5743 bug, reintroduced). existingBootstrapDBPlan's "none" verdict was copied into the working plan, so every later branch inherited its HasExisting/Blocked/Reason. A probe error on top of a seeded HasExisting=true produced action=none + blocked=true + has_existing=true, which printed the success tick and exited 0; a seeded server-verify error leaked blocked=true into a *successful* sync plan's JSON, violating the field's own contract that nothing clones from a blocked plan. The verdict is now held aside as an explicit fallback instead of merged in, and Blocked is derived in one place from the finished plan rather than hand-set by any branch. Local recovery. A probe error used to return immediately, so an offline laptop or credential-less CI runner was denied the backup/JSONL restore that the same machine would have got if the probe had merely answered "no data". Restoring a local backup touches no remote: the fail-closed rule now applies only to remote actions, and the block is emitted at the tail, after every local source has been tried. Origin auto-detect is skipped once sync.remote is configured. sync.remote is the authoritative remote, so hydrating from whatever git origin happens to be — a fork, an old experiment, the code repo under the dedicated-data-repo pattern — is not recovery, and finalizeSyncedBootstrap would then have rewritten the team's committed sync.remote to point at it. This also removes the second identical ls-remote in the flagship pre-first-push case (init persists the git origin as sync.remote, so the two legs were probing the same repository for the same ref), and makes the fall-through tests hermetic: the origin leg is not behind the injectable probe seam. Remote wiring. The fall-through made executeInitAction reachable with a configured sync.remote for the first time, and it never registered the remote as Dolt "origin". The promise that "the first bd dolt push creates refs/dolt/data on that same git remote" then breaks: push either refuses to adopt a remote without consent or adopts the git origin — a different repository — and rewrites the committed sync.remote to match. Two machines bootstrapping in the pre-push window would mint independent Dolt identities and diverge. The provisioning actions now wire the routed remote, as the sync action and bd init already did. bd init gets the same routing on its own wiring call: its no-Dolt-data-yet path never clones, so a raw https forge URL was stored verbatim in dolt_remotes and the *first* push took it to the remotesapi client — #4421, one leg further down. --dry-run always exits 0. bd doctor documents `bd bootstrap --dry-run` as the safe inspection step, and a preview that aborts a set -e provisioning script is a worse contract than a preview that merely reports. The blocked state is still printed and still in the JSON, so the preview does predict what a real run would refuse to do. Credentials. sync.remote is commonly an https URL with an embedded CI token. The reason, the hint and the blocked_remote JSON field all echoed it verbatim into logs and agent transcripts; so did the sync plan's "Remote:" line. All display paths now go through redactRemoteURL, which drops http(s) userinfo whole and keeps only the account selector for ssh so the hint stays runnable. Hints. The old remediation could name a config file that does not hold the key (resolveSyncRemote reads a merged scope and a deprecated alias), and following it was actively harmful: deleting sync.remote and re-running hits the same auth failure on the git-origin probe, which swallows its error, so bootstrap would "succeed" by creating a fresh database diverged from the team's history. The hints now reproduce the probe, point at access, and offer `bd dolt remote add` — never deletion. One owner for the routing rule. doltRemoteURL (sync_remote.go) is now the single definition of "classify, then normalize forge URLs only". It replaces initCloneURL and the copy this PR had inlined into detectBootstrapAction: if bd init and bd bootstrap ever disagree about the URL derived from one committed sync.remote, that is the #5743 class of skew all over again. Tests: blocked-with-HasExisting and blocked-leaking-into-sync (neither was covered), probe-error-still-restores/imports, credential scrubbing on both the plan and the printed output, dry-run exit status, the real probe's stderr thread-through, and redactRemoteURL itself. The probe-error fixture now uses the error shape production actually produces instead of a fabricated one, the printer tests use the shared mutexed capture helpers, and the "#4421 pin" comment is rescoped: it pins the routing, not the classifier, which still has an unlisted-forge gap tracked as a follow-up. Refs #5743, #5663, #4421 * fix(bootstrap): redact --json credentials, scrub git stderr, cut detect complexity Maintainer fixup addressing PR #6037 review findings. - Add BootstrapPlan.MarshalJSON so `bd bootstrap --json` (and `--dry-run --json`) redacts the credential-bearing sync_remote on a copy, matching the human print path. Closes the deterministic token leak to stdout while leaving the raw plan executeBootstrapPlan clones from untouched. - Scrub credential-bearing URLs from the folded `git ls-remote` stderr at the source (scrubURLCredentials in gitLsRemoteProbeError) so a tokenized URL git echoes cannot reach plan.Reason, which flows to both stderr and JSON. - Reduce detectBootstrapPlan cyclomatic 22->12 and cognitive 27->13 by extracting planConfiguredSyncRemote, gitOriginSyncPlan, and localFileRecoveryPlan. Detection order and plan-mutation semantics are preserved (a forge remote with no Dolt data still records SyncRemote so a local-recovery workspace stays wired to it). - Drop the dead `plan.Blocked = true` in existingBootstrapDBPlan; the wrapper derives Blocked once from Action=="none" && !HasExisting. Tests: assert the token is absent from the plan JSON (and MarshalJSON does not mutate the source plan), and that tokenized git stderr is scrubbed while credential-free diagnostics pass through unchanged. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
201 lines
6.9 KiB
Go
201 lines
6.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"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.
|
|
//
|
|
// The error names the failure, not the remote: every caller already names the
|
|
// remote it probed, and wrapping here too produced messages that said the URL
|
|
// and the ref twice ("...on sync.remote \"X\": probe refs/dolt/data on X: exit
|
|
// status 128").
|
|
func gitRemoteHasDoltDataRefStatus(remote string) (bool, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), gitDoltDataProbeTimeout)
|
|
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, gitLsRemoteProbeError(ctx, err)
|
|
}
|
|
return strings.TrimSpace(string(output)) != "", nil
|
|
}
|
|
|
|
const gitDoltDataProbeTimeout = 10 * time.Second
|
|
|
|
// gitLsRemoteProbeError turns a failed `git ls-remote` into something a user
|
|
// can act on. exec.ExitError.Error() is only "exit status 128" — git's actual
|
|
// complaint (auth vs. DNS vs. deleted repo) sits unread in ExitError.Stderr
|
|
// because cmd.Output() captures it there, and a context timeout surfaces as
|
|
// the equally opaque "signal: killed".
|
|
func gitLsRemoteProbeError(ctx context.Context, err error) error {
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|
return fmt.Errorf("git ls-remote did not answer within %s (network, or an SSH key that wants a passphrase)", gitDoltDataProbeTimeout)
|
|
}
|
|
var exitErr *exec.ExitError
|
|
if errors.As(err, &exitErr) {
|
|
// git can echo the remote it failed to reach — including a CI token in
|
|
// https://x-access-token:<token>@host form — so scrub credentials before
|
|
// this detail reaches plan.Reason, which flows to both stderr and
|
|
// `bd bootstrap --json`. exitErr itself is only "exit status 128".
|
|
if detail := scrubURLCredentials(firstNonEmptyLine(string(exitErr.Stderr))); detail != "" {
|
|
return fmt.Errorf("git ls-remote: %v: %s", exitErr, detail)
|
|
}
|
|
}
|
|
return fmt.Errorf("git ls-remote: %w", err)
|
|
}
|
|
|
|
// firstNonEmptyLine returns the first meaningful line of git's stderr. git
|
|
// prefixes most failures with a "remote:"/"fatal:" line that already says
|
|
// everything; the rest is usually a hint block we do not want inside a
|
|
// single-line Reason.
|
|
func firstNonEmptyLine(s string) string {
|
|
for _, line := range strings.Split(s, "\n") {
|
|
if trimmed := strings.TrimSpace(line); trimmed != "" {
|
|
return trimmed
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
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
|
|
}
|