Artifact 083c590d36c34b1a01291e799282c48f5a1ef53c89e9f574c9c9a49da371f8da:
- File
internal/verify/rebuild.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: 2257) [annotate] [blame] [check-ins using]
package verify import ( "fmt" "time" "github.com/danmestas/libfossil/db" "github.com/danmestas/libfossil/internal/repo" ) // rebuildTableList is the set of derived tables dropped and reconstructed // during a rebuild. Order does not matter — they are all DELETEd first. var rebuildTableList = []string{ "event", "mlink", "plink", "tagxref", "filename", "leaf", "unclustered", "unsent", } // Rebuild reconstructs all derived tables from raw blob content. // It first verifies blob integrity (read-only), then drops and // rebuilds event/mlink/plink/tagxref/filename/leaf/unclustered/unsent // in a single transaction. func Rebuild(r *repo.Repo) (*Report, error) { if r == nil { panic("verify.Rebuild: nil *repo.Repo") } start := time.Now() report := &Report{} // Phase 1: verify blobs (read-only, outside transaction) if err := checkBlobs(r, report); err != nil { return nil, fmt.Errorf("verify.Rebuild: %w", err) } // Phases 2-4: drop + reconstruct in transaction if err := r.WithTx(func(tx *db.Tx) error { if err := dropDerivedTables(tx); err != nil { return err } if err := rebuildManifests(r, tx, report); err != nil { return fmt.Errorf("rebuild manifests: %w", err) } if err := rebuildTags(r, tx, report); err != nil { return fmt.Errorf("rebuild tags: %w", err) } if err := rebuildLeaves(tx); err != nil { return fmt.Errorf("rebuild leaves: %w", err) } if err := rebuildBookkeeping(tx); err != nil { return fmt.Errorf("rebuild bookkeeping: %w", err) } return nil }); err != nil { return nil, fmt.Errorf("verify.Rebuild: %w", err) } report.TablesRebuilt = rebuildTableList report.Duration = time.Since(start) // Postcondition: TablesRebuilt must be populated after successful rebuild. if len(report.TablesRebuilt) == 0 { panic("verify.Rebuild: postcondition: TablesRebuilt empty after successful rebuild") } return report, nil } // dropDerivedTables deletes all rows from the derived tables. func dropDerivedTables(tx *db.Tx) error { if tx == nil { panic("verify.dropDerivedTables: nil *db.Tx") } for _, table := range rebuildTableList { if _, err := tx.Exec("DELETE FROM " + table); err != nil { return fmt.Errorf("drop %s: %w", table, err) } } return nil }