| 1 | package checkpoint |
| 2 | |
| 3 | import "slices" |
| 4 | |
| 5 | // CaptureAfter records the after fingerprint and reports a preimage change. |
| 6 | func (s *Store) CaptureAfter(path string, opts CaptureAfterOpts) bool { |
| 7 | if path == "" { |
| 8 | return false |
| 9 | } |
| 10 | pathKey := NormalizeRelPath(s.root, path) |
| 11 | fp, gap, err := CapturePath(path, CaptureOptions{WorkspaceRoot: s.root, ReadContent: true}) |
| 12 | if gap != nil { |
| 13 | s.RecordGap(*gap) |
| 14 | } |
| 15 | if err != nil { |
| 16 | return false |
| 17 | } |
| 18 | s.mu.Lock() |
| 19 | defer s.mu.Unlock() |
| 20 | s.mutationSeq = opts.Seq |
| 21 | if s.cur != nil { |
| 22 | s.cur.LastMutationSeq = opts.Seq |
| 23 | for i := range s.cur.Files { |
| 24 | if NormalizeRelPath(s.root, s.cur.Files[i].Path) != pathKey { |
| 25 | continue |
| 26 | } |
| 27 | changed := updateAfterFingerprint(&s.cur.Files[i], fp) |
| 28 | s.recomputeCoverageLocked(s.cur) |
| 29 | s.persistBestEffort(s.cur) |
| 30 | s.lastUndo = nil |
| 31 | return changed |
| 32 | } |
| 33 | } |
| 34 | for _, c := range slices.Backward(s.done) { |
| 35 | for j := range c.Files { |
| 36 | if NormalizeRelPath(s.root, c.Files[j].Path) != pathKey { |
| 37 | continue |
| 38 | } |
| 39 | changed := updateAfterFingerprint(&c.Files[j], fp) |
| 40 | s.persistBestEffort(c) |
| 41 | s.lastUndo = nil |
| 42 | return changed |
| 43 | } |
| 44 | } |
| 45 | return false |
| 46 | } |
| 47 | |
| 48 | func updateAfterFingerprint(before *FileSnap, after Fingerprint) bool { |
| 49 | existed := before.Content != nil || before.BlobRef != "" || before.SHA256 != "" |
| 50 | changed := existed != after.Existed || before.SHA256 != after.SHA256 || before.Mode != after.Mode |
| 51 | before.AfterExisted, before.AfterSHA256, before.AfterMode = &after.Existed, after.SHA256, after.Mode |
| 52 | return changed |
| 53 | } |
| 54 |