1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
3 ;;; Copyright © 2018 Ludovic Courtès <ludo@gnu.org>
5 ;;; This file is part of GNU Guix.
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20 (define-module (guix git)
22 #:use-module (git object)
23 #:use-module (guix i18n)
24 #:use-module (guix base32)
25 #:use-module (gcrypt hash)
26 #:use-module ((guix build utils) #:select (mkdir-p))
27 #:use-module (guix store)
28 #:use-module (guix utils)
29 #:use-module (guix records)
30 #:use-module (guix gexp)
31 #:use-module (rnrs bytevectors)
32 #:use-module (ice-9 match)
33 #:use-module (srfi srfi-1)
34 #:use-module (srfi srfi-11)
35 #:use-module (srfi srfi-34)
36 #:use-module (srfi srfi-35)
37 #:export (%repository-cache-directory
38 update-cached-checkout
39 latest-repository-commit
46 (define %repository-cache-directory
47 (make-parameter (string-append (cache-directory #:ensure? #f)
50 (define-syntax-rule (with-libgit2 thunk ...)
52 ;; XXX: The right thing to do would be to call (libgit2-shutdown) here,
53 ;; but pointer finalizers used in guile-git may be called after shutdown,
54 ;; resulting in a segfault. Hence, let's skip shutdown call for now.
58 (define* (url-cache-directory url
59 #:optional (cache-directory
60 (%repository-cache-directory)))
61 "Return the directory associated to URL in %repository-cache-directory."
64 (bytevector->base32-string (sha256 (string->utf8 url)))))
66 (define (clone* url directory)
67 "Clone git repository at URL into DIRECTORY. Upon failure,
68 make sure no empty directory is left behind."
69 (with-throw-handler #t
73 ;; Note: Explicitly pass options to work around the invalid default
74 ;; value in Guile-Git: <https://bugs.gnu.org/29238>.
75 (if (module-defined? (resolve-interface '(git))
77 (clone url directory (clone-init-options))
78 (clone url directory)))
80 (false-if-exception (rmdir directory)))))
82 (define (url+commit->name url sha1)
83 "Return the string \"<REPO-NAME>-<SHA1:7>\" where REPO-NAME is the name of
84 the git repository, extracted from URL and SHA1:7 the seven first digits
87 (string-replace-substring
88 (last (string-split url #\/)) ".git" "")
89 "-" (string-take sha1 7)))
91 (define (switch-to-ref repository ref)
92 "Switch to REPOSITORY's branch, commit or tag specified by REF. Return the
93 OID (roughly the commit hash) corresponding to REF."
97 (let ((oid (reference-target
98 (branch-lookup repository branch BRANCH-REMOTE))))
99 (object-lookup repository oid)))
101 (let ((len (string-length commit)))
102 ;; 'object-lookup-prefix' appeared in Guile-Git in Mar. 2018, so we
103 ;; can't be sure it's available. Furthermore, 'string->oid' used to
104 ;; read out-of-bounds when passed a string shorter than 40 chars,
105 ;; which is why we delay calls to it below.
107 (if (module-defined? (resolve-interface '(git object))
108 'object-lookup-prefix)
109 (object-lookup-prefix repository (string->oid commit) len)
112 (message "long Git object ID is required")))))
113 (object-lookup repository (string->oid commit)))))
115 (let ((oid (reference-name->oid repository
116 (string-append "refs/tags/" tag))))
117 (object-lookup repository oid)))))
119 (reset repository obj RESET_HARD)
122 (define* (update-cached-checkout url
124 (ref '(branch . "master"))
127 url (%repository-cache-directory))))
128 "Update the cached checkout of URL to REF in CACHE-DIRECTORY. Return two
129 values: the cache directory name, and the SHA1 commit (a string) corresponding
132 REF is pair whose key is [branch | commit | tag] and value the associated
133 data, respectively [<branch name> | <sha1> | <tag name>]."
134 (define canonical-ref
135 ;; We used to require callers to specify "origin/" for each branch, which
136 ;; made little sense since the cache should be transparent to them. So
137 ;; here we append "origin/" if it's missing and otherwise keep it.
140 `(branch . ,(if (string-prefix? "origin/" branch)
142 (string-append "origin/" branch))))
146 (let* ((cache-exists? (openable-repository? cache-directory))
147 (repository (if cache-exists?
148 (repository-open cache-directory)
149 (clone* url cache-directory))))
150 ;; Only fetch remote if it has not been cloned just before.
152 (remote-fetch (remote-lookup repository "origin")))
153 (let ((oid (switch-to-ref repository canonical-ref)))
155 ;; Reclaim file descriptors and memory mappings associated with
156 ;; REPOSITORY as soon as possible.
157 (when (module-defined? (resolve-interface '(git repository))
159 (repository-close! repository))
161 (values cache-directory (oid->string oid))))))
163 (define* (latest-repository-commit store url
165 (log-port (%make-void-port "w"))
167 (%repository-cache-directory))
168 (ref '(branch . "master")))
169 "Return two values: the content of the git repository at URL copied into a
170 store directory and the sha1 of the top level commit in this directory. The
171 reference to be checkout, once the repository is fetched, is specified by REF.
172 REF is pair whose key is [branch | commit | tag] and value the associated
173 data, respectively [<branch name> | <sha1> | <tag name>].
175 Git repositories are kept in the cache directory specified by
176 %repository-cache-directory parameter.
178 Log progress and checkout info to LOG-PORT."
179 (define (dot-git? file stat)
180 (and (string=? (basename file) ".git")
181 (eq? 'directory (stat:type stat))))
183 (format log-port "updating checkout of '~a'...~%" url)
186 (update-cached-checkout url
189 (url-cache-directory url cache-directory)))
191 (url+commit->name url commit)))
192 (format log-port "retrieved commit ~a~%" commit)
193 (values (add-to-store store name #t "sha256" checkout
194 #:select? (negate dot-git?))
202 ;; Representation of the "latest" checkout of a branch or a specific commit.
203 (define-record-type* <git-checkout>
204 git-checkout make-git-checkout
206 (url git-checkout-url)
207 (branch git-checkout-branch (default "master"))
208 (commit git-checkout-commit (default #f)))
210 (define* (latest-repository-commit* url #:key ref log-port)
211 ;; Monadic variant of 'latest-repository-commit'.
213 ;; The caller--e.g., (guix scripts build)--may not handle 'git-error' so
214 ;; translate it into '&message' conditions that we know will be properly
218 (values (latest-repository-commit store url
219 #:ref ref #:log-port log-port)
221 (lambda (key error . _)
227 (format #f (G_ "cannot fetch commit ~a from ~a: ~a")
228 commit url (git-error-message error)))
230 (format #f (G_ "cannot fetch branch '~a' from ~a: ~a")
231 branch url (git-error-message error)))
233 (format #f (G_ "Git failure while fetching ~a: ~a")
234 url (git-error-message error))))))))))))
236 (define-gexp-compiler (git-checkout-compiler (checkout <git-checkout>)
238 ;; "Compile" CHECKOUT by updating the local checkout and adding it to the
241 (($ <git-checkout> url branch commit)
242 (latest-repository-commit* url
246 #:log-port (current-error-port)))))