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>
6 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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
39 git-reference-recursive?
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>
54 (define-record-type* <git-reference>
55 git-reference make-git-reference
57 (url git-reference-url)
58 (commit git-reference-commit)
59 (recursive? git-reference-recursive? ; whether to recurse into sub-modules
63 "Return the default Git package."
64 (let ((distro (resolve-interface '(gnu packages version-control))))
65 (module-ref distro 'git)))
67 (define* (git-fetch ref hash-algo hash
69 #:key (system (%current-system)) (guile (default-guile))
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."
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)
82 (module-ref (resolve-interface '(gnu packages compression)) 'zlib))
85 (scheme-file "config.scm"
87 (define-module (guix config)
91 #+(file-append zlib "/lib/libz")))))
94 (cons `((guix config) => ,config.scm)
95 (delete '(guix config)
96 (source-module-closure '((guix build git)
98 (guix build download-nar))))))
101 (with-imported-modules modules
103 (use-modules (guix build git)
105 (guix build download-nar)
108 ;; The 'git submodule' commands expects Coreutils, sed,
109 ;; grep, etc. to be in $PATH.
110 (set-path-environment-variable "PATH" '("bin")
115 (or (git-fetch (getenv "git url") (getenv "git commit")
117 #:recursive? (call-with-input-string
118 (getenv "git recursive?")
120 #:git-command (string-append #+git "/bin/git"))
121 (download-nar #$output)))))
123 (mlet %store-monad ((guile (package->derivation guile system)))
124 (gexp->derivation (or name "git-checkout") build
126 ;; Use environment variables and a fixed script name so
127 ;; there's only one script in store for all the
129 #:script-name "git-download"
131 `(("git url" . ,(git-reference-url ref))
132 ("git commit" . ,(git-reference-commit ref))
133 ("git recursive?" . ,(object->string
134 (git-reference-recursive? ref))))
137 #:local-build? #t ;don't offload repo cloning
138 #:hash-algo hash-algo
141 #:guile-for-build guile)))
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)))
147 (define (git-file-name name version)
148 "Return the file-name for packages using git-download."
149 (string-append name "-" version "-checkout"))
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 #\/))
165 ((directory children ...)
166 (match (vhash-assoc directory result)
168 (vhash-cons directory (loop children vlist-null)
171 ;; XXX: 'vhash-delete' is O(n).
172 (vhash-cons directory (loop children previous)
173 (vhash-delete directory result)))))
179 (define (directory-in-tree? tree directory)
180 "Return true if DIRECTORY, a string like \"a/b\", denotes a directory listed
182 (let loop ((directory (string-split directory #\/))
188 (match (vhash-assoc head tree)
189 ((_ . sub-tree) (loop tail sub-tree))
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.
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)
205 (loop (cons line lines))))))
206 (directory-tree (files->directory-tree files))
207 (inodes (fold (lambda (file result)
209 (lstat (string-append directory "/"
211 (vhash-consv (stat:ino stat) (stat:dev stat)
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)))
221 (status (close-pipe pipe)))
224 (match (stat:type stat)
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)))
237 ;;; git-download.scm ends here