Lazy per-exercise pristine snapshots (wave 5)

.lisplings-orig/ was written once, on the first run, so the 35
wave-4 exercises added later had no pristine copy and reset/x
refused them. Snapshot lazily per exercise now: reset prefers git
HEAD (git show HEAD:./exercises/...), falls back to the current
file while it still has its ;; TODO marker, and refuses when
neither is safe. The first-run store rebuild uses the same
per-exercise sources, so a deleted .lisplings-orig/ recovers too,
and is no longer repopulated from files the user may have solved.
This commit is contained in:
venomade committed 2026-11-22 11:37:07 +00:00
1 parent a6e3fa2cf3
commit 40af05dbff
2 files changed
+82 -13

No files matched your search

+4 -3
View File
@@ -88,9 +88,10 @@ solved.
```
Progress lives in `.lisplings-state.sexp` - a plain s-expression you can
read and edit. `reset` restores from a pristine snapshot taken under
`.lisplings-orig/` the first time lisplings ran there, so start lisplings
before editing anything.
read and edit. `reset` restores from a pristine snapshot under
`.lisplings-orig/`; snapshots are taken lazily per exercise, from git
when possible, so an exercise added after the first run still resets to
its original broken file.
## How an exercise works
+78 -10
View File
@@ -148,15 +148,81 @@ through, anything else fails fast at startup."
(merge-pathnames (format nil "~a/~a.lisp" (exercise-dir ex) (exercise-name ex))
(orig-root)))
(defun git-pristine-source (ex)
"EX's exercise source as git HEAD stores it, or NIL when git is
unavailable, this is not a git repository, or HEAD does not know the
file yet (an exercise not committed, say)."
(ignore-errors
(let* ((spec (format nil "HEAD:./exercises/~a/~a.lisp"
(exercise-dir ex) (exercise-name ex)))
(proc (sb-ext:run-program "git" (list "show" spec)
:directory (namestring *project*)
:wait nil :input nil :search t
:output :stream :error :output))
(text (with-output-to-string (out)
(let ((stream (sb-ext:process-output proc)))
(handler-case
(loop for ch = (read-char stream nil :eof)
until (eq ch :eof)
do (write-char ch out))
(error () nil))))))
(sb-ext:process-wait proc)
(and (zerop (sb-ext:process-exit-code proc))
(plusp (length text))
text))))
(defun looks-broken-p (ex)
"Does EX's current file still carry a ;; TODO marker line, so that
it looks pristine-broken? Only such a file is safe to snapshot when
git cannot supply the original."
(let ((path (exercise-path ex)))
(and (probe-file path)
(with-open-file (in path)
(loop for line = (read-line in nil :eof)
until (eq line :eof)
thereis (and (>= (length line) 7)
(string= ";; TODO" line :end2 7)))))))
(defun ensure-orig-exercise (ex)
"Return the pathname of EX's pristine copy under .lisplings-orig/,
taking it lazily when missing: from git HEAD when git has the file,
else from the current file while that still looks broken. NIL when
neither source is safe to snapshot; `reset' then refuses."
(let ((orig (orig-path ex)))
(or (and (probe-file orig) orig)
(let ((text (git-pristine-source ex)))
(cond (text
(ensure-directories-exist orig)
(with-open-file (out orig :direction :output
:if-exists :supersede
:if-does-not-exist :create)
(write-string text out))
orig)
((looks-broken-p ex)
(copy-file (exercise-path ex) orig)
orig)
(t nil))))))
(defun ensure-orig-snapshot ()
"Snapshot the pristine exercises the first time lisplings runs here,
so `reset' can restore them. Start lisplings before editing anything!"
so `reset' can restore them. Every copy is lazy and per exercise: git
HEAD when possible, else a still-broken current file, so exercises
added after the first run and a deleted .lisplings-orig/ recover the
same way."
(let ((orig (orig-root)))
(unless (probe-file orig)
(let ((stored 0) (skipped 0))
(dolist (ex *exercises*)
(copy-file (exercise-path ex) (orig-path ex)))
(if (ensure-orig-exercise ex)
(incf stored)
(incf skipped)))
(when (plusp stored)
(format *error-output*
"~&Snapshotted pristine exercises to .lisplings-orig/ (this is what `reset' restores).~%"))))
"~&Snapshotted pristine exercises to .lisplings-orig/ (this is what `reset' restores).~%"))
(when (plusp skipped)
(format *error-output*
"~&~d exercise~:p had no safe pristine source: no git copy, and the current file no longer looks broken.~%"
skipped))))))
;;; ---------------------------------------------------------------------
;;; Terminal helpers
@@ -491,8 +557,9 @@ the queue. Returns NIL at EOF."
(defun reset-prompt (state queue lock open-p)
"Ask y/n; on yes restore the pristine copy of the current exercise
and rerun it, on no (or EOF) just say so."
(let ((ex (current-exercise state)))
(cond ((not (probe-file (orig-path ex)))
(let* ((ex (current-exercise state))
(orig (ensure-orig-exercise ex)))
(cond ((null orig)
(setf (screen-state-notice state)
(format nil "No pristine copy of ~a under .lisplings-orig/; cannot reset."
(exercise-name ex)))
@@ -503,7 +570,7 @@ and rerun it, on no (or EOF) just say so."
(exercise-dir ex) (exercise-name ex))
queue lock open-p)))
(cond ((and answer (member answer '("y" "yes") :test #'string-equal))
(copy-file (orig-path ex) (exercise-path ex))
(copy-file orig (exercise-path ex))
(unmark-done ex)
(run-and-show state queue lock))
(t
@@ -670,16 +737,17 @@ empty answer (or EOF) stays on the current exercise."
(defun reset-command (name)
(let ((ex (resolve-exercise name)))
(ensure-orig-snapshot)
(cond ((not (probe-file (orig-path ex)))
(let ((orig (ensure-orig-exercise ex)))
(cond ((null orig)
(format t "~&No pristine copy of ~a under .lisplings-orig/; cannot reset.~%"
(exercise-name ex))
(sb-ext:exit :code 1))
(t
(copy-file (orig-path ex) (exercise-path ex))
(copy-file orig (exercise-path ex))
(unmark-done ex)
(format t "~&The exercise ~a has been reset.~%" (exercise-name ex))
(format t "~&(It now matches the pristine copy from when lisplings first ran here.)~%")
(sb-ext:exit :code 0)))))
(format t "~&(It now matches the pristine copy under .lisplings-orig/.)~%")
(sb-ext:exit :code 0))))))
(defun check-all-command ()
(ensure-orig-snapshot)