* 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>
195 lines
5.2 KiB
Go
195 lines
5.2 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestIsBareGitRepo(t *testing.T) {
|
|
t.Run("returns true in bare repository", func(t *testing.T) {
|
|
repoDir := filepath.Join(t.TempDir(), "bare.git")
|
|
runGitForSyncTest(t, "", "init", "--bare", repoDir)
|
|
|
|
oldWd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("failed to get cwd: %v", err)
|
|
}
|
|
defer func() { _ = os.Chdir(oldWd) }()
|
|
if err := os.Chdir(repoDir); err != nil {
|
|
t.Fatalf("failed to chdir: %v", err)
|
|
}
|
|
|
|
if !isBareGitRepo() {
|
|
t.Fatal("isBareGitRepo() = false, want true")
|
|
}
|
|
})
|
|
|
|
t.Run("returns false in non-bare repository", func(t *testing.T) {
|
|
repoDir := t.TempDir()
|
|
runGitForSyncTest(t, repoDir, "init")
|
|
|
|
oldWd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("failed to get cwd: %v", err)
|
|
}
|
|
defer func() { _ = os.Chdir(oldWd) }()
|
|
if err := os.Chdir(repoDir); err != nil {
|
|
t.Fatalf("failed to chdir: %v", err)
|
|
}
|
|
|
|
if isBareGitRepo() {
|
|
t.Fatal("isBareGitRepo() = true, want false")
|
|
}
|
|
})
|
|
|
|
t.Run("returns false outside git repository", func(t *testing.T) {
|
|
nonRepoDir := t.TempDir()
|
|
|
|
oldWd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("failed to get cwd: %v", err)
|
|
}
|
|
defer func() { _ = os.Chdir(oldWd) }()
|
|
if err := os.Chdir(nonRepoDir); err != nil {
|
|
t.Fatalf("failed to chdir: %v", err)
|
|
}
|
|
|
|
if isBareGitRepo() {
|
|
t.Fatal("isBareGitRepo() = true, want false")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestGitOriginGetURL(t *testing.T) {
|
|
t.Run("returns origin URL", func(t *testing.T) {
|
|
repoDir := t.TempDir()
|
|
bareDir := filepath.Join(t.TempDir(), "bare.git")
|
|
runGitForSyncTest(t, "", "init", "--bare", bareDir)
|
|
runGitForSyncTest(t, repoDir, "init")
|
|
runGitForSyncTest(t, repoDir, "remote", "add", "origin", bareDir)
|
|
|
|
oldWd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("failed to get cwd: %v", err)
|
|
}
|
|
defer func() { _ = os.Chdir(oldWd) }()
|
|
if err := os.Chdir(repoDir); err != nil {
|
|
t.Fatalf("failed to chdir: %v", err)
|
|
}
|
|
|
|
url, err := gitOriginGetURL()
|
|
if err != nil {
|
|
t.Fatalf("gitOriginGetURL failed: %v", err)
|
|
}
|
|
if url != bareDir {
|
|
t.Errorf("gitOriginGetURL = %q, want %q", url, bareDir)
|
|
}
|
|
})
|
|
|
|
t.Run("returns error for nonexistent remote", func(t *testing.T) {
|
|
repoDir := t.TempDir()
|
|
runGitForSyncTest(t, repoDir, "init")
|
|
|
|
oldWd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("failed to get cwd: %v", err)
|
|
}
|
|
defer func() { _ = os.Chdir(oldWd) }()
|
|
if err := os.Chdir(repoDir); err != nil {
|
|
t.Fatalf("failed to chdir: %v", err)
|
|
}
|
|
|
|
_, err = gitOriginGetURL()
|
|
if err == nil {
|
|
t.Fatal("expected error for nonexistent remote")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestGitOriginHasDoltDataRef(t *testing.T) {
|
|
t.Run("returns false for nonexistent ref", func(t *testing.T) {
|
|
bareDir := filepath.Join(t.TempDir(), "bare.git")
|
|
runGitForSyncTest(t, "", "init", "--bare", bareDir)
|
|
|
|
repoDir := t.TempDir()
|
|
runGitForSyncTest(t, repoDir, "init")
|
|
runGitForSyncTest(t, repoDir, "remote", "add", "origin", bareDir)
|
|
|
|
oldWd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("failed to get cwd: %v", err)
|
|
}
|
|
defer func() { _ = os.Chdir(oldWd) }()
|
|
if err := os.Chdir(repoDir); err != nil {
|
|
t.Fatalf("failed to chdir: %v", err)
|
|
}
|
|
|
|
if gitOriginHasDoltDataRef() {
|
|
t.Fatal("expected false for nonexistent ref")
|
|
}
|
|
})
|
|
|
|
t.Run("returns true for existing ref", func(t *testing.T) {
|
|
bareDir := filepath.Join(t.TempDir(), "bare.git")
|
|
runGitForSyncTest(t, "", "init", "--bare", bareDir)
|
|
|
|
// Create a repo, commit, push to create refs/dolt/data
|
|
repoDir := t.TempDir()
|
|
runGitForSyncTest(t, repoDir, "init", "-b", "main")
|
|
runGitForSyncTest(t, repoDir, "config", "user.email", "test@test.com")
|
|
runGitForSyncTest(t, repoDir, "config", "user.name", "Test User")
|
|
runGitForSyncTest(t, repoDir, "commit", "--allow-empty", "-m", "init")
|
|
runGitForSyncTest(t, repoDir, "remote", "add", "origin", bareDir)
|
|
runGitForSyncTest(t, repoDir, "push", "origin", "HEAD:refs/dolt/data")
|
|
|
|
oldWd, err := os.Getwd()
|
|
if err != nil {
|
|
t.Fatalf("failed to get cwd: %v", err)
|
|
}
|
|
defer func() { _ = os.Chdir(oldWd) }()
|
|
if err := os.Chdir(repoDir); err != nil {
|
|
t.Fatalf("failed to chdir: %v", err)
|
|
}
|
|
|
|
if !gitOriginHasDoltDataRef() {
|
|
t.Fatal("expected true for existing ref")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestGitURLToDoltRemote(t *testing.T) {
|
|
tests := []struct {
|
|
input string
|
|
want string
|
|
}{
|
|
{"https://github.com/org/repo.git", "git+https://github.com/org/repo.git"},
|
|
{"http://github.com/org/repo.git", "git+http://github.com/org/repo.git"},
|
|
{"ssh://git@github.com/org/repo.git", "git+ssh://git@github.com/org/repo.git"},
|
|
{"git@github.com:org/repo.git", "git+ssh://git@github.com/org/repo.git"},
|
|
{"git+https://github.com/org/repo.git", "git+https://github.com/org/repo.git"},
|
|
{"git+ssh://git@github.com/org/repo.git", "git+ssh://git@github.com/org/repo.git"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.input, func(t *testing.T) {
|
|
got := gitURLToDoltRemote(tt.input)
|
|
if got != tt.want {
|
|
t.Errorf("gitURLToDoltRemote(%q) = %q, want %q", tt.input, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func runGitForSyncTest(t *testing.T, dir string, args ...string) {
|
|
t.Helper()
|
|
|
|
cmd := exec.Command("git", args...)
|
|
if dir != "" {
|
|
cmd.Dir = dir
|
|
}
|
|
output, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
t.Fatalf("git %v failed: %v\n%s", args, err, string(output))
|
|
}
|
|
}
|