scripts: show: Replace 'args-fold*' by 'parse-command-line'.
[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
LC
67
68 (define modules
e9f8a7e2
MO
69 (delete '(guix config)
70 (source-module-closure '((guix build hg)
71 (guix build download-nar)))))
37ce440d 72
c4e48b68 73 (define build
37ce440d 74 (with-imported-modules modules
e9f8a7e2
MO
75 (with-extensions (list guile-zlib)
76 #~(begin
77 (use-modules (guix build hg)
78 (guix build download-nar))
c4e48b68 79
e9f8a7e2
MO
80 (or (hg-fetch '#$(hg-reference-url ref)
81 '#$(hg-reference-changeset ref)
82 #$output
83 #:hg-command (string-append #+hg "/bin/hg"))
84 (download-nar #$output))))))
c4e48b68
RW
85
86 (mlet %store-monad ((guile (package->derivation guile system)))
87 (gexp->derivation (or name "hg-checkout") build
67c2db17
LC
88 #:leaked-env-vars '("http_proxy" "https_proxy"
89 "LC_ALL" "LC_MESSAGES" "LANG"
90 "COLUMNS")
c4e48b68
RW
91 #:system system
92 #:local-build? #t ;don't offload repo cloning
93 #:hash-algo hash-algo
94 #:hash hash
95 #:recursive? #t
c4e48b68
RW
96 #:guile-for-build guile)))
97
521d33cd
HP
98(define (hg-file-list directory)
99 "Evaluates to a list of files contained in the repository at path
100 @var{directory}"
101 (let* ((port (open-input-pipe (format #f "hg files --repository ~s" directory)))
102 (files (let loop ((files '()))
103 (let ((line (read-line port)))
104 (cond
105 ((eof-object? line) files)
106 (else
107 (loop (cons line files))))))))
108 (close-pipe port)
109 (map canonicalize-path files)))
110
111(define (should-select? path-list candidate)
112 "Returns #t in case that @var{candidate} is a file that is part of the given
113@var{path-list}."
114 (let ((canon-candidate (canonicalize-path candidate)))
115 (let loop ((xs path-list))
116 (cond
117 ((null? xs)
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)))))))
123
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
127Mercurial."
128 (let ((files (hg-file-list directory)))
129 (lambda (file stat)
130 (should-select? files file))))
131
c4e48b68 132;;; hg-download.scm ends here