Artifact a7643f9cd76ef21b1e74f9b1c27f095765a07ced8cef3f7fa18bca7dbdb336f4:
- File
internal/manifest/log.go
— part of check-in
[2c2e5e74b6]
at
2026-10-09 15:35:38
on branch trunk
— Store check-in, link and tag rows as fossil does (#258) (#261)
* fix(manifest): store check-in, link and tag rows as fossil does (#258)
A rebuilt or locally written repository differed from fossil's in three derived values: event and plink times in their last digits, event.omtime left NULL, and a valueless tag stored as '' instead of NULL.
Fossil stores a tag time as SQLite's julianday() bound as a double, but writes event, plink, attachment and forumpost times into SQL text with its own printf("%.17g"), which emits 16 digits in long double arithmetic. TimeToJulian is now julianday() exactly (whole milliseconds over 86400000) and TimeToTextJulian ports fossil's printf conversion, so both match fossil bit for bit. The conversion follows builds where long double is a double (macOS, Windows); fossil itself differs across platforms in the last digit.
Check-in events record omtime, tag writes bind NULL for an empty value, the readers of event and plink times under the ncruces driver rebuild the text form, and the CLI's tag listing reads NULL values.
* test(db): sweep both stored julian forms; scan the text form from driver times (#258)
* test(verify): compare text times bit for bit only where fossil computes them in double (#258) (user: [email protected], size: 4087) [annotate] [blame] [check-ins using]
package manifest import ( "database/sql" "fmt" "time" "github.com/danmestas/go-libfossil/db" libfossil "github.com/danmestas/go-libfossil/internal/fsltype" "github.com/danmestas/go-libfossil/internal/repo" ) type LogOpts struct { Start libfossil.FslID Limit int } // LogEntry represents a single event as returned by Log (the ancestry // walk). It carries no pagination cursor: Log paginates by Start/Limit, // not by cursor, so there is nothing valid to put in one. See // TimelineEntry, Timeline's result type, for the cursor-carrying // counterpart. type LogEntry struct { RID libfossil.FslID UUID string Comment string User string Time time.Time Kind libfossil.EventKind Parents []string } // maxAncestryDepth bounds the first-parent walk in Log so that a cyclic or // otherwise corrupt plink table cannot hang the process (TigerStyle: every // loop needs an explicit, defensible bound). It is a var rather than a // const so tests can shrink it to exercise the bound deterministically // without walking a million rows. No real fossil repository's primary-parent // chain approaches this depth. var maxAncestryDepth = 1_000_000 func Log(r *repo.Repo, opts LogOpts) ([]LogEntry, error) { if r == nil { panic("manifest.Log: r must not be nil") } if opts.Start <= 0 { return nil, fmt.Errorf("manifest.Log: invalid start rid %d", opts.Start) } var entries []LogEntry current := opts.Start for depth := 0; ; depth++ { if depth >= maxAncestryDepth { return nil, fmt.Errorf("manifest.Log: exceeded max ancestry depth %d starting at rid=%d (possible plink cycle)", maxAncestryDepth, opts.Start) } if opts.Limit > 0 && len(entries) >= opts.Limit { break } // event.user and event.comment are nullable in fossil's schema: the // U-card and C-card are optional on a check-in manifest (the only // required cards are "D" and "Z"). A NULL here is valid data, not // malformed input, so scan through sql.NullString and let its zero // value ("") stand in for "no value recorded" — the same substitution // fossil itself makes when reading these columns. var uuid string var user, comment sql.NullString var mtimeScanned any err := r.DB().QueryRow( "SELECT b.uuid, e.user, e.comment, e.mtime FROM blob b JOIN event e ON e.objid=b.rid WHERE b.rid=?", current, ).Scan(&uuid, &user, &comment, &mtimeScanned) if err != nil { return nil, fmt.Errorf("manifest.Log: rid=%d: %w", current, err) } // mtime is a julianday float. modernc returns float64; // ncruces returns time.Time for DATETIME/TIMESTAMP columns. Handle both. mtime, ok := db.ScanTextJulianDay(mtimeScanned) if !ok { return nil, fmt.Errorf("manifest.Log: rid=%d: unexpected mtime type %T", current, mtimeScanned) } entries = append(entries, LogEntry{ RID: current, UUID: uuid, Comment: comment.String, User: user.String, Time: libfossil.JulianToTime(mtime), Kind: libfossil.EventKindCheckin, Parents: parentUUIDs(r, current), }) var parentRid int64 if err := r.DB().QueryRow( "SELECT pid FROM plink WHERE cid=? AND isprim=1", current, ).Scan(&parentRid); err != nil { break } current = libfossil.FslID(parentRid) } return entries, nil } // parentUUIDs returns the UUIDs of cid's parents (primary parent first), // via the plink table. Best-effort: a query or scan failure yields a nil // (empty) result rather than aborting the caller's walk or enumeration — // matching the original manifest.Log behavior this was extracted from. // Shared by Log (ancestry walk) and Timeline (enumeration) so entry // construction is not duplicated between the two query paths. func parentUUIDs(r *repo.Repo, cid libfossil.FslID) []string { var parents []string rows, err := r.DB().Query( "SELECT b.uuid FROM plink p JOIN blob b ON b.rid=p.pid WHERE p.cid=? ORDER BY p.isprim DESC", cid, ) if err != nil { return parents } defer rows.Close() for rows.Next() { var puuid string if err := rows.Scan(&puuid); err != nil { continue } parents = append(parents, puuid) } return parents }