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