Artifact 0e203d4f17e08a6b46db45f2defafa96920a4883b625a213c82958fa1ca53438:
- File
internal/checkout/types.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: 7313) [annotate] [blame] [check-ins using] [more...]
package checkout import ( "time" libfossil "github.com/danmestas/go-libfossil/internal/fsltype" "github.com/danmestas/go-libfossil/internal/vfile" "github.com/danmestas/go-libfossil/simio" ) // VFileChange — vfile.chnged column values (matches Fossil's vfile states) type VFileChange int const ( VFileNone VFileChange = 0 // unchanged VFileMod VFileChange = 1 // modified VFileMergeMod VFileChange = 2 // modified via merge VFileMergeAdd VFileChange = 3 // added via merge VFileIntMod VFileChange = 4 // modified via integrate VFileIntAdd VFileChange = 5 // added via integrate VFileIsExec VFileChange = 6 // became executable VFileBecameLink VFileChange = 7 // became symlink VFileNotExec VFileChange = 8 // lost executable VFileNotLink VFileChange = 9 // lost symlink ) // FileChange — checkout-level change types (maps to fsl_ckout_change_e). // Package vfile owns their meaning; these names keep the checkout API. type FileChange = vfile.Change const ( ChangeNone = vfile.ChangeNone ChangeAdded = vfile.ChangeAdded ChangeRemoved = vfile.ChangeRemoved ChangeMissing = vfile.ChangeMissing ChangeRenamed = vfile.ChangeRenamed ChangeModified = vfile.ChangeModified ) // UpdateChange — file states during extract/update (maps to fsl_ckup_fchange_e) type UpdateChange int const ( UpdateNone UpdateChange = iota UpdateAdded UpdateAddPropagated UpdateRemoved UpdateRmPropagated UpdateUpdated UpdateUpdatedBinary UpdateMerged UpdateConflictMerged UpdateConflictAdded UpdateConflictUnmanaged UpdateConflictRm UpdateConflictSymlink UpdateConflictBinary UpdateRenamed UpdateEdited ) // RevertChange — revert operation types (maps to fsl_ckout_revert_e) type RevertChange int const ( RevertNone RevertChange = iota RevertUnmanage RevertRemove RevertRename RevertPermissions RevertContents ) // ScanFlags — controls ScanChanges behavior type ScanFlags uint32 const ( ScanHash ScanFlags = 1 << iota // hash file content (not just mtime) ScanENotFile // mark non-regular files ScanSetMTime // update mtime in vfile ScanKeepOthers // keep other version entries ) // ManifestFlags — controls WriteManifest behavior type ManifestFlags int const ( ManifestMain ManifestFlags = 1 << iota // write manifest file ManifestUUID // write manifest.uuid file ) // OpenOpts configures opening an existing checkout. type OpenOpts struct { Env *simio.Env // nil → RealEnv Observer Observer // nil → nopObserver SearchParents bool // search parent dirs for .fslckout } // CreateOpts configures creating a new checkout from a repo. type CreateOpts struct { Env *simio.Env // nil → RealEnv Observer Observer // nil → nopObserver } // ExtractOpts configures file extraction from a checkin. type ExtractOpts struct { Callback func(name string, change UpdateChange) error // per-file notification SetMTime bool // as fossil's --setmtime: each file's last check-in time DryRun bool Force bool // overwrite locally modified files } // UpdateOpts configures updating to a new version with merge. type UpdateOpts struct { TargetRID libfossil.FslID // 0 → auto-calculate via CalcUpdateVersion Callback func(name string, change UpdateChange) error SetMTime bool // as fossil's --setmtime: each file's last check-in time DryRun bool } // UpdateResult reports what Update actually did. Paths, not counts: a // caller needs to know which files to show a user, not merely how many // changed. All four slices are sorted (sort.Strings) before Update // returns, so the order is deterministic and safe to compare with // reflect.DeepEqual — it does not depend on Go's randomized map iteration. // // Conflicted is non-empty when three-way merge could not resolve a file // cleanly and wrote "<<<<<<<"-style conflict markers into it on disk. That // is a successful update, not a failure — Update still returns a nil error // in that case. A caller that only checks the error gets today's behavior; // a caller that wants to detect marker text in the working tree must // inspect Conflicted. Every path in Conflicted also appears in // FilesWritten: the marker text is what was written there. // // Unmerged lists the files fossil's update also counts as conflicts but // that hold no markers, left for the caller to reconcile: // - a file edited locally that the target deletes: kept on disk as it // was, no longer tracked; // - a file added locally that the target also adds: the local file kept, // now tracked as the target's file, so it shows as edited; // - a binary file both the working copy and the target changed: the // target's content written (so also in FilesWritten), the working // copy saved beside it as NAME-original. type UpdateResult struct { FilesWritten []string // paths written (added, updated, or merged, clean or not); includes every Conflicted path FilesRemoved []string // paths deleted from the working tree Conflicted []string // paths that now contain conflict markers; also present in FilesWritten Unmerged []string // paths left to reconcile without markers (see above) } // ManageOpts configures adding files to tracking. type ManageOpts struct { Paths []string Callback func(name string, added bool) error } // ManageCounts reports the result of a Manage operation. type ManageCounts struct { Added, Updated, Skipped int } // UnmanageOpts configures removing files from tracking. type UnmanageOpts struct { Paths []string // pathnames to unmanage VFileIDs []libfossil.FslID // alternative: pass IDs directly Callback func(name string) error } // EnqueueOpts configures staging files for commit. type EnqueueOpts struct { Paths []string Callback func(name string) error } // DequeueOpts configures unstaging files. type DequeueOpts struct { Paths []string // empty → dequeue all } // CommitOpts configures creating a checkin from staged files. type CommitOpts struct { Message string User string Branch string // empty → current branch Tags []string // additional T-cards Delta bool Time time.Time // zero → env.Clock.Now() PreCommitCheck func() error // nil = no check; non-nil error aborts commit AllowConflict bool // commit files that hold merge conflict markers AllowEmpty bool // commit even when nothing changed } // RevertOpts configures reverting file changes. type RevertOpts struct { Paths []string // empty → revert all Callback func(name string, change RevertChange) error } // RenameOpts configures renaming a tracked file. type RenameOpts struct { From, To string DoFsMove bool // also move on filesystem via Storage Callback func(from, to string) error } // ChangeEntry describes a single file change in the checkout. type ChangeEntry struct { Name string Change FileChange VFileID libfossil.FslID IsExec bool IsLink bool OrigName string // non-empty if renamed } // ChangeVisitor is called for each changed file during VisitChanges. type ChangeVisitor func(entry ChangeEntry) error