Artifact 3271511a9544f2664b40d2e69ef2b3d746fd96afe2fdbb3f78b94b8b190d8a31:
- File
cli/repo_merge.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: 4856) [annotate] [blame] [check-ins using]
package cli import ( "fmt" "os" "path/filepath" libfossil "github.com/danmestas/libfossil" "github.com/danmestas/libfossil/db" "github.com/danmestas/libfossil/internal/content" "github.com/danmestas/libfossil/internal/fsltype" "github.com/danmestas/libfossil/internal/merge" ) // RepoMergeCmd merges a divergent version into the current checkout. type RepoMergeCmd struct { Version string `arg:"" help:"Version to merge into current checkout"` Strategy string `help:"Override merge strategy for all files"` DryRun bool `help:"Show what would be merged without writing"` Dir string `short:"d" help:"Checkout directory" default:"."` } func (c *RepoMergeCmd) Run(g *Globals) error { r, err := g.OpenRepo() if err != nil { return err } defer r.Close() localRid, err := resolveRID(r, "tip") if err != nil { return fmt.Errorf("resolving local tip: %w", err) } remoteRid, err := resolveRID(r, c.Version) if err != nil { return fmt.Errorf("resolving remote version: %w", err) } inner := r.Inner() idb := inner.DB() ancestorRid, err := r.FindCommonAncestor(localRid, remoteRid) if err != nil { return fmt.Errorf("finding common ancestor: %w", err) } var ancestorUUID string idb.QueryRow("SELECT uuid FROM blob WHERE rid=?", ancestorRid).Scan(&ancestorUUID) if len(ancestorUUID) > 10 { ancestorUUID = ancestorUUID[:10] } fmt.Printf("common ancestor: %s (rid=%d)\n", ancestorUUID, ancestorRid) baseFiles, err := r.ListFiles(ancestorRid) if err != nil { return fmt.Errorf("listing ancestor files: %w", err) } localFiles, err := r.ListFiles(localRid) if err != nil { return fmt.Errorf("listing local files: %w", err) } remoteFiles, err := r.ListFiles(remoteRid) if err != nil { return fmt.Errorf("listing remote files: %w", err) } baseMap := toFileMap(baseFiles) localMap := toFileMap(localFiles) remoteMap := toFileMap(remoteFiles) resolver := merge.LoadResolver(inner, fsltype.FslID(localRid)) ckout, err := openCheckout(c.Dir) if err != nil && !c.DryRun { return err } if ckout != nil { defer ckout.Close() } vid, _ := checkoutVid(ckout) merged, conflicts := 0, 0 allFiles := make(map[string]bool) for name := range localMap { allFiles[name] = true } for name := range remoteMap { allFiles[name] = true } for name := range allFiles { localUUID := localMap[name] remoteUUID := remoteMap[name] baseUUID := baseMap[name] if localUUID == remoteUUID { continue } stratName := c.Strategy if stratName == "" { stratName = resolver.Resolve(name) } strat, ok := merge.StrategyByName(stratName) if !ok { return fmt.Errorf("unknown strategy %q for %s", stratName, name) } baseContent := loadBlobByUUID(idb, baseUUID) localContent := loadBlobByUUID(idb, localUUID) remoteContent := loadBlobByUUID(idb, remoteUUID) if c.DryRun { fmt.Printf(" [%s] %s\n", stratName, name) continue } result, err := strat.Merge(baseContent, localContent, remoteContent) if err != nil { return fmt.Errorf("merging %s: %w", name, err) } outPath := filepath.Join(c.Dir, name) os.MkdirAll(filepath.Dir(outPath), 0o755) if result.Clean { os.WriteFile(outPath, result.Content, 0o644) if ckout != nil { ckout.Exec("UPDATE vfile SET chnged=1 WHERE pathname=? AND vid=?", name, vid) } fmt.Printf(" [merged] %s (%s)\n", name, stratName) merged++ } else if strat.Name() == "conflict-fork" { merge.EnsureConflictTable(inner) var baseRID, localRID, remoteRID int64 idb.QueryRow("SELECT rid FROM blob WHERE uuid=?", baseUUID).Scan(&baseRID) idb.QueryRow("SELECT rid FROM blob WHERE uuid=?", localUUID).Scan(&localRID) idb.QueryRow("SELECT rid FROM blob WHERE uuid=?", remoteUUID).Scan(&remoteRID) merge.RecordConflictFork(inner, name, baseRID, localRID, remoteRID) fmt.Printf(" [fork] %s (conflict-fork: all versions preserved)\n", name) conflicts++ } else { os.WriteFile(outPath, result.Content, 0o644) os.WriteFile(outPath+".LOCAL", localContent, 0o644) os.WriteFile(outPath+".BASELINE", baseContent, 0o644) os.WriteFile(outPath+".MERGE", remoteContent, 0o644) if ckout != nil { ckout.Exec("UPDATE vfile SET chnged=5 WHERE pathname=? AND vid=?", name, vid) } fmt.Printf(" [CONFLICT] %s (%s, %d regions)\n", name, stratName, len(result.Conflicts)) conflicts++ } } fmt.Printf("\n%d files merged, %d conflicts\n", merged, conflicts) return nil } func toFileMap(files []libfossil.FileEntry) map[string]string { m := make(map[string]string) for _, f := range files { m[f.Name] = f.UUID } return m } func loadBlobByUUID(d *db.DB, uuid string) []byte { if uuid == "" { return nil } rid, ok := content.AvailableByUUID(d, uuid) if !ok { return nil } data, err := content.Expand(d, rid) if err != nil { return nil } return data }