Subject: [PATCH v2 1/2] [Sloppy PoC] self: Use a more meaningful VERSION string for manuals.

From: Tobias Geerinckx-Rice <[email-masked]>
Date: Sun, 17 Sep 2023 01:59:59 +0200

* guix/self.scm (info-manual): Use the last public Guix release,
according to our own guix package.
---
 gnu/packages/package-management.scm |  2 +-
 guix/self.scm                       | 52 ++++++++++++++++++++++++-----
 2 files changed, 44 insertions(+), 10 deletions(-)

diff --git a/gnu/packages/package-management.scm b/gnu/packages/package-management.scm
index cbda40f78b..a7f3363f4e 100644
--- a/gnu/packages/package-management.scm
+++ b/gnu/packages/package-management.scm
@@ -172,7 +172,7 @@ (define (boot-guile-uri arch)
 (define-public guix
   ;; Latest version of Guix, which may or may not correspond to a release.
   ;; Note: the 'update-guix-package.scm' script expects this definition to
-  ;; start precisely like this.
+  ;; start precisely like this.  Also match the regexps in (guix self)!
   (let ((version "1.4.0")
         (commit "4dfdd822102690b5687acf28365ab707b68d9476")
         (revision 10))
diff --git a/guix/self.scm b/guix/self.scm
index 5c8c00e0b0..2d11dd47a4 100644
--- a/guix/self.scm
+++ b/guix/self.scm
@@ -418,23 +418,57 @@ (define (info-manual source)
   (define examples
     (file-append* source "gnu/system/examples"))
 
+  ;; Sniff the latest public Guix release version number from this file.
+  (define guix-package-module
+    (file-append* source "gnu/packages/package-management.scm"))
+
   (define build
     (with-imported-modules '((guix build utils))
       #~(begin
           (use-modules (guix build utils)
-                       (ice-9 match))
+                       (ice-9 match)
+                       (ice-9 rdelim)
+                       (ice-9 regex))
+
+          ;; Use regular expressions rather than READ to ‘parse’ the guix
+          ;; package, in an attempt not to limit any future reader extensions.
+          (define guix-package-regexp
+            (make-regexp
+             "^\\(define[-[:alnum:]]*[[:blank:]]+guix([^[:alnum:]]|$)"))
+
+          (define guix-package-version-regexp
+            (make-regexp "\\(version[[:blank:]]*\"([^\"]*)\"\\)"))
+
+          (define (get-guix-package-version port)
+            (let loop ()
+              (let ((line (read-line port)))
+                (unless (or (eof-object? line)
+                            (regexp-exec guix-package-regexp line))
+                  (loop))))
+            (let loop ()
+              (let ((line (read-line port)))
+                (if (eof-object? line)
+                    #f
+                    (let ((rx (regexp-exec guix-package-version-regexp line)))
+                      (if (eq? 2 (and=> rx match:count))
+                          (match:substring rx 1)
+                          (loop)))))))
 
           (mkdir #$output)
 
-          ;; Create 'version.texi'.
-          ;; XXX: Can we use a more meaningful version string yet one that
-          ;; doesn't change at each commit?
+          ;; Create 'version.texi'.  We could sniff the git commit from the
+          ;; SOURCE file name, but don't: not only is the manual expensive to
+          ;; rebuild, VERSION is used almost exclusively to construct release
+          ;; URLs and the like.
           (call-with-output-file "version.texi"
-            (lambda (port)
-              (let ((version "0.0-git"))
-                (pk (find-files #$source "."))
-                (punt)
-                (format port "
+            (lambda (output)
+              (let* ((input   (open-file #$guix-package-module "r"))
+                     (version (if input
+                                  (let ((v (get-guix-package-version input)))
+                                    (close-port input)
+                                    v)
+                                  "0.0-git")))
+                (format output "\
 @set UPDATED 1 January 1970
 @set UPDATED-MONTH January 1970
 @set EDITION ~a

base-commit: ee7c9d254117fa470686210ad2ef5e7f1ba4fefc
prerequisite-patch-id: 4df99ef206175ed2fc52016929d4b7a3413a36dc
prerequisite-patch-id: 61f4235965cf06a4a9a619afb1606c6b9f9eeaf1
prerequisite-patch-id: 4e22c12f447e759bbd52a486f25e279829c7b88f
prerequisite-patch-id: 7e6e4ab87b52996e9bb6cd8595889f21ba87e9fe
prerequisite-patch-id: 6f86f2a54f77c0e8841b6f22953c2f748e358107
prerequisite-patch-id: 7c88071ffd6af8c9de9a44ef2e745e3b111d28e7
prerequisite-patch-id: dacf336396c4f629906e4689dc2868d437179a37
prerequisite-patch-id: 35641348fcba881313d08cb85da3722ba0891264
prerequisite-patch-id: ea98f949fde81f63a309e36405b87463dc07ac50
prerequisite-patch-id: a299994eb9a52db942a5cc3a038b8bc34529c799
prerequisite-patch-id: 9ffc6d3a9be37af59ec29bfce0543cf8371edacb
prerequisite-patch-id: c489fbc8af7d431f9372d0b8bc9e4c609703680e
prerequisite-patch-id: 471c932817cc1044fd03f56e1a1f84e4ce4029a9
prerequisite-patch-id: 88f3a5981ea62ce654a48c258df09315f24ba73e
prerequisite-patch-id: 62b7b8d8b26d642f524d45e910f7685a57345d76
prerequisite-patch-id: 6ad7cb518d3f48614c97e7ef851289a8f4375306
prerequisite-patch-id: 758284e9a8f0cbc718feace3ba3575478360b88a
prerequisite-patch-id: 3fec7d86bd725207d19e77b38cba7f989af68ea3
prerequisite-patch-id: 3bd20d51aa3a07f86722c5894b85f58b96e1e798
prerequisite-patch-id: 1011132081c09cd02295c999c9af44d1191bc4b2
prerequisite-patch-id: c3b42b9e20d68f2d3522424af509bfa6c8e79ee8
prerequisite-patch-id: 760df26aee26e14249c412f32630ae6e71a3fa3e
prerequisite-patch-id: b89039b55bef0639c3679b1a5ba13b7a5593af5b
prerequisite-patch-id: 24fc8fdb82bb9287ed944673e2f922587bc49503
prerequisite-patch-id: 077e45c8081930192499bfa5c7391d882b1ad401
prerequisite-patch-id: 68aa5fda780581e44d43d179d71b1232e568bcd3
prerequisite-patch-id: f31368d6b77f811e4b1fba489492ac4d6cde0948
prerequisite-patch-id: fc4e5787813af14bc812bc61a18acd684bd319af
prerequisite-patch-id: 08f122a2c9dec498a089d665280f37191f96a1ba
prerequisite-patch-id: 8df25a01fcb36ee0616d12930524131a6cb5af40
-- 
2.41.0