Artifact 12dc9dca8f6666722cbde17f0511955881fd669d9e9383f1b3a38ea5d75c962c:
- File
internal/merge/resolve.go
— part of check-in
[670ca0e75f]
at
2026-07-19 16:00:09
on branch trunk
— fix: separate blob existence from blob availability (#43) (#52)
* fix: separate blob existence from blob availability (#43)
A phantom is a real blob row with size = -1 and NULL content, so blob.Exists — "SELECT rid FROM blob WHERE uuid=?" — answers true for it. Every guard built on Exists therefore admitted artifacts whose content cannot be read, and content.Expand aborted with "rid N is a phantom".
Canonical Fossil keeps two predicates and never substitutes one for the other: rid_from_uuid (existence, xfer.c:70) and content_is_available (availability, content.c:163). The latter is transitive — it follows delta.srcid up the chain, so a delta whose base is a phantom is unavailable even though every row in the chain exists.
Add content.IsAvailable / content.AvailableByUUID, ported from content_is_available including the chain walk, with an explicit depth bound so a cyclic delta table terminates instead of hanging. The walk lives in exactly one place; no caller touches the delta table.
blob.Exists keeps its behavior and gains a doc comment stating that it answers existence only, returns true for phantoms, and naming the availability predicate as the alternative.
manifest.blobPresent is deleted. It was a local patch at one call site that tested size >= 0 without the chain walk, so it still reported a delta-on-phantom as present — the same class of bug it was written to work around.
Every blob.Exists call site was classified as existence or availability and converted where it guards a content read. Sites that want rid identity — plink/mlink/tagxref population, cluster phantom creation, private-status bookkeeping, the post-store sanity check in handleFile, and phantom filling in storeReceivedFile — keep existence, since delta-before-base is a normal steady state during clone.
Verified against a loopback fossil 2.28 server: the 23-artifact repro that previously failed roughly half the time now clones 6 of 6.
* fix: IsAvailable must fail closed when the delta lookup errors
Only sql.ErrNoRows means the chain is grounded. Any other failure — cancelled context, closed connection, NULL srcid — says nothing about groundedness, so returning true there claims readable content that was never verified. That is the same class of bug this predicate exists to prevent, and it runs on data arriving over the wire during clone.
The blob-size lookup above already fails closed on the same pattern; this restores the symmetry. (user: [email protected], size: 2139) [annotate] [blame] [check-ins using]
package merge import ( "bufio" "path/filepath" "strings" libfossil "github.com/danmestas/libfossil/internal/fsltype" "github.com/danmestas/libfossil/internal/content" "github.com/danmestas/libfossil/internal/manifest" "github.com/danmestas/libfossil/internal/repo" ) // PatternRule maps a glob pattern to a strategy name. type PatternRule struct { Glob string Strategy string } // Resolver picks the merge strategy for a given filename. type Resolver struct { patterns []PatternRule fallback string } // LoadResolver reads the .libfossil-merge file from the repo at the given // version, plus the merge-strategy config key as fallback. func LoadResolver(r *repo.Repo, tipRid libfossil.FslID) *Resolver { if r == nil { panic("merge.LoadResolver: r must not be nil") } res := &Resolver{fallback: "three-way"} var cfgDefault string if r.DB().QueryRow("SELECT value FROM config WHERE name='merge-strategy'").Scan(&cfgDefault) == nil && cfgDefault != "" { res.fallback = cfgDefault } if tipRid > 0 { files, err := manifest.ListFiles(r, tipRid) if err == nil { for _, f := range files { if f.Name == ".libfossil-merge" { rid, ok := content.AvailableByUUID(r.DB(), f.UUID) if ok { data, err := content.Expand(r.DB(), rid) if err == nil { res.patterns = parseMergeFile(data) } } break } } } } return res } // Resolve returns the strategy name for a filename. func (res *Resolver) Resolve(filename string) string { for _, p := range res.patterns { matched, matchErr := filepath.Match(p.Glob, filepath.Base(filename)) if matchErr != nil { continue } if matched { return p.Strategy } } return res.fallback } func parseMergeFile(data []byte) []PatternRule { var rules []PatternRule scanner := bufio.NewScanner(strings.NewReader(string(data))) for scanner.Scan() { line := strings.TrimSpace(scanner.Text()) if line == "" || strings.HasPrefix(line, "#") { continue } parts := strings.Fields(line) if len(parts) >= 2 { rules = append(rules, PatternRule{Glob: parts[0], Strategy: parts[1]}) } } return rules }