Artifact bc040350eaecccc0670ce21cd1ebd0ec17dce94d6a1555f1bb66681eb730d378:
- File
internal/checkout/schema.go
— part of check-in
[6e6969bf97]
at
2026-10-18 21:59:04
on branch main
— fix(checkout): keep uncommitted changes across Update, as fossil does (#450)
* fix(checkout): keep uncommitted changes across Update, as fossil does
Update compared only committed versions: it wrote the target over a locally edited file, deleted an edited file the target removed, and merged the current and target check-ins with their ancestor while ignoring the working file.
It now follows fossil's update (src/update.c): scan the checkout, then for each file take the target's content when there is no local change, merge a local edit three ways (the checkout's version as base, the working file as local, the target as other) with conflicts listed in Conflicted, keep an edited file the target deletes as an untracked file, keep pending adds and removals, restore a missing file, and copy an untracked file the target adds over to NAME-original first. A dry run writes nothing. Files written with the target's content record the mtime of the write. Files are matched by name; a target-side rename is not followed.
The two tests that relied on merging two branches' committed changes now make the clashing edit locally. A fossil 2.28 interop test runs the same edits through fossil update and compares files, fossil changes and Status.
Tracked in #443
* fix(checkout): make Update safe on binary files, failures and dry runs
A binary file (a NUL byte, or a line over 32,767 bytes, as fossil's line diff refuses) is no longer line-merged. Update saves the working copy as NAME-original and writes the target, as fossil's update does; Merge keeps the checkout's copy, as fossil's merge does. Both used to splice the versions and report a clean merge. merge.IsText is fossil's test.
Update refuses before writing anything when a file it must write or delete has a directory or a read-only file in the way, moves the checkout database to the target in one transaction, and in a dry run leaves the database untouched (the scan's findings stay in memory). Every target file gets the target's executable bit, as fossil's checkout_set_all_exe does, so a bit-only change reaches the disk.
UpdateResult.Unmerged lists files left to reconcile without markers: edited files the target deletes, files added both locally and by the target, and binary conflicts. The interop test now covers a binary conflict against fossil 2.28.
Tracked in #443
* fix(checkout): check real write access before Update writes anything
The pre-write check read the owner-write bit: it missed an unwritable directory (a remove failed after a merge had been written), files owned by others, and wrongly refused for root. It now asks the storage, via an optional CheckWritable: OSStorage uses access(2) with W_OK on unix and a read-only-attribute check elsewhere (js/wasm, Windows). Every step needs its directory writable; a write or merge over an existing file needs the file writable too.
A read-only untracked file where the target adds one is backed up to NAME-original, deleted and replaced, as fossil does, instead of refused.
Merge keeps a binary file whatever its strategy, as fossil's merge does. setVVar's precondition now catches a nil *sql.DB or *sql.Tx inside the interface.
Tracked in #443 (user: [email protected], size: 2385) [annotate] [blame] [check-ins using] [more...]
package checkout import ( "database/sql" "fmt" ) const vfileSchema = ` CREATE TABLE IF NOT EXISTS vfile( id INTEGER PRIMARY KEY, vid INTEGER NOT NULL, chnged INTEGER DEFAULT 0, deleted INTEGER DEFAULT 0, isexe INTEGER DEFAULT 0, islink INTEGER DEFAULT 0, rid INTEGER DEFAULT 0, mrid INTEGER DEFAULT 0, mtime INTEGER DEFAULT 0, pathname TEXT NOT NULL, origname TEXT, mhash TEXT, UNIQUE(pathname, vid) ); ` const vmergeSchema = ` CREATE TABLE IF NOT EXISTS vmerge( id INTEGER REFERENCES vfile, merge INTEGER, mhash TEXT ); ` const vvarSchema = ` CREATE TABLE IF NOT EXISTS vvar( name TEXT PRIMARY KEY, value TEXT ); ` // EnsureTables creates the vfile, vmerge, and vvar tables if they don't exist. // This is idempotent — safe to call multiple times. // // Panics if db is nil (TigerStyle precondition). func EnsureTables(db *sql.DB) error { if db == nil { panic("checkout.EnsureTables: nil *sql.DB") } schemas := []string{vfileSchema, vmergeSchema, vvarSchema} for _, schema := range schemas { if _, err := db.Exec(schema); err != nil { return fmt.Errorf("checkout.EnsureTables: %w", err) } } return nil } // getVVar retrieves a vvar value by name. Returns empty string if not found. func getVVar(db *sql.DB, name string) (string, error) { if db == nil { panic("checkout.getVVar: nil *sql.DB") } var value string err := db.QueryRow("SELECT value FROM vvar WHERE name = ?", name).Scan(&value) if err == sql.ErrNoRows { return "", nil } if err != nil { return "", fmt.Errorf("checkout.getVVar: %w", err) } return value, nil } // sqlExecer is what the checkout writes its tables through: the checkout // database, or a transaction on it. type sqlExecer interface { Exec(query string, args ...any) (sql.Result, error) } // setVVar sets a vvar value, replacing any existing value. func setVVar(db sqlExecer, name, value string) error { switch q := db.(type) { case *sql.DB: if q == nil { panic("checkout.setVVar: nil *sql.DB") } case *sql.Tx: if q == nil { panic("checkout.setVVar: nil *sql.Tx") } default: panic(fmt.Sprintf("checkout.setVVar: unexpected database %T", db)) } _, err := db.Exec("INSERT OR REPLACE INTO vvar (name, value) VALUES (?, ?)", name, value) if err != nil { return fmt.Errorf("checkout.setVVar: %w", err) } return nil }