beads/internal/storage/versioncontrolops/clone_test.go
Stephen Hodges 3efcd3571c
feat(storage): carry the git data ref of a git-backed Dolt remote (#6521)
* feat(storage): carry the git data ref of a git-backed Dolt remote

Dolt lets a git-backed remote keep its data on a ref other than
refs/dolt/data (the git_ref remote parameter, --ref on dolt remote add,
dolt clone, DOLT_REMOTE, and DOLT_CLONE). The storage layer never passed
it through, so a host that only accepts pushes under refs/heads/, or one
repository holding several databases on refs such as
refs/dolt/units/<key>, could not be used. Part of #6261; the commands
and the config key follow in a stacked PR.

- storage.RemoteInfo gains Ref (empty = refs/dolt/data);
  RemoteRefsMatch, EffectiveGitDataRef, and GitRefFromParamsJSON give
  every package one reading of it, pinned against dolt's constants.
- RemoteStore.AddRemoteWithRef on both stores, and CreateRemoteWithRef
  on the proxied-server remote use case, pass a non-empty ref as
  DOLT_REMOTE's --ref. AddRemote is unchanged; UpdateRemote keeps the
  ref through replacement and restore.
- versioncontrolops.DoltCloneWithRef and dolt.BootstrapFromRemoteWithDBRef
  pass the ref to DOLT_CLONE and to the dolt clone argv.
- ListRemotes reads the ref back from dolt_remotes.params (git_ref) in
  both remote repositories; PersistedRemotes reads it from
  repo_state.json. Malformed params is an error, never a silent default.
- The server-mode CLI mirror is compared on URL and ref and
  re-materialized with dolt remote add --ref when the ref differs. Where
  the CLI is proxied to the running server: the check reads the server's
  own repo_state.json when the listing is empty, the cold-start fallback
  re-adds a ref remote over SQL (which accepts --ref) so the server knows
  it, and a throwaway probe remote proves --ref works before an existing
  mirror remote is removed; probe remotes left by runs no longer alive
  are swept before any change to the mirror. For a server this bd does
  not own, the route stops and names the DOLT_REMOTE call to run
  against that server through bd sql, as one shell-quoted word.
- A ref is checked for a space, an ASCII control character, DEL, and a
  leading dash before it becomes a dolt argument; no other ref-name rule
  applies at this layer.
- Tests carry both ref shapes; the embedded-engine test proves add, list,
  push, and clone on the real engine; sqlmock tests cover the route
  decider and the cold-start re-add at the store seam; the dolt-CLI tests
  skip without a dolt binary.

* fix(storage): make both git_ref readers fail closed; pin the edges (#6521 review)

Maintainer review fixup for PR #6521. The review found no blocker or major;
this is the non-gating minor/nit set plus the test pins the synthesis named as
the residual defects.

- One reader for git_ref. storage.GitRefFromParams is now the single decoder
  for both planes (the SQL params column through GitRefFromParamsJSON, and
  repo_state.json through doltutil.PersistedRemotes), and a git_ref that is
  present but not a string is an error naming the remote instead of silently
  the default ref. Reading it as the default is how a mirror pinned to a custom
  ref would be kept on refs/dolt/data -- the wrong-ref push this PR closes,
  reached from the opposite direction.

- CreateRemoteWithRef checks the ref against what the dolt argv boundary will
  accept, so the recordable ref set is no longer wider than the routable one
  and a bad ref is refused at add time rather than diagnosed at push time.
  UpdateRemote deliberately does not re-check: it carries and restores refs
  that are already recorded and must stay able to put one back.

- BootstrapFromRemoteWithDBRef validates the ref before the already-exists
  early return, so the same --ref is accepted or refused the same way on a
  fresh host and on a bootstrapped one.

- sweepLeftoverProbes warns when it cannot remove another run's leftover probe
  remote, following PersistedRemoteInfos' precedent: the remote stays visible
  in `bd remote list` until some run succeeds, and nothing ever said why.

- EnsureCLIRemote's doc comment records that escalating a state-file read error
  is intentional for default-ref remotes too, and
  TestCLIMirrorUnreadableStateIsAnError now pins the ref == "" direction that a
  later "helpful" relaxation would gate away. The suggested URL-match
  short-circuit is deliberately NOT applied: when the state file is unreadable
  currentRef is unknown, so returning nil on a URL match alone would route a
  push onto whatever ref the mirror is secretly pinned to.

- AddCLIRemote's doc comment records that it is kept deliberately as the
  default-ref spelling now that EnsureCLIRemote uses AddCLIRemoteWithRef.

Not applied: widening probeCLIRemoteRef to `ref != "" || currentRef != ""` and
probing the restore shape. On a proxied server that turns a working custom-ref
-> default-ref transition into a hard failure (the target add is plain and
succeeds today, while probing the restore shape would be refused), and in the
one window that survives calibration -- a non-proxied plain-add failure from
disk/lock contention -- a capability probe predicts nothing about it.

Each new pin was mutation-proved: reverting the fix it guards reds that pin,
and only that pin.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Julian Knutsen <julianknutsen@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: Julian Knutsen <julian@gastownhall.dev>
2026-09-27 15:56:18 -07:00

90 lines
2.5 KiB
Go

package versioncontrolops
import (
"context"
"regexp"
"testing"
"github.com/DATA-DOG/go-sqlmock"
)
func TestDoltCloneWithoutUser(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("sqlmock.New: %v", err)
}
defer db.Close()
mock.ExpectExec(regexp.QuoteMeta("CALL DOLT_CLONE(?, ?)")).
WithArgs("https://example.com/repo", "beads").
WillReturnResult(sqlmock.NewResult(0, 1))
if err := DoltClone(context.Background(), db, "https://example.com/repo", "beads", ""); err != nil {
t.Fatalf("DoltClone: %v", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatal(err)
}
}
// A ref reaches DOLT_CLONE as --ref, verbatim, for both ref shapes.
func TestDoltCloneWithRef(t *testing.T) {
for _, ref := range gitDataRefShapes {
t.Run(ref, func(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("sqlmock.New: %v", err)
}
defer db.Close()
mock.ExpectExec(regexp.QuoteMeta("CALL DOLT_CLONE('--ref', ?, ?, ?)")).
WithArgs(ref, "git+https://example.com/repo.git", "beads").
WillReturnResult(sqlmock.NewResult(0, 1))
if err := DoltCloneWithRef(context.Background(), db, "git+https://example.com/repo.git", "beads", "", ref); err != nil {
t.Fatalf("DoltCloneWithRef: %v", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatal(err)
}
})
}
}
func TestDoltCloneWithUserAndRef(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("sqlmock.New: %v", err)
}
defer db.Close()
mock.ExpectExec(regexp.QuoteMeta("CALL DOLT_CLONE('--user', ?, '--ref', ?, ?, ?)")).
WithArgs("alice", "refs/dolt/units/team-12542", "git+https://example.com/repo.git", "beads").
WillReturnResult(sqlmock.NewResult(0, 1))
if err := DoltCloneWithRef(context.Background(), db, "git+https://example.com/repo.git", "beads", "alice", "refs/dolt/units/team-12542"); err != nil {
t.Fatalf("DoltCloneWithRef: %v", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatal(err)
}
}
func TestDoltCloneWithUser(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("sqlmock.New: %v", err)
}
defer db.Close()
mock.ExpectExec(regexp.QuoteMeta("CALL DOLT_CLONE('--user', ?, ?, ?)")).
WithArgs("alice", "https://example.com/repo", "beads").
WillReturnResult(sqlmock.NewResult(0, 1))
if err := DoltClone(context.Background(), db, "https://example.com/repo", "beads", "alice"); err != nil {
t.Fatalf("DoltClone: %v", err)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Fatal(err)
}
}