From 40af05dbffc95715c1b219cdb625e95ace8c1814 Mon Sep 17 00:00:00 2001 From: venomade Date: Sun, 22 Nov 2026 11:37:07 +0000 Subject: [PATCH] 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. --- README.md | 7 ++-- src/core.lisp | 104 +++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 90 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 96c6ce0..da7f035 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/core.lisp b/src/core.lisp index ff1607c..94c3fba 100644 --- a/src/core.lisp +++ b/src/core.lisp @@ -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) - (dolist (ex *exercises*) - (copy-file (exercise-path ex) (orig-path ex))) - (format *error-output* - "~&Snapshotted pristine exercises to .lisplings-orig/ (this is what `reset' restores).~%")))) + (let ((stored 0) (skipped 0)) + (dolist (ex *exercises*) + (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).~%")) + (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))) - (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)) - (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))))) + (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 (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 under .lisplings-orig/.)~%") + (sb-ext:exit :code 0)))))) (defun check-all-command () (ensure-orig-snapshot)