Artifact 3e39a275248a3ee4fc62bd5d4848b3d57d173f47e28a7cb8779ee92cf8d67d29:
- File
cli/repo_diff.go
— part of check-in
[f5744359d3]
at
2026-04-19 16:30:54
on branch trunk
— Initial open source release
libfossil is a pure-Go implementation of the Fossil SCM, reading and writing the same .fossil SQLite repository format as upstream Fossil.
This is the first public release of code that previously lived in a private repository. The module path is github.com/danmestas/libfossil and the binary is named libfossil to avoid $PATH collisions with upstream fossil.
Included: - Repo lifecycle (create, clone), checkout/checkin, timeline, merge, diff, annotate, shun, tag, branch, bisect, stash, undo/redo - Sync-over-HTTP protocol (client and handler) - Two SQLite drivers: modernc (pure Go, default) and ncruces (wasm-capable) - Observer interfaces (SyncObserver, CheckoutObserver) with an optional OpenTelemetry implementation in observer/otel (separate submodule) - Deterministic simulation testing harness with BUGGIFY fault injection - Kong-based embeddable CLI under cli/ and a standalone binary under cmd/libfossil/ (user: [email protected], size: 1799) [annotate] [blame] [check-ins using]
package cli import ( "fmt" "os" "path/filepath" "github.com/danmestas/libfossil/internal/blob" "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 := blob.Exists(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 }