gnu: Add 'git-minimal'.
[jackhill/guix/guix.git] / guix / git-download.scm
CommitLineData
9b5b5c17 1;;; GNU Guix --- Functional package management for GNU
aed0a594 2;;; Copyright © 2014, 2015, 2016, 2017, 2018 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)
6119ebf1 22 #:use-module (guix gexp)
e87f0591 23 #:use-module (guix store)
6119ebf1 24 #:use-module (guix monads)
9b5b5c17 25 #:use-module (guix records)
9b5b5c17 26 #:use-module (guix packages)
37ce440d 27 #:use-module (guix modules)
0d5a559f 28 #:autoload (guix build-system gnu) (standard-packages)
aed0a594 29 #:use-module (git)
9b5b5c17 30 #:use-module (ice-9 match)
f135b4ae 31 #:use-module (ice-9 vlist)
6554be68 32 #:use-module (srfi srfi-1)
9b5b5c17
LC
33 #:export (git-reference
34 git-reference?
35 git-reference-url
36 git-reference-commit
6750877f 37 git-reference-recursive?
9b5b5c17 38
ee17a9e0
DC
39 git-fetch
40 git-version
6554be68
ML
41 git-file-name
42 git-predicate))
9b5b5c17
LC
43
44;;; Commentary:
45;;;
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>
48;;; object.
49;;;
50;;; Code:
51
52(define-record-type* <git-reference>
53 git-reference make-git-reference
54 git-reference?
6750877f
LC
55 (url git-reference-url)
56 (commit git-reference-commit)
57 (recursive? git-reference-recursive? ; whether to recurse into sub-modules
58 (default #f)))
9b5b5c17 59
6119ebf1
LC
60(define (git-package)
61 "Return the default Git package."
62 (let ((distro (resolve-interface '(gnu packages version-control))))
63 (module-ref distro 'git)))
64
f220a838 65(define* (git-fetch ref hash-algo hash
9b5b5c17 66 #:optional name
f220a838 67 #:key (system (%current-system)) (guile (default-guile))
6119ebf1 68 (git (git-package)))
f220a838
LC
69 "Return a fixed-output derivation that fetches REF, a <git-reference>
70object. The output is expected to have recursive hash HASH of type
71HASH-ALGO (a symbol). Use NAME as the file name, or a generic name if #f."
6750877f
LC
72 (define inputs
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)
0d5a559f 76 (standard-packages)
6750877f
LC
77 '()))
78
37ce440d
LC
79 (define zlib
80 (module-ref (resolve-interface '(gnu packages compression)) 'zlib))
81
82 (define config.scm
83 (scheme-file "config.scm"
84 #~(begin
85 (define-module (guix config)
86 #:export (%libz))
87
88 (define %libz
89 #+(file-append zlib "/lib/libz")))))
90
91 (define modules
92 (cons `((guix config) => ,config.scm)
93 (delete '(guix config)
94 (source-module-closure '((guix build git)
95 (guix build utils)
96 (guix build download-nar))))))
97
6119ebf1 98 (define build
37ce440d 99 (with-imported-modules modules
e9b046fd
LC
100 #~(begin
101 (use-modules (guix build git)
102 (guix build utils)
37ce440d 103 (guix build download-nar)
e9b046fd 104 (ice-9 match))
6119ebf1 105
e9b046fd
LC
106 ;; The 'git submodule' commands expects Coreutils, sed,
107 ;; grep, etc. to be in $PATH.
108 (set-path-environment-variable "PATH" '("bin")
109 (match '#+inputs
be95bcf0 110 (((names dirs outputs ...) ...)
e9b046fd 111 dirs)))
6750877f 112
37ce440d
LC
113 (or (git-fetch (getenv "git url") (getenv "git commit")
114 #$output
115 #:recursive? (call-with-input-string
116 (getenv "git recursive?")
117 read)
118 #:git-command (string-append #+git "/bin/git"))
119 (download-nar #$output)))))
6750877f 120
f220a838 121 (mlet %store-monad ((guile (package->derivation guile system)))
6119ebf1 122 (gexp->derivation (or name "git-checkout") build
c0b2d08b
LC
123
124 ;; Use environment variables and a fixed script name so
125 ;; there's only one script in store for all the
126 ;; downloads.
127 #:script-name "git-download"
128 #:env-vars
129 `(("git url" . ,(git-reference-url ref))
130 ("git commit" . ,(git-reference-commit ref))
131 ("git recursive?" . ,(object->string
132 (git-reference-recursive? ref))))
133
6119ebf1 134 #:system system
6b44a097 135 #:local-build? #t ;don't offload repo cloning
6119ebf1
LC
136 #:hash-algo hash-algo
137 #:hash hash
138 #:recursive? #t
5c6a30c5 139 #:guile-for-build guile)))
9b5b5c17 140
ee17a9e0
DC
141(define (git-version version revision commit)
142 "Return the version string for packages using git-download."
143 (string-append version "-" revision "." (string-take commit 7)))
144
145(define (git-file-name name version)
146 "Return the file-name for packages using git-download."
147 (string-append name "-" version "-checkout"))
148
f135b4ae
CB
149\f
150;;;
151;;; 'git-predicate'.
152;;;
153
aed0a594
LC
154(define (git-file-list directory)
155 "Return the list of files checked in in the Git repository at DIRECTORY.
156The result is similar to that of the 'git ls-files' command, except that it
157also includes directories, not just regular files. The returned file names
158are relative to DIRECTORY, which is not necessarily the root of the checkout."
280fc835
MB
159 (let* (;; 'repository-working-directory' always returns a trailing "/",
160 ;; so add one here to ease the comparisons below.
161 (directory (string-append (canonicalize-path directory) "/"))
aed0a594 162 (dot-git (repository-discover directory))
aed0a594 163 (repository (repository-open dot-git))
280fc835
MB
164 ;; XXX: This procedure is mistakenly private in Guile-Git 0.1.0.
165 (workdir ((@@ (git repository) repository-working-directory)
166 repository))
aed0a594
LC
167 (head (repository-head repository))
168 (oid (reference-target head))
169 (commit (commit-lookup repository oid))
170 (tree (commit-tree commit))
171 (files (tree-list tree)))
172 (repository-close! repository)
280fc835 173 (if (string=? workdir directory)
aed0a594 174 files
280fc835 175 (let ((relative (string-drop directory (string-length workdir))))
aed0a594
LC
176 (filter-map (lambda (file)
177 (and (string-prefix? relative file)
178 (string-drop file (string-length relative))))
179 files)))))
f135b4ae 180
6554be68
ML
181(define (git-predicate directory)
182 "Return a predicate that returns true if a file is part of the Git checkout
13512e1b
LC
183living at DIRECTORY. If DIRECTORY does not lie within a Git checkout, and
184upon Git errors, return #f instead of a predicate.
6554be68
ML
185
186The returned predicate takes two arguments FILE and STAT where FILE is an
187absolute file name and STAT is the result of 'lstat'."
13512e1b
LC
188 (catch 'git-error
189 (lambda ()
190 (let* ((files (git-file-list directory))
191 (inodes (fold (lambda (file result)
192 (let ((stat
193 (lstat (string-append directory "/"
194 file))))
195 (vhash-consv (stat:ino stat) (stat:dev stat)
196 result)))
197 vlist-null
198 files)))
199 (lambda (file stat)
200 ;; Comparing file names is always tricky business so we rely on inode
201 ;; numbers instead.
202 (match (vhash-assv (stat:ino stat) inodes)
203 ((_ . dev) (= dev (stat:dev stat)))
204 (#f #f)))))
205 (const #f)))
6554be68 206
9b5b5c17 207;;; git-download.scm ends here