Subject: [PATCH] scripts: package: Make an argument for '--delete-generations' mandatory

From: Tomás Ortín <[email-masked]>
Date: Fri, 23 Feb 2024 13:06:33 +0100

* guix/scripts/package.scm (delete-matching-generations): Ensure 
'pattern' is always a string
* guix/ui.scm (string->generations): Add '..' as syntax to delete all 
previous generations
* guix/ui.scm (matching-generations): Handle case of empty pattern

Recently, I've been bitten by mistyping 'guix package -d' without 
specifying any pattern. To my surprise, this results in all previous 
generations being deleted without any kind of confirmation. This is a 
quite easy mistake to make, so it seems like a footgun.

This change makes it mandatory to specify an argument to 'guix package 
--delete-generations'. Complete deletion of old generations is now 
required to be explicit. In line with the existing pattern syntax, 'guix 
package --delete-generations ..' now deletes all previous generations.

Change-Id: Ia51b33886a25661cea47aef56966cf1a3bfa7658
---
  guix/scripts/package.scm | 4 +++-
  guix/ui.scm              | 9 +++++++--
  2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/guix/scripts/package.scm b/guix/scripts/package.scm
index a489e06e73..c4a096fafe 100644
--- a/guix/scripts/package.scm
+++ b/guix/scripts/package.scm
@@ -13,6 +13,7 @@
  ;;; Copyright © 2022 Josselin Poiret <[email-masked]>
  ;;; Copyright © 2022 Arun Isaac <[email-masked]>
  ;;; Copyright © 2022 Antero Mejr <[email-masked]>
+;;; Copyright © 2024 Tomás Ortín Fernández <[email-masked]>
  ;;;
  ;;; This file is part of GNU Guix.
  ;;;
@@ -109,7 +110,8 @@ (define (delete-matching-generations store profile 
pattern)
  a string denoting a set of generations: the empty list means \"all 
generations
  but the current one\", a number designates a generation, and other 
patterns
  denote ranges as interpreted by 'matching-generations'."
-  (let ((current (generation-number profile)))
+  (let ((current (generation-number profile))
+        (pattern (if pattern pattern ""))) ; ensure pattern is a string
      (cond ((not (file-exists? profile))            ; XXX: race condition
             (raise (condition (&profile-not-found-error
                                (profile profile)))))
diff --git a/guix/ui.scm b/guix/ui.scm
index 962d291d2e..723f53946a 100644
--- a/guix/ui.scm
+++ b/guix/ui.scm
@@ -19,6 +19,7 @@
  ;;; Copyright © 2018 Steve Sprang <[email-masked]>
  ;;; Copyright © 2022 Taiju HIGASHI <[email-masked]>
  ;;; Copyright © 2022 Liliana Marie Prikler <[email-masked]>
+;;; Copyright © 2024 Tomás Ortín Fernández <[email-masked]>
  ;;;
  ;;; This file is part of GNU Guix.
  ;;;
@@ -1850,7 +1851,7 @@ (define* (display-search-results matches port
  
  (define (string->generations str)
    "Return the list of generations matching a pattern in STR.  This 
function
-accepts the following patterns: \"1\", \"1,2,3\", \"1..9\", \"1..\", 
\"..9\"."
+accepts the following patterns: \"1\", \"1,2,3\", \"1..9\", \"1..\", 
\"..9\", \"..\"."
    (define (maybe-integer)
      (let ((x (string->number str)))
        (and (integer? x)
@@ -1869,6 +1870,7 @@ (define (string->generations str)
          ((maybe-comma-separated-integers)
           =>
           identity)
+        ((string= ".." str) '(>= 0))
          ((string-match "^([0-9]+)\\.\\.([0-9]+)$" str)
           =>
           (lambda (match)
@@ -1987,7 +1989,10 @@ (define* (matching-generations str profile
           filter-by-duration)
          (else
           (raise
-          (formatted-message (G_ "invalid syntax: ~a~%") str)))))
+          (formatted-message (G_ "invalid syntax: ~a~%")
+                             (if (string= "" str)
+                                 "pattern missing"
+                                 str))))))

  (define (display-generation profile number)
    "Display a one-line summary of generation NUMBER of PROFILE."

base-commit: aefc7428203203ae88c64cc4769113453c01a185
-- 
2.41.0