gnu: Add emacs-exec-path-from-shell.
[jackhill/guix/guix.git] / guix / git-download.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2017 Mathieu Lirzin <mthl@gnu.org>
4 ;;; Copyright © 2017 Christopher Baines <mail@cbaines.net>
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 git-download)
22 #:use-module (guix build utils)
23 #:use-module (guix gexp)
24 #:use-module (guix store)
25 #:use-module (guix monads)
26 #:use-module (guix records)
27 #:use-module (guix packages)
28 #:use-module (guix modules)
29 #:autoload (guix build-system gnu) (standard-packages)
30 #:use-module (ice-9 match)
31 #:use-module (ice-9 popen)
32 #:use-module (ice-9 rdelim)
33 #:use-module (ice-9 vlist)
34 #:use-module (srfi srfi-1)
35 #:export (git-reference
36 git-reference?
37 git-reference-url
38 git-reference-commit
39 git-reference-recursive?
40
41 git-fetch
42 git-version
43 git-file-name
44 git-predicate))
45
46 ;;; Commentary:
47 ;;;
48 ;;; An <origin> method that fetches a specific commit from a Git repository.
49 ;;; The repository URL and commit hash are specified with a <git-reference>
50 ;;; object.
51 ;;;
52 ;;; Code:
53
54 (define-record-type* <git-reference>
55 git-reference make-git-reference
56 git-reference?
57 (url git-reference-url)
58 (commit git-reference-commit)
59 (recursive? git-reference-recursive? ; whether to recurse into sub-modules
60 (default #f)))
61
62 (define (git-package)
63 "Return the default Git package."
64 (let ((distro (resolve-interface '(gnu packages version-control))))
65 (module-ref distro 'git)))
66
67 (define* (git-fetch ref hash-algo hash
68 #:optional name
69 #:key (system (%current-system)) (guile (default-guile))
70 (git (git-package)))
71 "Return a fixed-output derivation that fetches REF, a <git-reference>
72 object. The output is expected to have recursive hash HASH of type
73 HASH-ALGO (a symbol). Use NAME as the file name, or a generic name if #f."
74 (define inputs
75 ;; When doing 'git clone --recursive', we need sed, grep, etc. to be
76 ;; available so that 'git submodule' works.
77 (if (git-reference-recursive? ref)
78 (standard-packages)
79 '()))
80
81 (define zlib
82 (module-ref (resolve-interface '(gnu packages compression)) 'zlib))
83
84 (define config.scm
85 (scheme-file "config.scm"
86 #~(begin
87 (define-module (guix config)
88 #:export (%libz))
89
90 (define %libz
91 #+(file-append zlib "/lib/libz")))))
92
93 (define modules
94 (cons `((guix config) => ,config.scm)
95 (delete '(guix config)
96 (source-module-closure '((guix build git)
97 (guix build utils)
98 (guix build download-nar))))))
99
100 (define build
101 (with-imported-modules modules
102 #~(begin
103 (use-modules (guix build git)
104 (guix build utils)
105 (guix build download-nar)
106 (ice-9 match))
107
108 ;; The 'git submodule' commands expects Coreutils, sed,
109 ;; grep, etc. to be in $PATH.
110 (set-path-environment-variable "PATH" '("bin")
111 (match '#+inputs
112 (((names dirs) ...)
113 dirs)))
114
115 (or (git-fetch (getenv "git url") (getenv "git commit")
116 #$output
117 #:recursive? (call-with-input-string
118 (getenv "git recursive?")
119 read)
120 #:git-command (string-append #+git "/bin/git"))
121 (download-nar #$output)))))
122
123 (mlet %store-monad ((guile (package->derivation guile system)))
124 (gexp->derivation (or name "git-checkout") build
125
126 ;; Use environment variables and a fixed script name so
127 ;; there's only one script in store for all the
128 ;; downloads.
129 #:script-name "git-download"
130 #:env-vars
131 `(("git url" . ,(git-reference-url ref))
132 ("git commit" . ,(git-reference-commit ref))
133 ("git recursive?" . ,(object->string
134 (git-reference-recursive? ref))))
135
136 #:system system
137 #:local-build? #t ;don't offload repo cloning
138 #:hash-algo hash-algo
139 #:hash hash
140 #:recursive? #t
141 #:guile-for-build guile)))
142
143 (define (git-version version revision commit)
144 "Return the version string for packages using git-download."
145 (string-append version "-" revision "." (string-take commit 7)))
146
147 (define (git-file-name name version)
148 "Return the file-name for packages using git-download."
149 (string-append name "-" version "-checkout"))
150
151 \f
152 ;;;
153 ;;; 'git-predicate'.
154 ;;;
155
156 (define (files->directory-tree files)
157 "Return a tree of vhashes representing the directory listed in FILES, a list
158 like '(\"a/b\" \"b/c/d\")."
159 (fold (lambda (file result)
160 (let loop ((file (string-split file #\/))
161 (result result))
162 (match file
163 ((_)
164 result)
165 ((directory children ...)
166 (match (vhash-assoc directory result)
167 (#f
168 (vhash-cons directory (loop children vlist-null)
169 result))
170 ((_ . previous)
171 ;; XXX: 'vhash-delete' is O(n).
172 (vhash-cons directory (loop children previous)
173 (vhash-delete directory result)))))
174 (()
175 result))))
176 vlist-null
177 files))
178
179 (define (directory-in-tree? tree directory)
180 "Return true if DIRECTORY, a string like \"a/b\", denotes a directory listed
181 in TREE."
182 (let loop ((directory (string-split directory #\/))
183 (tree tree))
184 (match directory
185 (()
186 #t)
187 ((head . tail)
188 (match (vhash-assoc head tree)
189 ((_ . sub-tree) (loop tail sub-tree))
190 (#f #f))))))
191
192 (define (git-predicate directory)
193 "Return a predicate that returns true if a file is part of the Git checkout
194 living at DIRECTORY. Upon Git failure, return #f instead of a predicate.
195
196 The returned predicate takes two arguments FILE and STAT where FILE is an
197 absolute file name and STAT is the result of 'lstat'."
198 (let* ((pipe (with-directory-excursion directory
199 (open-pipe* OPEN_READ "git" "ls-files")))
200 (files (let loop ((lines '()))
201 (match (read-line pipe)
202 ((? eof-object?)
203 (reverse lines))
204 (line
205 (loop (cons line lines))))))
206 (directory-tree (files->directory-tree files))
207 (inodes (fold (lambda (file result)
208 (let ((stat
209 (lstat (string-append directory "/"
210 file))))
211 (vhash-consv (stat:ino stat) (stat:dev stat)
212 result)))
213 vlist-null
214 files))
215
216 ;; Note: For this to work we must *not* call 'canonicalize-path' on
217 ;; DIRECTORY or we would get discrepancies of the returned lambda is
218 ;; called with a non-canonical file name.
219 (prefix-length (+ 1 (string-length directory)))
220
221 (status (close-pipe pipe)))
222 (and (zero? status)
223 (lambda (file stat)
224 (match (stat:type stat)
225 ('directory
226 (directory-in-tree? directory-tree
227 (string-drop file prefix-length)))
228 ((or 'regular 'symlink)
229 ;; Comparing file names is always tricky business so we rely on
230 ;; inode numbers instead
231 (match (vhash-assv (stat:ino stat) inodes)
232 ((_ . dev) (= dev (stat:dev stat)))
233 (#f #f)))
234 (_
235 #f))))))
236
237 ;;; git-download.scm ends here