Artifact e37e529bc0c940f6e2f3f2b56702f603da419df94d27f9b482fbc4b14388c715:
- File
internal/checkout/setmtime_fossilbin_test.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: 5757) [annotate] [blame] [check-ins using] [more...]
package checkout import ( "os" "os/exec" "path/filepath" "strings" "testing" "time" libfossil "github.com/danmestas/go-libfossil/internal/fsltype" "github.com/danmestas/go-libfossil/internal/repo" "github.com/danmestas/go-libfossil/testutil" ) func runFossilIn(t *testing.T, bin, dir string, args ...string) string { t.Helper() cmd := exec.Command(bin, args...) cmd.Dir = dir out, err := cmd.CombinedOutput() if err != nil { t.Fatalf("fossil %s: %v\n%s", strings.Join(args, " "), err, out) } return string(out) } // setmtimeHistory commits three versions with fossil at fixed times, one on // a whole second: v1 adds a, b, c and e; v2 edits a; v3 edits b and adds d. // It returns the repository and the three versions' hashes. func setmtimeHistory(t *testing.T, bin string) (string, []string) { t.Helper() dir := t.TempDir() repoPath := filepath.Join(dir, "r.fossil") ck := filepath.Join(dir, "ck") if err := os.Mkdir(ck, 0o755); err != nil { t.Fatal(err) } runFossilIn(t, bin, dir, "init", repoPath) runFossilIn(t, bin, ck, "open", repoPath) write := func(name, body string) { if err := os.WriteFile(filepath.Join(ck, name), []byte(body), 0o644); err != nil { t.Fatal(err) } } write("a.txt", "a1\n") write("b.txt", "b1\n") write("c.txt", "c1\n") write("e.txt", "e1\n") runFossilIn(t, bin, ck, "add", "a.txt", "b.txt", "c.txt", "e.txt") runFossilIn(t, bin, ck, "commit", "-m", "v1", "--date-override", "2030-01-02 03:04:05.000") write("a.txt", "a2, longer\n") runFossilIn(t, bin, ck, "commit", "-m", "v2", "--date-override", "2030-01-02 03:05:06.789") write("b.txt", "b3, longer\n") write("d.txt", "d3\n") runFossilIn(t, bin, ck, "add", "d.txt") runFossilIn(t, bin, ck, "commit", "-m", "v3", "--date-override", "2030-01-02 03:06:07.500") runFossilIn(t, bin, ck, "close") out := runFossilIn(t, bin, dir, "sql", "-R", repoPath, "SELECT uuid FROM blob JOIN event ON rid=objid WHERE type='ci'"+ " AND comment GLOB 'v[123]' ORDER BY event.mtime") versions := strings.Fields(out) if len(versions) != 3 { t.Fatalf("want 3 versions, got %q", out) } return repoPath, versions } // fileTimes returns each named file's mtime in nanoseconds since 1970. func fileTimes(t *testing.T, dir string, names []string) map[string]int64 { t.Helper() out := map[string]int64{} for _, n := range names { info, err := os.Stat(filepath.Join(dir, n)) if err != nil { t.Fatal(err) } out[n] = info.ModTime().UnixNano() } return out } // sentinel is the mtime given to a locally edited file, which --setmtime // must leave alone. var sentinel = time.Date(2001, 2, 3, 4, 5, 6, 0, time.UTC) func editLocally(t *testing.T, path string) { t.Helper() if err := os.WriteFile(path, []byte("local edit\n"), 0o644); err != nil { t.Fatal(err) } if err := os.Chtimes(path, sentinel, sentinel); err != nil { t.Fatal(err) } } func ridOf(t *testing.T, r *repo.Repo, uuid string) libfossil.FslID { t.Helper() var rid int64 if err := r.DB().QueryRow("SELECT rid FROM blob WHERE uuid=?", uuid).Scan(&rid); err != nil { t.Fatal(err) } return libfossil.FslID(rid) } // TestSetMTimeMatchesFossil checks out the newest version, and updates to it // from the oldest with a local edit pending, with fossil (--setmtime) and with // go-libfossil (SetMTime). Fossil gives each unedited file the time of the // newest check-in in the version's primary ancestry that recorded its content, // in whole seconds, and leaves an edited file alone; both must agree. e.txt's // mlink rows are removed first: with no check-in recording a file, fossil's // max() is NULL, read as 0, and the file is set to 1970. func TestSetMTimeMatchesFossil(t *testing.T) { bin := testutil.RequireFossilBin(t) repoPath, v := setmtimeHistory(t, bin) runFossilIn(t, bin, filepath.Dir(repoPath), "sql", "-R", repoPath, "DELETE FROM mlink WHERE fnid=(SELECT fnid FROM filename WHERE name='e.txt')") names := []string{"a.txt", "b.txt", "c.txt", "d.txt", "e.txt"} fossilOpen := t.TempDir() runFossilIn(t, bin, fossilOpen, "open", repoPath, v[2], "--setmtime") fossilUpdate := t.TempDir() runFossilIn(t, bin, fossilUpdate, "open", repoPath, v[0]) editLocally(t, filepath.Join(fossilUpdate, "c.txt")) runFossilIn(t, bin, fossilUpdate, "update", v[2], "--setmtime") want := map[string]map[string]int64{ "extract": fileTimes(t, fossilOpen, names), "update": fileTimes(t, fossilUpdate, names), } runFossilIn(t, bin, fossilOpen, "close") runFossilIn(t, bin, fossilUpdate, "close", "--force") if want["update"]["c.txt"] != sentinel.UnixNano() { t.Fatalf("fossil changed the edited file's mtime; the test's premise is wrong") } r, err := repo.Open(repoPath) if err != nil { t.Fatal(err) } defer r.Close() got := map[string]map[string]int64{} goExtract := t.TempDir() co, err := Create(r, goExtract, CreateOpts{}) if err != nil { t.Fatal(err) } if err := co.Extract(ridOf(t, r, v[2]), ExtractOpts{SetMTime: true, Force: true}); err != nil { t.Fatal(err) } co.Close() got["extract"] = fileTimes(t, goExtract, names) goUpdate := t.TempDir() co, err = Create(r, goUpdate, CreateOpts{}) if err != nil { t.Fatal(err) } defer co.Close() if err := co.Extract(ridOf(t, r, v[0]), ExtractOpts{Force: true}); err != nil { t.Fatal(err) } editLocally(t, filepath.Join(goUpdate, "c.txt")) if _, err := co.Update(UpdateOpts{TargetRID: ridOf(t, r, v[2]), SetMTime: true}); err != nil { t.Fatal(err) } got["update"] = fileTimes(t, goUpdate, names) for _, op := range []string{"extract", "update"} { for _, n := range names { if got[op][n] != want[op][n] { t.Errorf("%s %s: mtime %s, fossil sets %s", op, n, time.Unix(0, got[op][n]).UTC(), time.Unix(0, want[op][n]).UTC()) } } } }