diff mbox series

[bug#50377,v3,2/2] transformations: 'git describe' style commit IDs are used as version.

Message ID 20210906103846.14941-3-marius@gnu.org
State Accepted
Headers show
Series Support 'git describe' style commit IDs in transformations | expand

Checks

Context Check Description
cbaines/applying patch fail View Laminar job
cbaines/issue success View issue

Commit Message

Marius Bakke Sept. 6, 2021, 10:38 a.m. UTC
* guix/transformations.scm (transform-package-source-commit): Look for
'git describe' style IDs and use it as the version if applicable.
* tests/transformations.scm
("options->transformation, with-commit, 'git describe' style version"): New
test.
---
 guix/transformations.scm  | 37 ++++++++++++++++++++++++++++---------
 tests/transformations.scm | 19 +++++++++++++++++++
 2 files changed, 47 insertions(+), 9 deletions(-)

Comments

Ludovic Courtès Sept. 6, 2021, 9:04 p.m. UTC | #1
Marius Bakke <marius@gnu.org> skribis:

> * guix/transformations.scm (transform-package-source-commit): Look for
> 'git describe' style IDs and use it as the version if applicable.
> * tests/transformations.scm
> ("options->transformation, with-commit, 'git describe' style version"): New
> test.

[...]

> +      (version (cond ((and (string-contains commit "-g")
> +                           (match (string-split commit #\-)
> +                             ((version ... revision g+commit)
> +                              (and (> (string-length g+commit) 4)
> +                                   (string-every char-set:digit revision)
> +                                   (string-every char-set:hex-digit
> +                                                 (string-drop g+commit 1))))
> +                             (_ #f)))
> +                      ;; This looks like a 'git describe' style ID.  Drop
> +                      ;; the 'v' prefix if applicable.
> +                      (if (and (string-prefix? "v" commit)
> +                               (char-set-contains? char-set:digit
> +                                                   (string-take
> +                                                    (string-drop commit 1)
> +                                                    1)))
> +                          (string-drop commit 1)
> +                          commit))
> +                     ((and (> (string-length commit) 1)
> +                           (string-prefix? "v" commit)
> +                           (char-set-contains? char-set:digit
> +                                               (string-ref commit 1)))
> +                      (string-drop commit 1))       ;looks like a tag like "v1.0"
> +                     (else
> +                      (string-append "git."
> +                                     (if (< (string-length commit) 7)
> +                                         commit
> +                                         (string-take commit 7))))))

For clarity, I’d extract this as a ‘commit->version-string’ procedure.

Like Xinglu writes, it’d be great to add a sentence in the manual about
these IDs.

Apart from that, it’s a good idea and it LGTM!

Thanks,
Ludo’.
Marius Bakke Sept. 8, 2021, 4:08 p.m. UTC | #2
Ludovic Courtès <ludo@gnu.org> skriver:

> Marius Bakke <marius@gnu.org> skribis:
>
>> +      (version (cond ((and (string-contains commit "-g")
>> +                           (match (string-split commit #\-)
>> +                             ((version ... revision g+commit)
>> +                              (and (> (string-length g+commit) 4)
>> +                                   (string-every char-set:digit revision)
>> +                                   (string-every char-set:hex-digit
>> +                                                 (string-drop g+commit 1))))
>> +                             (_ #f)))
>> +                      ;; This looks like a 'git describe' style ID.  Drop
>> +                      ;; the 'v' prefix if applicable.
>> +                      (if (and (string-prefix? "v" commit)
>> +                               (char-set-contains? char-set:digit
>> +                                                   (string-take
>> +                                                    (string-drop commit 1)
>> +                                                    1)))
>> +                          (string-drop commit 1)
>> +                          commit))
>> +                     ((and (> (string-length commit) 1)
>> +                           (string-prefix? "v" commit)
>> +                           (char-set-contains? char-set:digit
>> +                                               (string-ref commit 1)))
>> +                      (string-drop commit 1))       ;looks like a tag like "v1.0"
>> +                     (else
>> +                      (string-append "git."
>> +                                     (if (< (string-length commit) 7)
>> +                                         commit
>> +                                         (string-take commit 7))))))
>
> For clarity, I’d extract this as a ‘commit->version-string’ procedure.
>
> Like Xinglu writes, it’d be great to add a sentence in the manual about
> these IDs.

Thanks for the feedback!  Looking at this again, I realized tags would
not be used as version either which seemed like an oversight.  So I
fixed(?) that too and vastly simplified this patch.  :-)

Also adjusted the test to more thoroughly excercise the new
commit->version-string procedure with the different arguments, and
updated the documentation.

Pushed in:
  1dc3825e99 git: 'resolve-reference' handles 'git describe'-style commit IDs.
  16ef7b4938 transformations: Git tags and 'git describe' style IDs are used as version.

Thanks,
Marius
diff mbox series

Patch

diff --git a/guix/transformations.scm b/guix/transformations.scm
index 5122baa403..af3eda76f8 100644
--- a/guix/transformations.scm
+++ b/guix/transformations.scm
@@ -1,5 +1,6 @@ 
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2021 Marius Bakke <marius@gnu.org>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -278,15 +279,33 @@  strings like \"guile-next=cabba9e\" meaning that packages are built using
   (define (replace old url commit)
     (package
       (inherit old)
-      (version (if (and (> (string-length commit) 1)
-                        (string-prefix? "v" commit)
-                        (char-set-contains? char-set:digit
-                                            (string-ref commit 1)))
-                   (string-drop commit 1)        ;looks like a tag like "v1.0"
-                   (string-append "git."
-                                  (if (< (string-length commit) 7)
-                                      commit
-                                      (string-take commit 7)))))
+      (version (cond ((and (string-contains commit "-g")
+                           (match (string-split commit #\-)
+                             ((version ... revision g+commit)
+                              (and (> (string-length g+commit) 4)
+                                   (string-every char-set:digit revision)
+                                   (string-every char-set:hex-digit
+                                                 (string-drop g+commit 1))))
+                             (_ #f)))
+                      ;; This looks like a 'git describe' style ID.  Drop
+                      ;; the 'v' prefix if applicable.
+                      (if (and (string-prefix? "v" commit)
+                               (char-set-contains? char-set:digit
+                                                   (string-take
+                                                    (string-drop commit 1)
+                                                    1)))
+                          (string-drop commit 1)
+                          commit))
+                     ((and (> (string-length commit) 1)
+                           (string-prefix? "v" commit)
+                           (char-set-contains? char-set:digit
+                                               (string-ref commit 1)))
+                      (string-drop commit 1))       ;looks like a tag like "v1.0"
+                     (else
+                      (string-append "git."
+                                     (if (< (string-length commit) 7)
+                                         commit
+                                         (string-take commit 7))))))
       (source (git-checkout (url url) (commit commit)
                             (recursive? #t)))))
 
diff --git a/tests/transformations.scm b/tests/transformations.scm
index 3417c994ec..44fccffcce 100644
--- a/tests/transformations.scm
+++ b/tests/transformations.scm
@@ -1,5 +1,6 @@ 
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2016, 2017, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
+;;; Copyright © 2021 Marius Bakke <marius@gnu.org>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -235,6 +236,24 @@ 
                    (string=? (package-name dep2) "chbouib")
                    (package-source dep2))))))))
 
+(test-equal "options->transformation, with-commit, 'git describe' style version"
+  "1.0-gcabba9e2"
+  (let* ((p (dummy-package "guix.scm"
+              (inputs `(("foo" ,grep)
+                        ("bar" ,(dummy-package "chbouib"
+                                  (source (origin
+                                            (method git-fetch)
+                                            (uri (git-reference
+                                                  (url "https://example.org")
+                                                  (commit "cabba9e")))
+                                            (sha256 #f)))))))))
+         (t (options->transformation '((with-commit . "chbouib=v1.0-gcabba9e2")))))
+    (let ((new (t p)))
+      (and (not (eq? new p))
+           (match (package-inputs new)
+             ((("foo" dep1) ("bar" dep2))
+              (package-version dep2)))))))
+
 (test-equal "options->transformation, with-git-url"
   (let ((source (git-checkout (url "https://example.org")
                               (recursive? #t))))