Artifact c29f0173328c27ad39eae38e4072a82600de0506f75b0f51627d40ed5c3beddf:
- File
internal/stash/stash_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: 13677) [annotate] [blame] [check-ins using] [more...]
//go:build !js package stash import ( "database/sql" "os" "path/filepath" "reflect" "regexp" "strings" "testing" libdb "github.com/danmestas/go-libfossil/db" "github.com/danmestas/go-libfossil/internal/blob" "github.com/danmestas/go-libfossil/internal/hash" _ "github.com/danmestas/go-libfossil/internal/testdriver" ) // testEnv sets up a repo DB with one blob ("hello" in a.txt), a checkout DB // with vvar/vfile/vmerge tables, writes a.txt to disk, and returns both DBs, // the working directory, and the rid+uuid of the stored blob. func testEnv(t *testing.T) (repoDB *sql.DB, ckout *sql.DB, dir string) { t.Helper() dir = t.TempDir() // --- Repo DB --- repoPath := filepath.Join(dir, "repo.fossil") var err error repoDB, err = sql.Open(libdb.RegisteredDriver().Name, repoPath) if err != nil { t.Fatalf("open repo db: %v", err) } t.Cleanup(func() { repoDB.Close() }) repoSchema := []string{ `CREATE TABLE blob( rid INTEGER PRIMARY KEY, rcvid INTEGER, size INTEGER, uuid TEXT UNIQUE NOT NULL, content BLOB, CHECK( length(uuid)>=40 AND rid>0 ) )`, `CREATE TABLE delta( rid INTEGER PRIMARY KEY, srcid INTEGER NOT NULL REFERENCES blob )`, `CREATE INDEX delta_i1 ON delta(srcid)`, `INSERT INTO blob(rid, uuid, size, content) VALUES(0, '0000000000000000000000000000000000000000', 0, NULL)`, } // The CHECK constraint requires rid>0, so we need to insert rid=0 differently. // Actually let's just create schema and use blob.Store. repoSchema2 := []string{ `CREATE TABLE blob( rid INTEGER PRIMARY KEY, rcvid INTEGER, size INTEGER, uuid TEXT UNIQUE NOT NULL, content BLOB )`, `CREATE TABLE delta( rid INTEGER PRIMARY KEY, srcid INTEGER NOT NULL REFERENCES blob )`, `CREATE INDEX delta_i1 ON delta(srcid)`, `CREATE TABLE unclustered(rid INTEGER PRIMARY KEY)`, } _ = repoSchema for _, s := range repoSchema2 { if _, err := repoDB.Exec(s); err != nil { t.Fatalf("repo schema: %v\n SQL: %s", err, s) } } // Store "hello" blob. rid, uuid, err := blob.Store(repoDB, []byte("hello")) if err != nil { t.Fatalf("blob.Store: %v", err) } // --- Checkout DB --- ckoutPath := filepath.Join(dir, ".fslckout") ckout, err = sql.Open(libdb.RegisteredDriver().Name, ckoutPath) if err != nil { t.Fatalf("open ckout db: %v", err) } t.Cleanup(func() { ckout.Close() }) ckoutSchema := []string{ `CREATE TABLE vvar(name TEXT PRIMARY KEY, value TEXT)`, `CREATE TABLE vfile( id INTEGER PRIMARY KEY, vid INTEGER, chnged INTEGER DEFAULT 0, deleted INTEGER DEFAULT 0, isexe BOOLEAN DEFAULT 0, islink BOOLEAN DEFAULT 0, rid INTEGER DEFAULT 0, mrid INTEGER DEFAULT 0, mtime REAL DEFAULT 0, pathname TEXT, origname TEXT )`, `CREATE TABLE vmerge(id INTEGER, merge INTEGER, mhash TEXT)`, } for _, s := range ckoutSchema { if _, err := ckout.Exec(s); err != nil { t.Fatalf("ckout schema: %v\n SQL: %s", err, s) } } // Populate vvar and vfile. if _, err := ckout.Exec("INSERT INTO vvar(name,value) VALUES('checkout','1')"); err != nil { t.Fatal(err) } if _, err := ckout.Exec("INSERT INTO vvar(name,value) VALUES('checkout-hash',?)", uuid); err != nil { t.Fatal(err) } if _, err := ckout.Exec("INSERT INTO vfile(id,vid,pathname,rid,mrid) VALUES(1,1,'a.txt',?,?)", int64(rid), int64(rid)); err != nil { t.Fatal(err) } // Write a.txt to disk with baseline content. if err := os.WriteFile(filepath.Join(dir, "a.txt"), []byte("hello"), 0o644); err != nil { t.Fatal(err) } return repoDB, ckout, dir } // modifyFile changes a.txt on disk and marks it changed in vfile. func modifyFile(t *testing.T, ckout *sql.DB, dir string) { t.Helper() if err := os.WriteFile(filepath.Join(dir, "a.txt"), []byte("hello world"), 0o644); err != nil { t.Fatal(err) } if _, err := ckout.Exec("UPDATE vfile SET chnged=1 WHERE pathname='a.txt'"); err != nil { t.Fatal(err) } } func readFile(t *testing.T, path string) string { t.Helper() data, err := os.ReadFile(path) if err != nil { t.Fatalf("readFile(%s): %v", path, err) } return string(data) } func TestSaveAndList(t *testing.T) { repoDB, ckout, dir := testEnv(t) // Modify file. modifyFile(t, ckout, dir) // Save. if err := Save(ckout, repoDB, dir, "test stash"); err != nil { t.Fatalf("Save: %v", err) } // File should be reverted to baseline. got := readFile(t, filepath.Join(dir, "a.txt")) if got != "hello" { t.Errorf("after Save: a.txt = %q, want %q", got, "hello") } // vfile should show chnged=0. var chnged int if err := ckout.QueryRow("SELECT chnged FROM vfile WHERE pathname='a.txt'").Scan(&chnged); err != nil { t.Fatal(err) } if chnged != 0 { t.Errorf("after Save: vfile chnged = %d, want 0", chnged) } // List should have 1 entry. entries, err := List(ckout) if err != nil { t.Fatalf("List: %v", err) } if len(entries) != 1 { t.Fatalf("List: got %d entries, want 1", len(entries)) } if entries[0].Comment != "test stash" { t.Errorf("List: comment = %q, want %q", entries[0].Comment, "test stash") } // Verify baseline hash is stored. expectedHash := hash.SHA1([]byte("hello")) if entries[0].Hash != expectedHash { // Hash is checkout-hash from vvar, not file hash — it's the manifest UUID. // Just check it's not empty. } } func TestPopRestoresChanges(t *testing.T) { repoDB, ckout, dir := testEnv(t) modifyFile(t, ckout, dir) if err := Save(ckout, repoDB, dir, "pop test"); err != nil { t.Fatalf("Save: %v", err) } // Verify reverted. if got := readFile(t, filepath.Join(dir, "a.txt")); got != "hello" { t.Fatalf("after Save: a.txt = %q, want %q", got, "hello") } // Pop. if err := Pop(ckout, repoDB, dir); err != nil { t.Fatalf("Pop: %v", err) } // File should have modified content. got := readFile(t, filepath.Join(dir, "a.txt")) if got != "hello world" { t.Errorf("after Pop: a.txt = %q, want %q", got, "hello world") } // List should be empty. entries, err := List(ckout) if err != nil { t.Fatalf("List: %v", err) } if len(entries) != 0 { t.Errorf("after Pop: List has %d entries, want 0", len(entries)) } } func TestApplyKeepsStash(t *testing.T) { repoDB, ckout, dir := testEnv(t) modifyFile(t, ckout, dir) if err := Save(ckout, repoDB, dir, "apply test"); err != nil { t.Fatalf("Save: %v", err) } // Get the stash ID. entries, err := List(ckout) if err != nil { t.Fatal(err) } if len(entries) != 1 { t.Fatalf("expected 1 entry, got %d", len(entries)) } // Apply. if err := Apply(ckout, repoDB, dir, entries[0].ID); err != nil { t.Fatalf("Apply: %v", err) } // File should have modified content. got := readFile(t, filepath.Join(dir, "a.txt")) if got != "hello world" { t.Errorf("after Apply: a.txt = %q, want %q", got, "hello world") } // List should still have 1 entry. entries, err = List(ckout) if err != nil { t.Fatal(err) } if len(entries) != 1 { t.Errorf("after Apply: List has %d entries, want 1", len(entries)) } } func TestDropAndClear(t *testing.T) { repoDB, ckout, dir := testEnv(t) // Save twice (need to re-modify between saves). modifyFile(t, ckout, dir) if err := Save(ckout, repoDB, dir, "first"); err != nil { t.Fatalf("Save 1: %v", err) } modifyFile(t, ckout, dir) if err := Save(ckout, repoDB, dir, "second"); err != nil { t.Fatalf("Save 2: %v", err) } entries, err := List(ckout) if err != nil { t.Fatal(err) } if len(entries) != 2 { t.Fatalf("expected 2 entries, got %d", len(entries)) } // Drop first (oldest = first in list, which is ordered by creation). firstID := entries[0].ID if err := Drop(ckout, firstID); err != nil { t.Fatalf("Drop: %v", err) } entries, err = List(ckout) if err != nil { t.Fatal(err) } if len(entries) != 1 { t.Fatalf("after Drop: expected 1 entry, got %d", len(entries)) } if entries[0].Comment != "second" { t.Errorf("remaining entry comment = %q, want %q", entries[0].Comment, "second") } // Clear. if err := Clear(ckout); err != nil { t.Fatalf("Clear: %v", err) } entries, err = List(ckout) if err != nil { t.Fatal(err) } if len(entries) != 0 { t.Errorf("after Clear: expected 0 entries, got %d", len(entries)) } } // Save cannot yet stash a pending merge: its revert path would rewrite the // committed version and drop the merge. It must refuse, naming the file, // rather than silently leave the merged file out of the stash (#233). func TestSaveRefusesPendingMerge(t *testing.T) { repoDB, ckout, dir := testEnv(t) modifyFile(t, ckout, dir) if _, err := ckout.Exec("UPDATE vfile SET chnged=2 WHERE pathname='a.txt'"); err != nil { t.Fatal(err) } err := Save(ckout, repoDB, dir, "merge") if err == nil || !strings.Contains(err.Error(), "merge") { t.Fatalf("Save over a pending merge = %v, want a pending-merge refusal", err) } if got := readFile(t, filepath.Join(dir, "a.txt")); got != "hello world" { t.Fatalf("a.txt = %q after refusal, want the merged content kept", got) } } // A clean three-way merge leaves a plain edit (chnged=1, mrid=rid) and a // vmerge record. Stashing the edit would leave the record naming a merge // whose content is gone, and the next commit would claim the merge (#244). func TestSaveRefusesCleanThreeWayMerge(t *testing.T) { repoDB, ckout, dir := testEnv(t) modifyFile(t, ckout, dir) if _, err := ckout.Exec("INSERT INTO vmerge(id, merge, mhash) VALUES(0, 99, 'h')"); err != nil { t.Fatal(err) } err := Save(ckout, repoDB, dir, "merged") if err == nil || !strings.Contains(err.Error(), "merge") { t.Fatalf("Save during a merge = %v, want a pending-merge refusal", err) } if got := readFile(t, filepath.Join(dir, "a.txt")); got != "hello world" { t.Fatalf("a.txt = %q after refusal, want the merged content kept", got) } } // Save cannot yet stash a rename: it records and restores files by their // current name only. It must refuse rather than stash the rename as a plain // edit and lose it (#233). func TestSaveRefusesRename(t *testing.T) { repoDB, ckout, dir := testEnv(t) modifyFile(t, ckout, dir) if _, err := ckout.Exec("UPDATE vfile SET origname='old.txt' WHERE pathname='a.txt'"); err != nil { t.Fatal(err) } err := Save(ckout, repoDB, dir, "rename") if err == nil || !strings.Contains(err.Error(), "rename") { t.Fatalf("Save over a rename = %v, want a rename refusal", err) } } // An edit made on top of a merge drops chnged back to 1 but leaves mrid on // the merged-in content: still a pending merge, still refused (#233). func TestSaveRefusesEditOnTopOfMerge(t *testing.T) { repoDB, ckout, dir := testEnv(t) modifyFile(t, ckout, dir) if _, err := ckout.Exec("UPDATE vfile SET mrid=mrid+100 WHERE pathname='a.txt'"); err != nil { t.Fatal(err) } err := Save(ckout, repoDB, dir, "edited merge") if err == nil || !strings.Contains(err.Error(), "merge") { t.Fatalf("Save over an edited merge = %v, want a pending-merge refusal", err) } } // chnged 6 through 9 record a change to the executable or symlink bit, not a // merge. Save cannot restore modes yet, so it refuses with an accurate reason // rather than calling it a merge (#233). func TestSaveRefusesModeChangeAccurately(t *testing.T) { repoDB, ckout, dir := testEnv(t) if _, err := ckout.Exec("UPDATE vfile SET chnged=6 WHERE pathname='a.txt'"); err != nil { t.Fatal(err) } err := Save(ckout, repoDB, dir, "mode") if err == nil || !strings.Contains(err.Error(), "permission") { t.Fatalf("Save over a mode change = %v, want a permission-change refusal", err) } if strings.Contains(err.Error(), "merge") { t.Fatalf("a mode change was reported as a merge: %v", err) } } // A stash's creation time is stored as fossil stores it, a Julian day // (src/stash.c: julianday('now')), and listed as fossil's stash ls shows it, // datetime(ctime). A row fossil wrote lists the same way. func TestStashTimeAsFossil(t *testing.T) { repoDB, ckout, dir := testEnv(t) modifyFile(t, ckout, dir) if err := Save(ckout, repoDB, dir, "test stash"); err != nil { t.Fatalf("Save: %v", err) } var typ string if err := ckout.QueryRow("SELECT typeof(ctime) FROM stash").Scan(&typ); err != nil { t.Fatal(err) } if typ != "real" { t.Errorf("stash.ctime stored as %s, want a Julian day (real)", typ) } if _, err := ckout.Exec("INSERT INTO stash(stashid, hash, comment, ctime)" + " VALUES(99, '', 'fossil', 2461332.5)"); err != nil { t.Fatal(err) } entries, err := List(ckout) if err != nil { t.Fatalf("List: %v", err) } shape := regexp.MustCompile(`^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d$`) for _, e := range entries { if !shape.MatchString(e.CTime) { t.Errorf("stash %d: CTime %q, want datetime(ctime)'s YYYY-MM-DD HH:MM:SS", e.ID, e.CTime) } if e.ID == 99 && e.CTime != "2026-10-19 00:00:00" { t.Errorf("fossil's stash: CTime %q, want 2026-10-19 00:00:00", e.CTime) } } } // Stashes list in creation order, oldest first, as fossil's stash ls orders // them (ORDER BY ctime); stash ids break a tie. func TestListOrdersByCreationTime(t *testing.T) { _, ckout, _ := testEnv(t) if err := EnsureTables(ckout); err != nil { t.Fatal(err) } for _, row := range []string{ "(1, '', 'b', 2461332.6)", "(2, '', 'a', 2461332.5)", "(3, '', 'd', 2461332.7)", "(4, '', 'c', 2461332.6)", } { if _, err := ckout.Exec("INSERT INTO stash(stashid, hash, comment, ctime) VALUES" + row); err != nil { t.Fatal(err) } } entries, err := List(ckout) if err != nil { t.Fatal(err) } var got []int64 for _, e := range entries { got = append(got, e.ID) } if want := []int64{2, 1, 4, 3}; !reflect.DeepEqual(got, want) { t.Fatalf("List order = %v, want %v", got, want) } }