Merge branch 'master' into core-updates
[jackhill/guix/guix.git] / guix / git-download.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2015, 2016 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 gexp)
21 #:use-module (guix store)
22 #:use-module (guix monads)
23 #:use-module (guix records)
24 #:use-module (guix packages)
25 #:autoload (guix build-system gnu) (standard-packages)
26 #:use-module (ice-9 match)
27 #:export (git-reference
28 git-reference?
29 git-reference-url
30 git-reference-commit
31 git-reference-recursive?
32
33 git-fetch))
34
35 ;;; Commentary:
36 ;;;
37 ;;; An <origin> method that fetches a specific commit from a Git repository.
38 ;;; The repository URL and commit hash are specified with a <git-reference>
39 ;;; object.
40 ;;;
41 ;;; Code:
42
43 (define-record-type* <git-reference>
44 git-reference make-git-reference
45 git-reference?
46 (url git-reference-url)
47 (commit git-reference-commit)
48 (recursive? git-reference-recursive? ; whether to recurse into sub-modules
49 (default #f)))
50
51 (define (git-package)
52 "Return the default Git package."
53 (let ((distro (resolve-interface '(gnu packages version-control))))
54 (module-ref distro 'git)))
55
56 (define* (git-fetch ref hash-algo hash
57 #:optional name
58 #:key (system (%current-system)) (guile (default-guile))
59 (git (git-package)))
60 "Return a fixed-output derivation that fetches REF, a <git-reference>
61 object. The output is expected to have recursive hash HASH of type
62 HASH-ALGO (a symbol). Use NAME as the file name, or a generic name if #f."
63 (define inputs
64 ;; When doing 'git clone --recursive', we need sed, grep, etc. to be
65 ;; available so that 'git submodule' works.
66 (if (git-reference-recursive? ref)
67 (standard-packages)
68 '()))
69
70 (define build
71 (with-imported-modules '((guix build git)
72 (guix build utils))
73 #~(begin
74 (use-modules (guix build git)
75 (guix build utils)
76 (ice-9 match))
77
78 ;; The 'git submodule' commands expects Coreutils, sed,
79 ;; grep, etc. to be in $PATH.
80 (set-path-environment-variable "PATH" '("bin")
81 (match '#+inputs
82 (((names dirs) ...)
83 dirs)))
84
85 (git-fetch '#$(git-reference-url ref)
86 '#$(git-reference-commit ref)
87 #$output
88 #:recursive? '#$(git-reference-recursive? ref)
89 #:git-command (string-append #+git "/bin/git")))))
90
91 (mlet %store-monad ((guile (package->derivation guile system)))
92 (gexp->derivation (or name "git-checkout") build
93 #:system system
94 #:local-build? #t ;don't offload repo cloning
95 #:hash-algo hash-algo
96 #:hash hash
97 #:recursive? #t
98 #:guile-for-build guile
99 #:local-build? #t)))
100
101 ;;; git-download.scm ends here