1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2017, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
3 ;;; Copyright © 2018, 2019, 2020 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 (guix sets)
32 #:use-module ((guix diagnostics) #:select (leave))
33 #:use-module (rnrs bytevectors)
34 #:use-module (ice-9 match)
35 #:use-module (srfi srfi-1)
36 #:use-module (srfi srfi-11)
37 #:use-module (srfi srfi-34)
38 #:use-module (srfi srfi-35)
39 #:export (%repository-cache-directory
40 honor-system-x509-certificates!
43 with-git-error-handling
44 false-if-git-not-found
45 update-cached-checkout
47 latest-repository-commit
56 git-checkout-recursive?))
58 (define %repository-cache-directory
59 (make-parameter (string-append (cache-directory #:ensure? #f)
62 (define (honor-system-x509-certificates!)
63 "Use the system's X.509 certificates for Git checkouts over HTTPS. Honor
64 the 'SSL_CERT_FILE' and 'SSL_CERT_DIR' environment variables."
65 ;; On distros such as CentOS 7, /etc/ssl/certs contains only a couple of
66 ;; files (instead of all the certificates) among which "ca-bundle.crt". On
67 ;; other distros /etc/ssl/certs usually contains the whole set of
68 ;; certificates along with "ca-certificates.crt". Try to choose the right
70 (let ((file (letrec-syntax ((choose
74 (if (and f (file-exists? f))
79 (choose (getenv "SSL_CERT_FILE")
80 "/etc/ssl/certs/ca-certificates.crt"
81 "/etc/ssl/certs/ca-bundle.crt")))
82 (directory (or (getenv "SSL_CERT_DIR") "/etc/ssl/certs")))
84 (and=> (stat directory #f)
86 (> (stat:nlink st) 2))))
88 (set-tls-certificate-locations! directory file)
91 (define %certificates-initialized?
92 ;; Whether 'honor-system-x509-certificates!' has already been called.
95 (define-syntax-rule (with-libgit2 thunk ...)
97 ;; XXX: The right thing to do would be to call (libgit2-shutdown) here,
98 ;; but pointer finalizers used in guile-git may be called after shutdown,
99 ;; resulting in a segfault. Hence, let's skip shutdown call for now.
101 (unless %certificates-initialized?
102 (honor-system-x509-certificates!)
103 (set! %certificates-initialized? #t))
106 (define* (url-cache-directory url
107 #:optional (cache-directory
108 (%repository-cache-directory))
110 "Return the directory associated to URL in %repository-cache-directory."
113 (bytevector->base32-string
114 (sha256 (string->utf8 (if recursive?
115 (string-append "R:" url)
118 ;; Authentication appeared in Guile-Git 0.3.0, check if it is available.
119 (define auth-supported?
120 (false-if-exception (resolve-interface '(git auth))))
122 (define (clone* url directory)
123 "Clone git repository at URL into DIRECTORY. Upon failure,
124 make sure no empty directory is left behind."
125 (with-throw-handler #t
129 ;; Note: Explicitly pass options to work around the invalid default
130 ;; value in Guile-Git: <https://bugs.gnu.org/29238>.
131 (if (module-defined? (resolve-interface '(git))
133 (let ((auth-method (and auth-supported?
134 (%make-auth-ssh-agent))))
138 #:fetch-options (make-fetch-options auth-method))
139 (clone-init-options))))
140 (clone url directory)))
142 (false-if-exception (rmdir directory)))))
144 (define (url+commit->name url sha1)
145 "Return the string \"<REPO-NAME>-<SHA1:7>\" where REPO-NAME is the name of
146 the git repository, extracted from URL and SHA1:7 the seven first digits
149 (string-replace-substring
150 (last (string-split url #\/)) ".git" "")
151 "-" (string-take sha1 7)))
153 (define (resolve-reference repository ref)
154 "Resolve the branch, commit or tag specified by REF, and return the
155 corresponding Git object."
156 (let resolve ((ref ref))
159 (let ((oid (reference-target
160 (branch-lookup repository branch BRANCH-REMOTE))))
161 (object-lookup repository oid)))
163 (let ((len (string-length commit)))
164 ;; 'object-lookup-prefix' appeared in Guile-Git in Mar. 2018, so we
165 ;; can't be sure it's available. Furthermore, 'string->oid' used to
166 ;; read out-of-bounds when passed a string shorter than 40 chars,
167 ;; which is why we delay calls to it below.
169 (if (module-defined? (resolve-interface '(git object))
170 'object-lookup-prefix)
171 (object-lookup-prefix repository (string->oid commit) len)
174 (message "long Git object ID is required")))))
175 (object-lookup repository (string->oid commit)))))
176 (('tag-or-commit . str)
177 (if (or (> (string-length str) 40)
178 (not (string-every char-set:hex-digit str)))
179 (resolve `(tag . ,str)) ;definitely a tag
182 (resolve `(tag . ,str)))
184 ;; There's no such tag, so it must be a commit ID.
185 (resolve `(commit . ,str))))))
187 (let ((oid (reference-name->oid repository
188 (string-append "refs/tags/" tag))))
189 ;; OID may point to a "tag" object, but it can also point directly
190 ;; to a "commit" object, as surprising as it may seem. Return that
191 ;; object, whatever that is.
192 (object-lookup repository oid))))))
194 (define (switch-to-ref repository ref)
195 "Switch to REPOSITORY's branch, commit or tag specified by REF. Return the
196 OID (roughly the commit hash) corresponding to REF."
198 (resolve-reference repository ref))
200 (reset repository obj RESET_HARD)
203 (define (call-with-repository directory proc)
204 (let ((repository #f))
207 (set! repository (repository-open directory)))
211 (repository-close! repository)))))
213 (define-syntax-rule (with-repository directory repository exp ...)
214 "Open the repository at DIRECTORY and bind REPOSITORY to it within the
215 dynamic extent of EXP."
216 (call-with-repository directory
217 (lambda (repository) exp ...)))
219 (define (report-git-error error)
220 "Report the given Guile-Git error."
221 ;; Prior to Guile-Git commit b6b2760c2fd6dfaa5c0fedb43eeaff06166b3134,
222 ;; errors would be represented by integers.
224 ((? integer? error) ;old Guile-Git
225 (leave (G_ "Git error ~a~%") error))
226 ((? git-error? error) ;new Guile-Git
227 (leave (G_ "Git error: ~a~%") (git-error-message error)))))
229 (define-syntax-rule (with-git-error-handling body ...)
234 (report-git-error err))))
236 (define (load-git-submodules)
237 "Attempt to load (git submodules), which was missing until Guile-Git 0.2.0.
238 Return true on success, false on failure."
239 (match (false-if-exception (resolve-interface '(git submodule)))
241 (set! load-git-submodules (const #f))
244 (module-use! (resolve-module '(guix git)) iface)
245 (set! load-git-submodules (const #t))
248 (define* (update-submodules repository
249 #:key (log-port (current-error-port)))
250 "Update the submodules of REPOSITORY, a Git repository object."
251 ;; Guile-Git < 0.2.0 did not have (git submodule).
252 (if (load-git-submodules)
253 (for-each (lambda (name)
254 (let ((submodule (submodule-lookup repository name)))
255 (format log-port (G_ "updating submodule '~a'...~%")
257 (submodule-update submodule)
259 ;; Recurse in SUBMODULE.
260 (let ((directory (string-append
261 (repository-working-directory repository)
262 "/" (submodule-path submodule))))
263 (with-repository directory repository
264 (update-submodules repository
265 #:log-port log-port)))))
266 (repository-submodules repository))
267 (format (current-error-port)
268 (G_ "Support for submodules is missing; \
269 please upgrade Guile-Git.~%"))))
271 (define-syntax-rule (false-if-git-not-found exp)
272 "Evaluate EXP, returning #false if a GIT_ENOTFOUND error is raised."
276 (lambda (key error . rest)
277 (if (= GIT_ENOTFOUND (git-error-code error))
279 (apply throw key error rest)))))
281 (define (reference-available? repository ref)
282 "Return true if REF, a reference such as '(commit . \"cabba9e\"), is
283 definitely available in REPOSITORY, false otherwise."
286 (false-if-git-not-found
287 (->bool (commit-lookup repository (string->oid commit)))))
291 (define* (update-cached-checkout url
293 (ref '(branch . "master"))
297 (log-port (%make-void-port "w"))
300 url (%repository-cache-directory)
301 #:recursive? recursive?)))
302 "Update the cached checkout of URL to REF in CACHE-DIRECTORY. Return three
303 values: the cache directory name, and the SHA1 commit (a string) corresponding
304 to REF, and the relation of the new commit relative to STARTING-COMMIT (if
305 provided) as returned by 'commit-relation'.
307 REF is pair whose key is [branch | commit | tag | tag-or-commit ] and value
308 the associated data: [<branch name> | <sha1> | <tag name> | <string>].
310 When RECURSIVE? is true, check out submodules as well, if any.
312 When CHECK-OUT? is true, reset the cached working tree to REF; otherwise leave
314 (define canonical-ref
315 ;; We used to require callers to specify "origin/" for each branch, which
316 ;; made little sense since the cache should be transparent to them. So
317 ;; here we append "origin/" if it's missing and otherwise keep it.
320 `(branch . ,(if (string-prefix? "origin/" branch)
322 (string-append "origin/" branch))))
326 (let* ((cache-exists? (openable-repository? cache-directory))
327 (repository (if cache-exists?
328 (repository-open cache-directory)
329 (clone* url cache-directory))))
330 ;; Only fetch remote if it has not been cloned just before.
331 (when (and cache-exists?
332 (not (reference-available? repository ref)))
334 (let ((auth-method (and auth-supported?
335 (%make-auth-ssh-agent))))
336 (remote-fetch (remote-lookup repository "origin")
337 #:fetch-options (make-fetch-options auth-method)))
338 (remote-fetch (remote-lookup repository "origin"))))
340 (update-submodules repository #:log-port log-port))
342 ;; Note: call 'commit-relation' from here because it's more efficient
343 ;; than letting users re-open the checkout later on.
344 (let* ((oid (if check-out?
345 (switch-to-ref repository canonical-ref)
347 (resolve-reference repository canonical-ref))))
348 (new (and starting-commit
349 (commit-lookup repository oid)))
350 (old (and starting-commit
351 (false-if-git-not-found
352 (commit-lookup repository
353 (string->oid starting-commit)))))
354 (relation (and starting-commit
356 (commit-relation old new)
359 ;; Reclaim file descriptors and memory mappings associated with
360 ;; REPOSITORY as soon as possible.
361 (when (module-defined? (resolve-interface '(git repository))
363 (repository-close! repository))
365 (values cache-directory (oid->string oid) relation)))))
367 (define* (latest-repository-commit store url
370 (log-port (%make-void-port "w"))
372 (%repository-cache-directory))
373 (ref '(branch . "master")))
374 "Return two values: the content of the git repository at URL copied into a
375 store directory and the sha1 of the top level commit in this directory. The
376 reference to be checkout, once the repository is fetched, is specified by REF.
377 REF is pair whose key is [branch | commit | tag] and value the associated
378 data, respectively [<branch name> | <sha1> | <tag name>].
380 When RECURSIVE? is true, check out submodules as well, if any.
382 Git repositories are kept in the cache directory specified by
383 %repository-cache-directory parameter.
385 Log progress and checkout info to LOG-PORT."
386 (define (dot-git? file stat)
387 (and (string=? (basename file) ".git")
388 (or (eq? 'directory (stat:type stat))
390 ;; Submodule checkouts end up with a '.git' regular file that
391 ;; contains metadata about where their actual '.git' directory
394 (eq? 'regular (stat:type stat))))))
396 (format log-port "updating checkout of '~a'...~%" url)
398 (((checkout commit _)
399 (update-cached-checkout url
400 #:recursive? recursive?
403 (url-cache-directory url cache-directory
406 #:log-port log-port))
408 (url+commit->name url commit)))
409 (format log-port "retrieved commit ~a~%" commit)
410 (values (add-to-store store name #t "sha256" checkout
411 #:select? (negate dot-git?))
414 (define (print-git-error port key args default-printer)
416 (((? git-error? error) . _)
417 (format port (G_ "Git error: ~a~%")
418 (git-error-message error)))))
420 (set-exception-printer! 'git-error print-git-error)
424 ;;; Commit difference.
427 (define* (commit-closure commit #:optional (visited (setq)))
428 "Return the closure of COMMIT as a set. Skip commits contained in VISITED,
429 a set, and adjoin VISITED to the result."
430 (let loop ((commits (list commit))
436 (if (set-contains? visited head)
438 (loop (append (commit-parents head) tail)
439 (set-insert head visited)))))))
441 (define* (commit-difference new old #:optional (excluded '()))
442 "Return the list of commits between NEW and OLD, where OLD is assumed to be
443 an ancestor of NEW. Exclude all the commits listed in EXCLUDED along with
446 Essentially, this computes the set difference between the closure of NEW and
448 (let loop ((commits (list new))
450 (visited (fold commit-closure
452 (cons old excluded))))
457 (if (set-contains? visited head)
458 (loop tail result visited)
459 (loop (append (commit-parents head) tail)
461 (set-insert head visited)))))))
463 (define (commit-relation old new)
464 "Return a symbol denoting the relation between OLD and NEW, two commit
465 objects: 'ancestor (meaning that OLD is an ancestor of NEW), 'descendant, or
466 'unrelated, or 'self (OLD and NEW are the same commit)."
469 (let ((newest (commit-closure new)))
470 (if (set-contains? newest old)
472 (let* ((seen (list->setq (commit-parents new)))
473 (oldest (commit-closure old seen)))
474 (if (set-contains? oldest new)
483 ;; Representation of the "latest" checkout of a branch or a specific commit.
484 (define-record-type* <git-checkout>
485 git-checkout make-git-checkout
487 (url git-checkout-url)
488 (branch git-checkout-branch (default "master"))
489 (commit git-checkout-commit (default #f)) ;#f | tag | commit
490 (recursive? git-checkout-recursive? (default #f)))
492 (define* (latest-repository-commit* url #:key ref recursive? log-port)
493 ;; Monadic variant of 'latest-repository-commit'.
495 ;; The caller--e.g., (guix scripts build)--may not handle 'git-error' so
496 ;; translate it into '&message' conditions that we know will be properly
500 (values (latest-repository-commit store url
502 #:recursive? recursive?
505 (lambda (key error . _)
511 (format #f (G_ "cannot fetch commit ~a from ~a: ~a")
512 commit url (git-error-message error)))
514 (format #f (G_ "cannot fetch branch '~a' from ~a: ~a")
515 branch url (git-error-message error)))
517 (format #f (G_ "Git failure while fetching ~a: ~a")
518 url (git-error-message error))))))))))))
520 (define-gexp-compiler (git-checkout-compiler (checkout <git-checkout>)
522 ;; "Compile" CHECKOUT by updating the local checkout and adding it to the
525 (($ <git-checkout> url branch commit recursive?)
526 (latest-repository-commit* url
528 `(tag-or-commit . ,commit)
530 #:recursive? recursive?
531 #:log-port (current-error-port)))))
534 ;; eval: (put 'with-repository 'scheme-indent-function 2)