Commit | Line | Data |
---|---|---|
9b5b5c17 | 1 | ;;; GNU Guix --- Functional package management for GNU |
f135b4ae | 2 | ;;; Copyright © 2014, 2015, 2016, 2017 Ludovic Courtès <ludo@gnu.org> |
6554be68 | 3 | ;;; Copyright © 2017 Mathieu Lirzin <mthl@gnu.org> |
f135b4ae | 4 | ;;; Copyright © 2017 Christopher Baines <mail@cbaines.net> |
9b5b5c17 LC |
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) | |
6554be68 | 22 | #:use-module (guix build utils) |
6119ebf1 | 23 | #:use-module (guix gexp) |
e87f0591 | 24 | #:use-module (guix store) |
6119ebf1 | 25 | #:use-module (guix monads) |
9b5b5c17 | 26 | #:use-module (guix records) |
9b5b5c17 | 27 | #:use-module (guix packages) |
37ce440d | 28 | #:use-module (guix modules) |
0d5a559f | 29 | #:autoload (guix build-system gnu) (standard-packages) |
9b5b5c17 | 30 | #:use-module (ice-9 match) |
6554be68 ML |
31 | #:use-module (ice-9 popen) |
32 | #:use-module (ice-9 rdelim) | |
f135b4ae | 33 | #:use-module (ice-9 vlist) |
6554be68 | 34 | #:use-module (srfi srfi-1) |
9b5b5c17 LC |
35 | #:export (git-reference |
36 | git-reference? | |
37 | git-reference-url | |
38 | git-reference-commit | |
6750877f | 39 | git-reference-recursive? |
9b5b5c17 | 40 | |
ee17a9e0 DC |
41 | git-fetch |
42 | git-version | |
6554be68 ML |
43 | git-file-name |
44 | git-predicate)) | |
9b5b5c17 LC |
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? | |
6750877f LC |
57 | (url git-reference-url) |
58 | (commit git-reference-commit) | |
59 | (recursive? git-reference-recursive? ; whether to recurse into sub-modules | |
60 | (default #f))) | |
9b5b5c17 | 61 | |
6119ebf1 LC |
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 | ||
f220a838 | 67 | (define* (git-fetch ref hash-algo hash |
9b5b5c17 | 68 | #:optional name |
f220a838 | 69 | #:key (system (%current-system)) (guile (default-guile)) |
6119ebf1 | 70 | (git (git-package))) |
f220a838 LC |
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." | |
6750877f LC |
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) | |
0d5a559f | 78 | (standard-packages) |
6750877f LC |
79 | '())) |
80 | ||
37ce440d LC |
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 | ||
6119ebf1 | 100 | (define build |
37ce440d | 101 | (with-imported-modules modules |
e9b046fd LC |
102 | #~(begin |
103 | (use-modules (guix build git) | |
104 | (guix build utils) | |
37ce440d | 105 | (guix build download-nar) |
e9b046fd | 106 | (ice-9 match)) |
6119ebf1 | 107 | |
e9b046fd LC |
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))) | |
6750877f | 114 | |
37ce440d LC |
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))))) | |
6750877f | 122 | |
f220a838 | 123 | (mlet %store-monad ((guile (package->derivation guile system))) |
6119ebf1 | 124 | (gexp->derivation (or name "git-checkout") build |
c0b2d08b LC |
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 | ||
6119ebf1 | 136 | #:system system |
6b44a097 | 137 | #:local-build? #t ;don't offload repo cloning |
6119ebf1 LC |
138 | #:hash-algo hash-algo |
139 | #:hash hash | |
140 | #:recursive? #t | |
5c6a30c5 | 141 | #:guile-for-build guile))) |
9b5b5c17 | 142 | |
ee17a9e0 DC |
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 | ||
f135b4ae CB |
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 | ||
6554be68 ML |
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'." | |
f135b4ae CB |
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)) | |
228a3982 LC |
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 | ||
f135b4ae | 221 | (status (close-pipe pipe))) |
6554be68 ML |
222 | (and (zero? status) |
223 | (lambda (file stat) | |
224 | (match (stat:type stat) | |
225 | ('directory | |
f135b4ae CB |
226 | (directory-in-tree? directory-tree |
227 | (string-drop file prefix-length))) | |
6554be68 | 228 | ((or 'regular 'symlink) |
ba2260db LC |
229 | ;; Comparing file names is always tricky business so we rely on |
230 | ;; inode numbers instead | |
f135b4ae CB |
231 | (match (vhash-assv (stat:ino stat) inodes) |
232 | ((_ . dev) (= dev (stat:dev stat))) | |
233 | (#f #f))) | |
6554be68 ML |
234 | (_ |
235 | #f)))))) | |
236 | ||
9b5b5c17 | 237 | ;;; git-download.scm ends here |