Artifact 941f62756321b259bb2592791b0a2df8ead81b259ce913b4518fcb2f0b24cd77:
- File
cli/repo_diff.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: 1763) [annotate] [blame] [check-ins using]
package cli import ( "fmt" "os" "path/filepath" "github.com/danmestas/libfossil/internal/content" "github.com/hexops/gotextdiff" "github.com/hexops/gotextdiff/myers" "github.com/hexops/gotextdiff/span" ) // RepoDiffCmd shows changes in the working directory vs a repository version. type RepoDiffCmd struct { Version string `arg:"" optional:"" help:"Version to diff against (default: tip)"` Dir string `short:"d" help:"Working directory to compare" default:"."` Unified int `short:"U" help:"Lines of context" default:"3"` } func (c *RepoDiffCmd) Run(g *Globals) error { r, err := g.OpenRepo() if err != nil { return err } defer r.Close() rid, err := resolveRID(r, c.Version) if err != nil { return err } files, err := r.ListFiles(rid) if err != nil { return err } db := r.Inner().DB() hasDiff := false for _, f := range files { diskPath := filepath.Join(c.Dir, f.Name) diskData, err := os.ReadFile(diskPath) if err != nil { if os.IsNotExist(err) { fmt.Printf("--- a/%s\n+++ /dev/null\n@@ deleted @@\n", f.Name) hasDiff = true continue } return err } fileRid, ok := content.AvailableByUUID(db, f.UUID) if !ok { return fmt.Errorf("blob %s not found for %s", f.UUID, f.Name) } repoContent, err := content.Expand(db, fileRid) if err != nil { return fmt.Errorf("%s: %w", f.Name, err) } repoStr := string(repoContent) diskStr := string(diskData) if repoStr == diskStr { continue } edits := myers.ComputeEdits(span.URIFromPath(f.Name), repoStr, diskStr) diff := fmt.Sprint(gotextdiff.ToUnified("a/"+f.Name, "b/"+f.Name, repoStr, edits)) if diff != "" { fmt.Print(diff) hasDiff = true } } if !hasDiff { fmt.Println("no changes") } return nil }