feat(M5): lifecycle hooks 🪝
- internal/hooks: Runner finds <mount>/.devbox/hooks/<event>, runs via bash (or .ps1->pwsh), injects DEVBOX_EVENT/MOUNT/SHARE/HOST/REMOTE/SNAPSHOT/CHANGED_FILES, 60s timeout (WaitDelay bounds post-kill cleanup), pre-* non-zero exit = veto - syncer fires pre-push/post-push (in Push) + pre-pull/post-pull/on-conflict (in Pull); pre-* veto -> PullResult/Result.Vetoed -> Sync skips, waits for next nudge - daemon builds a Runner per mount; cmd mount/publish pass nil (daemon drives hooks) - tests: env+changed-files injection, absent=no-op, pre-* veto, 60s timeout kill, post-pull fires with changed list, pre-push vetoes (head not advanced)
This commit is contained in:
parent
931fd30432
commit
613de19818
9 changed files with 373 additions and 29 deletions
|
|
@ -133,7 +133,7 @@ func publishCmd() *cobra.Command {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
res, err := syncer.Push(c, root, share, "", ig, guard, head)
|
res, err := syncer.Push(c, root, share, "", ig, guard, head, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -204,10 +204,10 @@ func mountCmd() *cobra.Command {
|
||||||
var pr syncer.PullResult
|
var pr syncer.PullResult
|
||||||
var newBase string
|
var newBase string
|
||||||
if ro {
|
if ro {
|
||||||
pr, err = syncer.Pull(c, local, share, subpath, st[key], host, time.Now().UnixNano(), ig, guard)
|
pr, err = syncer.Pull(c, local, share, subpath, st[key], host, time.Now().UnixNano(), ig, guard, nil)
|
||||||
newBase = pr.Base
|
newBase = pr.Base
|
||||||
} else {
|
} else {
|
||||||
newBase, pr, err = syncer.Sync(c, local, share, subpath, st[key], host, time.Now().UnixNano(), ig, guard)
|
newBase, pr, err = syncer.Sync(c, local, share, subpath, st[key], host, time.Now().UnixNano(), ig, guard, nil)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.shoemoney.ai/shoemoney/devbox/internal/config"
|
"git.shoemoney.ai/shoemoney/devbox/internal/config"
|
||||||
|
"git.shoemoney.ai/shoemoney/devbox/internal/hooks"
|
||||||
"git.shoemoney.ai/shoemoney/devbox/internal/secret"
|
"git.shoemoney.ai/shoemoney/devbox/internal/secret"
|
||||||
"git.shoemoney.ai/shoemoney/devbox/internal/syncer"
|
"git.shoemoney.ai/shoemoney/devbox/internal/syncer"
|
||||||
"git.shoemoney.ai/shoemoney/devbox/internal/transport"
|
"git.shoemoney.ai/shoemoney/devbox/internal/transport"
|
||||||
|
|
@ -155,9 +156,10 @@ func (d *Daemon) syncMount(c *transport.Client, m config.Mount) {
|
||||||
}
|
}
|
||||||
base := d.getBase(m)
|
base := d.getBase(m)
|
||||||
now := time.Now().UnixNano() // nanoseconds so conflict-copy names don't collide
|
now := time.Now().UnixNano() // nanoseconds so conflict-copy names don't collide
|
||||||
|
hk := hooks.New(m.Local, m.Share, d.host, m.Hub)
|
||||||
|
|
||||||
if m.ReadOnly {
|
if m.ReadOnly {
|
||||||
pr, err := syncer.Pull(c, m.Local, m.Share, m.Subpath, base, d.host, now, ig, d.guard)
|
pr, err := syncer.Pull(c, m.Local, m.Share, m.Subpath, base, d.host, now, ig, d.guard, hk)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
d.logf("pull %s: %v", m.Share, err)
|
d.logf("pull %s: %v", m.Share, err)
|
||||||
return
|
return
|
||||||
|
|
@ -167,7 +169,7 @@ func (d *Daemon) syncMount(c *transport.Client, m config.Mount) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
newBase, pr, err := syncer.Sync(c, m.Local, m.Share, m.Subpath, base, d.host, now, ig, d.guard)
|
newBase, pr, err := syncer.Sync(c, m.Local, m.Share, m.Subpath, base, d.host, now, ig, d.guard, hk)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
d.logf("sync %s: %v", m.Share, err)
|
d.logf("sync %s: %v", m.Share, err)
|
||||||
return
|
return
|
||||||
|
|
|
||||||
133
internal/hooks/hooks.go
Normal file
133
internal/hooks/hooks.go
Normal file
|
|
@ -0,0 +1,133 @@
|
||||||
|
// Package hooks runs user lifecycle scripts from a mount's .devbox/hooks/ dir on
|
||||||
|
// sync events. A script runs via bash (or pwsh for a .ps1 hook) with the sync
|
||||||
|
// context injected as env vars; a pre-* hook's non-zero exit vetoes that step.
|
||||||
|
package hooks
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DefaultTimeout bounds a hook; a hung hook is killed, never wedging the sync loop.
|
||||||
|
const DefaultTimeout = 60 * time.Second
|
||||||
|
|
||||||
|
// Event names (also the hook script filenames).
|
||||||
|
const (
|
||||||
|
PrePush = "pre-push"
|
||||||
|
PostPush = "post-push"
|
||||||
|
PrePull = "pre-pull"
|
||||||
|
PostPull = "post-pull"
|
||||||
|
OnConflict = "on-conflict"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Runner runs hooks for one mount.
|
||||||
|
type Runner struct {
|
||||||
|
Root string // mount local root; hooks live at Root/.devbox/hooks/
|
||||||
|
Share string
|
||||||
|
Host string
|
||||||
|
Remote string
|
||||||
|
Timeout time.Duration
|
||||||
|
Stdout io.Writer // hook stdout (defaults to os.Stdout)
|
||||||
|
Stderr io.Writer // hook stderr (defaults to os.Stderr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// New returns a Runner for a mount with the default timeout.
|
||||||
|
func New(root, share, host, remote string) *Runner {
|
||||||
|
return &Runner{Root: root, Share: share, Host: host, Remote: remote, Timeout: DefaultTimeout}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run executes the hook for event if a matching executable script exists. The
|
||||||
|
// changed file list is written to a temp file referenced by DEVBOX_CHANGED_FILES.
|
||||||
|
// Returns nil if no hook exists; otherwise the hook's error (non-nil from a pre-*
|
||||||
|
// hook means the caller should veto that step). Nil receiver is a no-op.
|
||||||
|
func (r *Runner) Run(event string, changed []string, snapshot string) error {
|
||||||
|
if r == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
script, interp, ok := r.find(event)
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp, err := os.CreateTemp("", "devbox-changes-*")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer os.Remove(tmp.Name())
|
||||||
|
if len(changed) > 0 {
|
||||||
|
_, _ = tmp.WriteString(strings.Join(changed, "\n") + "\n")
|
||||||
|
}
|
||||||
|
tmp.Close()
|
||||||
|
|
||||||
|
timeout := r.Timeout
|
||||||
|
if timeout <= 0 {
|
||||||
|
timeout = DefaultTimeout
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
cmd := exec.CommandContext(ctx, interp, script)
|
||||||
|
cmd.Dir = r.Root
|
||||||
|
cmd.Env = append(os.Environ(),
|
||||||
|
"DEVBOX_EVENT="+event,
|
||||||
|
"DEVBOX_MOUNT="+r.Root,
|
||||||
|
"DEVBOX_SHARE="+r.Share,
|
||||||
|
"DEVBOX_HOST="+r.Host,
|
||||||
|
"DEVBOX_REMOTE="+r.Remote,
|
||||||
|
"DEVBOX_SNAPSHOT="+snapshot,
|
||||||
|
"DEVBOX_CHANGED_FILES="+tmp.Name(),
|
||||||
|
)
|
||||||
|
cmd.Stdout = orElse(r.Stdout, os.Stdout)
|
||||||
|
cmd.Stderr = orElse(r.Stderr, os.Stderr)
|
||||||
|
// ponytail: after a timeout kill, bound how long Wait blocks on a child that
|
||||||
|
// inherited the output pipe (e.g. a `sleep`). Killing the whole process group
|
||||||
|
// is the upgrade path if orphaned grandchildren become a problem.
|
||||||
|
cmd.WaitDelay = 2 * time.Second
|
||||||
|
|
||||||
|
runErr := cmd.Run()
|
||||||
|
if ctx.Err() == context.DeadlineExceeded {
|
||||||
|
return fmt.Errorf("hook %s timed out after %s", event, timeout)
|
||||||
|
}
|
||||||
|
if runErr != nil {
|
||||||
|
return fmt.Errorf("hook %s: %w", event, runErr)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// find locates the hook script + interpreter: an executable <event>.ps1 (pwsh)
|
||||||
|
// or an executable <event> (bash).
|
||||||
|
func (r *Runner) find(event string) (script, interp string, ok bool) {
|
||||||
|
dir := filepath.Join(r.Root, ".devbox", "hooks")
|
||||||
|
if ps := filepath.Join(dir, event+".ps1"); executable(ps) {
|
||||||
|
return ps, "pwsh", true
|
||||||
|
}
|
||||||
|
if base := filepath.Join(dir, event); executable(base) {
|
||||||
|
return base, "bash", true
|
||||||
|
}
|
||||||
|
return "", "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
func executable(path string) bool {
|
||||||
|
info, err := os.Stat(path)
|
||||||
|
if err != nil || info.IsDir() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
return true // no exec bit on Windows
|
||||||
|
}
|
||||||
|
return info.Mode().Perm()&0o111 != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func orElse(w, def io.Writer) io.Writer {
|
||||||
|
if w == nil {
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
return w
|
||||||
|
}
|
||||||
83
internal/hooks/hooks_test.go
Normal file
83
internal/hooks/hooks_test.go
Normal file
|
|
@ -0,0 +1,83 @@
|
||||||
|
package hooks
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func writeHook(t *testing.T, root, event, script string) {
|
||||||
|
t.Helper()
|
||||||
|
dir := filepath.Join(root, ".devbox", "hooks")
|
||||||
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(filepath.Join(dir, event), []byte(script), 0o755); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRunInjectsEnvAndChangedFiles(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
out := filepath.Join(root, "out.txt")
|
||||||
|
// Hook records its event, share, and the changed-files contents.
|
||||||
|
writeHook(t, root, "post-pull", "#!/usr/bin/env bash\n"+
|
||||||
|
"{ echo \"$DEVBOX_EVENT $DEVBOX_SHARE $DEVBOX_SNAPSHOT\"; cat \"$DEVBOX_CHANGED_FILES\"; } > '"+out+"'\n")
|
||||||
|
|
||||||
|
r := New(root, "projects", "myhost", "hub.example")
|
||||||
|
r.Stdout, r.Stderr = io.Discard, io.Discard
|
||||||
|
if err := r.Run("post-pull", []string{"a.txt", "sub/b.go"}, "snap123"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
b, err := os.ReadFile(out)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
got := string(b)
|
||||||
|
want := "post-pull projects snap123\na.txt\nsub/b.go\n"
|
||||||
|
if got != want {
|
||||||
|
t.Fatalf("hook output = %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRunAbsentHookIsNoOp(t *testing.T) {
|
||||||
|
r := New(t.TempDir(), "s", "h", "r")
|
||||||
|
if err := r.Run("pre-push", nil, ""); err != nil {
|
||||||
|
t.Fatalf("absent hook should be a no-op, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPreHookVetoReturnsError(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
writeHook(t, root, "pre-push", "#!/usr/bin/env bash\nexit 3\n")
|
||||||
|
r := New(root, "s", "h", "r")
|
||||||
|
r.Stdout, r.Stderr = io.Discard, io.Discard
|
||||||
|
if err := r.Run("pre-push", nil, ""); err == nil {
|
||||||
|
t.Fatal("a non-zero pre-push hook must return an error (veto)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRunTimeoutKillsHook(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
writeHook(t, root, "post-pull", "#!/usr/bin/env bash\nsleep 5\n")
|
||||||
|
r := New(root, "s", "h", "r")
|
||||||
|
r.Stdout, r.Stderr = io.Discard, io.Discard
|
||||||
|
r.Timeout = 200 * time.Millisecond
|
||||||
|
start := time.Now()
|
||||||
|
err := r.Run("post-pull", nil, "")
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("a hanging hook must time out with an error")
|
||||||
|
}
|
||||||
|
if time.Since(start) > 4*time.Second {
|
||||||
|
t.Fatal("hook was not killed promptly on timeout (should be ~timeout + WaitDelay, not 5s)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNilRunnerIsNoOp(t *testing.T) {
|
||||||
|
var r *Runner
|
||||||
|
if err := r.Run("post-pull", []string{"x"}, "s"); err != nil {
|
||||||
|
t.Fatalf("nil runner should be a no-op, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
93
internal/syncer/hooks_integration_test.go
Normal file
93
internal/syncer/hooks_integration_test.go
Normal file
|
|
@ -0,0 +1,93 @@
|
||||||
|
package syncer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"git.shoemoney.ai/shoemoney/devbox/internal/hooks"
|
||||||
|
"git.shoemoney.ai/shoemoney/devbox/internal/hub"
|
||||||
|
"git.shoemoney.ai/shoemoney/devbox/internal/hub/blobstore"
|
||||||
|
"git.shoemoney.ai/shoemoney/devbox/internal/hub/meta"
|
||||||
|
"git.shoemoney.ai/shoemoney/devbox/internal/secret"
|
||||||
|
)
|
||||||
|
|
||||||
|
func writeExecHook(t *testing.T, root, event, script string) {
|
||||||
|
t.Helper()
|
||||||
|
dir := filepath.Join(root, ".devbox", "hooks")
|
||||||
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(filepath.Join(dir, event), []byte(script), 0o755); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func quietRunner(root, share string) *hooks.Runner {
|
||||||
|
r := hooks.New(root, share, "host", "remote")
|
||||||
|
r.Stdout, r.Stderr = io.Discard, io.Discard
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPostPullHookFires(t *testing.T) {
|
||||||
|
db, _ := meta.Open(":memory:")
|
||||||
|
defer db.Close()
|
||||||
|
store, _ := blobstore.NewDisk(t.TempDir())
|
||||||
|
srv := httptest.NewServer(hub.NewServer(db, store).Handler())
|
||||||
|
defer srv.Close()
|
||||||
|
guard, _ := secret.New(nil)
|
||||||
|
ig, _ := LoadIgnore(t.TempDir())
|
||||||
|
|
||||||
|
A := joinDevice(t, db, srv.URL, "alice")
|
||||||
|
B := joinDevice(t, db, srv.URL, "bob")
|
||||||
|
_ = A.Publish("s")
|
||||||
|
rootA := t.TempDir()
|
||||||
|
writeFile(t, rootA, "app.txt", "v1\n")
|
||||||
|
if _, _, err := Sync(A, rootA, "s", "", "", "alice", 1, ig, guard, nil); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rootB := t.TempDir()
|
||||||
|
marker := filepath.Join(rootB, "hook-ran.txt")
|
||||||
|
writeExecHook(t, rootB, "post-pull", "#!/usr/bin/env bash\ncp \"$DEVBOX_CHANGED_FILES\" '"+marker+"'\n")
|
||||||
|
if _, _, err := Sync(B, rootB, "s", "", "", "bob", 2, ig, guard, quietRunner(rootB, "s")); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
b, err := os.ReadFile(marker)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("post-pull hook did not run: %v", err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(string(b), "app.txt") {
|
||||||
|
t.Fatalf("post-pull DEVBOX_CHANGED_FILES = %q, want it to list app.txt", string(b))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrePushHookVetoes(t *testing.T) {
|
||||||
|
db, _ := meta.Open(":memory:")
|
||||||
|
defer db.Close()
|
||||||
|
store, _ := blobstore.NewDisk(t.TempDir())
|
||||||
|
srv := httptest.NewServer(hub.NewServer(db, store).Handler())
|
||||||
|
defer srv.Close()
|
||||||
|
guard, _ := secret.New(nil)
|
||||||
|
ig, _ := LoadIgnore(t.TempDir())
|
||||||
|
|
||||||
|
C := joinDevice(t, db, srv.URL, "carol")
|
||||||
|
_ = C.Publish("s")
|
||||||
|
rootC := t.TempDir()
|
||||||
|
writeFile(t, rootC, "x.txt", "hi\n")
|
||||||
|
writeExecHook(t, rootC, "pre-push", "#!/usr/bin/env bash\nexit 1\n")
|
||||||
|
|
||||||
|
res, err := Push(C, rootC, "s", "", ig, guard, "", quietRunner(rootC, "s"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !res.Vetoed {
|
||||||
|
t.Fatal("a pre-push hook exiting non-zero must veto the push")
|
||||||
|
}
|
||||||
|
if h, _ := C.Head("s"); h != "" {
|
||||||
|
t.Fatalf("a vetoed push must not advance head, got %q", h)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"git.shoemoney.ai/shoemoney/devbox/internal/hooks"
|
||||||
"git.shoemoney.ai/shoemoney/devbox/internal/ignore"
|
"git.shoemoney.ai/shoemoney/devbox/internal/ignore"
|
||||||
"git.shoemoney.ai/shoemoney/devbox/internal/manifest"
|
"git.shoemoney.ai/shoemoney/devbox/internal/manifest"
|
||||||
"git.shoemoney.ai/shoemoney/devbox/internal/secret"
|
"git.shoemoney.ai/shoemoney/devbox/internal/secret"
|
||||||
|
|
@ -19,6 +20,7 @@ type PullResult struct {
|
||||||
Deleted []string // files deleted to match the hub
|
Deleted []string // files deleted to match the hub
|
||||||
Conflicts []string // conflict copies created (local edits preserved beside canonical)
|
Conflicts []string // conflict copies created (local edits preserved beside canonical)
|
||||||
Skipped []string // paths a filesystem error skipped (e.g. file<->dir clash)
|
Skipped []string // paths a filesystem error skipped (e.g. file<->dir clash)
|
||||||
|
Vetoed bool // a pre-pull hook aborted applying inbound changes
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pull fetches the hub head manifest for share, three-way merges it against the
|
// Pull fetches the hub head manifest for share, three-way merges it against the
|
||||||
|
|
@ -30,7 +32,7 @@ type PullResult struct {
|
||||||
// - delete-vs-edit never loses the edit (the edit always survives).
|
// - delete-vs-edit never loses the edit (the edit always survives).
|
||||||
//
|
//
|
||||||
// Never destroys a byte: every losing local edit becomes a conflict copy.
|
// Never destroys a byte: every losing local edit becomes a conflict copy.
|
||||||
func Pull(c *transport.Client, root, share, subpath, base, host string, now int64, ig *ignore.Matcher, guard *secret.Guard) (PullResult, error) {
|
func Pull(c *transport.Client, root, share, subpath, base, host string, now int64, ig *ignore.Matcher, guard *secret.Guard, hk *hooks.Runner) (PullResult, error) {
|
||||||
head, err := c.Head(share)
|
head, err := c.Head(share)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return PullResult{}, err
|
return PullResult{}, err
|
||||||
|
|
@ -58,6 +60,13 @@ func Pull(c *transport.Client, root, share, subpath, base, host string, now int6
|
||||||
return PullResult{}, err
|
return PullResult{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pre-pull may veto (e.g. stop a running container before files change).
|
||||||
|
incoming := manifest.Diff(baseM, theirs)
|
||||||
|
pending := append(append(append([]string{}, incoming.Added...), incoming.Modified...), incoming.Deleted...)
|
||||||
|
if err := hk.Run(hooks.PrePull, pending, head); err != nil {
|
||||||
|
return PullResult{Base: base, Vetoed: true}, nil
|
||||||
|
}
|
||||||
|
|
||||||
bi, oi, ti := indexEntries(baseM), indexEntries(ours), indexEntries(theirs)
|
bi, oi, ti := indexEntries(baseM), indexEntries(ours), indexEntries(theirs)
|
||||||
for p := range unionKeys(bi, oi, ti) {
|
for p := range unionKeys(bi, oi, ti) {
|
||||||
be, bOK := bi[p]
|
be, bOK := bi[p]
|
||||||
|
|
@ -125,6 +134,11 @@ func Pull(c *transport.Client, root, share, subpath, base, host string, now int6
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(res.Conflicts) > 0 {
|
||||||
|
_ = hk.Run(hooks.OnConflict, res.Conflicts, head)
|
||||||
|
}
|
||||||
|
_ = hk.Run(hooks.PostPull, append(append([]string{}, res.Written...), res.Deleted...), head)
|
||||||
|
|
||||||
res.Base = head
|
res.Base = head
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
@ -132,17 +146,23 @@ func Pull(c *transport.Client, root, share, subpath, base, host string, now int6
|
||||||
// Sync brings a mount into agreement with the hub: pull+merge to head, then push
|
// Sync brings a mount into agreement with the hub: pull+merge to head, then push
|
||||||
// the merged local state; retries if the hub advanced underneath. Returns the new
|
// the merged local state; retries if the hub advanced underneath. Returns the new
|
||||||
// base snapshot and the pull result.
|
// base snapshot and the pull result.
|
||||||
func Sync(c *transport.Client, root, share, subpath, base, host string, now int64, ig *ignore.Matcher, guard *secret.Guard) (string, PullResult, error) {
|
func Sync(c *transport.Client, root, share, subpath, base, host string, now int64, ig *ignore.Matcher, guard *secret.Guard, hk *hooks.Runner) (string, PullResult, error) {
|
||||||
for attempt := 0; attempt < 5; attempt++ {
|
for attempt := 0; attempt < 5; attempt++ {
|
||||||
pr, err := Pull(c, root, share, subpath, base, host, now, ig, guard)
|
pr, err := Pull(c, root, share, subpath, base, host, now, ig, guard, hk)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return base, pr, err
|
return base, pr, err
|
||||||
}
|
}
|
||||||
|
if pr.Vetoed {
|
||||||
|
return base, pr, nil // pre-pull vetoed; wait for the next nudge
|
||||||
|
}
|
||||||
base = pr.Base
|
base = pr.Base
|
||||||
push, err := Push(c, root, share, subpath, ig, guard, base)
|
push, err := Push(c, root, share, subpath, ig, guard, base, hk)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return base, pr, err
|
return base, pr, err
|
||||||
}
|
}
|
||||||
|
if push.Vetoed {
|
||||||
|
return base, pr, nil // pre-push vetoed
|
||||||
|
}
|
||||||
if push.Conflict {
|
if push.Conflict {
|
||||||
base = push.Head // hub advanced; re-pull against the new head
|
base = push.Head // hub advanced; re-pull against the new head
|
||||||
continue
|
continue
|
||||||
|
|
|
||||||
|
|
@ -56,13 +56,13 @@ func TestTwoWayConvergeAndConflict(t *testing.T) {
|
||||||
// A creates foo + bar and syncs them up.
|
// A creates foo + bar and syncs them up.
|
||||||
writeFile(t, rootA, "foo.txt", "A1\n")
|
writeFile(t, rootA, "foo.txt", "A1\n")
|
||||||
writeFile(t, rootA, "bar.txt", "shared\n")
|
writeFile(t, rootA, "bar.txt", "shared\n")
|
||||||
baseA, _, err := Sync(A, rootA, "s", "", "", "alice", 1000, ig, guard)
|
baseA, _, err := Sync(A, rootA, "s", "", "", "alice", 1000, ig, guard, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("A sync: %v", err)
|
t.Fatalf("A sync: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// B clones the share into an empty dir.
|
// B clones the share into an empty dir.
|
||||||
baseB, _, err := Sync(B, rootB, "s", "", "", "bob", 1001, ig, guard)
|
baseB, _, err := Sync(B, rootB, "s", "", "", "bob", 1001, ig, guard, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("B sync: %v", err)
|
t.Fatalf("B sync: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -72,14 +72,14 @@ func TestTwoWayConvergeAndConflict(t *testing.T) {
|
||||||
|
|
||||||
// A edits foo.txt; it becomes the hub head.
|
// A edits foo.txt; it becomes the hub head.
|
||||||
writeFile(t, rootA, "foo.txt", "A2\n")
|
writeFile(t, rootA, "foo.txt", "A2\n")
|
||||||
baseA, _, err = Sync(A, rootA, "s", "", baseA, "alice", 1002, ig, guard)
|
baseA, _, err = Sync(A, rootA, "s", "", baseA, "alice", 1002, ig, guard, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("A sync 2: %v", err)
|
t.Fatalf("A sync 2: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// B edits the SAME file (still on the old base) and syncs -> conflict.
|
// B edits the SAME file (still on the old base) and syncs -> conflict.
|
||||||
writeFile(t, rootB, "foo.txt", "B2\n")
|
writeFile(t, rootB, "foo.txt", "B2\n")
|
||||||
_, prB2, err := Sync(B, rootB, "s", "", baseB, "bob", 1003, ig, guard)
|
_, prB2, err := Sync(B, rootB, "s", "", baseB, "bob", 1003, ig, guard, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("B sync 2: %v", err)
|
t.Fatalf("B sync 2: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -94,7 +94,7 @@ func TestTwoWayConvergeAndConflict(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// A syncs again and must receive B's conflict copy — full convergence.
|
// A syncs again and must receive B's conflict copy — full convergence.
|
||||||
if _, _, err = Sync(A, rootA, "s", "", baseA, "alice", 1004, ig, guard); err != nil {
|
if _, _, err = Sync(A, rootA, "s", "", baseA, "alice", 1004, ig, guard, nil); err != nil {
|
||||||
t.Fatalf("A sync 3: %v", err)
|
t.Fatalf("A sync 3: %v", err)
|
||||||
}
|
}
|
||||||
if read(t, rootA, "foo.txt") != "A2\n" {
|
if read(t, rootA, "foo.txt") != "A2\n" {
|
||||||
|
|
@ -126,13 +126,13 @@ func TestPullDeleteAndEditBeatsDelete(t *testing.T) {
|
||||||
|
|
||||||
writeFile(t, rootA, "keep.txt", "k\n")
|
writeFile(t, rootA, "keep.txt", "k\n")
|
||||||
writeFile(t, rootA, "gone.txt", "g\n")
|
writeFile(t, rootA, "gone.txt", "g\n")
|
||||||
baseA, _, _ := Sync(A, rootA, "s", "", "", "alice", 1, ig, guard)
|
baseA, _, _ := Sync(A, rootA, "s", "", "", "alice", 1, ig, guard, nil)
|
||||||
baseB, _, _ := Sync(B, rootB, "s", "", "", "bob", 2, ig, guard)
|
baseB, _, _ := Sync(B, rootB, "s", "", "", "bob", 2, ig, guard, nil)
|
||||||
|
|
||||||
// A deletes gone.txt; B (clone) should drop it on next sync.
|
// A deletes gone.txt; B (clone) should drop it on next sync.
|
||||||
os.Remove(filepath.Join(rootA, "gone.txt"))
|
os.Remove(filepath.Join(rootA, "gone.txt"))
|
||||||
baseA, _, _ = Sync(A, rootA, "s", "", baseA, "alice", 3, ig, guard)
|
baseA, _, _ = Sync(A, rootA, "s", "", baseA, "alice", 3, ig, guard, nil)
|
||||||
if _, _, err := Sync(B, rootB, "s", "", baseB, "bob", 4, ig, guard); err != nil {
|
if _, _, err := Sync(B, rootB, "s", "", baseB, "bob", 4, ig, guard, nil); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if exists(rootB, "gone.txt") {
|
if exists(rootB, "gone.txt") {
|
||||||
|
|
@ -158,7 +158,7 @@ func TestPullSkipsFileDirClash(t *testing.T) {
|
||||||
rootA := t.TempDir()
|
rootA := t.TempDir()
|
||||||
writeFile(t, rootA, "data", "hello\n")
|
writeFile(t, rootA, "data", "hello\n")
|
||||||
writeFile(t, rootA, "ok.txt", "fine\n")
|
writeFile(t, rootA, "ok.txt", "fine\n")
|
||||||
if _, _, err := Sync(A, rootA, "s", "", "", "alice", 1, ig, guard); err != nil {
|
if _, _, err := Sync(A, rootA, "s", "", "", "alice", 1, ig, guard, nil); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -167,7 +167,7 @@ func TestPullSkipsFileDirClash(t *testing.T) {
|
||||||
if err := os.MkdirAll(filepath.Join(rootB, "data", "child"), 0o755); err != nil {
|
if err := os.MkdirAll(filepath.Join(rootB, "data", "child"), 0o755); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
pr, err := Pull(B, rootB, "s", "", "", "bob", 2, ig, guard)
|
pr, err := Pull(B, rootB, "s", "", "", "bob", 2, ig, guard, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("pull must not abort on a file/dir clash: %v", err)
|
t.Fatalf("pull must not abort on a file/dir clash: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -201,14 +201,14 @@ func TestSubPathMount(t *testing.T) {
|
||||||
writeFile(t, rootA, "app/util.go", "package app\n")
|
writeFile(t, rootA, "app/util.go", "package app\n")
|
||||||
writeFile(t, rootA, "docs/readme.md", "# docs\n")
|
writeFile(t, rootA, "docs/readme.md", "# docs\n")
|
||||||
writeFile(t, rootA, "root.txt", "top\n")
|
writeFile(t, rootA, "root.txt", "top\n")
|
||||||
baseA, _, err := Sync(A, rootA, "proj", "", "", "alice", 1, ig, guard)
|
baseA, _, err := Sync(A, rootA, "proj", "", "", "alice", 1, ig, guard, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// B: SUB-PATH mount of proj/app -> empty dir; sees only app/, stripped.
|
// B: SUB-PATH mount of proj/app -> empty dir; sees only app/, stripped.
|
||||||
rootB := t.TempDir()
|
rootB := t.TempDir()
|
||||||
baseB, _, err := Sync(B, rootB, "proj", "app", "", "bob", 2, ig, guard)
|
baseB, _, err := Sync(B, rootB, "proj", "app", "", "bob", 2, ig, guard, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("B subpath sync: %v", err)
|
t.Fatalf("B subpath sync: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -221,12 +221,12 @@ func TestSubPathMount(t *testing.T) {
|
||||||
|
|
||||||
// B edits main.go and pushes: the splice must preserve docs/ and root.txt.
|
// B edits main.go and pushes: the splice must preserve docs/ and root.txt.
|
||||||
writeFile(t, rootB, "main.go", "package main // B-edit\n")
|
writeFile(t, rootB, "main.go", "package main // B-edit\n")
|
||||||
if _, _, err = Sync(B, rootB, "proj", "app", baseB, "bob", 3, ig, guard); err != nil {
|
if _, _, err = Sync(B, rootB, "proj", "app", baseB, "bob", 3, ig, guard, nil); err != nil {
|
||||||
t.Fatalf("B subpath push: %v", err)
|
t.Fatalf("B subpath push: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// A (whole share) gets B's edit AND still has docs/ + root.txt intact.
|
// A (whole share) gets B's edit AND still has docs/ + root.txt intact.
|
||||||
if _, _, err = Sync(A, rootA, "proj", "", baseA, "alice", 4, ig, guard); err != nil {
|
if _, _, err = Sync(A, rootA, "proj", "", baseA, "alice", 4, ig, guard, nil); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if read(t, rootA, "app/main.go") != "package main // B-edit\n" {
|
if read(t, rootA, "app/main.go") != "package main // B-edit\n" {
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.shoemoney.ai/shoemoney/devbox/internal/chunk"
|
"git.shoemoney.ai/shoemoney/devbox/internal/chunk"
|
||||||
|
"git.shoemoney.ai/shoemoney/devbox/internal/hooks"
|
||||||
"git.shoemoney.ai/shoemoney/devbox/internal/ignore"
|
"git.shoemoney.ai/shoemoney/devbox/internal/ignore"
|
||||||
"git.shoemoney.ai/shoemoney/devbox/internal/manifest"
|
"git.shoemoney.ai/shoemoney/devbox/internal/manifest"
|
||||||
"git.shoemoney.ai/shoemoney/devbox/internal/secret"
|
"git.shoemoney.ai/shoemoney/devbox/internal/secret"
|
||||||
|
|
@ -25,6 +26,7 @@ type Result struct {
|
||||||
Uploaded int // blobs actually uploaded (the hub lacked)
|
Uploaded int // blobs actually uploaded (the hub lacked)
|
||||||
Files int // files in the manifest
|
Files int // files in the manifest
|
||||||
Conflict bool // hub rejected the push: device is behind head (pull + reconcile)
|
Conflict bool // hub rejected the push: device is behind head (pull + reconcile)
|
||||||
|
Vetoed bool // a pre-push hook aborted the push
|
||||||
}
|
}
|
||||||
|
|
||||||
// Push builds a manifest of root (filtered by ig + guard), uploads any blobs the
|
// Push builds a manifest of root (filtered by ig + guard), uploads any blobs the
|
||||||
|
|
@ -33,12 +35,20 @@ type Result struct {
|
||||||
// subtree is spliced into the share head (entries outside subpath are preserved)
|
// subtree is spliced into the share head (entries outside subpath are preserved)
|
||||||
// so a partial mount can push without dropping the rest of the share.
|
// so a partial mount can push without dropping the rest of the share.
|
||||||
// parent is the device's last-known head ("" if none).
|
// parent is the device's last-known head ("" if none).
|
||||||
func Push(c *transport.Client, root, share, subpath string, ig *ignore.Matcher, guard *secret.Guard, parent string) (Result, error) {
|
func Push(c *transport.Client, root, share, subpath string, ig *ignore.Matcher, guard *secret.Guard, parent string, hk *hooks.Runner) (Result, error) {
|
||||||
localM, blocked, err := manifest.Build(root, ig, guard)
|
localM, blocked, err := manifest.Build(root, ig, guard)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result{}, err
|
return Result{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
paths := make([]string, len(localM.Entries))
|
||||||
|
for i, e := range localM.Entries {
|
||||||
|
paths[i] = e.Path
|
||||||
|
}
|
||||||
|
if err := hk.Run(hooks.PrePush, paths, parent); err != nil {
|
||||||
|
return Result{Vetoed: true}, nil // pre-push hook vetoed; skip this push
|
||||||
|
}
|
||||||
|
|
||||||
// Gather the bytes of every distinct local chunk (with sizes).
|
// Gather the bytes of every distinct local chunk (with sizes).
|
||||||
blobs := map[string][]byte{}
|
blobs := map[string][]byte{}
|
||||||
refs := map[string]int64{} // local chunk hash -> size
|
refs := map[string]int64{} // local chunk hash -> size
|
||||||
|
|
@ -111,6 +121,9 @@ func Push(c *transport.Client, root, share, subpath string, ig *ignore.Matcher,
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Result{}, err
|
return Result{}, err
|
||||||
}
|
}
|
||||||
|
if !resp.Conflict {
|
||||||
|
_ = hk.Run(hooks.PostPush, paths, resp.Snapshot) // best-effort; never aborts
|
||||||
|
}
|
||||||
return Result{
|
return Result{
|
||||||
Snapshot: resp.Snapshot,
|
Snapshot: resp.Snapshot,
|
||||||
Head: resp.Head,
|
Head: resp.Head,
|
||||||
|
|
|
||||||
|
|
@ -98,7 +98,7 @@ func TestPushEndToEnd(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := Push(c, root, "projects", "", ig, guard, "")
|
res, err := Push(c, root, "projects", "", ig, guard, "", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("push: %v", err)
|
t.Fatalf("push: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -136,7 +136,7 @@ func TestPushEndToEnd(t *testing.T) {
|
||||||
|
|
||||||
// Second push of the unchanged tree: dedup means zero blobs uploaded and the
|
// Second push of the unchanged tree: dedup means zero blobs uploaded and the
|
||||||
// same content-addressed snapshot id.
|
// same content-addressed snapshot id.
|
||||||
res2, err := Push(c, root, "projects", "", ig, guard, res.Head)
|
res2, err := Push(c, root, "projects", "", ig, guard, res.Head, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("second push: %v", err)
|
t.Fatalf("second push: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -173,7 +173,7 @@ func TestPushConflictWhenBehind(t *testing.T) {
|
||||||
}
|
}
|
||||||
rootA := t.TempDir()
|
rootA := t.TempDir()
|
||||||
writeFile(t, rootA, "a.txt", "from alice\n")
|
writeFile(t, rootA, "a.txt", "from alice\n")
|
||||||
resA, err := Push(a, rootA, "shared", "", ig, guard, "")
|
resA, err := Push(a, rootA, "shared", "", ig, guard, "", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("A push: %v", err)
|
t.Fatalf("A push: %v", err)
|
||||||
}
|
}
|
||||||
|
|
@ -185,7 +185,7 @@ func TestPushConflictWhenBehind(t *testing.T) {
|
||||||
b := joinDevice(t, db, srv.URL, "bob")
|
b := joinDevice(t, db, srv.URL, "bob")
|
||||||
rootB := t.TempDir()
|
rootB := t.TempDir()
|
||||||
writeFile(t, rootB, "b.txt", "from bob\n")
|
writeFile(t, rootB, "b.txt", "from bob\n")
|
||||||
resB, err := Push(b, rootB, "shared", "", ig, guard, "")
|
resB, err := Push(b, rootB, "shared", "", ig, guard, "", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("B push: %v", err)
|
t.Fatalf("B push: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue