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 base32)
24 #:use-module (guix hash)
25 #:use-module ((guix build utils) #:select (mkdir-p))
26 #:use-module (guix store)
27 #:use-module (guix utils)
28 #:use-module (rnrs bytevectors)
29 #:use-module (ice-9 match)
30 #:use-module (srfi srfi-1)
31 #:use-module (srfi srfi-11)
32 #:use-module (srfi srfi-34)
33 #:use-module (srfi srfi-35)
34 #:export (%repository-cache-directory
35 update-cached-checkout
36 latest-repository-commit))
38 (define %repository-cache-directory
39 (make-parameter "/var/cache/guix/checkouts"))
41 (define-syntax-rule (with-libgit2 thunk ...)
43 ;; XXX: The right thing to do would be to call (libgit2-shutdown) here,
44 ;; but pointer finalizers used in guile-git may be called after shutdown,
45 ;; resulting in a segfault. Hence, let's skip shutdown call for now.
49 (define* (url-cache-directory url
50 #:optional (cache-directory
51 (%repository-cache-directory)))
52 "Return the directory associated to URL in %repository-cache-directory."
55 (bytevector->base32-string (sha256 (string->utf8 url)))))
57 (define (clone* url directory)
58 "Clone git repository at URL into DIRECTORY. Upon failure,
59 make sure no empty directory is left behind."
60 (with-throw-handler #t
64 ;; Note: Explicitly pass options to work around the invalid default
65 ;; value in Guile-Git: <https://bugs.gnu.org/29238>.
66 (if (module-defined? (resolve-interface '(git))
68 (clone url directory (clone-init-options))
69 (clone url directory)))
71 (false-if-exception (rmdir directory)))))
73 (define (url+commit->name url sha1)
74 "Return the string \"<REPO-NAME>-<SHA1:7>\" where REPO-NAME is the name of
75 the git repository, extracted from URL and SHA1:7 the seven first digits
78 (string-replace-substring
79 (last (string-split url #\/)) ".git" "")
80 "-" (string-take sha1 7)))
82 (define (switch-to-ref repository ref)
83 "Switch to REPOSITORY's branch, commit or tag specified by REF. Return the
84 OID (roughly the commit hash) corresponding to REF."
88 (let ((oid (reference-target
89 (branch-lookup repository branch BRANCH-REMOTE))))
90 (object-lookup repository oid)))
92 (let ((len (string-length commit)))
93 ;; 'object-lookup-prefix' appeared in Guile-Git in Mar. 2018, so we
94 ;; can't be sure it's available. Furthermore, 'string->oid' used to
95 ;; read out-of-bounds when passed a string shorter than 40 chars,
96 ;; which is why we delay calls to it below.
98 (if (module-defined? (resolve-interface '(git object))
99 'object-lookup-prefix)
100 (object-lookup-prefix repository (string->oid commit) len)
103 (message "long Git object ID is required")))))
104 (object-lookup repository (string->oid commit)))))
106 (let ((oid (reference-name->oid repository
107 (string-append "refs/tags/" tag))))
108 (object-lookup repository oid)))))
110 (reset repository obj RESET_HARD)
113 (define* (update-cached-checkout url
115 (ref '(branch . "origin/master"))
117 (%repository-cache-directory)))
118 "Update the cached checkout of URL to REF in CACHE-DIRECTORY. Return two
119 values: the cache directory name, and the SHA1 commit (a string) corresponding
122 REF is pair whose key is [branch | commit | tag] and value the associated
123 data, respectively [<branch name> | <sha1> | <tag name>]."
125 (let* ((cache-dir (url-cache-directory url cache-directory))
126 (cache-exists? (openable-repository? cache-dir))
127 (repository (if cache-exists?
128 (repository-open cache-dir)
129 (clone* url cache-dir))))
130 ;; Only fetch remote if it has not been cloned just before.
132 (remote-fetch (remote-lookup repository "origin")))
133 (let ((oid (switch-to-ref repository ref)))
135 ;; Reclaim file descriptors and memory mappings associated with
136 ;; REPOSITORY as soon as possible.
137 (when (module-defined? (resolve-interface '(git repository))
139 (repository-close! repository))
141 (values cache-dir (oid->string oid))))))
143 (define* (latest-repository-commit store url
146 (%repository-cache-directory))
147 (ref '(branch . "origin/master")))
148 "Return two values: the content of the git repository at URL copied into a
149 store directory and the sha1 of the top level commit in this directory. The
150 reference to be checkout, once the repository is fetched, is specified by REF.
151 REF is pair whose key is [branch | commit | tag] and value the associated
152 data, respectively [<branch name> | <sha1> | <tag name>].
154 Git repositories are kept in the cache directory specified by
155 %repository-cache-directory parameter."
156 (define (dot-git? file stat)
157 (and (string=? (basename file) ".git")
158 (eq? 'directory (stat:type stat))))
160 (let*-values (((checkout commit)
161 (update-cached-checkout url
163 #:cache-directory cache-directory))
165 (url+commit->name url commit)))
166 (values (add-to-store store name #t "sha256" checkout
167 #:select? (negate dot-git?))