From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id 8GS4FeMxA2BvXQAA0tVLHw (envelope-from ) for ; Sat, 16 Jan 2021 18:35:15 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id AGFvEeMxA2B8WwAAbx9fmQ (envelope-from ) for ; Sat, 16 Jan 2021 18:35:15 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id D6B369402B1 for ; Sat, 16 Jan 2021 18:35:14 +0000 (UTC) Received: from localhost ([::1]:39966 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l0qPp-0004IJ-RR for larch@yhetil.org; Sat, 16 Jan 2021 13:35:13 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:51130) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l0qPe-0004I5-FC for guix-patches@gnu.org; Sat, 16 Jan 2021 13:35:02 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:60728) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1l0qPe-0006Np-7b for guix-patches@gnu.org; Sat, 16 Jan 2021 13:35:02 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1l0qPe-0002Wp-5a for guix-patches@gnu.org; Sat, 16 Jan 2021 13:35:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#45919] [PATCH 2/8] utils: Add 'version-unique-prefix'. Resent-From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sat, 16 Jan 2021 18:35:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 45919 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 45919@debbugs.gnu.org Received: via spool by 45919-submit@debbugs.gnu.org id=B45919.16108220689609 (code B ref 45919); Sat, 16 Jan 2021 18:35:02 +0000 Received: (at 45919) by debbugs.gnu.org; 16 Jan 2021 18:34:28 +0000 Received: from localhost ([127.0.0.1]:44024 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1l0qP5-0002Un-N7 for submit@debbugs.gnu.org; Sat, 16 Jan 2021 13:34:28 -0500 Received: from eggs.gnu.org ([209.51.188.92]:57898) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1l0qP3-0002UD-8V for 45919@debbugs.gnu.org; Sat, 16 Jan 2021 13:34:26 -0500 Received: from fencepost.gnu.org ([2001:470:142:3::e]:57159) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l0qOy-0006Aa-25; Sat, 16 Jan 2021 13:34:20 -0500 Received: from [2a01:e0a:1d:7270:af76:b9b:ca24:c465] (port=36420 helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1l0qOx-0000li-Jw; Sat, 16 Jan 2021 13:34:19 -0500 From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Date: Sat, 16 Jan 2021 19:34:03 +0100 Message-Id: <20210116183409.31229-2-ludo@gnu.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210116183409.31229-1-ludo@gnu.org> References: <20210116183409.31229-1-ludo@gnu.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: -1.85 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of guix-patches-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=guix-patches-bounces@gnu.org X-Migadu-Queue-Id: D6B369402B1 X-Spam-Score: -1.85 X-Migadu-Scanner: scn1.migadu.com X-TUID: Ed1ttraa9bEM * guix/utils.scm (version-unique-prefix): New procedure. * tests/utils.scm ("version-unique-prefix"): New test. --- guix/utils.scm | 35 ++++++++++++++++++++++++++++++++++- tests/utils.scm | 8 +++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/guix/utils.scm b/guix/utils.scm index f8b05e7e80..a85e2f495c 100644 --- a/guix/utils.scm +++ b/guix/utils.scm @@ -1,5 +1,5 @@ ;;; GNU Guix --- Functional package management for GNU -;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès +;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès ;;; Copyright © 2013, 2014, 2015 Mark H Weaver ;;; Copyright © 2014 Eric Bavier ;;; Copyright © 2014 Ian Denhardt @@ -88,6 +88,7 @@ version-major+minor+point version-major+minor version-major + version-unique-prefix guile-version>? version-prefix? string-replace-substring @@ -589,6 +590,38 @@ minor version numbers from version-string." "Return the major version number as string from the version-string." (version-prefix version-string 1)) +(define (version-unique-prefix version versions) + "Return the shortest version prefix to unambiguously identify VERSION among +VERSIONS. For example: + + (version-unique-prefix \"2.0\" '(\"3.0\" \"2.0\")) + => \"2\" + + (version-unique-prefix \"2.2\" '(\"3.0.5\" \"2.0.9\" \"2.2.7\")) + => \"2.2\" + + (version-unique-prefix \"27.1\" '(\"27.1\")) + => \"\" +" + (define not-dot + (char-set-complement (char-set #\.))) + + (define other-versions + (delete version versions)) + + (let loop ((prefix '()) + (components (string-tokenize version not-dot))) + (define prefix-str + (string-join prefix ".")) + + (if (any (cut string-prefix? prefix-str <>) other-versions) + (match components + ((head . tail) + (loop `(,@prefix ,head) tail)) + (() + version)) + prefix-str))) + (define (version>? a b) "Return #t when A denotes a version strictly newer than B." (eq? '> (version-compare a b))) diff --git a/tests/utils.scm b/tests/utils.scm index 9bce446d98..62ec7e8b4c 100644 --- a/tests/utils.scm +++ b/tests/utils.scm @@ -1,5 +1,5 @@ ;;; GNU Guix --- Functional package management for GNU -;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès +;;; Copyright © 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès ;;; Copyright © 2014 Eric Bavier ;;; Copyright © 2016 Mathieu Lirzin ;;; @@ -78,6 +78,12 @@ (not (version-prefix? "4.1" "4.16.2")) (not (version-prefix? "4.1" "4")))) +(test-equal "version-unique-prefix" + '("2" "2.2" "") + (list (version-unique-prefix "2.0" '("3.0" "2.0")) + (version-unique-prefix "2.2" '("3.0.5" "2.0.9" "2.2.7")) + (version-unique-prefix "27.1" '("27.1")))) + (test-equal "string-tokenize*" '(("foo") ("foo" "bar" "baz") -- 2.30.0