gnu-maintenance: 'release-file?' accepts 'v' prefix as in "PKG-v1.2.tgz".
[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
RW
3;;; Copyright © 2016 Ricardo Wurmus <rekado@elephly.net>
4;;;
5;;; This file is part of GNU Guix.
6;;;
7;;; GNU Guix is free software; you can redistribute it and/or modify it
8;;; under the terms of the GNU General Public License as published by
9;;; the Free Software Foundation; either version 3 of the License, or (at
10;;; your option) any later version.
11;;;
12;;; GNU Guix is distributed in the hope that it will be useful, but
13;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;;; GNU General Public License for more details.
16;;;
17;;; You should have received a copy of the GNU General Public License
18;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20(define-module (guix hg-download)
21 #:use-module (guix gexp)
22 #:use-module (guix store)
23 #:use-module (guix monads)
24 #:use-module (guix records)
37ce440d 25 #:use-module (guix modules)
c4e48b68
RW
26 #:use-module (guix packages)
27 #:autoload (guix build-system gnu) (standard-packages)
28 #:use-module (ice-9 match)
521d33cd
HP
29 #:use-module (ice-9 popen)
30 #:use-module (ice-9 rdelim)
c4e48b68
RW
31 #:export (hg-reference
32 hg-reference?
33 hg-reference-url
34 hg-reference-changeset
35 hg-reference-recursive?
521d33cd 36 hg-predicate
c4e48b68
RW
37 hg-fetch))
38
39;;; Commentary:
40;;;
41;;; An <origin> method that fetches a specific changeset from a Mercurial
42;;; repository. The repository URL and changeset ID are specified with a
43;;; <hg-reference> object.
44;;;
45;;; Code:
46
47(define-record-type* <hg-reference>
48 hg-reference make-hg-reference
49 hg-reference?
50 (url hg-reference-url)
51 (changeset hg-reference-changeset))
52
53(define (hg-package)
54 "Return the default Mercurial package."
55 (let ((distro (resolve-interface '(gnu packages version-control))))
56 (module-ref distro 'mercurial)))
57
58(define* (hg-fetch ref hash-algo hash
59 #:optional name
60 #:key (system (%current-system)) (guile (default-guile))
61 (hg (hg-package)))
62 "Return a fixed-output derivation that fetches REF, a <hg-reference>
63object. The output is expected to have recursive hash HASH of type
64HASH-ALGO (a symbol). Use NAME as the file name, or a generic name if #f."
e9f8a7e2
MO
65 (define guile-zlib
66 (module-ref (resolve-interface '(gnu packages guile)) 'guile-zlib))
37ce440d 67
d2b5bb5f
MO
68 (define guile-json
69 (module-ref (resolve-interface '(gnu packages guile)) 'guile-json-4))
70
71 (define gnutls
72 (module-ref (resolve-interface '(gnu packages tls)) 'gnutls))
73
37ce440d 74 (define modules
e9f8a7e2
MO
75 (delete '(guix config)
76 (source-module-closure '((guix build hg)
77 (guix build download-nar)))))
37ce440d 78
c4e48b68 79 (define build
37ce440d 80 (with-imported-modules modules
d2b5bb5f
MO
81 (with-extensions (list guile-json gnutls ;for (guix swh)
82 guile-zlib)
e9f8a7e2
MO
83 #~(begin
84 (use-modules (guix build hg)
85 (guix build download-nar))
c4e48b68 86
e9f8a7e2
MO
87 (or (hg-fetch '#$(hg-reference-url ref)
88 '#$(hg-reference-changeset ref)
89 #$output
90 #:hg-command (string-append #+hg "/bin/hg"))
91 (download-nar #$output))))))
c4e48b68
RW
92
93 (mlet %store-monad ((guile (package->derivation guile system)))
94 (gexp->derivation (or name "hg-checkout") build
67c2db17
LC
95 #:leaked-env-vars '("http_proxy" "https_proxy"
96 "LC_ALL" "LC_MESSAGES" "LANG"
97 "COLUMNS")
c4e48b68
RW
98 #:system system
99 #:local-build? #t ;don't offload repo cloning
100 #:hash-algo hash-algo
101 #:hash hash
102 #:recursive? #t
c4e48b68
RW
103 #:guile-for-build guile)))
104
521d33cd
HP
105(define (hg-file-list directory)
106 "Evaluates to a list of files contained in the repository at path
107 @var{directory}"
108 (let* ((port (open-input-pipe (format #f "hg files --repository ~s" directory)))
109 (files (let loop ((files '()))
110 (let ((line (read-line port)))
111 (cond
112 ((eof-object? line) files)
113 (else
114 (loop (cons line files))))))))
115 (close-pipe port)
116 (map canonicalize-path files)))
117
118(define (should-select? path-list candidate)
119 "Returns #t in case that @var{candidate} is a file that is part of the given
120@var{path-list}."
121 (let ((canon-candidate (canonicalize-path candidate)))
122 (let loop ((xs path-list))
123 (cond
124 ((null? xs)
125 ;; Directories are not part of `hg files', but `local-file' will not
126 ;; recurse if we don't return #t for directories.
127 (equal? (array-ref (lstat candidate) 13) 'directory))
128 ((string-contains candidate (car xs)) #t)
129 (else (loop (cdr xs)))))))
130
131(define (hg-predicate directory)
132 "This procedure evaluates to a predicate that reports back whether a given
133@var{file} - @var{stat} combination is part of the files tracked by
134Mercurial."
135 (let ((files (hg-file-list directory)))
136 (lambda (file stat)
137 (should-select? files file))))
138
c4e48b68 139;;; hg-download.scm ends here