Some checks are pending
CI / vet-build-test (push) Waiting to run
- LICENSE: MIT (enables the adoption push; public repo was all-rights-reserved). - go.mod + all imports: git.shoemoney.ai/shoemoney/devbox -> github.com/shoemoney/devbox, so outsiders can 'go install github.com/shoemoney/devbox/cmd/devbox@latest'. Forgejo stays the self-hosted home; the registry (devbox-hub) + public dist (devbox-dist) are unchanged. vet + build + go test -race all green after the rename. - README: document the go install path. Security: full git-history scan (manual blob sweep + gitleaks, 84 commits) found ZERO secrets — Forgejo token never committed, no keys/passwords/private keys.
174 lines
5.3 KiB
Go
174 lines
5.3 KiB
Go
// Package syncer drives the device-side push: build a content-addressed manifest
|
|
// of a tree (filtered by .devignore and the secret guard), upload only the blobs
|
|
// the hub is missing, and commit a snapshot.
|
|
package syncer
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/shoemoney/devbox/internal/chunk"
|
|
"github.com/shoemoney/devbox/internal/hooks"
|
|
"github.com/shoemoney/devbox/internal/ignore"
|
|
"github.com/shoemoney/devbox/internal/manifest"
|
|
"github.com/shoemoney/devbox/internal/secret"
|
|
"github.com/shoemoney/devbox/internal/transport"
|
|
"github.com/shoemoney/devbox/pkg/proto"
|
|
)
|
|
|
|
// Result summarizes a push.
|
|
type Result struct {
|
|
Snapshot string
|
|
Head string
|
|
Blocked []string // secret files refused upload
|
|
Uploaded int // blobs actually uploaded (the hub lacked)
|
|
Files int // files in the manifest
|
|
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
|
|
// hub is missing (chunks + the manifest blob), then commits a snapshot on share.
|
|
// For a sub-path mount (subpath != "") root maps to share/subpath/: the local
|
|
// 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.
|
|
// 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, hk *hooks.Runner) (Result, error) {
|
|
localM, blocked, err := manifest.Build(root, ig, guard)
|
|
if err != nil {
|
|
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).
|
|
blobs := map[string][]byte{}
|
|
refs := map[string]int64{} // local chunk hash -> size
|
|
for _, e := range localM.Entries {
|
|
data, err := os.ReadFile(filepath.Join(root, filepath.FromSlash(e.Path)))
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
cs, err := chunk.Split(data)
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
for _, ch := range cs {
|
|
if _, ok := blobs[ch.Hash]; !ok {
|
|
blobs[ch.Hash] = data[ch.Offset : ch.Offset+ch.Size]
|
|
refs[ch.Hash] = ch.Size
|
|
}
|
|
}
|
|
}
|
|
|
|
// Splice the local subtree into the full share manifest (no-op when subpath == "").
|
|
fullM, err := splice(c, localM, subpath, parent)
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
manBytes, err := fullM.Marshal()
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
manHash := fullM.ID()
|
|
blobs[manHash] = manBytes
|
|
|
|
// Upload only what the hub lacks.
|
|
all := make([]string, 0, len(blobs))
|
|
for h := range blobs {
|
|
all = append(all, h)
|
|
}
|
|
missing, err := c.Have(all)
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
for _, h := range missing {
|
|
if err := c.PutBlob(h, blobs[h]); err != nil {
|
|
return Result{}, err
|
|
}
|
|
}
|
|
|
|
// Declare every distinct chunk the full manifest references so the hub's
|
|
// refcounts stay correct. Local chunks carry their size; chunks already on
|
|
// the hub (outside the subpath) carry 0 — the hub keeps the size it recorded
|
|
// on first upload and just bumps the refcount.
|
|
seen := map[string]bool{}
|
|
var chunks []proto.ChunkRef
|
|
for _, e := range fullM.Entries {
|
|
for _, h := range e.Chunks {
|
|
if seen[h] {
|
|
continue
|
|
}
|
|
seen[h] = true
|
|
chunks = append(chunks, proto.ChunkRef{Hash: h, Size: refs[h]})
|
|
}
|
|
}
|
|
|
|
resp, err := c.Push(proto.PushRequest{
|
|
Share: share,
|
|
Parent: parent,
|
|
ManifestHash: manHash,
|
|
Chunks: chunks,
|
|
})
|
|
if err != nil {
|
|
return Result{}, err
|
|
}
|
|
if !resp.Conflict {
|
|
_ = hk.Run(hooks.PostPush, paths, resp.Snapshot) // best-effort; never aborts
|
|
}
|
|
return Result{
|
|
Snapshot: resp.Snapshot,
|
|
Head: resp.Head,
|
|
Blocked: blocked,
|
|
Uploaded: len(missing),
|
|
Files: len(localM.Entries),
|
|
Conflict: resp.Conflict,
|
|
}, nil
|
|
}
|
|
|
|
// splice builds the full share manifest from a sub-path mount's local manifest:
|
|
// it re-prefixes local entries with subpath and keeps the parent snapshot's
|
|
// entries outside subpath. For a whole-share mount it returns localM unchanged.
|
|
func splice(c *transport.Client, localM manifest.Manifest, subpath, parent string) (manifest.Manifest, error) {
|
|
if subpath == "" {
|
|
return localM, nil
|
|
}
|
|
entries := make([]manifest.Entry, 0, len(localM.Entries))
|
|
if parent != "" {
|
|
base, err := fetchManifest(c, parent)
|
|
if err != nil {
|
|
return manifest.Manifest{}, err
|
|
}
|
|
for _, e := range base.Entries {
|
|
if !under(e.Path, subpath) {
|
|
entries = append(entries, e)
|
|
}
|
|
}
|
|
}
|
|
for _, e := range localM.Entries {
|
|
e.Path = prefixPath(e.Path, subpath)
|
|
entries = append(entries, e)
|
|
}
|
|
sort.Slice(entries, func(i, j int) bool { return entries[i].Path < entries[j].Path })
|
|
return manifest.Manifest{Entries: entries}, nil
|
|
}
|
|
|
|
// LoadIgnore compiles root/.devignore (an empty matcher if the file is absent).
|
|
func LoadIgnore(root string) (*ignore.Matcher, error) {
|
|
b, err := os.ReadFile(filepath.Join(root, ".devignore"))
|
|
if os.IsNotExist(err) {
|
|
return ignore.Compile(nil)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return ignore.Compile(strings.Split(string(b), "\n"))
|
|
}
|