* fix(dolt): disable git hooks on the in-process push path too (#4272)
PR #3740 (for GH#3724) disabled client-side git hooks for bd's internal
refs/dolt/data push, but only on the CLI shell-out fallback, by decorating
the exec.Cmd. The primary path is CALL DOLT_PUSH/DOLT_FETCH, so the
original symptom is still reproducible: a templated pre-push hook in the
cache-mirror kills the push with "fatal: this operation must be run in a
work tree".
The two remaining paths need different fixes, because the git child is
spawned from a different process in each:
- Embedded mode runs the CALL in bd's own process, and every remote
operation reaches it through internal/storage/versioncontrolops. The
override has to be on bd's environment for the duration of the call.
- Server mode runs the CALL inside the spawned sql-server, where bd's own
environment is invisible, so the override goes in that child's env at
startup. That arm is the approach from PR #4281 by pmgledhill102, which
the author closed after a month without maintainer response; it is
reworked here onto the shared helper it could not import at the time.
New internal/githooksenv holds the shared constants and a WithDisabled
scope. It refcounts rather than holding a mutex across the call: remote
operations are network-bound and can run for minutes, and serializing all
of them behind one lock would be a real throughput regression when all
they need is the variable set while any is running. The refcount also
makes nesting safe.
applyNoGitHooksToCmd now appends to an existing GIT_CONFIG_PARAMETERS
instead of replacing it, so a caller's own overrides survive — also from
pmgledhill102's PR.
Verified end-to-end, not inferred: with the reporter's hostile pre-push
hook planted in .dolt/git-remote-cache/<hash>/repo.git/hooks, on identical
repo state, `bd dolt push` built from main at 8bb0d36be fails with the
reported error while the same command built from this branch prints "Push
complete."
Co-authored-by: Paul Gledhill <33487571+pmgledhill102@users.noreply.github.com>
Agent-Signature: claude-opus-5-medium on behalf of maphew
* review fixes: cover the two remaining unhooked paths — dbproxy spawn and DOLT_CLONE
The proxied-server stack spawns its own dolt sql-server with a plain
os.Environ(), so the in-process CALL DOLT_PUSH/FETCH there still ran
templated git hooks; it now gets the same GIT_CONFIG_PARAMETERS override
as the directly-managed spawn. DoltClone's initial fetch is likewise
wrapped in githooksenv.WithDisabled, with Clone cases added to the
recording-conn regression table.
Agent-Signature: claude-code-claude-fable-5-unknown-reasoning on behalf of Steve Yegge
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Xmjba2GjaHnrvaU2sM8Kz5
---------
Co-authored-by: Paul Gledhill <33487571+pmgledhill102@users.noreply.github.com>
Co-authored-by: Beadwright <beadwright@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
7e10378b91
commit
bfdc54b06a
8 changed files with 418 additions and 32 deletions
|
|
@ -37,6 +37,7 @@ import (
|
|||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/debug"
|
||||
"github.com/steveyegge/beads/internal/fdhygiene"
|
||||
"github.com/steveyegge/beads/internal/githooksenv"
|
||||
"github.com/steveyegge/beads/internal/lockfile"
|
||||
"github.com/steveyegge/beads/internal/storage/doltutil"
|
||||
)
|
||||
|
|
@ -1288,7 +1289,13 @@ func Start(beadsDir string) (*State, error) {
|
|||
cmd.Stderr = logFile
|
||||
cmd.Stdin = nil
|
||||
cmd.SysProcAttr = procAttrDetached()
|
||||
cmd.Env = os.Environ()
|
||||
// In server mode the CALL DOLT_PUSH runs inside THIS child, not in
|
||||
// bd, so the hooks override has to be in the child's environment —
|
||||
// setting it on bd's own process (what the embedded path does)
|
||||
// would be invisible here. GH#4272; approach from PR #4281 by
|
||||
// pmgledhill102.
|
||||
cmd.Env = append(os.Environ(), githooksenv.ParametersEnv+"="+
|
||||
githooksenv.AppendParameter(os.Getenv(githooksenv.ParametersEnv), githooksenv.NoHooksParam))
|
||||
|
||||
// GH#4634: the server outlives the caller, so any descriptor the
|
||||
// caller left non-CLOEXEC (a shell `exec 9>lock`, a CI harness, an
|
||||
|
|
|
|||
107
internal/githooksenv/githooksenv.go
Normal file
107
internal/githooksenv/githooksenv.go
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
// Package githooksenv disables git's client-side hooks for the git commands
|
||||
// Dolt runs inside bd's own process.
|
||||
//
|
||||
// Dolt transfers refs/dolt/data for a git-protocol remote by shelling out to
|
||||
// git against its embedded cache-mirror, .dolt/git-remote-cache/<hash>/repo.git.
|
||||
// That mirror is a bare-style repo with no work tree, but `git init` still
|
||||
// honors the user's init.templateDir and copies template hooks into its
|
||||
// hooks/ dir. A templated pre-push hook that runs `git diff` or `git status`
|
||||
// — the pre-commit framework's staged_files_only setup, for instance — then
|
||||
// dies with "fatal: this operation must be run in a work tree" and takes bd's
|
||||
// push down with it (GH#3724, GH#4272).
|
||||
//
|
||||
// PR #3740 fixed this for the CLI shell-out fallback by decorating the
|
||||
// exec.Cmd. The primary path is CALL DOLT_PUSH/DOLT_FETCH, which runs in the
|
||||
// Dolt engine inside bd and spawns git itself — there is no exec.Cmd to
|
||||
// decorate, so the override has to be on bd's own process environment for the
|
||||
// duration of the call. That is what this package provides.
|
||||
package githooksenv
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// ParametersEnv is git's environment variable for injecting one-off config
|
||||
// values into every git invocation in the process subtree.
|
||||
const ParametersEnv = "GIT_CONFIG_PARAMETERS"
|
||||
|
||||
// NoHooksParam points core.hooksPath at /dev/null, i.e. tells git to find no
|
||||
// hooks at all. Same intent as the --no-verify fix on the commit side
|
||||
// (GH#3340 / GH#3598 / PR #3626).
|
||||
const NoHooksParam = "'core.hooksPath=/dev/null'"
|
||||
|
||||
// AppendParameter adds param to an existing GIT_CONFIG_PARAMETERS value rather
|
||||
// than replacing it, so a caller's own overrides survive. Git applies the
|
||||
// entries left to right, which is also what makes an appended override win.
|
||||
func AppendParameter(existing, param string) string {
|
||||
if existing == "" {
|
||||
return param
|
||||
}
|
||||
return existing + " " + param
|
||||
}
|
||||
|
||||
// Extract returns the GIT_CONFIG_PARAMETERS value from an environment slice,
|
||||
// using exec's last-wins semantics for duplicate keys.
|
||||
func Extract(env []string) string {
|
||||
prefix := ParametersEnv + "="
|
||||
var val string
|
||||
for _, e := range env {
|
||||
if v, ok := strings.CutPrefix(e, prefix); ok {
|
||||
val = v
|
||||
}
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
// Process-environment state for WithDisabled. A refcount rather than a plain
|
||||
// mutex held across fn: remote operations are network-bound and can take
|
||||
// minutes, and serializing every one of them behind a single lock would be a
|
||||
// real throughput regression for a change that only needs the variable to be
|
||||
// set while any of them is running. Overlapping operations all want the exact
|
||||
// same value, so sharing one activation is correct; the last one out restores.
|
||||
// The refcount also makes nesting safe, which matters because the server-mode
|
||||
// credential path and the shared versioncontrolops path can both wrap the same
|
||||
// call.
|
||||
var (
|
||||
mu sync.Mutex
|
||||
depth int
|
||||
saved string
|
||||
hadSaved bool
|
||||
)
|
||||
|
||||
// WithDisabled runs fn with git client-side hooks disabled process-wide,
|
||||
// restoring the previous GIT_CONFIG_PARAMETERS afterwards.
|
||||
func WithDisabled(fn func() error) error {
|
||||
acquire()
|
||||
defer release()
|
||||
return fn()
|
||||
}
|
||||
|
||||
func acquire() {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if depth == 0 {
|
||||
saved, hadSaved = os.LookupEnv(ParametersEnv)
|
||||
// Best effort: Setenv failure is extremely rare, and a push that runs
|
||||
// hooks is better than no push at all.
|
||||
_ = os.Setenv(ParametersEnv, AppendParameter(saved, NoHooksParam))
|
||||
}
|
||||
depth++
|
||||
}
|
||||
|
||||
func release() {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
depth--
|
||||
if depth > 0 {
|
||||
return
|
||||
}
|
||||
if hadSaved {
|
||||
_ = os.Setenv(ParametersEnv, saved)
|
||||
} else {
|
||||
_ = os.Unsetenv(ParametersEnv)
|
||||
}
|
||||
saved, hadSaved = "", false
|
||||
}
|
||||
128
internal/githooksenv/githooksenv_test.go
Normal file
128
internal/githooksenv/githooksenv_test.go
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
package githooksenv
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAppendParameter(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
existing string
|
||||
want string
|
||||
}{
|
||||
{"absent", "", NoHooksParam},
|
||||
{"preserved and appended so ours wins", "'user.email=ci@example.com'", "'user.email=ci@example.com' " + NoHooksParam},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := AppendParameter(tt.existing, NoHooksParam); got != tt.want {
|
||||
t.Errorf("AppendParameter(%q) = %q, want %q", tt.existing, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestExtract pins the last-wins rule, matching how exec resolves a duplicated
|
||||
// key in an environment slice.
|
||||
func TestExtract(t *testing.T) {
|
||||
env := []string{"PATH=/bin", ParametersEnv + "='a=1'", "TZ=UTC", ParametersEnv + "='b=2'"}
|
||||
if got, want := Extract(env), "'b=2'"; got != want {
|
||||
t.Errorf("Extract = %q, want %q", got, want)
|
||||
}
|
||||
if got := Extract([]string{"PATH=/bin"}); got != "" {
|
||||
t.Errorf("Extract with no entry = %q, want empty", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithDisabled_SetsAndRestoresWhenUnset(t *testing.T) {
|
||||
if err := os.Unsetenv(ParametersEnv); err != nil {
|
||||
t.Fatalf("unsetenv: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = os.Unsetenv(ParametersEnv) })
|
||||
|
||||
var inside string
|
||||
err := WithDisabled(func() error {
|
||||
inside = os.Getenv(ParametersEnv)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("WithDisabled: %v", err)
|
||||
}
|
||||
if inside != NoHooksParam {
|
||||
t.Errorf("inside fn %s = %q, want %q", ParametersEnv, inside, NoHooksParam)
|
||||
}
|
||||
if _, ok := os.LookupEnv(ParametersEnv); ok {
|
||||
t.Errorf("%s still set after WithDisabled; want unset again", ParametersEnv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithDisabled_RestoresPreviousValue(t *testing.T) {
|
||||
const prev = "'user.email=ci@example.com'"
|
||||
t.Setenv(ParametersEnv, prev)
|
||||
|
||||
var inside string
|
||||
if err := WithDisabled(func() error {
|
||||
inside = os.Getenv(ParametersEnv)
|
||||
return nil
|
||||
}); err != nil {
|
||||
t.Fatalf("WithDisabled: %v", err)
|
||||
}
|
||||
if want := prev + " " + NoHooksParam; inside != want {
|
||||
t.Errorf("inside fn %s = %q, want %q", ParametersEnv, inside, want)
|
||||
}
|
||||
if got := os.Getenv(ParametersEnv); got != prev {
|
||||
t.Errorf("after WithDisabled %s = %q, want the original %q", ParametersEnv, got, prev)
|
||||
}
|
||||
}
|
||||
|
||||
// TestWithDisabled_Nested guards the refcount: the server-mode credential path
|
||||
// and the shared versioncontrolops path can both wrap the same call, and the
|
||||
// inner scope must not restore the variable out from under the outer one.
|
||||
func TestWithDisabled_Nested(t *testing.T) {
|
||||
if err := os.Unsetenv(ParametersEnv); err != nil {
|
||||
t.Fatalf("unsetenv: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = os.Unsetenv(ParametersEnv) })
|
||||
|
||||
var afterInner string
|
||||
err := WithDisabled(func() error {
|
||||
if err := WithDisabled(func() error { return nil }); err != nil {
|
||||
return err
|
||||
}
|
||||
afterInner = os.Getenv(ParametersEnv)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("WithDisabled: %v", err)
|
||||
}
|
||||
if afterInner != NoHooksParam {
|
||||
t.Errorf("after inner scope exited, %s = %q, want still %q", ParametersEnv, afterInner, NoHooksParam)
|
||||
}
|
||||
if _, ok := os.LookupEnv(ParametersEnv); ok {
|
||||
t.Errorf("%s still set after the outer scope; want unset again", ParametersEnv)
|
||||
}
|
||||
}
|
||||
|
||||
// TestWithDisabled_NoDoubleAppendWhenNested checks the inner scope reuses the
|
||||
// outer activation rather than stacking a second copy of the parameter, which
|
||||
// git would apply twice.
|
||||
func TestWithDisabled_NoDoubleAppendWhenNested(t *testing.T) {
|
||||
if err := os.Unsetenv(ParametersEnv); err != nil {
|
||||
t.Fatalf("unsetenv: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = os.Unsetenv(ParametersEnv) })
|
||||
|
||||
var innermost string
|
||||
if err := WithDisabled(func() error {
|
||||
return WithDisabled(func() error {
|
||||
innermost = os.Getenv(ParametersEnv)
|
||||
return nil
|
||||
})
|
||||
}); err != nil {
|
||||
t.Fatalf("WithDisabled: %v", err)
|
||||
}
|
||||
if innermost != NoHooksParam {
|
||||
t.Errorf("innermost %s = %q, want a single %q", ParametersEnv, innermost, NoHooksParam)
|
||||
}
|
||||
}
|
||||
|
|
@ -22,6 +22,7 @@ import (
|
|||
"golang.org/x/sync/errgroup"
|
||||
|
||||
"github.com/steveyegge/beads/internal/doltserver"
|
||||
"github.com/steveyegge/beads/internal/githooksenv"
|
||||
"github.com/steveyegge/beads/internal/procid"
|
||||
"github.com/steveyegge/beads/internal/storage/dbproxy/identity"
|
||||
"github.com/steveyegge/beads/internal/storage/dbproxy/pidfile"
|
||||
|
|
@ -254,7 +255,11 @@ func (s *DoltServer) Start(ctx context.Context) error {
|
|||
cmd.Stderr = s.logFile
|
||||
}
|
||||
|
||||
cmd.Env = os.Environ()
|
||||
// GH#4272: like the directly-managed sql-server spawn in
|
||||
// internal/doltserver, the proxied server executes CALL DOLT_PUSH/FETCH
|
||||
// in-process, so templated git hooks must be disabled in its environment.
|
||||
cmd.Env = append(os.Environ(), githooksenv.ParametersEnv+"="+
|
||||
githooksenv.AppendParameter(os.Getenv(githooksenv.ParametersEnv), githooksenv.NoHooksParam))
|
||||
|
||||
if err := cmd.Start(); err != nil {
|
||||
s.eg, s.egCtx, s.cancel = nil, nil, nil
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ import (
|
|||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/steveyegge/beads/internal/githooksenv"
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
)
|
||||
|
||||
|
|
@ -498,7 +499,15 @@ func applyS3ChecksumEnvToCmd(cmd *exec.Cmd) {
|
|||
// skip them. Same intent as the `--no-verify` fix on the commit side
|
||||
// (GH#3340 / GH#3598 / PR #3626), applied at the push site (GH#3724).
|
||||
func applyNoGitHooksToCmd(cmd *exec.Cmd) {
|
||||
setCmdEnv(cmd, "GIT_CONFIG_PARAMETERS", "'core.hooksPath=/dev/null'")
|
||||
base := cmd.Env
|
||||
if base == nil {
|
||||
base = os.Environ()
|
||||
}
|
||||
// Append rather than replace: a caller that set its own
|
||||
// GIT_CONFIG_PARAMETERS (a test harness pinning user.email, say) would
|
||||
// otherwise have it silently dropped by this call.
|
||||
setCmdEnv(cmd, githooksenv.ParametersEnv,
|
||||
githooksenv.AppendParameter(githooksenv.Extract(base), githooksenv.NoHooksParam))
|
||||
}
|
||||
|
||||
// setFederationCredentials sets DOLT_REMOTE_USER and DOLT_REMOTE_PASSWORD env vars.
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"github.com/steveyegge/beads/internal/githooksenv"
|
||||
)
|
||||
|
||||
// DoltClone clones a Dolt database from a remote URL.
|
||||
|
|
@ -19,10 +21,14 @@ func DoltClone(ctx context.Context, conn DBConn, remoteURL, database, user strin
|
|||
args = []any{user, remoteURL, database}
|
||||
}
|
||||
|
||||
if _, err := conn.ExecContext(ctx, query, args...); err != nil {
|
||||
return fmt.Errorf("dolt clone %s: %w", sanitizeURL(remoteURL), err)
|
||||
}
|
||||
return nil
|
||||
// GH#4272: the initial fetch runs git hooks just like push/pull; disable
|
||||
// them for the clone window too (see remotes.go for the full rationale).
|
||||
return githooksenv.WithDisabled(func() error {
|
||||
if _, err := conn.ExecContext(ctx, query, args...); err != nil {
|
||||
return fmt.Errorf("dolt clone %s: %w", sanitizeURL(remoteURL), err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// sanitizeURL removes credentials from a URL for safe error reporting.
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/steveyegge/beads/internal/githooksenv"
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
)
|
||||
|
||||
|
|
@ -45,12 +46,14 @@ func RemoveRemote(ctx context.Context, db DBConn, name string) error {
|
|||
// every other open session on the same engine, so a failed fetch would break
|
||||
// concurrent in-flight connections (bd-6dnrw.10).
|
||||
func Fetch(ctx context.Context, db DBConn, peer, user string) error {
|
||||
var err error
|
||||
if user != "" {
|
||||
_, err = db.ExecContext(ctx, "CALL DOLT_FETCH('--user', ?, ?)", user, peer)
|
||||
} else {
|
||||
_, err = db.ExecContext(ctx, "CALL DOLT_FETCH(?)", peer)
|
||||
}
|
||||
err := githooksenv.WithDisabled(func() error {
|
||||
if user != "" {
|
||||
_, err := db.ExecContext(ctx, "CALL DOLT_FETCH('--user', ?, ?)", user, peer)
|
||||
return err
|
||||
}
|
||||
_, err := db.ExecContext(ctx, "CALL DOLT_FETCH(?)", peer)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("fetch from %s: %w", peer, err)
|
||||
}
|
||||
|
|
@ -62,13 +65,15 @@ func Fetch(ctx context.Context, db DBConn, peer, user string) error {
|
|||
// must be set in the in-process Dolt server's environment. Required when
|
||||
// pushing to a remotesapi server that enforces CLONE_ADMIN authentication.
|
||||
func Push(ctx context.Context, db DBConn, remote, branch, user string) error {
|
||||
if user != "" {
|
||||
if _, err := db.ExecContext(ctx, "CALL DOLT_PUSH('--user', ?, ?, ?)", user, remote, branch); err != nil {
|
||||
return fmt.Errorf("push to %s/%s: %w", remote, branch, err)
|
||||
err := githooksenv.WithDisabled(func() error {
|
||||
if user != "" {
|
||||
_, err := db.ExecContext(ctx, "CALL DOLT_PUSH('--user', ?, ?, ?)", user, remote, branch)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if _, err := db.ExecContext(ctx, "CALL DOLT_PUSH(?, ?)", remote, branch); err != nil {
|
||||
_, err := db.ExecContext(ctx, "CALL DOLT_PUSH(?, ?)", remote, branch)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("push to %s/%s: %w", remote, branch, err)
|
||||
}
|
||||
return nil
|
||||
|
|
@ -77,13 +82,15 @@ func Push(ctx context.Context, db DBConn, remote, branch, user string) error {
|
|||
// ForcePush force-pushes the given branch to the named remote.
|
||||
// See Push for the user/auth contract.
|
||||
func ForcePush(ctx context.Context, db DBConn, remote, branch, user string) error {
|
||||
if user != "" {
|
||||
if _, err := db.ExecContext(ctx, "CALL DOLT_PUSH('--force', '--user', ?, ?, ?)", user, remote, branch); err != nil {
|
||||
return fmt.Errorf("force push to %s/%s: %w", remote, branch, err)
|
||||
err := githooksenv.WithDisabled(func() error {
|
||||
if user != "" {
|
||||
_, err := db.ExecContext(ctx, "CALL DOLT_PUSH('--force', '--user', ?, ?, ?)", user, remote, branch)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if _, err := db.ExecContext(ctx, "CALL DOLT_PUSH('--force', ?, ?)", remote, branch); err != nil {
|
||||
_, err := db.ExecContext(ctx, "CALL DOLT_PUSH('--force', ?, ?)", remote, branch)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("force push to %s/%s: %w", remote, branch, err)
|
||||
}
|
||||
return nil
|
||||
|
|
@ -110,14 +117,15 @@ func Pull(ctx context.Context, db DBConn, remote, branch, user string) error {
|
|||
// Pull's behavior. See MergeAndSettleWithStrategy/SettleMerge for the
|
||||
// resolution logic.
|
||||
func PullWithStrategy(ctx context.Context, db DBConn, remote, branch, user, strategy string) error {
|
||||
if user != "" {
|
||||
if _, err := db.ExecContext(ctx, "CALL DOLT_FETCH('--user', ?, ?, ?)", user, remote, branch); err != nil {
|
||||
return fmt.Errorf("fetch from %s/%s: %w", remote, branch, err)
|
||||
}
|
||||
} else {
|
||||
if _, err := db.ExecContext(ctx, "CALL DOLT_FETCH(?, ?)", remote, branch); err != nil {
|
||||
return fmt.Errorf("fetch from %s/%s: %w", remote, branch, err)
|
||||
if err := githooksenv.WithDisabled(func() error {
|
||||
if user != "" {
|
||||
_, err := db.ExecContext(ctx, "CALL DOLT_FETCH('--user', ?, ?, ?)", user, remote, branch)
|
||||
return err
|
||||
}
|
||||
_, err := db.ExecContext(ctx, "CALL DOLT_FETCH(?, ?)", remote, branch)
|
||||
return err
|
||||
}); err != nil {
|
||||
return fmt.Errorf("fetch from %s/%s: %w", remote, branch, err)
|
||||
}
|
||||
trackingRef := remote + "/" + branch
|
||||
if err := MergeAndSettleWithStrategy(ctx, db, trackingRef, strategy); err != nil {
|
||||
|
|
|
|||
116
internal/storage/versioncontrolops/remotes_githooks_test.go
Normal file
116
internal/storage/versioncontrolops/remotes_githooks_test.go
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
package versioncontrolops
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/githooksenv"
|
||||
)
|
||||
|
||||
// envRecordingConn records the GIT_CONFIG_PARAMETERS value visible at the
|
||||
// moment each statement executes. That is the instant Dolt would spawn git for
|
||||
// a git-protocol remote, so it is the only point where the override matters.
|
||||
type envRecordingConn struct {
|
||||
queries []string
|
||||
seen []string
|
||||
}
|
||||
|
||||
func (c *envRecordingConn) ExecContext(_ context.Context, query string, _ ...any) (sql.Result, error) {
|
||||
c.queries = append(c.queries, query)
|
||||
c.seen = append(c.seen, os.Getenv(githooksenv.ParametersEnv))
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// QueryContext always errors: nothing here needs to read rows, and returning a
|
||||
// nil *sql.Rows would panic in the caller instead of failing cleanly.
|
||||
func (c *envRecordingConn) QueryContext(context.Context, string, ...any) (*sql.Rows, error) {
|
||||
return nil, errStubConn
|
||||
}
|
||||
|
||||
var errStubConn = errors.New("envRecordingConn: queries are not supported")
|
||||
|
||||
func (c *envRecordingConn) QueryRowContext(context.Context, string, ...any) *sql.Row {
|
||||
return nil
|
||||
}
|
||||
|
||||
// TestRemoteCallsDisableGitHooks is the regression test for GH#4272. PR #3740
|
||||
// disabled hooks only on the CLI shell-out fallback, but the primary path is
|
||||
// these in-process CALL DOLT_PUSH/DOLT_FETCH statements: Dolt shells out to git
|
||||
// against .dolt/git-remote-cache/<hash>/repo.git, and a templated pre-push hook
|
||||
// there kills the push with "fatal: this operation must be run in a work tree".
|
||||
//
|
||||
// Embedded mode reaches every remote operation through this package, so
|
||||
// asserting here covers the mode the bug was reported against.
|
||||
func TestRemoteCallsDisableGitHooks(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
call func(DBConn) error
|
||||
}{
|
||||
{"Push", func(db DBConn) error { return Push(context.Background(), db, "origin", "main", "") }},
|
||||
{"Push with user", func(db DBConn) error { return Push(context.Background(), db, "origin", "main", "u") }},
|
||||
{"ForcePush", func(db DBConn) error { return ForcePush(context.Background(), db, "origin", "main", "") }},
|
||||
{"Fetch", func(db DBConn) error { return Fetch(context.Background(), db, "peer", "") }},
|
||||
{"Clone", func(db DBConn) error {
|
||||
return DoltClone(context.Background(), db, "https://example.com/r.git", "beads", "")
|
||||
}},
|
||||
{"Clone with user", func(db DBConn) error {
|
||||
return DoltClone(context.Background(), db, "https://example.com/r.git", "beads", "u")
|
||||
}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := os.Unsetenv(githooksenv.ParametersEnv); err != nil {
|
||||
t.Fatalf("unsetenv: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = os.Unsetenv(githooksenv.ParametersEnv) })
|
||||
|
||||
conn := &envRecordingConn{}
|
||||
if err := tt.call(conn); err != nil {
|
||||
t.Fatalf("call: %v", err)
|
||||
}
|
||||
if len(conn.seen) == 0 {
|
||||
t.Fatal("no statement executed; the test proves nothing")
|
||||
}
|
||||
for i, got := range conn.seen {
|
||||
if !strings.Contains(got, githooksenv.NoHooksParam) {
|
||||
t.Errorf("during %q, %s = %q, want it to contain %q",
|
||||
conn.queries[i], githooksenv.ParametersEnv, got, githooksenv.NoHooksParam)
|
||||
}
|
||||
}
|
||||
if _, ok := os.LookupEnv(githooksenv.ParametersEnv); ok {
|
||||
t.Errorf("%s left set after the call returned", githooksenv.ParametersEnv)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestPullFetchStepDisablesGitHooks covers Pull separately: only its fetch step
|
||||
// talks to the remote, and the merge that follows is local, so the assertion is
|
||||
// scoped to the DOLT_FETCH statement. Pull is not in the table above because
|
||||
// its merge step needs a real engine.
|
||||
func TestPullFetchStepDisablesGitHooks(t *testing.T) {
|
||||
if err := os.Unsetenv(githooksenv.ParametersEnv); err != nil {
|
||||
t.Fatalf("unsetenv: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = os.Unsetenv(githooksenv.ParametersEnv) })
|
||||
|
||||
conn := &envRecordingConn{}
|
||||
// The merge step will fail against this stub conn; the fetch statement has
|
||||
// already been recorded by then, which is all this test asserts.
|
||||
_ = PullWithStrategy(context.Background(), conn, "origin", "main", "", "")
|
||||
|
||||
if len(conn.seen) == 0 {
|
||||
t.Fatal("no statement executed; the test proves nothing")
|
||||
}
|
||||
if !strings.HasPrefix(conn.queries[0], "CALL DOLT_FETCH") {
|
||||
t.Fatalf("first statement = %q, want the DOLT_FETCH step", conn.queries[0])
|
||||
}
|
||||
if !strings.Contains(conn.seen[0], githooksenv.NoHooksParam) {
|
||||
t.Errorf("during the fetch step, %s = %q, want it to contain %q",
|
||||
githooksenv.ParametersEnv, conn.seen[0], githooksenv.NoHooksParam)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue