Artifact 8847c198255308a5051821c7095fc1ea70658a73002b3abca753d513f2fa3d19:
- File
internal/repo/repo.go
— part of check-in
[d810a8118f]
at
2026-10-18 20:01:53
on branch main
— fix: handle time exactly as fossil does (#442)
* fix(manifest): round a caller's check-in and tag time as fossil does
Fossil reads a --date-override through SQLite's strftime, which rounds the seconds to the millisecond (holding a fraction past .999 in its second) rather than truncating them. A caller time finer than a millisecond now gets the D card fossil would write for it; the default clock time is still truncated, as fossil reads its clock.
fsltype.CardTime and CardTimeNow are the one place both rules live, and the checkout commit path and branch creation take their default time through them.
* fix(repo): stamp config and user rows in seconds since 1970
Fossil writes config.mtime and user.mtime with its now() SQL function, time(0). SetConfig, SetHashPolicy and CreateUser wrote julianday('now') instead, so fossil read those rows as dating from 1970 when comparing times in a configuration merge, and SetCaps and SetPassword left the user's mtime alone where fossil's user command restamps it.
* fix(auth): read and write user.cexpire as a Julian day
Fossil keeps user.cexpire as a Julian day (src/login.c). A sync login parsed it as RFC 3339 or 'YYYY-MM-DD HH:MM:SS' text, so any value fossil had written failed to parse and the login was refused as expired. The expiry is now read through julianday(), which takes both forms, and repo invite --ttl writes a Julian day; its write error is now checked.
* fix(checkout): set file times as fossil's --setmtime does
Fossil's --setmtime gives each unedited file the time of the newest check-in, among the version and its primary ancestors, that recorded the file's content, truncated to whole seconds (mtime_of_manifest_file), and it does so for every unedited file of the version once the checkout is up to date (vfile_check_signature with CKSIG_SETMTIME). SetMTime gave every written file the target check-in's time to the millisecond, and an update left the files it did not write alone.
* fix(stash): keep and show stash and bisect times as fossil does
Fossil stores stash.ctime as julianday('now') and lists it with datetime(ctime); go-libfossil stored CURRENT_TIMESTAMP text, which the two drivers returned in different forms, and showed a fossil-written time as a bare number. bisect's list now shows datetime(event.mtime), as fossil's bisect ls does, instead of the Julian day.
* test(verify): fossil rebuild keeps the times go-libfossil stores
A repository go-libfossil wrote, with check-ins at caller and clock times, a tag and a branch, is rebuilt by the fossil binary, and every check-in event, plink and tagxref time must come back unchanged.
* test(sync): a fossil clone and push agree on every synced time
The fossil binary clones a go-libfossil server, unversioned files included, commits at a time finer than a millisecond and pushes back. Each side derives check-in event, plink and tagxref times from the artifacts itself, and the unversioned time travels on the wire; both repositories must then hold the same values.
* fix(resolve): bound NAME:TIMESTAMP with julianday() as fossil does
Fossil resolves BRANCH:TIMESTAMP with event.mtime<=julianday(BOUND). The bound was computed in the text form event rows hold, so on a millisecond where that form is larger than julianday()'s, a check-in at the bound itself resolved here and not in fossil.
* fix(manifest): round a caller's card time with integer arithmetic
CardTime followed SQLite's printing of the seconds as a double, which is not a well-defined rounding: at exact half-millisecond ties fossil 2.28's SQLite gives .124 for .1245 but .020 for .0195, and other SQLite versions differ again. A caller time is now rounded to the nearest millisecond on its nanoseconds, ties up, held at .999 as fossil holds it. The fossil binary test compares every non-tie value, across second, minute, day and year ends; ties are asserted on their own.
* fix(auth): ignore user.cexpire at sync login, as fossil does
user.cexpire is the expiry of fossil's web-login cookie: fossil sets it at web login, writes 0 at logout, and its sync login (check_login) never reads it. Refusing a sync login on it locked out anyone who had logged into or out of fossil's web UI, or whose cookie had lapsed.
The invite TTL that relied on it now lives in a config row of its own, invite-expire:LOGIN, as a UTC instant checked at sync login; repo invite no longer writes cexpire, and the expiry is no longer written in local time and read as UTC. Deleting a user drops its invite expiry.
Tracked in #441.
* fix(repo): convert Julian config and user mtimes on open
Repositories go-libfossil wrote before config and user rows were stamped in seconds since 1970 still hold Julian days there, which fossil reads as January 1970. Open converts those rows to the seconds they stand for. It checks first and writes only when a row needs it, so it converts once and takes no write lock on an up-to-date repository.
* fix(checkout): set a file no check-in recorded to 1970, as fossil does
With no mlink row for a file's artifact in the version's ancestry, fossil's max(event.mtime) is NULL, which mtime_of_manifest_file reads as 0, so --setmtime sets the file to 1970. SetMTime skipped the file.
* fix(stash): list stashes by creation time, as fossil's stash ls does
fossil stash ls orders by ctime, oldest first. List ordered by stash id, newest first; it now orders by ctime with the id breaking a tie.
* fix(auth): clear a login's invite expiry with the user, atomically
DeleteUser removed the user and its invite expiry in two statements; a failure between them left the user's expiry behind or the user without it. Both now go in one transaction. A user fossil deleted leaves its invite-expire row behind, so CreateUser clears it in the same transaction as the insert; repo invite sets its own expiry afterwards.
The on-open conversion of Julian config and user mtimes is now best-effort: while another connection holds the write lock, Open skips it and a later open converts the rows, instead of failing. (user: [email protected], size: 10017) [annotate] [blame] [check-ins using] [more...]
package repo import ( "fmt" "strconv" "strings" "github.com/danmestas/go-libfossil/db" "github.com/danmestas/go-libfossil/internal/content" libfossil "github.com/danmestas/go-libfossil/internal/fsltype" "github.com/danmestas/go-libfossil/internal/hash" "github.com/danmestas/go-libfossil/simio" ) type Repo struct { db *db.DB path string } // Create creates a new repo at path, seeded with the given user. // If projectCode is empty, a fresh project-code is generated; otherwise // projectCode must already be valid (40-char lowercase hex) — validation // is enforced at the libfossil.Create boundary. func Create(path string, user string, rng simio.Rand, projectCode string) (*Repo, error) { env := simio.RealEnv() env.Rand = rng return CreateWithEnv(path, user, env, projectCode) } func CreateWithEnv(path string, user string, env *simio.Env, projectCode string) (*Repo, error) { if path == "" { panic("repo.CreateWithEnv: path must not be empty") } if env == nil { env = simio.RealEnv() } if env.Rand == nil { panic("repo.CreateWithEnv: env.Rand must not be nil") } if env.Storage == nil { env.Storage = simio.OSStorage{} } if _, err := env.Storage.Stat(path); err == nil { return nil, fmt.Errorf("repo.CreateWithEnv: file already exists: %s", path) } d, err := db.Open(path) if err != nil { return nil, fmt.Errorf("repo.CreateWithEnv open: %w", err) } if err := db.CreateRepoSchema(d); err != nil { d.Close() if rmErr := env.Storage.Remove(path); rmErr != nil { return nil, fmt.Errorf("repo.CreateWithEnv: %w (cleanup failed: %v)", err, rmErr) } return nil, fmt.Errorf("repo.CreateWithEnv schema: %w", err) } if err := db.SeedUser(d, user); err != nil { d.Close() if rmErr := env.Storage.Remove(path); rmErr != nil { return nil, fmt.Errorf("repo.CreateWithEnv: %w (cleanup failed: %v)", err, rmErr) } return nil, fmt.Errorf("repo.CreateWithEnv seed user: %w", err) } if err := db.SeedConfig(d, env.Rand, projectCode); err != nil { d.Close() if rmErr := env.Storage.Remove(path); rmErr != nil { return nil, fmt.Errorf("repo.CreateWithEnv: %w (cleanup failed: %v)", err, rmErr) } return nil, fmt.Errorf("repo.CreateWithEnv seed config: %w", err) } // After SeedConfig, so a seeded Rand still yields the project and server // codes it did before these users were added. if err := db.SeedDefaultUsers(d, env.Rand); err != nil { d.Close() if rmErr := env.Storage.Remove(path); rmErr != nil { return nil, fmt.Errorf("repo.CreateWithEnv: %w (cleanup failed: %v)", err, rmErr) } return nil, fmt.Errorf("repo.CreateWithEnv seed default users: %w", err) } return &Repo{db: d, path: path}, nil } func Open(path string) (*Repo, error) { return OpenWithEnv(path, nil) } func OpenWithEnv(path string, env *simio.Env) (*Repo, error) { if path == "" { panic("repo.OpenWithEnv: path must not be empty") } if env == nil { env = simio.RealEnv() } if env.Storage == nil { env.Storage = simio.OSStorage{} } if err := checkExists(env, path); err != nil { return nil, fmt.Errorf("repo.OpenWithEnv: %w", err) } d, err := db.Open(path) if err != nil { return nil, fmt.Errorf("repo.OpenWithEnv: %w", err) } id, err := d.ApplicationID() if err != nil { d.Close() return nil, fmt.Errorf("repo.OpenWithEnv application_id: %w", err) } if id != libfossil.FossilApplicationID { d.Close() return nil, fmt.Errorf("repo.OpenWithEnv: not a fossil repo (application_id=%d, want %d)", id, libfossil.FossilApplicationID) } // The conversion is best-effort: while another connection holds the // write lock it is left for a later open, rather than failing this one. if err := upgradeJulianSeconds(d); err != nil && !isBusy(err) { d.Close() return nil, fmt.Errorf("repo.OpenWithEnv: %w", err) } return &Repo{db: d, path: path}, nil } // julianSecondsRows matches the config and user rows whose mtime is a Julian // day, as go-libfossil wrote it before writing seconds since 1970 as fossil // does. A Julian day now is about 2.46e6; as seconds that is January 1970, // a time no row fossil or go-libfossil writes today holds. const julianSecondsRows = "mtime BETWEEN 2000000 AND 3000000" // isBusy reports whether err is SQLite's SQLITE_BUSY or SQLITE_LOCKED, as // either driver words it. func isBusy(err error) bool { msg := err.Error() for _, s := range []string{"database is locked", "database table is locked", "SQLITE_BUSY", "SQLITE_LOCKED"} { if strings.Contains(msg, s) { return true } } return false } // upgradeJulianSeconds converts those rows to the seconds they stand for. // It reads first and writes only when a row needs it, so opening an // up-to-date repository takes no write lock. func upgradeJulianSeconds(d *db.DB) error { var n int err := d.QueryRow("SELECT (SELECT count(*) FROM config WHERE " + julianSecondsRows + ") + (SELECT count(*) FROM user WHERE " + julianSecondsRows + ")").Scan(&n) if err != nil { return fmt.Errorf("check julian mtimes: %w", err) } if n == 0 { return nil } return d.WithTx(func(tx *db.Tx) error { for _, table := range []string{"config", "user"} { if _, err := tx.Exec("UPDATE " + table + " SET mtime=CAST((mtime-2440587.5)*86400 AS INTEGER) WHERE " + julianSecondsRows); err != nil { return fmt.Errorf("convert %s julian mtimes: %w", table, err) } } return nil }) } func (r *Repo) Close() error { return r.db.Close() } // Checkpoint runs PRAGMA wal_checkpoint(<mode>) against the repo's database. // See db.DB.Checkpoint for semantics. func (r *Repo) Checkpoint(mode db.CheckpointMode) error { return r.db.Checkpoint(mode) } func (r *Repo) Path() string { return r.path } func (r *Repo) DB() *db.DB { return r.db } func (r *Repo) WithTx(fn func(tx *db.Tx) error) error { return r.db.WithTx(fn) } // WithBoundTx runs fn in one write transaction with a Repo bound to it, as // db.DB.WithBoundTx binds its DB: everything fn does through that Repo, // including the transactions code running on it opens, commits together when // fn returns nil and rolls back together otherwise. The bound Repo is valid // only until fn returns and must not be closed. func (r *Repo) WithBoundTx(fn func(bound *Repo) error) error { if fn == nil { panic("repo.WithBoundTx: fn must not be nil") } return r.db.WithBoundTx(func(b *db.DB) error { return fn(&Repo{db: b, path: r.path}) }) } // Config reads a configuration value from the repo's config table. func (r *Repo) Config(key string) (string, error) { var value string err := r.db.QueryRow("SELECT value FROM config WHERE name=?", key).Scan(&value) if err != nil { return "", fmt.Errorf("repo.Config %q: %w", key, err) } return value, nil } // UUIDByRID returns the UUID (manifest hash) of the artifact with the given // rid by querying the blob table. Returns sql.ErrNoRows (wrapped) when no // blob row exists for the rid. func (r *Repo) UUIDByRID(rid int64) (string, error) { var uuid string err := r.db.QueryRow("SELECT uuid FROM blob WHERE rid=?", rid).Scan(&uuid) if err != nil { return "", fmt.Errorf("repo.UUIDByRID rid=%d: %w", rid, err) } return uuid, nil } // SetConfig writes a configuration value to the repo's config table, stamped // with the time in seconds since 1970, as fossil's db_set stamps it (now()). // // A hash-policy value must be one of the ordinals 0 through 4, the only form // the reader accepts (AF-7.1-2). Fossil makes an unknown policy fatal where // the value comes in (hname.c hash_policy_command), so a bad value is refused // here instead of poisoning every later store. func (r *Repo) SetConfig(key, value string) error { if key == "hash-policy" { if n, err := strconv.Atoi(value); err != nil || n < 0 || n > 4 { return fmt.Errorf("repo.SetConfig: hash-policy %q is not a policy ordinal (0-4)", value) } } _, err := r.db.Exec( "INSERT OR REPLACE INTO config(name, value, mtime) VALUES(?, ?, strftime('%s','now'))", key, value, ) if err != nil { return fmt.Errorf("repo.SetConfig %q: %w", key, err) } return nil } // SetHashPolicy assigns the hash policy by its value string, as fossil's // hash-policy command does: name must be one of sha1, auto, sha3, sha3-only // or shun-sha1, matched case-sensitively and exactly, and anything else is // an error that leaves the stored policy alone (artifact-format-01 7.1, // AF-7.1-2). The ordinal is what is stored, the form the reader takes. // // Asking for auto in a repo that already holds a SHA3 artifact stores sha3 // (7.3, set-time coercion): auto would be promoted by the first SHA3 store // anyway, and fossil's command coerces the same way. func (r *Repo) SetHashPolicy(name string) error { if r == nil { panic("repo.SetHashPolicy: receiver must not be nil") } policy, err := hash.PolicyByName(name) if err != nil { return fmt.Errorf("repo.SetHashPolicy: %w", err) } if policy != db.HashPolicyAuto { return r.SetConfig("hash-policy", strconv.Itoa(policy)) } // The SHA3 check and the write are one statement, so a SHA3 artifact // stored concurrently cannot slip between them and have the write undo // the ratchet (7.3). _, err = r.db.Exec( "INSERT OR REPLACE INTO config(name, value, mtime) VALUES('hash-policy', "+ "CASE WHEN EXISTS(SELECT 1 FROM blob WHERE length(uuid)>40) THEN ? ELSE ? END, "+ "strftime('%s','now'))", strconv.Itoa(db.HashPolicySHA3), strconv.Itoa(db.HashPolicyAuto), ) if err != nil { return fmt.Errorf("repo.SetHashPolicy: %w", err) } return nil } func (r *Repo) Verify() error { if r == nil { panic("repo.Verify: receiver must not be nil") } rows, err := r.db.Query("SELECT rid FROM blob WHERE size >= 0") if err != nil { return fmt.Errorf("repo.Verify query: %w", err) } defer rows.Close() for rows.Next() { var rid int64 if err := rows.Scan(&rid); err != nil { return fmt.Errorf("repo.Verify scan: %w", err) } if err := content.Verify(r.db, libfossil.FslID(rid)); err != nil { return err } } return rows.Err() }