build-system/go: Respect #:imported-modules when cross-compiling.
[jackhill/guix/guix.git] / guix / hg-download.scm
CommitLineData
c4e48b68 1;;; GNU Guix --- Functional package management for GNU
67c2db17 2;;; Copyright © 2014, 2015, 2016, 2017, 2019 Ludovic Courtès <ludo@gnu.org>
c4e48b68 3;;; Copyright © 2016 Ricardo Wurmus <rekado@elephly.net>
aaafd19b 4;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
c4e48b68
RW
5;;;
6;;; This file is part of GNU Guix.
7;;;
8;;; GNU Guix is free software; you can redistribute it and/or modify it
9;;; under the terms of the GNU General Public License as published by
10;;; the Free Software Foundation; either version 3 of the License, or (at
11;;; your option) any later version.
12;;;
13;;; GNU Guix is distributed in the hope that it will be useful, but
14;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;;; GNU General Public License for more details.
17;;;
18;;; You should have received a copy of the GNU General Public License
19;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21(define-module (guix hg-download)
22 #:use-module (guix gexp)
23 #:use-module (guix store)
24 #:use-module (guix monads)
25 #:use-module (guix records)
37ce440d 26 #:use-module (guix modules)
c4e48b68
RW
27 #:use-module (guix packages)
28 #:autoload (guix build-system gnu) (standard-packages)
aaafd19b
XC
29 #:use-module (srfi srfi-34)
30 #:use-module (srfi srfi-35)
c4e48b68 31 #:use-module (ice-9 match)
521d33cd
HP
32 #:use-module (ice-9 popen)
33 #:use-module (ice-9 rdelim)
c4e48b68
RW
34 #:export (hg-reference
35 hg-reference?
36 hg-reference-url
37 hg-reference-changeset
521d33cd 38 hg-predicate
aaafd19b
XC
39 hg-fetch
40 hg-version
41 hg-file-name))
c4e48b68
RW
42
43;;; Commentary:
44;;;
45;;; An <origin> method that fetches a specific changeset from a Mercurial
46;;; repository. The repository URL and changeset ID are specified with a
47;;; <hg-reference> object.
48;;;
49;;; Code:
50
51(define-record-type* <hg-reference>
52 hg-reference make-hg-reference
53 hg-reference?
54 (url hg-reference-url)
55 (changeset hg-reference-changeset))
56
57(define (hg-package)
58 "Return the default Mercurial package."
59 (let ((distro (resolve-interface '(gnu packages version-control))))
60 (module-ref distro 'mercurial)))
61
62(define* (hg-fetch ref hash-algo hash
63 #:optional name
64 #:key (system (%current-system)) (guile (default-guile))
65 (hg (hg-package)))
66 "Return a fixed-output derivation that fetches REF, a <hg-reference>
67object. The output is expected to have recursive hash HASH of type
68HASH-ALGO (a symbol). Use NAME as the file name, or a generic name if #f."
69d73332
XC
69 (define inputs
70 ;; The 'swh-download' procedure requires tar and gzip.
71 `(("gzip" ,(module-ref (resolve-interface '(gnu packages compression))
72 'gzip))
73 ("tar" ,(module-ref (resolve-interface '(gnu packages base))
74 'tar))))
75
e9f8a7e2
MO
76 (define guile-zlib
77 (module-ref (resolve-interface '(gnu packages guile)) 'guile-zlib))
37ce440d 78
d2b5bb5f
MO
79 (define guile-json
80 (module-ref (resolve-interface '(gnu packages guile)) 'guile-json-4))
81
82 (define gnutls
83 (module-ref (resolve-interface '(gnu packages tls)) 'gnutls))
84
37ce440d 85 (define modules
e9f8a7e2
MO
86 (delete '(guix config)
87 (source-module-closure '((guix build hg)
69d73332
XC
88 (guix build download-nar)
89 (guix swh)))))
37ce440d 90
c4e48b68 91 (define build
37ce440d 92 (with-imported-modules modules
d2b5bb5f
MO
93 (with-extensions (list guile-json gnutls ;for (guix swh)
94 guile-zlib)
e9f8a7e2
MO
95 #~(begin
96 (use-modules (guix build hg)
69d73332
XC
97 (guix build utils) ;for `set-path-environment-variable'
98 (guix build download-nar)
99 (guix swh)
100 (ice-9 match))
101
102 (set-path-environment-variable "PATH" '("bin")
103 (match '#+inputs
104 (((names dirs outputs ...) ...)
105 dirs)))
c4e48b68 106
0b535f7b
LC
107 (setvbuf (current-output-port) 'line)
108 (setvbuf (current-error-port) 'line)
109
e9f8a7e2
MO
110 (or (hg-fetch '#$(hg-reference-url ref)
111 '#$(hg-reference-changeset ref)
112 #$output
113 #:hg-command (string-append #+hg "/bin/hg"))
69d73332
XC
114 (download-nar #$output)
115 ;; As a last resort, attempt to download from Software Heritage.
116 ;; Disable X.509 certificate verification to avoid depending
117 ;; on nss-certs--we're authenticating the checkout anyway.
118 (parameterize ((%verify-swh-certificate? #f))
119 (format (current-error-port)
120 "Trying to download from Software Heritage...~%")
121 (swh-download #$(hg-reference-url ref)
122 #$(hg-reference-changeset ref)
123 #$output)))))))
c4e48b68
RW
124
125 (mlet %store-monad ((guile (package->derivation guile system)))
126 (gexp->derivation (or name "hg-checkout") build
67c2db17
LC
127 #:leaked-env-vars '("http_proxy" "https_proxy"
128 "LC_ALL" "LC_MESSAGES" "LANG"
129 "COLUMNS")
c4e48b68
RW
130 #:system system
131 #:local-build? #t ;don't offload repo cloning
132 #:hash-algo hash-algo
133 #:hash hash
134 #:recursive? #t
c4e48b68
RW
135 #:guile-for-build guile)))
136
aaafd19b
XC
137(define (hg-version version revision changeset)
138 "Return the version string for packages using hg-download."
139 ;; hg-version is almost exclusively executed while modules are being loaded.
140 ;; This makes any errors hide their backtrace. Avoid the mysterious error
141 ;; "Value out of range 0 to N: 7" when the commit ID is too short, which
142 ;; can happen, for example, when the user swapped the revision and commit
143 ;; arguments by mistake.
144 (when (< (string-length changeset) 7)
145 (raise
146 (condition
147 (&message (message "hg-version: changeset ID unexpectedly short")))))
148 (string-append version "-" revision "." (string-take changeset 7)))
149
150(define (hg-file-name name version)
151 "Return the file-name for packages using hg-download."
152 (string-append name "-" version "-checkout"))
153
521d33cd
HP
154(define (hg-file-list directory)
155 "Evaluates to a list of files contained in the repository at path
156 @var{directory}"
157 (let* ((port (open-input-pipe (format #f "hg files --repository ~s" directory)))
158 (files (let loop ((files '()))
159 (let ((line (read-line port)))
160 (cond
161 ((eof-object? line) files)
162 (else
163 (loop (cons line files))))))))
164 (close-pipe port)
165 (map canonicalize-path files)))
166
167(define (should-select? path-list candidate)
168 "Returns #t in case that @var{candidate} is a file that is part of the given
169@var{path-list}."
170 (let ((canon-candidate (canonicalize-path candidate)))
171 (let loop ((xs path-list))
172 (cond
173 ((null? xs)
174 ;; Directories are not part of `hg files', but `local-file' will not
175 ;; recurse if we don't return #t for directories.
176 (equal? (array-ref (lstat candidate) 13) 'directory))
177 ((string-contains candidate (car xs)) #t)
178 (else (loop (cdr xs)))))))
179
180(define (hg-predicate directory)
181 "This procedure evaluates to a predicate that reports back whether a given
182@var{file} - @var{stat} combination is part of the files tracked by
183Mercurial."
184 (let ((files (hg-file-list directory)))
185 (lambda (file stat)
186 (should-select? files file))))
187
c4e48b68 188;;; hg-download.scm ends here