Factorize test suite support in (guix tests).
[jackhill/guix/guix.git] / guix / git-download.scm
CommitLineData
9b5b5c17
LC
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2014 Ludovic Courtès <ludo@gnu.org>
3;;;
4;;; This file is part of GNU Guix.
5;;;
6;;; GNU Guix is free software; you can redistribute it and/or modify it
7;;; under the terms of the GNU General Public License as published by
8;;; the Free Software Foundation; either version 3 of the License, or (at
9;;; your option) any later version.
10;;;
11;;; GNU Guix is distributed in the hope that it will be useful, but
12;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19(define-module (guix git-download)
20 #:use-module (guix records)
21 #:use-module (guix derivations)
22 #:use-module (guix packages)
6750877f 23 #:autoload (guix build-system gnu) (standard-inputs)
9b5b5c17
LC
24 #:use-module (ice-9 match)
25 #:export (git-reference
26 git-reference?
27 git-reference-url
28 git-reference-commit
6750877f 29 git-reference-recursive?
9b5b5c17
LC
30
31 git-fetch))
32
33;;; Commentary:
34;;;
35;;; An <origin> method that fetches a specific commit from a Git repository.
36;;; The repository URL and commit hash are specified with a <git-reference>
37;;; object.
38;;;
39;;; Code:
40
41(define-record-type* <git-reference>
42 git-reference make-git-reference
43 git-reference?
6750877f
LC
44 (url git-reference-url)
45 (commit git-reference-commit)
46 (recursive? git-reference-recursive? ; whether to recurse into sub-modules
47 (default #f)))
9b5b5c17
LC
48
49(define* (git-fetch store ref hash-algo hash
50 #:optional name
51 #:key (system (%current-system)) guile git)
52 "Return a fixed-output derivation in STORE that fetches REF, a
53<git-reference> object. The output is expected to have recursive hash HASH of
54type HASH-ALGO (a symbol). Use NAME as the file name, or a generic name if
55#f."
56 (define guile-for-build
57 (match guile
58 ((? package?)
59 (package-derivation store guile system))
60 (#f ; the default
61 (let* ((distro (resolve-interface '(gnu packages base)))
62 (guile (module-ref distro 'guile-final)))
63 (package-derivation store guile system)))))
64
65 (define git-for-build
66 (match git
67 ((? package?)
68 (package-derivation store git system))
69 (#f ; the default
70 (let* ((distro (resolve-interface '(gnu packages version-control)))
71 (git (module-ref distro 'git)))
72 (package-derivation store git system)))))
73
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)
78 (standard-inputs (%current-system))
79 '()))
80
9b5b5c17
LC
81 (let* ((command (string-append (derivation->output-path git-for-build)
82 "/bin/git"))
83 (builder `(begin
6750877f
LC
84 (use-modules (guix build git)
85 (guix build utils)
86 (ice-9 match))
87
88 ;; The 'git submodule' commands expects Coreutils, sed,
89 ;; grep, etc. to be in $PATH.
90 (set-path-environment-variable "PATH" '("bin")
91 (match %build-inputs
92 (((names . dirs) ...)
93 dirs)))
94
9b5b5c17
LC
95 (git-fetch ',(git-reference-url ref)
96 ',(git-reference-commit ref)
97 %output
6750877f 98 #:recursive? ',(git-reference-recursive? ref)
9b5b5c17
LC
99 #:git-command ',command))))
100 (build-expression->derivation store (or name "git-checkout") builder
101 #:system system
102 #:local-build? #t
6750877f
LC
103 #:inputs `(("git" ,git-for-build)
104 ,@inputs)
9b5b5c17
LC
105 #:hash-algo hash-algo
106 #:hash hash
107 #:recursive? #t
108 #:modules '((guix build git)
109 (guix build utils))
56c72822
LC
110 #:guile-for-build guile-for-build
111 #:local-build? #t)))
9b5b5c17
LC
112
113;;; git-download.scm ends here