diff options
| author | Maxim Cournoyer <maxim@guixotic.coop> | 2026-08-08 17:22:18 +0900 |
|---|---|---|
| committer | Maxim Cournoyer <maxim@guixotic.coop> | 2026-08-25 10:07:51 +0900 |
| commit | d131c228dc32bb71e75717f9dc260b6e6812f73d (patch) | |
| tree | 9d08e71c08af111caee178ad8176858c7bc6423c | |
| parent | d485ff319a82fb4d497e1d5435d881608f4bdf10 (diff) | |
import/utils: Allow `find-version' to match an over-specified version.
This fixes the use case of using --target-version with an exact
match (--target-version causes the partial? mode to be enabled).
* tests/import/utils.scm ("find-version, latest")
("find-version, empty versions list")
("find-version, partial, under-specified")
("find-version, partial, exact")
("find-version, partial, over-specified"): New tests.
* guix/import/utils.scm (find-version): [PARTIAL?]: Truncate VERSION to the
maximum length of VERSIONS. Explicitly handle the empty versions case.
| -rw-r--r-- | guix/import/utils.scm | 19 | ||||
| -rw-r--r-- | tests/import/utils.scm | 25 |
2 files changed, 39 insertions, 5 deletions
diff --git a/guix/import/utils.scm b/guix/import/utils.scm index 4e0e2876af7..aef52253590 100644 --- a/guix/import/utils.scm +++ b/guix/import/utils.scm | |||
| @@ -785,14 +785,23 @@ separated by PRED." | |||
| 785 | (define* (find-version versions #:optional version partial?) | 785 | (define* (find-version versions #:optional version partial?) |
| 786 | "Find VERSION amongst VERSIONS. When VERSION is not provided, return the | 786 | "Find VERSION amongst VERSIONS. When VERSION is not provided, return the |
| 787 | latest version. When PARTIAL? is #t, VERSION is treated as a version prefix; | 787 | latest version. When PARTIAL? is #t, VERSION is treated as a version prefix; |
| 788 | e.g. finding version \"0.1\" may return \"0.1.8\" if it is the newest \"0.1\" | 788 | e.g. finding version \"0.1\" may return \"0.1.8\", if it is the newest \"0.1\" |
| 789 | prefixed version found in VERSIONS. Return #f when VERSION could not be | 789 | prefixed version found in VERSIONS. In PARTIAL? mode, if the VERSION string |
| 790 | found." | 790 | provided is longer than the longest version in VERSIONS, then it is truncated |
| 791 | to that length. Return #f when VERSION could not be found." | ||
| 791 | (let ((versions (sort versions version>?))) | 792 | (let ((versions (sort versions version>?))) |
| 792 | (cond | 793 | (cond |
| 794 | ((null? versions) | ||
| 795 | #f) | ||
| 793 | ((and version partial?) ;partial version | 796 | ((and version partial?) ;partial version |
| 794 | (find (cut version-prefix? version <>) versions)) | 797 | (let* ((max-version-length (apply max (map string-length versions))) |
| 795 | ((and version (not partial?)) ;exact version | 798 | (version-length (string-length version)) |
| 799 | (version (if (> version-length max-version-length) | ||
| 800 | (string-drop-right version (- version-length | ||
| 801 | max-version-length)) | ||
| 802 | version))) | ||
| 803 | (find (cut version-prefix? version <>) versions))) | ||
| 804 | (version ;exact version | ||
| 796 | (find (cut string=? version <>) versions)) | 805 | (find (cut string=? version <>) versions)) |
| 797 | ((not (null? versions)) ;latest version | 806 | ((not (null? versions)) ;latest version |
| 798 | (first versions)) | 807 | (first versions)) |
diff --git a/tests/import/utils.scm b/tests/import/utils.scm index c82fef78ec7..44559e2c237 100644 --- a/tests/import/utils.scm +++ b/tests/import/utils.scm | |||
| @@ -281,6 +281,31 @@ Differences are hard to spot, e.g. in CLOS vs. GOOPS.")) | |||
| 281 | (map spdx-string->license | 281 | (map spdx-string->license |
| 282 | '("GPL-3.0-oR-LaTeR" "AGPL-3.0" "GPL-2.0+"))) | 282 | '("GPL-3.0-oR-LaTeR" "AGPL-3.0" "GPL-2.0+"))) |
| 283 | 283 | ||
| 284 | |||
| 285 | ;;; | ||
| 286 | ;;; find-version. | ||
| 287 | ;;; | ||
| 288 | |||
| 289 | (test-equal "find-version, empty versions list" | ||
| 290 | #f | ||
| 291 | (find-version '())) | ||
| 292 | |||
| 293 | (test-equal "find-version, latest" | ||
| 294 | "6.11.1" | ||
| 295 | (find-version '("6.9" "6.8" "6.5" "6.11.1" "6.10.3"))) | ||
| 296 | |||
| 297 | (test-equal "find-version, partial, exact" | ||
| 298 | "6.10.3" | ||
| 299 | (find-version '("6.9" "6.8" "6.5" "6.11.1" "6.10.3") "6.10.3" #t)) | ||
| 300 | |||
| 301 | (test-equal "find-version, partial, under-specified" | ||
| 302 | "6.10.3" | ||
| 303 | (find-version '("6.9" "6.8" "6.5" "6.11.1" "6.10.3") "6.10" #t)) | ||
| 304 | |||
| 305 | (test-equal "find-version, partial, over-specified" | ||
| 306 | "6.10" | ||
| 307 | (find-version '("6.9" "6.8" "6.5" "6.11" "6.10") "6.10.3" #t)) | ||
| 308 | |||
| 284 | ;;; | 309 | ;;; |
| 285 | ;;; default-git-error | 310 | ;;; default-git-error |
| 286 | ;;; | 311 | ;;; |
