* fix(init): refuse --force when remote has Dolt history bd init --force in a repo whose origin has refs/dolt/data silently skipped the bootstrap-from-remote path and then wired origin as a Dolt remote anyway. The next write auto-pushed an orphan history, failing with "no common ancestor". Recovery options were all destructive. This change: - Adds --reinit-local (named-intent replacement for --force) and --discard-remote (explicit cross-boundary authorization). - Routes --force to --reinit-local with a DeprecationWarning. --force continues to work for ≥2 releases. - Refuses bd init --force / --reinit-local when origin has refs/dolt/data, unless --discard-remote is also passed. Exit code 10 (ExitRemoteDivergenceRefused). - Adds stable numeric exit codes for init refusals (10/11/12). - Adds bd init-safety documenting the full flag surface and the destroy-token format. - Adds an internal race-safety re-check: if remote state changes during the TTY confirmation window, abort rather than overwrite. - Retrofits the existing destroy-token echo at init.go:231 to not print a copy-pasteable destructive invocation, matching the new error-text-no-echo ADR invariant. Adds: - cmd/bd/init_safety.go — CheckRemoteSafety chokepoint (pure fn). - cmd/bd/init_safety_test.go — guard matrix (13 cases) + subprocess regression test with a synthetic refs/dolt/data fixture. - docs/adr/0002-init-safety-invariants.md — ADR encoding the five invariants (single-source identity, scope-bound --force, central chokepoint, error-text-no-echo, race-safety). - docs/RECOVERY.md — named recovery playbooks per refusal exit code. - .github/CODEOWNERS — review ownership for init*.go pointing at the ADR so future flags must extend the guard matrix. Historical pattern (git log for cmd/bd/init.go): 8+ prior commits patched individual surfaces of this class without encoding the invariant. The guard matrix test is the structural hedge — adding a new flag or data source is a signal to extend the table, not to add another `&& !someFlag` at the call site. (bd-q83) * fix(init): satisfy lint for origin safety helpers (bd-main-nwo) --------- Co-authored-by: matt wilkie <maphew@gmail.com>
139 lines
4.5 KiB
Go
139 lines
4.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/steveyegge/beads/internal/beads"
|
|
)
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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 {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
cmd := exec.CommandContext(ctx, "git", "ls-remote", "origin", "refs/dolt/data")
|
|
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
|
|
output, err := cmd.Output()
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return strings.TrimSpace(string(output)) != ""
|
|
}
|
|
|
|
// 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 {
|
|
if strings.HasPrefix(url, "git+") {
|
|
return url
|
|
}
|
|
if strings.HasPrefix(url, "https://") || strings.HasPrefix(url, "http://") {
|
|
return "git+" + url
|
|
}
|
|
if strings.HasPrefix(url, "ssh://") {
|
|
return "git+" + url
|
|
}
|
|
// SCP-style: git@github.com:org/repo.git → git+ssh://git@github.com/org/repo.git
|
|
if idx := strings.Index(url, ":"); idx > 0 && !strings.Contains(url[:idx], "/") {
|
|
return "git+ssh://" + url[:idx] + "/" + url[idx+1:]
|
|
}
|
|
// Fallback: just prepend git+
|
|
return "git+" + 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
|
|
}
|