1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2016 Ricardo Wurmus <rekado@elephly.net>
5 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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)
25 #:use-module (guix modules)
26 #:use-module (guix packages)
27 #:autoload (guix build-system gnu) (standard-packages)
28 #:use-module (ice-9 match)
29 #:use-module (ice-9 popen)
30 #:use-module (ice-9 rdelim)
31 #:export (hg-reference
34 hg-reference-changeset
35 hg-reference-recursive?
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.
47 (define-record-type* <hg-reference>
48 hg-reference make-hg-reference
50 (url hg-reference-url)
51 (changeset hg-reference-changeset))
54 "Return the default Mercurial package."
55 (let ((distro (resolve-interface '(gnu packages version-control))))
56 (module-ref distro 'mercurial)))
58 (define* (hg-fetch ref hash-algo hash
60 #:key (system (%current-system)) (guile (default-guile))
62 "Return a fixed-output derivation that fetches REF, a <hg-reference>
63 object. The output is expected to have recursive hash HASH of type
64 HASH-ALGO (a symbol). Use NAME as the file name, or a generic name if #f."
66 (module-ref (resolve-interface '(gnu packages guile)) 'guile-zlib))
69 (delete '(guix config)
70 (source-module-closure '((guix build hg)
71 (guix build download-nar)))))
74 (with-imported-modules modules
75 (with-extensions (list guile-zlib)
77 (use-modules (guix build hg)
78 (guix build download-nar))
80 (or (hg-fetch '#$(hg-reference-url ref)
81 '#$(hg-reference-changeset ref)
83 #:hg-command (string-append #+hg "/bin/hg"))
84 (download-nar #$output))))))
86 (mlet %store-monad ((guile (package->derivation guile system)))
87 (gexp->derivation (or name "hg-checkout") build
88 #:leaked-env-vars '("http_proxy" "https_proxy"
89 "LC_ALL" "LC_MESSAGES" "LANG"
92 #:local-build? #t ;don't offload repo cloning
96 #:guile-for-build guile)))
98 (define (hg-file-list directory)
99 "Evaluates to a list of files contained in the repository at path
101 (let* ((port (open-input-pipe (format #f "hg files --repository ~s" directory)))
102 (files (let loop ((files '()))
103 (let ((line (read-line port)))
105 ((eof-object? line) files)
107 (loop (cons line files))))))))
109 (map canonicalize-path files)))
111 (define (should-select? path-list candidate)
112 "Returns #t in case that @var{candidate} is a file that is part of the given
114 (let ((canon-candidate (canonicalize-path candidate)))
115 (let loop ((xs path-list))
118 ;; Directories are not part of `hg files', but `local-file' will not
119 ;; recurse if we don't return #t for directories.
120 (equal? (array-ref (lstat candidate) 13) 'directory))
121 ((string-contains candidate (car xs)) #t)
122 (else (loop (cdr xs)))))))
124 (define (hg-predicate directory)
125 "This procedure evaluates to a predicate that reports back whether a given
126 @var{file} - @var{stat} combination is part of the files tracked by
128 (let ((files (hg-file-list directory)))
130 (should-select? files file))))
132 ;;; hg-download.scm ends here