1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016, 2017, 2018 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 gexp)
23 #:use-module (guix store)
24 #:use-module (guix monads)
25 #:use-module (guix records)
26 #:use-module (guix packages)
27 #:use-module (guix modules)
28 #:autoload (guix build-system gnu) (standard-packages)
30 #:use-module (ice-9 match)
31 #:use-module (ice-9 vlist)
32 #:use-module (srfi srfi-1)
33 #:export (git-reference
37 git-reference-recursive?
46 ;;; An <origin> method that fetches a specific commit from a Git repository.
47 ;;; The repository URL and commit hash are specified with a <git-reference>
52 (define-record-type* <git-reference>
53 git-reference make-git-reference
55 (url git-reference-url)
56 (commit git-reference-commit)
57 (recursive? git-reference-recursive? ; whether to recurse into sub-modules
61 "Return the default Git package."
62 (let ((distro (resolve-interface '(gnu packages version-control))))
63 (module-ref distro 'git-minimal)))
65 (define* (git-fetch ref hash-algo hash
67 #:key (system (%current-system)) (guile (default-guile))
69 "Return a fixed-output derivation that fetches REF, a <git-reference>
70 object. The output is expected to have recursive hash HASH of type
71 HASH-ALGO (a symbol). Use NAME as the file name, or a generic name if #f."
73 ;; When doing 'git clone --recursive', we need sed, grep, etc. to be
74 ;; available so that 'git submodule' works.
75 (if (git-reference-recursive? ref)
78 ;; The 'swh-download' procedure requires tar and gzip.
79 `(("gzip" ,(module-ref (resolve-interface '(gnu packages compression))
81 ("tar" ,(module-ref (resolve-interface '(gnu packages base))
85 (module-ref (resolve-interface '(gnu packages compression)) 'zlib))
88 (module-ref (resolve-interface '(gnu packages guile)) 'guile-json))
91 (module-ref (resolve-interface '(gnu packages tls)) 'gnutls))
94 (scheme-file "config.scm"
96 (define-module (guix config)
100 #+(file-append zlib "/lib/libz")))))
103 (cons `((guix config) => ,config.scm)
104 (delete '(guix config)
105 (source-module-closure '((guix build git)
107 (guix build download-nar)
111 (with-imported-modules modules
112 (with-extensions (list guile-json gnutls) ;for (guix swh)
114 (use-modules (guix build git)
116 (guix build download-nar)
121 (call-with-input-string (getenv "git recursive?") read))
123 ;; The 'git submodule' commands expects Coreutils, sed,
124 ;; grep, etc. to be in $PATH.
125 (set-path-environment-variable "PATH" '("bin")
127 (((names dirs outputs ...) ...)
130 (setvbuf (current-output-port) 'line)
131 (setvbuf (current-error-port) 'line)
133 (or (git-fetch (getenv "git url") (getenv "git commit")
135 #:recursive? recursive?
136 #:git-command (string-append #+git "/bin/git"))
137 (download-nar #$output)
139 ;; As a last resort, attempt to download from Software Heritage.
140 ;; XXX: Currently recursive checkouts are not supported.
141 (and (not recursive?)
142 (swh-download (getenv "git url") (getenv "git commit")
145 (mlet %store-monad ((guile (package->derivation guile system)))
146 (gexp->derivation (or name "git-checkout") build
148 ;; Use environment variables and a fixed script name so
149 ;; there's only one script in store for all the
151 #:script-name "git-download"
153 `(("git url" . ,(git-reference-url ref))
154 ("git commit" . ,(git-reference-commit ref))
155 ("git recursive?" . ,(object->string
156 (git-reference-recursive? ref))))
159 #:local-build? #t ;don't offload repo cloning
160 #:hash-algo hash-algo
163 #:guile-for-build guile)))
165 (define (git-version version revision commit)
166 "Return the version string for packages using git-download."
167 (string-append version "-" revision "." (string-take commit 7)))
169 (define (git-file-name name version)
170 "Return the file-name for packages using git-download."
171 (string-append name "-" version "-checkout"))
178 (define (git-file-list directory)
179 "Return the list of files checked in in the Git repository at DIRECTORY.
180 The result is similar to that of the 'git ls-files' command, except that it
181 also includes directories, not just regular files. The returned file names
182 are relative to DIRECTORY, which is not necessarily the root of the checkout."
183 (let* (;; 'repository-working-directory' always returns a trailing "/",
184 ;; so add one here to ease the comparisons below.
185 (directory (string-append (canonicalize-path directory) "/"))
186 (dot-git (repository-discover directory))
187 (repository (repository-open dot-git))
188 ;; XXX: This procedure is mistakenly private in Guile-Git 0.1.0.
189 (workdir ((@@ (git repository) repository-working-directory)
191 (head (repository-head repository))
192 (oid (reference-target head))
193 (commit (commit-lookup repository oid))
194 (tree (commit-tree commit))
195 (files (tree-list tree)))
196 (repository-close! repository)
197 (if (string=? workdir directory)
199 (let ((relative (string-drop directory (string-length workdir))))
200 (filter-map (lambda (file)
201 (and (string-prefix? relative file)
202 (string-drop file (string-length relative))))
205 (define (git-predicate directory)
206 "Return a predicate that returns true if a file is part of the Git checkout
207 living at DIRECTORY. If DIRECTORY does not lie within a Git checkout, and
208 upon Git errors, return #f instead of a predicate.
210 The returned predicate takes two arguments FILE and STAT where FILE is an
211 absolute file name and STAT is the result of 'lstat'."
214 (let* ((files (git-file-list directory))
215 (inodes (fold (lambda (file result)
217 (lstat (string-append directory "/"
219 (vhash-consv (stat:ino stat) (stat:dev stat)
224 ;; Comparing file names is always tricky business so we rely on inode
226 (match (vhash-assv (stat:ino stat) inodes)
227 ((_ . dev) (= dev (stat:dev stat)))
231 ;;; git-download.scm ends here