summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2026-03-04 21:48:22 +0100
committerLudovic Courtès <ludo@gnu.org>2026-03-10 22:53:54 +0100
commit78ddf62bfe2264c0085fdce4b2cbcad07f6eaf16 (patch)
tree480f058d1672b0578e4aacac6de77b992bf63eaa
parentfeb52586ecabbde4c5003e106e0a8dd4b9a3add9 (diff)
style: git-source: Handle more URLs.
* guix/import/utils.scm (tarball-url->git-repository-url): New procedure. * guix/scripts/style.scm (url-fetch->git-fetch)[transform-source]: Add ‘repository-url’ parameter. Use ‘tarball-url->git-repository-url’ when ‘home-page’ is not a Git URL. (transform-to-git-fetch): Rename ‘home-page’ to ‘repository-url’. * tests/import/utils.scm ("tarball-url->git-repository-url, guile"): New test. * tests/style.scm ("url-fetch->git-fetch, mirror:// URL"): New test. Change-Id: I4f8ca7c67a58f917d69380678b62c00962b0f9cd Signed-off-by: Ludovic Courtès <ludo@gnu.org>
-rw-r--r--guix/import/utils.scm38
-rw-r--r--guix/scripts/style.scm49
-rw-r--r--tests/import/utils.scm9
-rw-r--r--tests/style.scm37
4 files changed, 110 insertions, 23 deletions
diff --git a/guix/import/utils.scm b/guix/import/utils.scm
index 5f8a4c22f6e..c435981ca99 100644
--- a/guix/import/utils.scm
+++ b/guix/import/utils.scm
@@ -1,5 +1,5 @@
1;;; GNU Guix --- Functional package management for GNU 1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2012-2013, 2018-2020, 2023, 2025 Ludovic Courtès <ludo@gnu.org> 2;;; Copyright © 2012-2013, 2018-2020, 2023, 2025-2026 Ludovic Courtès <ludo@gnu.org>
3;;; Copyright © 2016 Jelle Licht <jlicht@fsfe.org> 3;;; Copyright © 2016 Jelle Licht <jlicht@fsfe.org>
4;;; Copyright © 2016 David Craven <david@craven.ch> 4;;; Copyright © 2016 David Craven <david@craven.ch>
5;;; Copyright © 2017, 2019, 2020, 2022, 2023, 2024, 2025 Ricardo Wurmus <rekado@elephly.net> 5;;; Copyright © 2017, 2019, 2020, 2022, 2023, 2024, 2025 Ricardo Wurmus <rekado@elephly.net>
@@ -58,6 +58,11 @@
58 #:use-module (ice-9 rdelim) 58 #:use-module (ice-9 rdelim)
59 #:use-module (ice-9 receive) 59 #:use-module (ice-9 receive)
60 #:use-module (ice-9 regex) 60 #:use-module (ice-9 regex)
61 #:autoload (web uri) (string->uri
62 uri-scheme
63 uri-host
64 uri-path
65 split-and-decode-uri-path)
61 #:use-module (srfi srfi-1) 66 #:use-module (srfi srfi-1)
62 #:use-module (srfi srfi-9) 67 #:use-module (srfi srfi-9)
63 #:use-module (srfi srfi-11) 68 #:use-module (srfi srfi-11)
@@ -76,6 +81,7 @@
76 peek-body 81 peek-body
77 82
78 git-repository-url? 83 git-repository-url?
84 tarball-url->git-repository-url
79 download-git-repository 85 download-git-repository
80 git-origin 86 git-origin
81 git->origin 87 git->origin
@@ -202,6 +208,36 @@ thrown."
202 ;; Fallback. 208 ;; Fallback.
203 (string-suffix? ".git" url))) 209 (string-suffix? ".git" url)))
204 210
211(define (tarball-url->git-repository-url url)
212 "Given URL, the URL of a source code tarball, return the URL of the
213corresponding Git repository or #f if it could not be guessed."
214 (let ((uri (string->uri url)))
215 (match (uri-scheme uri)
216 ('mirror
217 (match (uri-host uri)
218 ((or "gnu" "savannah")
219 (string-append "https://https.git.savannah.gnu.org/git/"
220 (match (split-and-decode-uri-path (uri-path uri))
221 ((name _ ...)
222 (string-append name ".git")))))
223 ("gnome"
224 (string-append "https://gitlab.gnome.org/GNOME/"
225 (match (split-and-decode-uri-path (uri-path uri))
226 (("sources" name _ ...)
227 (string-append name ".git")))))
228 ;; TODO: Add "kernel" and other mirrors.
229 (_ #f)))
230 ((or 'https 'http)
231 (match (uri-host uri)
232 ((or "github.com" "gitlab.com")
233 (match (split-and-decode-uri-path (uri-path uri))
234 ((owner repository _ ...)
235 (string-append "https://" (uri-host uri)
236 "/" owner "/" repository))))
237 (_
238 #f)))
239 (_ #f))))
240
205(define* (download-git-repository url ref #:key recursive?) 241(define* (download-git-repository url ref #:key recursive?)
206 "Fetch the given REF from the Git repository at URL. Return three values : 242 "Fetch the given REF from the Git repository at URL. Return three values :
207the commit hash, the downloaded directory and its content hash." 243the commit hash, the downloaded directory and its content hash."
diff --git a/guix/scripts/style.scm b/guix/scripts/style.scm
index 9b9695b6018..049ce95b317 100644
--- a/guix/scripts/style.scm
+++ b/guix/scripts/style.scm
@@ -1,5 +1,5 @@
1;;; GNU Guix --- Functional package management for GNU 1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2021-2025 Ludovic Courtès <ludo@gnu.org> 2;;; Copyright © 2021-2026 Ludovic Courtès <ludo@gnu.org>
3;;; Copyright © 2024 Herman Rimm <herman@rimm.ee> 3;;; Copyright © 2024 Herman Rimm <herman@rimm.ee>
4;;; Copyright © 2025 Nicolas Graves <ngraves@ngraves.fr> 4;;; Copyright © 2025 Nicolas Graves <ngraves@ngraves.fr>
5;;; 5;;;
@@ -33,7 +33,8 @@
33 #:autoload (gnu packages) (specification->package fold-packages) 33 #:autoload (gnu packages) (specification->package fold-packages)
34 #:autoload (guix import utils) (default-git-error 34 #:autoload (guix import utils) (default-git-error
35 generate-git-source 35 generate-git-source
36 git-repository-url?) 36 git-repository-url?
37 tarball-url->git-repository-url)
37 #:use-module (guix combinators) 38 #:use-module (guix combinators)
38 #:use-module (guix scripts) 39 #:use-module (guix scripts)
39 #:use-module ((guix scripts build) #:select (%standard-build-options)) 40 #:use-module ((guix scripts build) #:select (%standard-build-options))
@@ -47,7 +48,6 @@
47 #:use-module (ice-9 control) 48 #:use-module (ice-9 control)
48 #:use-module (ice-9 match) 49 #:use-module (ice-9 match)
49 #:use-module (srfi srfi-1) 50 #:use-module (srfi srfi-1)
50 #:use-module (srfi srfi-2)
51 #:use-module (srfi srfi-9) 51 #:use-module (srfi srfi-9)
52 #:use-module (srfi srfi-11) 52 #:use-module (srfi srfi-11)
53 #:use-module (srfi srfi-26) 53 #:use-module (srfi srfi-26)
@@ -569,7 +569,7 @@ are put in alphabetical order."
569;;; url-fetch->git-fetch 569;;; url-fetch->git-fetch
570;;; 570;;;
571 571
572(define (transform-to-git-fetch location origin home-page version) 572(define (transform-to-git-fetch location origin repository-url version)
573 "Transform an origin using url-fetch to use git-fetch if appropriate. 573 "Transform an origin using url-fetch to use git-fetch if appropriate.
574Return the new origin S-expression or #f if transformation isn't applicable." 574Return the new origin S-expression or #f if transformation isn't applicable."
575 (match origin 575 (match origin
@@ -584,8 +584,8 @@ Return the new origin S-expression or #f if transformation isn't applicable."
584 (('snippet . _) #t) 584 (('snippet . _) #t)
585 (_ #f)) 585 (_ #f))
586 rest))) 586 rest)))
587 `(,@(generate-git-source home-page version 587 `(,@(generate-git-source repository-url version
588 (default-git-error home-page location)) 588 (default-git-error repository-url location))
589 ,@rest))) 589 ,@rest)))
590 (_ #f))) 590 (_ #f)))
591 591
@@ -594,12 +594,11 @@ Return the new origin S-expression or #f if transformation isn't applicable."
594 (policy 'safe) 594 (policy 'safe)
595 (edit-expression edit-expression)) 595 (edit-expression edit-expression))
596 "Transform PACKAGE's source from url-fetch to git-fetch when appropriate." 596 "Transform PACKAGE's source from url-fetch to git-fetch when appropriate."
597 (define (transform-source location str) 597 (define (transform-source location repository-url str)
598 (let* ((origin-exp (call-with-input-string str read-with-comments)) 598 (let* ((origin-exp (call-with-input-string str read-with-comments))
599 (home-page (package-home-page package))
600 (new-origin (transform-to-git-fetch location 599 (new-origin (transform-to-git-fetch location
601 origin-exp 600 origin-exp
602 home-page 601 repository-url
603 (package-version package)))) 602 (package-version package))))
604 (if new-origin 603 (if new-origin
605 (begin 604 (begin
@@ -607,18 +606,26 @@ Return the new origin S-expression or #f if transformation isn't applicable."
607 (object->string* new-origin (location-column location))) 606 (object->string* new-origin (location-column location)))
608 str))) 607 str)))
609 608
610 ;; Check if this package uses url-fetch and has a git repository home-page 609 ;; Check if this package uses 'url-fetch' and has a known corresponding Git
611 (and-let* ((source (package-source package)) 610 ;; repository.
612 (home-page (package-home-page package)) 611 (let* ((source (package-source package))
613 (location ; source might be inherited 612 (home-page (package-home-page package))
614 (and=> (and (origin? source) 613 (repository-url (and (origin? source)
615 (eq? url-fetch (origin-method source)) 614 (eq? url-fetch (origin-method source))
616 (git-repository-url? home-page) 615 (or (and (git-repository-url? home-page)
617 (package-field-location package 'source)) 616 home-page)
618 absolute-location))) 617 (and=> (match (origin-uri source)
619 (edit-expression 618 (((? string? head) . _) head)
620 (location->source-properties location) 619 ((? string? url) url)
621 (cut transform-source location <>)))) 620 (_ #f))
621 tarball-url->git-repository-url))))
622 (location ;source might be inherited
623 (and=> (package-field-location package 'source)
624 absolute-location)))
625 (when (and repository-url location)
626 (edit-expression
627 (location->source-properties location)
628 (cut transform-source location repository-url <>)))))
622 629
623 630
624;;; 631;;;
diff --git a/tests/import/utils.scm b/tests/import/utils.scm
index b631ba23268..c82fef78ec7 100644
--- a/tests/import/utils.scm
+++ b/tests/import/utils.scm
@@ -344,4 +344,13 @@ error procedure has been called."
344 (let ((sexp error-called? (test-generate-git-source "1.0.0" "2.0.0"))) 344 (let ((sexp error-called? (test-generate-git-source "1.0.0" "2.0.0")))
345 error-called?)) 345 error-called?))
346 346
347(test-equal "tarball-url->git-repository-url, guile"
348 '("https://https.git.savannah.gnu.org/git/guile.git"
349 "https://gitlab.gnome.org/GNOME/brasero.git"
350 "https://github.com/aide/aide")
351 (map tarball-url->git-repository-url
352 '("mirror://gnu/guile/guile-3.0.11.tar.gz"
353 "mirror://gnome/sources/brasero/3.12/brasero-3.12.3.tar.xz"
354 "https://github.com/aide/aide/releases/download/v0.19.3/aide-0.19.3.tar.gz")))
355
347(test-end "import-utils") 356(test-end "import-utils")
diff --git a/tests/style.scm b/tests/style.scm
index bc918a68bbd..350feed22b0 100644
--- a/tests/style.scm
+++ b/tests/style.scm
@@ -1,5 +1,5 @@
1;;; GNU Guix --- Functional package management for GNU 1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2021-2024 Ludovic Courtès <ludo@gnu.org> 2;;; Copyright © 2021-2024, 2026 Ludovic Courtès <ludo@gnu.org>
3;;; 3;;;
4;;; This file is part of GNU Guix. 4;;; This file is part of GNU Guix.
5;;; 5;;;
@@ -669,6 +669,41 @@
669 (cut string-contains <> "patches"))))))) 669 (cut string-contains <> "patches")))))))
670 "1")) 670 "1"))
671 671
672(unless (false-if-exception
673 (getaddrinfo "https.git.savannah.gnu.org" "https"))
674 (test-skip 1))
675(test-equal "url-fetch->git-fetch, mirror:// URL"
676 '(origin
677 (method git-fetch)
678 (uri (git-reference
679 (url "https://https.git.savannah.gnu.org/git/sed.git")
680 (commit (string-append "v" version))))
681 (file-name (git-file-name name version))
682 (sha256
683 (base32
684 "00p6v3aa22jz365scmifr06fspkylzrvbqda0waz4x06q5qv0263")))
685 (call-with-test-package
686 '((version "4.9")
687 (source
688 (origin
689 (method url-fetch)
690 (uri (string-append "mirror://gnu/sed/sed-"
691 version ".tar.gz"))
692 (sha256
693 (base32 "0000000000000000000000000000000000000000000000000000")))))
694 (lambda (directory)
695 (define file
696 (string-append directory "/my-packages-1.scm"))
697
698 ;; Note: This ends up cloning the 'sed' repository on Savannah.
699 (system* "guix" "style" "-L" directory "-S" "git-source" "my-coreutils-1")
700
701 (load file)
702 (call-with-input-string (read-package-field
703 (@ (my-packages-1) my-coreutils-1) 'source 8)
704 read))
705 "1"))
706
672(test-assert "url-fetch->git-fetch, non-git home-page unchanged" 707(test-assert "url-fetch->git-fetch, non-git home-page unchanged"
673 (call-with-test-package 708 (call-with-test-package
674 '((home-page "https://www.example.com") 709 '((home-page "https://www.example.com")