about summary refs log tree commit diff
path: root/scripts
diff options
context:
space:
mode:
Diffstat (limited to 'scripts')
-rw-r--r--scripts/index.scm61
1 files changed, 48 insertions, 13 deletions
diff --git a/scripts/index.scm b/scripts/index.scm
index ac83850..87e32f8 100644
--- a/scripts/index.scm
+++ b/scripts/index.scm
@@ -113,6 +113,9 @@
 (define (repo-name store)
   (basename (dirname (canonicalize-path store))))
 
+(define (alist-set table key value)
+  (cons (cons key value) table))
+
 (define (non-empty-string? value)
   (and (string? value) (not (string-null? value))))
 
@@ -228,12 +231,20 @@
                    (field 'text (string-append title " " (or body "")))))))
     (else (die "unexpected reasoning row: ~s" row))))
 
-(define (commit-record repo row)
+(define (commit-record repo row sessions)
   (match row
     ((hash subject body author_ts)
      (let* ((key (format #f "commit:~a" hash))
             (date (yyyymmdd author_ts))
-            (subject (or subject "")))
+            (subject (or subject ""))
+            ;; A commit inherits the topics of the reasoning attached to it:
+            ;; otherwise the pve story answers with its reasoning and not with
+            ;; the commits that carry it, which is exactly backwards for a
+            ;; reader following it.
+            (topics (append-map (lambda (session)
+                                  (map (lambda (word) (term "O" word))
+                                       (string-split session #\-)))
+                                sessions)))
        (list key
              (list (field 'type "commit")
                    (field 'key key)
@@ -241,17 +252,35 @@
                    (field 'date date)
                    (field 'commit hash)
                    (field 'title subject)
-                   (field 'terms (list (term "T" "commit")
-                                       (term "R" repo)
-                                       (term "D" (number->string date))
-                                       (term "C" (substring hash 0 7))))
+                   (field 'session (if (null? sessions) "" (car sessions)))
+                   (field 'terms (append (list (term "T" "commit")
+                                               (term "R" repo)
+                                               (term "D" (number->string date))
+                                               (term "C" (substring hash 0 7)))
+                                         (map (lambda (session) (term "S" session))
+                                              sessions)
+                                         topics))
                    (field 'body (or body ""))
                    (field 'text (string-append subject " " (or body "")))))))
     (else (die "unexpected commit row: ~s" row))))
 
-(define (records repo reasoning commits)
-  (append (map (lambda (row) (reasoning-record repo row)) reasoning)
-          (map (lambda (row) (commit-record repo row)) commits)))
+(define (sessions-by-commit attached)
+  (fold (lambda (row table)
+          (match row
+            ((commit_hash sessions)
+             (alist-set table commit_hash
+                        (if sessions (string-split sessions #\,) (list))))
+            (else table)))
+        '()
+        attached))
+
+(define (records repo reasoning commits attached)
+  (let ((sessions (sessions-by-commit attached)))
+    (append (map (lambda (row) (reasoning-record repo row)) reasoning)
+            (map (lambda (row)
+                   (commit-record repo row
+                                  (or (assoc-ref sessions (first row)) (list))))
+                 commits))))
 
 ;;; ---------------------------------------------------------------------
 ;;; Writing
@@ -310,9 +339,15 @@
                                LEFT JOIN commits c ON c.hash = r.commit_hash
                                ORDER BY r.id"))
           (commits (query db "SELECT hash, subject, COALESCE(body, ''), author_ts
-                              FROM commits ORDER BY author_ts")))
+                              FROM commits ORDER BY author_ts"))
+          ;; Which sessions' reasoning is attached to which commit, so a commit
+          ;; document can carry those sessions' topics.
+          (attached (query db "SELECT commit_hash, GROUP_CONCAT(DISTINCT session_id)
+                               FROM reasoning
+                               WHERE commit_hash IS NOT NULL AND session_id <> ''
+                               GROUP BY commit_hash")))
       (sqlite-close db)
-      (values reasoning commits))))
+      (values reasoning commits attached))))
 
 ;;; ---------------------------------------------------------------------
 ;;; Commands
@@ -325,9 +360,9 @@
     (unless index (die "update needs --index DIR"))
     (unless (file-exists? store) (die "no store at ~a" store))
     (call-with-values (lambda () (store-rows store))
-      (lambda (reasoning commits)
+      (lambda (reasoning commits attached)
         (let* ((repo (repo-name store))
-               (records (records repo reasoning commits))
+               (records (records repo reasoning commits attached))
                (db (open-writable index rebuild))
                (stemmer (new-Stem "english"))
                (written 0))