Subject: [PATCH v3 3/3] guix: import: go: Use correct tag for go module in subdirectory.

From: Simon Tournier <[email-masked]>
Date: Thu, 25 May 2023 18:07:49 +0200

From: Stephen Paul Weber <[email-masked]>

* guix/import/go.scm (vcs->origin): New argument module-path-subdirectory.
Use it to get Git tag as described by <https://go.dev/ref/mod>.

Co-authored-by: Simon Tournier <[email-masked]>
Signed-off-by: Simon Tournier <[email-masked]>
---
 guix/import/go.scm | 35 ++++++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 11 deletions(-)

diff --git a/guix/import/go.scm b/guix/import/go.scm
index c6258296f6..c2f9065d94 100644
--- a/guix/import/go.scm
+++ b/guix/import/go.scm
@@ -6,6 +6,7 @@
 ;;; Copyright © 2021-2022 Ludovic Courtès <[email-masked]>
 ;;; Copyright © 2021 Xinglu Chen <[email-masked]>
 ;;; Copyright © 2021 Sarah Morgensen <[email-masked]>
+;;; Copyright © 2022 Stephen Paul Weber <[email-masked]>
 ;;; Copyright © 2021, 2023 Simon Tournier <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
@@ -522,28 +523,35 @@ (define* (git-checkout-hash url reference algorithm)
       (nix-base32-string->bytevector
        "0000000000000000000000000000000000000000000000000000"))))
 
-(define (vcs->origin vcs-type vcs-repo-url version)
+(define (vcs->origin vcs-type vcs-repo-url module-path-subdirectory version)
   "Generate the `origin' block of a package depending on what type of source
 control system is being used."
   (case vcs-type
     ((git)
-     (let ((plain-version? (string=? version (go-version->git-ref version)))
-           (v-prefixed?    (string-prefix? "v" version)))
+     (let* ((plain-version? (string=? version (go-version->git-ref version)))
+            (v-prefixed?    (string-prefix? "v" version))
+            ;; A module in a subdirectory has a tag prefixed by this
+            ;; subdirectory.  See <https://go.dev/ref/mod>.
+            (tag-prefix     (if (string=? module-path-subdirectory "") ;
+                                ""
+                                (string-append
+                                 (substring module-path-subdirectory 1) "/")))
+            (prefix         (string-append tag-prefix "v"))
+            (git-commit     (if plain-version?
+                                (string-append tag-prefix version)
+                                (go-version->git-ref version))))
        `(origin
           (method git-fetch)
           (uri (git-reference
                 (url ,vcs-repo-url)
-                ;; This is done because the version field of the package,
-                ;; which the generated quoted expression refers to, has been
-                ;; stripped of any 'v' prefixed.
                 (commit ,(if (and plain-version? v-prefixed?)
-                             '(string-append "v" version)
+                             `(string-append ,prefix version)
                              '(go-version->git-ref version)))))
           (file-name (git-file-name name version))
           (sha256
            (base32
             ,(bytevector->nix-base32-string
-              (git-checkout-hash vcs-repo-url (go-version->git-ref version)
+              (git-checkout-hash vcs-repo-url git-commit
                                  (hash-algorithm sha256))))))))
     ((hg)
      `(origin
@@ -621,12 +629,17 @@ (define* (go-module->guix-package module-path #:key
                            dependencies+versions
                            (map car dependencies+versions)))
          (module-path-sans-suffix
-          (match:prefix (string-match "([\\./]v[0-9]+)?$" module-path)))
+           (if (string-prefix? "gopkg.in" module-path)
+               module-path
+               (match:prefix (string-match "([\\./]v[0-9]+)?$" module-path))))
          (guix-name (go-module->guix-package-name module-path))
-         (root-module-path (module-path->repository-root module-path))
+         (root-module-path (module-path->repository-root module-path-sans-suffix))
          ;; The VCS type and URL are not included in goproxy information. For
          ;; this we need to fetch it from the official module page.
          (meta-data (fetch-module-meta-data root-module-path))
+         (module-path-subdirectory
+           (substring module-path-sans-suffix
+             (string-length (module-meta-import-prefix meta-data))))
          (vcs-type (module-meta-vcs meta-data))
          (vcs-repo-url (module-meta-data-repo-url meta-data goproxy))
          (synopsis (go-package-synopsis module-path))
@@ -637,7 +650,7 @@ (define* (go-module->guix-package module-path #:key
         (name ,guix-name)
         (version ,(strip-v-prefix version*))
         (source
-         ,(vcs->origin vcs-type vcs-repo-url version*))
+         ,(vcs->origin vcs-type vcs-repo-url module-path-subdirectory version*))
         (build-system go-build-system)
         (arguments
          '(#:import-path ,module-path
-- 
2.38.1