import: hackage: Handle unknown packages gracefully.
[jackhill/guix/guix.git] / guix / git-download.scm
CommitLineData
9b5b5c17 1;;; GNU Guix --- Functional package management for GNU
e9b046fd 2;;; Copyright © 2014, 2015, 2016 Ludovic Courtès <ludo@gnu.org>
9b5b5c17
LC
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)
6119ebf1 20 #:use-module (guix gexp)
e87f0591 21 #:use-module (guix store)
6119ebf1 22 #:use-module (guix monads)
9b5b5c17 23 #:use-module (guix records)
9b5b5c17 24 #:use-module (guix packages)
0d5a559f 25 #:autoload (guix build-system gnu) (standard-packages)
9b5b5c17
LC
26 #:use-module (ice-9 match)
27 #:export (git-reference
28 git-reference?
29 git-reference-url
30 git-reference-commit
6750877f 31 git-reference-recursive?
9b5b5c17 32
ee17a9e0
DC
33 git-fetch
34 git-version
35 git-file-name))
9b5b5c17
LC
36
37;;; Commentary:
38;;;
39;;; An <origin> method that fetches a specific commit from a Git repository.
40;;; The repository URL and commit hash are specified with a <git-reference>
41;;; object.
42;;;
43;;; Code:
44
45(define-record-type* <git-reference>
46 git-reference make-git-reference
47 git-reference?
6750877f
LC
48 (url git-reference-url)
49 (commit git-reference-commit)
50 (recursive? git-reference-recursive? ; whether to recurse into sub-modules
51 (default #f)))
9b5b5c17 52
6119ebf1
LC
53(define (git-package)
54 "Return the default Git package."
55 (let ((distro (resolve-interface '(gnu packages version-control))))
56 (module-ref distro 'git)))
57
f220a838 58(define* (git-fetch ref hash-algo hash
9b5b5c17 59 #:optional name
f220a838 60 #:key (system (%current-system)) (guile (default-guile))
6119ebf1 61 (git (git-package)))
f220a838
LC
62 "Return a fixed-output derivation that fetches REF, a <git-reference>
63object. The output is expected to have recursive hash HASH of type
64HASH-ALGO (a symbol). Use NAME as the file name, or a generic name if #f."
6750877f
LC
65 (define inputs
66 ;; When doing 'git clone --recursive', we need sed, grep, etc. to be
67 ;; available so that 'git submodule' works.
68 (if (git-reference-recursive? ref)
0d5a559f 69 (standard-packages)
6750877f
LC
70 '()))
71
6119ebf1 72 (define build
e9b046fd
LC
73 (with-imported-modules '((guix build git)
74 (guix build utils))
75 #~(begin
76 (use-modules (guix build git)
77 (guix build utils)
78 (ice-9 match))
6119ebf1 79
e9b046fd
LC
80 ;; The 'git submodule' commands expects Coreutils, sed,
81 ;; grep, etc. to be in $PATH.
82 (set-path-environment-variable "PATH" '("bin")
83 (match '#+inputs
84 (((names dirs) ...)
85 dirs)))
6750877f 86
c0b2d08b 87 (git-fetch (getenv "git url") (getenv "git commit")
e9b046fd 88 #$output
c0b2d08b
LC
89 #:recursive? (call-with-input-string
90 (getenv "git recursive?")
91 read)
e9b046fd 92 #:git-command (string-append #+git "/bin/git")))))
6750877f 93
f220a838 94 (mlet %store-monad ((guile (package->derivation guile system)))
6119ebf1 95 (gexp->derivation (or name "git-checkout") build
c0b2d08b
LC
96
97 ;; Use environment variables and a fixed script name so
98 ;; there's only one script in store for all the
99 ;; downloads.
100 #:script-name "git-download"
101 #:env-vars
102 `(("git url" . ,(git-reference-url ref))
103 ("git commit" . ,(git-reference-commit ref))
104 ("git recursive?" . ,(object->string
105 (git-reference-recursive? ref))))
106
6119ebf1 107 #:system system
6b44a097 108 #:local-build? #t ;don't offload repo cloning
6119ebf1
LC
109 #:hash-algo hash-algo
110 #:hash hash
111 #:recursive? #t
5c6a30c5 112 #:guile-for-build guile)))
9b5b5c17 113
ee17a9e0
DC
114(define (git-version version revision commit)
115 "Return the version string for packages using git-download."
116 (string-append version "-" revision "." (string-take commit 7)))
117
118(define (git-file-name name version)
119 "Return the file-name for packages using git-download."
120 (string-append name "-" version "-checkout"))
121
9b5b5c17 122;;; git-download.scm ends here