2017-05-05 05:01:50 -04:00
|
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
2020-02-03 12:05:02 -05:00
|
|
|
|
;;; Copyright © 2017, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
|
2020-12-19 16:59:01 -05:00
|
|
|
|
;;; Copyright © 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
|
2021-04-09 23:50:13 -04:00
|
|
|
|
;;; Copyright © 2021 Kyle Meyer <kyle@kyleam.com>
|
2021-09-05 18:21:51 -04:00
|
|
|
|
;;; Copyright © 2021 Marius Bakke <marius@gnu.org>
|
2017-05-05 05:01:50 -04:00
|
|
|
|
;;;
|
|
|
|
|
;;; This file is part of GNU Guix.
|
|
|
|
|
;;;
|
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
|
;;; your option) any later version.
|
|
|
|
|
;;;
|
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
|
;;;
|
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
(define-module (guix git)
|
|
|
|
|
#:use-module (git)
|
|
|
|
|
#:use-module (git object)
|
2020-10-12 15:47:14 -04:00
|
|
|
|
#:use-module (git submodule)
|
2018-11-30 10:41:22 -05:00
|
|
|
|
#:use-module (guix i18n)
|
2017-05-05 05:01:50 -04:00
|
|
|
|
#:use-module (guix base32)
|
2020-12-19 16:59:01 -05:00
|
|
|
|
#:use-module (guix cache)
|
Switch to Guile-Gcrypt.
This removes (guix hash) and (guix pk-crypto), which now live as part of
Guile-Gcrypt (version 0.1.0.)
* guix/gcrypt.scm, guix/hash.scm, guix/pk-crypto.scm,
tests/hash.scm, tests/pk-crypto.scm: Remove.
* configure.ac: Test for Guile-Gcrypt. Remove LIBGCRYPT and
LIBGCRYPT_LIBDIR assignments.
* m4/guix.m4 (GUIX_ASSERT_LIBGCRYPT_USABLE): Remove.
* README: Add Guile-Gcrypt to the dependencies; move libgcrypt as
"required unless --disable-daemon".
* doc/guix.texi (Requirements): Likewise.
* gnu/packages/bash.scm, guix/derivations.scm, guix/docker.scm,
guix/git.scm, guix/http-client.scm, guix/import/cpan.scm,
guix/import/cran.scm, guix/import/crate.scm, guix/import/elpa.scm,
guix/import/gnu.scm, guix/import/hackage.scm,
guix/import/texlive.scm, guix/import/utils.scm, guix/nar.scm,
guix/pki.scm, guix/scripts/archive.scm,
guix/scripts/authenticate.scm, guix/scripts/download.scm,
guix/scripts/hash.scm, guix/scripts/pack.scm,
guix/scripts/publish.scm, guix/scripts/refresh.scm,
guix/scripts/substitute.scm, guix/store.scm,
guix/store/deduplication.scm, guix/tests.scm, tests/base32.scm,
tests/builders.scm, tests/challenge.scm, tests/cpan.scm,
tests/crate.scm, tests/derivations.scm, tests/gem.scm,
tests/nar.scm, tests/opam.scm, tests/pki.scm,
tests/publish.scm, tests/pypi.scm, tests/store-deduplication.scm,
tests/store.scm, tests/substitute.scm: Adjust imports.
* gnu/system/vm.scm: Likewise.
(guile-sqlite3&co): Rename to...
(gcrypt-sqlite3&co): ... this. Add GUILE-GCRYPT.
(expression->derivation-in-linux-vm)[config]: Remove.
(iso9660-image)[config]: Remove.
(qemu-image)[config]: Remove.
(system-docker-image)[config]: Remove.
* guix/scripts/pack.scm: Adjust imports.
(guile-sqlite3&co): Rename to...
(gcrypt-sqlite3&co): ... this. Add GUILE-GCRYPT.
(self-contained-tarball)[build]: Call 'make-config.scm' without
#:libgcrypt argument.
(squashfs-image)[libgcrypt]: Remove.
[build]: Call 'make-config.scm' without #:libgcrypt.
(docker-image)[config, json]: Remove.
[build]: Add GUILE-GCRYPT to the extensions Remove (guix config) from
the imported modules.
* guix/self.scm (specification->package): Remove "libgcrypt", add
"guile-gcrypt".
(compiled-guix): Remove #:libgcrypt.
[guile-gcrypt]: New variable.
[dependencies]: Add it.
[*core-modules*]: Remove #:libgcrypt from 'make-config.scm' call.
Add #:extensions.
[*config*]: Remove #:libgcrypt from 'make-config.scm' call.
(%dependency-variables): Remove %libgcrypt.
(make-config.scm): Remove #:libgcrypt.
* build-aux/build-self.scm (guile-gcrypt): New variable.
(make-config.scm): Remove #:libgcrypt.
(build-program)[fake-gcrypt-hash]: New variable.
Add (gcrypt hash) to the imported modules. Adjust load path
assignments.
* gnu/packages/package-management.scm (guix)[propagated-inputs]: Add
GUILE-GCRYPT.
[arguments]: In 'wrap-program' phase, add GUILE-GCRYPT to the search
path.
2018-08-31 11:07:07 -04:00
|
|
|
|
#:use-module (gcrypt hash)
|
2020-12-19 16:59:01 -05:00
|
|
|
|
#:use-module ((guix build utils)
|
|
|
|
|
#:select (mkdir-p delete-file-recursively))
|
2017-05-05 05:01:50 -04:00
|
|
|
|
#:use-module (guix store)
|
|
|
|
|
#:use-module (guix utils)
|
2018-11-27 08:50:48 -05:00
|
|
|
|
#:use-module (guix records)
|
|
|
|
|
#:use-module (guix gexp)
|
2019-09-14 11:54:06 -04:00
|
|
|
|
#:use-module (guix sets)
|
2020-07-06 04:10:01 -04:00
|
|
|
|
#:use-module ((guix diagnostics) #:select (leave))
|
2020-10-12 16:33:05 -04:00
|
|
|
|
#:use-module (guix progress)
|
2017-05-05 05:01:50 -04:00
|
|
|
|
#:use-module (rnrs bytevectors)
|
2020-10-12 16:33:05 -04:00
|
|
|
|
#:use-module (ice-9 format)
|
2017-05-05 05:01:50 -04:00
|
|
|
|
#:use-module (ice-9 match)
|
2020-12-19 16:59:01 -05:00
|
|
|
|
#:use-module (ice-9 ftw)
|
2017-05-05 05:01:50 -04:00
|
|
|
|
#:use-module (srfi srfi-1)
|
2018-04-02 17:11:07 -04:00
|
|
|
|
#:use-module (srfi srfi-11)
|
2018-03-17 18:59:18 -04:00
|
|
|
|
#:use-module (srfi srfi-34)
|
|
|
|
|
#:use-module (srfi srfi-35)
|
2017-05-05 05:01:50 -04:00
|
|
|
|
#:export (%repository-cache-directory
|
2019-02-08 04:31:23 -05:00
|
|
|
|
honor-system-x509-certificates!
|
|
|
|
|
|
2020-09-06 09:40:27 -04:00
|
|
|
|
url-cache-directory
|
2019-09-14 11:54:06 -04:00
|
|
|
|
with-repository
|
2020-07-06 04:10:01 -04:00
|
|
|
|
with-git-error-handling
|
2020-06-07 17:06:41 -04:00
|
|
|
|
false-if-git-not-found
|
2018-04-02 17:11:07 -04:00
|
|
|
|
update-cached-checkout
|
2020-05-06 16:45:31 -04:00
|
|
|
|
url+commit->name
|
2018-11-27 08:50:48 -05:00
|
|
|
|
latest-repository-commit
|
2019-09-14 11:54:06 -04:00
|
|
|
|
commit-difference
|
2020-05-20 07:01:26 -04:00
|
|
|
|
commit-relation
|
2018-11-27 08:50:48 -05:00
|
|
|
|
|
|
|
|
|
git-checkout
|
|
|
|
|
git-checkout?
|
|
|
|
|
git-checkout-url
|
2020-02-07 09:24:28 -05:00
|
|
|
|
git-checkout-branch
|
|
|
|
|
git-checkout-commit
|
|
|
|
|
git-checkout-recursive?))
|
2017-05-05 05:01:50 -04:00
|
|
|
|
|
|
|
|
|
(define %repository-cache-directory
|
2018-09-17 16:02:31 -04:00
|
|
|
|
(make-parameter (string-append (cache-directory #:ensure? #f)
|
|
|
|
|
"/checkouts")))
|
2017-05-05 05:01:50 -04:00
|
|
|
|
|
2019-02-08 04:31:23 -05:00
|
|
|
|
(define (honor-system-x509-certificates!)
|
|
|
|
|
"Use the system's X.509 certificates for Git checkouts over HTTPS. Honor
|
|
|
|
|
the 'SSL_CERT_FILE' and 'SSL_CERT_DIR' environment variables."
|
|
|
|
|
;; On distros such as CentOS 7, /etc/ssl/certs contains only a couple of
|
|
|
|
|
;; files (instead of all the certificates) among which "ca-bundle.crt". On
|
|
|
|
|
;; other distros /etc/ssl/certs usually contains the whole set of
|
|
|
|
|
;; certificates along with "ca-certificates.crt". Try to choose the right
|
|
|
|
|
;; one.
|
|
|
|
|
(let ((file (letrec-syntax ((choose
|
|
|
|
|
(syntax-rules ()
|
|
|
|
|
((_ file rest ...)
|
|
|
|
|
(let ((f file))
|
|
|
|
|
(if (and f (file-exists? f))
|
|
|
|
|
f
|
|
|
|
|
(choose rest ...))))
|
|
|
|
|
((_)
|
|
|
|
|
#f))))
|
|
|
|
|
(choose (getenv "SSL_CERT_FILE")
|
|
|
|
|
"/etc/ssl/certs/ca-certificates.crt"
|
|
|
|
|
"/etc/ssl/certs/ca-bundle.crt")))
|
|
|
|
|
(directory (or (getenv "SSL_CERT_DIR") "/etc/ssl/certs")))
|
|
|
|
|
(and (or file
|
|
|
|
|
(and=> (stat directory #f)
|
|
|
|
|
(lambda (st)
|
|
|
|
|
(> (stat:nlink st) 2))))
|
|
|
|
|
(begin
|
|
|
|
|
(set-tls-certificate-locations! directory file)
|
|
|
|
|
#t))))
|
|
|
|
|
|
|
|
|
|
(define %certificates-initialized?
|
|
|
|
|
;; Whether 'honor-system-x509-certificates!' has already been called.
|
|
|
|
|
#f)
|
|
|
|
|
|
2017-05-05 05:01:50 -04:00
|
|
|
|
(define-syntax-rule (with-libgit2 thunk ...)
|
2017-07-01 06:14:05 -04:00
|
|
|
|
(begin
|
|
|
|
|
;; XXX: The right thing to do would be to call (libgit2-shutdown) here,
|
|
|
|
|
;; but pointer finalizers used in guile-git may be called after shutdown,
|
|
|
|
|
;; resulting in a segfault. Hence, let's skip shutdown call for now.
|
|
|
|
|
(libgit2-init!)
|
2019-02-08 04:31:23 -05:00
|
|
|
|
(unless %certificates-initialized?
|
|
|
|
|
(honor-system-x509-certificates!)
|
|
|
|
|
(set! %certificates-initialized? #t))
|
2017-07-01 06:14:05 -04:00
|
|
|
|
thunk ...))
|
2017-05-05 05:01:50 -04:00
|
|
|
|
|
|
|
|
|
(define* (url-cache-directory url
|
|
|
|
|
#:optional (cache-directory
|
2019-02-08 03:12:07 -05:00
|
|
|
|
(%repository-cache-directory))
|
|
|
|
|
#:key recursive?)
|
2017-05-05 05:01:50 -04:00
|
|
|
|
"Return the directory associated to URL in %repository-cache-directory."
|
|
|
|
|
(string-append
|
|
|
|
|
cache-directory "/"
|
2019-02-08 03:12:07 -05:00
|
|
|
|
(bytevector->base32-string
|
|
|
|
|
(sha256 (string->utf8 (if recursive?
|
|
|
|
|
(string-append "R:" url)
|
|
|
|
|
url))))))
|
2017-05-05 05:01:50 -04:00
|
|
|
|
|
2020-10-12 16:33:05 -04:00
|
|
|
|
(define (show-progress progress)
|
|
|
|
|
"Display a progress bar as we fetch Git code. PROGRESS is an
|
|
|
|
|
<indexer-progress> record from (git)."
|
|
|
|
|
(define total
|
|
|
|
|
(indexer-progress-total-objects progress))
|
|
|
|
|
|
|
|
|
|
(define hundredth
|
|
|
|
|
(match (quotient (indexer-progress-total-objects progress) 100)
|
|
|
|
|
(0 1)
|
|
|
|
|
(x x)))
|
|
|
|
|
|
|
|
|
|
(define-values (done label)
|
|
|
|
|
(if (< (indexer-progress-received-objects progress) total)
|
|
|
|
|
(values (indexer-progress-received-objects progress)
|
|
|
|
|
(G_ "receiving objects"))
|
|
|
|
|
(values (indexer-progress-indexed-objects progress)
|
|
|
|
|
(G_ "indexing objects"))))
|
|
|
|
|
|
|
|
|
|
(define %
|
|
|
|
|
(* 100. (/ done total)))
|
|
|
|
|
|
|
|
|
|
(when (and (< % 100) (zero? (modulo done hundredth)))
|
|
|
|
|
(erase-current-line (current-error-port))
|
|
|
|
|
(let ((width (max (- (current-terminal-columns)
|
|
|
|
|
(string-length label) 7)
|
|
|
|
|
3)))
|
|
|
|
|
(format (current-error-port) "~a ~3,d% ~a"
|
|
|
|
|
label (inexact->exact (round %))
|
|
|
|
|
(progress-bar % width)))
|
|
|
|
|
(force-output (current-error-port)))
|
|
|
|
|
|
|
|
|
|
(when (= % 100.)
|
|
|
|
|
;; We're done, erase the line.
|
|
|
|
|
(erase-current-line (current-error-port))
|
|
|
|
|
(force-output (current-error-port)))
|
|
|
|
|
|
|
|
|
|
;; Return true to indicate that we should go on.
|
|
|
|
|
#t)
|
|
|
|
|
|
|
|
|
|
(define (make-default-fetch-options)
|
|
|
|
|
"Return the default fetch options."
|
|
|
|
|
(let ((auth-method (%make-auth-ssh-agent)))
|
2020-10-12 16:42:41 -04:00
|
|
|
|
;; The #:transfer-progress and #:proxy-url options appeared in Guile-Git
|
|
|
|
|
;; 0.4.0. Omit them when using an older version.
|
2020-10-12 16:33:05 -04:00
|
|
|
|
(catch 'wrong-number-of-args
|
|
|
|
|
(lambda ()
|
|
|
|
|
(make-fetch-options auth-method
|
2020-10-12 16:42:41 -04:00
|
|
|
|
;; Guile-Git doesn't distinguish between these.
|
|
|
|
|
#:proxy-url (or (getenv "http_proxy")
|
|
|
|
|
(getenv "https_proxy"))
|
2020-10-12 16:33:05 -04:00
|
|
|
|
#:transfer-progress
|
|
|
|
|
(and (isatty? (current-error-port))
|
|
|
|
|
show-progress)))
|
|
|
|
|
(lambda args
|
|
|
|
|
(make-fetch-options auth-method)))))
|
|
|
|
|
|
2017-05-05 05:01:50 -04:00
|
|
|
|
(define (clone* url directory)
|
|
|
|
|
"Clone git repository at URL into DIRECTORY. Upon failure,
|
|
|
|
|
make sure no empty directory is left behind."
|
|
|
|
|
(with-throw-handler #t
|
|
|
|
|
(lambda ()
|
|
|
|
|
(mkdir-p directory)
|
2017-11-10 06:59:55 -05:00
|
|
|
|
|
2021-04-02 04:30:07 -04:00
|
|
|
|
(clone url directory
|
|
|
|
|
(make-clone-options
|
|
|
|
|
#:fetch-options (make-default-fetch-options))))
|
2017-05-05 05:01:50 -04:00
|
|
|
|
(lambda _
|
|
|
|
|
(false-if-exception (rmdir directory)))))
|
|
|
|
|
|
|
|
|
|
(define (url+commit->name url sha1)
|
|
|
|
|
"Return the string \"<REPO-NAME>-<SHA1:7>\" where REPO-NAME is the name of
|
|
|
|
|
the git repository, extracted from URL and SHA1:7 the seven first digits
|
|
|
|
|
of SHA1 string."
|
|
|
|
|
(string-append
|
|
|
|
|
(string-replace-substring
|
|
|
|
|
(last (string-split url #\/)) ".git" "")
|
|
|
|
|
"-" (string-take sha1 7)))
|
|
|
|
|
|
2020-07-15 17:58:29 -04:00
|
|
|
|
(define (resolve-reference repository ref)
|
|
|
|
|
"Resolve the branch, commit or tag specified by REF, and return the
|
|
|
|
|
corresponding Git object."
|
|
|
|
|
(let resolve ((ref ref))
|
|
|
|
|
(match ref
|
|
|
|
|
(('branch . branch)
|
|
|
|
|
(let ((oid (reference-target
|
|
|
|
|
(branch-lookup repository branch BRANCH-REMOTE))))
|
|
|
|
|
(object-lookup repository oid)))
|
2021-04-09 23:50:13 -04:00
|
|
|
|
(('symref . symref)
|
|
|
|
|
(let ((oid (reference-name->oid repository symref)))
|
|
|
|
|
(object-lookup repository oid)))
|
2020-07-15 17:58:29 -04:00
|
|
|
|
(('commit . commit)
|
|
|
|
|
(let ((len (string-length commit)))
|
|
|
|
|
;; 'object-lookup-prefix' appeared in Guile-Git in Mar. 2018, so we
|
|
|
|
|
;; can't be sure it's available. Furthermore, 'string->oid' used to
|
|
|
|
|
;; read out-of-bounds when passed a string shorter than 40 chars,
|
|
|
|
|
;; which is why we delay calls to it below.
|
|
|
|
|
(if (< len 40)
|
2020-10-12 15:47:14 -04:00
|
|
|
|
(object-lookup-prefix repository (string->oid commit) len)
|
2020-07-15 17:58:29 -04:00
|
|
|
|
(object-lookup repository (string->oid commit)))))
|
|
|
|
|
(('tag-or-commit . str)
|
2021-09-05 18:21:51 -04:00
|
|
|
|
(cond ((and (string-contains str "-g")
|
|
|
|
|
(match (string-split str #\-)
|
|
|
|
|
((version ... revision g+commit)
|
|
|
|
|
(if (and (> (string-length g+commit) 4)
|
|
|
|
|
(string-every char-set:digit revision)
|
|
|
|
|
(string-every char-set:hex-digit
|
|
|
|
|
(string-drop g+commit 1)))
|
|
|
|
|
(string-drop g+commit 1)
|
|
|
|
|
#f))
|
|
|
|
|
(_ #f)))
|
|
|
|
|
;; Looks like a 'git describe' style ID, like
|
|
|
|
|
;; v1.3.0-7-gaa34d4d28d.
|
|
|
|
|
=> (lambda (commit) (resolve `(commit . ,commit))))
|
|
|
|
|
((or (> (string-length str) 40)
|
|
|
|
|
(not (string-every char-set:hex-digit str)))
|
|
|
|
|
(resolve `(tag . ,str))) ;definitely a tag
|
|
|
|
|
(else
|
|
|
|
|
(catch 'git-error
|
|
|
|
|
(lambda ()
|
|
|
|
|
(resolve `(tag . ,str)))
|
|
|
|
|
(lambda _
|
|
|
|
|
;; There's no such tag, so it must be a commit ID.
|
|
|
|
|
(resolve `(commit . ,str)))))))
|
2020-07-15 17:58:29 -04:00
|
|
|
|
(('tag . tag)
|
|
|
|
|
(let ((oid (reference-name->oid repository
|
|
|
|
|
(string-append "refs/tags/" tag))))
|
|
|
|
|
;; OID may point to a "tag" object, but it can also point directly
|
|
|
|
|
;; to a "commit" object, as surprising as it may seem. Return that
|
|
|
|
|
;; object, whatever that is.
|
|
|
|
|
(object-lookup repository oid))))))
|
|
|
|
|
|
2017-05-05 05:01:50 -04:00
|
|
|
|
(define (switch-to-ref repository ref)
|
2018-04-02 17:11:07 -04:00
|
|
|
|
"Switch to REPOSITORY's branch, commit or tag specified by REF. Return the
|
|
|
|
|
OID (roughly the commit hash) corresponding to REF."
|
2018-03-17 18:59:18 -04:00
|
|
|
|
(define obj
|
2020-07-15 17:58:29 -04:00
|
|
|
|
(resolve-reference repository ref))
|
2018-03-17 18:59:18 -04:00
|
|
|
|
|
2018-04-02 17:11:07 -04:00
|
|
|
|
(reset repository obj RESET_HARD)
|
|
|
|
|
(object-id obj))
|
|
|
|
|
|
2019-02-08 03:12:07 -05:00
|
|
|
|
(define (call-with-repository directory proc)
|
|
|
|
|
(let ((repository #f))
|
|
|
|
|
(dynamic-wind
|
|
|
|
|
(lambda ()
|
|
|
|
|
(set! repository (repository-open directory)))
|
|
|
|
|
(lambda ()
|
|
|
|
|
(proc repository))
|
|
|
|
|
(lambda ()
|
|
|
|
|
(repository-close! repository)))))
|
|
|
|
|
|
|
|
|
|
(define-syntax-rule (with-repository directory repository exp ...)
|
|
|
|
|
"Open the repository at DIRECTORY and bind REPOSITORY to it within the
|
|
|
|
|
dynamic extent of EXP."
|
|
|
|
|
(call-with-repository directory
|
|
|
|
|
(lambda (repository) exp ...)))
|
|
|
|
|
|
2020-07-06 04:10:01 -04:00
|
|
|
|
(define (report-git-error error)
|
|
|
|
|
"Report the given Guile-Git error."
|
|
|
|
|
;; Prior to Guile-Git commit b6b2760c2fd6dfaa5c0fedb43eeaff06166b3134,
|
|
|
|
|
;; errors would be represented by integers.
|
|
|
|
|
(match error
|
|
|
|
|
((? integer? error) ;old Guile-Git
|
|
|
|
|
(leave (G_ "Git error ~a~%") error))
|
|
|
|
|
((? git-error? error) ;new Guile-Git
|
|
|
|
|
(leave (G_ "Git error: ~a~%") (git-error-message error)))))
|
|
|
|
|
|
|
|
|
|
(define-syntax-rule (with-git-error-handling body ...)
|
|
|
|
|
(catch 'git-error
|
|
|
|
|
(lambda ()
|
|
|
|
|
body ...)
|
|
|
|
|
(lambda (key err)
|
|
|
|
|
(report-git-error err))))
|
|
|
|
|
|
2019-02-08 03:12:07 -05:00
|
|
|
|
(define* (update-submodules repository
|
2021-04-14 16:50:02 -04:00
|
|
|
|
#:key (log-port (current-error-port))
|
|
|
|
|
(fetch-options #f))
|
2019-02-08 03:12:07 -05:00
|
|
|
|
"Update the submodules of REPOSITORY, a Git repository object."
|
2020-10-12 15:47:14 -04:00
|
|
|
|
(for-each (lambda (name)
|
|
|
|
|
(let ((submodule (submodule-lookup repository name)))
|
|
|
|
|
(format log-port (G_ "updating submodule '~a'...~%")
|
|
|
|
|
name)
|
2021-04-14 16:50:02 -04:00
|
|
|
|
(submodule-update submodule
|
|
|
|
|
#:fetch-options fetch-options)
|
2020-10-12 15:47:14 -04:00
|
|
|
|
|
|
|
|
|
;; Recurse in SUBMODULE.
|
|
|
|
|
(let ((directory (string-append
|
|
|
|
|
(repository-working-directory repository)
|
|
|
|
|
"/" (submodule-path submodule))))
|
|
|
|
|
(with-repository directory repository
|
|
|
|
|
(update-submodules repository
|
2021-04-14 16:50:02 -04:00
|
|
|
|
#:fetch-options fetch-options
|
2020-10-12 15:47:14 -04:00
|
|
|
|
#:log-port log-port)))))
|
|
|
|
|
(repository-submodules repository)))
|
2019-02-08 03:12:07 -05:00
|
|
|
|
|
2020-06-07 16:14:56 -04:00
|
|
|
|
(define-syntax-rule (false-if-git-not-found exp)
|
|
|
|
|
"Evaluate EXP, returning #false if a GIT_ENOTFOUND error is raised."
|
|
|
|
|
(catch 'git-error
|
|
|
|
|
(lambda ()
|
|
|
|
|
exp)
|
|
|
|
|
(lambda (key error . rest)
|
|
|
|
|
(if (= GIT_ENOTFOUND (git-error-code error))
|
|
|
|
|
#f
|
|
|
|
|
(apply throw key error rest)))))
|
|
|
|
|
|
2019-09-14 11:46:34 -04:00
|
|
|
|
(define (reference-available? repository ref)
|
|
|
|
|
"Return true if REF, a reference such as '(commit . \"cabba9e\"), is
|
|
|
|
|
definitely available in REPOSITORY, false otherwise."
|
|
|
|
|
(match ref
|
|
|
|
|
(('commit . commit)
|
2020-12-04 12:43:04 -05:00
|
|
|
|
(let ((len (string-length commit))
|
|
|
|
|
(oid (string->oid commit)))
|
|
|
|
|
(false-if-git-not-found
|
|
|
|
|
(->bool (if (< len 40)
|
|
|
|
|
(object-lookup-prefix repository oid len OBJ-COMMIT)
|
|
|
|
|
(commit-lookup repository oid))))))
|
2019-09-14 11:46:34 -04:00
|
|
|
|
(_
|
|
|
|
|
#f)))
|
|
|
|
|
|
2020-12-19 16:59:01 -05:00
|
|
|
|
(define cached-checkout-expiration
|
|
|
|
|
;; Return the expiration time procedure for a cached checkout.
|
|
|
|
|
;; TODO: Honor $GUIX_GIT_CACHE_EXPIRATION.
|
|
|
|
|
|
|
|
|
|
;; Use the mtime rather than the atime to cope with file systems mounted
|
|
|
|
|
;; with 'noatime'.
|
|
|
|
|
(file-expiration-time (* 90 24 3600) stat:mtime))
|
|
|
|
|
|
|
|
|
|
(define %checkout-cache-cleanup-period
|
|
|
|
|
;; Period for the removal of expired cached checkouts.
|
|
|
|
|
(* 5 24 3600))
|
|
|
|
|
|
|
|
|
|
(define (delete-checkout directory)
|
|
|
|
|
"Delete DIRECTORY recursively, in an atomic fashion."
|
|
|
|
|
(let ((trashed (string-append directory ".trashed")))
|
|
|
|
|
(rename-file directory trashed)
|
|
|
|
|
(delete-file-recursively trashed)))
|
|
|
|
|
|
2018-04-02 17:11:07 -04:00
|
|
|
|
(define* (update-cached-checkout url
|
|
|
|
|
#:key
|
2021-04-09 23:50:13 -04:00
|
|
|
|
(ref '())
|
2019-02-08 03:12:07 -05:00
|
|
|
|
recursive?
|
2020-07-15 17:59:05 -04:00
|
|
|
|
(check-out? #t)
|
2020-05-20 11:57:54 -04:00
|
|
|
|
starting-commit
|
2019-02-08 03:12:07 -05:00
|
|
|
|
(log-port (%make-void-port "w"))
|
2018-04-02 17:11:07 -04:00
|
|
|
|
(cache-directory
|
2018-07-08 06:15:41 -04:00
|
|
|
|
(url-cache-directory
|
2019-02-08 03:12:07 -05:00
|
|
|
|
url (%repository-cache-directory)
|
|
|
|
|
#:recursive? recursive?)))
|
2020-05-20 11:57:54 -04:00
|
|
|
|
"Update the cached checkout of URL to REF in CACHE-DIRECTORY. Return three
|
2018-04-02 17:11:07 -04:00
|
|
|
|
values: the cache directory name, and the SHA1 commit (a string) corresponding
|
2020-05-20 11:57:54 -04:00
|
|
|
|
to REF, and the relation of the new commit relative to STARTING-COMMIT (if
|
|
|
|
|
provided) as returned by 'commit-relation'.
|
2018-04-02 17:11:07 -04:00
|
|
|
|
|
2019-07-26 04:59:24 -04:00
|
|
|
|
REF is pair whose key is [branch | commit | tag | tag-or-commit ] and value
|
|
|
|
|
the associated data: [<branch name> | <sha1> | <tag name> | <string>].
|
2021-04-09 23:50:13 -04:00
|
|
|
|
If REF is the empty list, the remote HEAD is used.
|
2019-02-08 03:12:07 -05:00
|
|
|
|
|
2020-07-15 17:59:05 -04:00
|
|
|
|
When RECURSIVE? is true, check out submodules as well, if any.
|
|
|
|
|
|
|
|
|
|
When CHECK-OUT? is true, reset the cached working tree to REF; otherwise leave
|
|
|
|
|
it unchanged."
|
2020-12-19 16:59:01 -05:00
|
|
|
|
(define (cache-entries directory)
|
|
|
|
|
(filter-map (match-lambda
|
|
|
|
|
((or "." "..")
|
|
|
|
|
#f)
|
|
|
|
|
(file
|
|
|
|
|
(string-append directory "/" file)))
|
|
|
|
|
(or (scandir directory) '())))
|
|
|
|
|
|
2018-09-05 17:31:51 -04:00
|
|
|
|
(define canonical-ref
|
|
|
|
|
;; We used to require callers to specify "origin/" for each branch, which
|
|
|
|
|
;; made little sense since the cache should be transparent to them. So
|
|
|
|
|
;; here we append "origin/" if it's missing and otherwise keep it.
|
|
|
|
|
(match ref
|
2021-04-09 23:50:13 -04:00
|
|
|
|
(() '(symref . "refs/remotes/origin/HEAD"))
|
2018-09-05 17:31:51 -04:00
|
|
|
|
(('branch . branch)
|
|
|
|
|
`(branch . ,(if (string-prefix? "origin/" branch)
|
|
|
|
|
branch
|
|
|
|
|
(string-append "origin/" branch))))
|
|
|
|
|
(_ ref)))
|
|
|
|
|
|
2018-04-02 17:11:07 -04:00
|
|
|
|
(with-libgit2
|
2018-07-08 06:15:41 -04:00
|
|
|
|
(let* ((cache-exists? (openable-repository? cache-directory))
|
2018-04-02 17:11:07 -04:00
|
|
|
|
(repository (if cache-exists?
|
2020-02-06 11:14:39 -05:00
|
|
|
|
(repository-open cache-directory)
|
2018-07-08 06:15:41 -04:00
|
|
|
|
(clone* url cache-directory))))
|
2018-04-02 17:11:07 -04:00
|
|
|
|
;; Only fetch remote if it has not been cloned just before.
|
2019-09-14 11:46:34 -04:00
|
|
|
|
(when (and cache-exists?
|
|
|
|
|
(not (reference-available? repository ref)))
|
2021-04-02 04:30:07 -04:00
|
|
|
|
(remote-fetch (remote-lookup repository "origin")
|
|
|
|
|
#:fetch-options (make-default-fetch-options)))
|
2019-02-08 03:12:07 -05:00
|
|
|
|
(when recursive?
|
2021-04-14 16:50:02 -04:00
|
|
|
|
(update-submodules repository #:log-port log-port
|
|
|
|
|
#:fetch-options (make-default-fetch-options)))
|
2020-05-20 11:57:54 -04:00
|
|
|
|
|
|
|
|
|
;; Note: call 'commit-relation' from here because it's more efficient
|
|
|
|
|
;; than letting users re-open the checkout later on.
|
2020-07-15 17:59:05 -04:00
|
|
|
|
(let* ((oid (if check-out?
|
|
|
|
|
(switch-to-ref repository canonical-ref)
|
|
|
|
|
(object-id
|
|
|
|
|
(resolve-reference repository canonical-ref))))
|
2020-05-20 11:57:54 -04:00
|
|
|
|
(new (and starting-commit
|
|
|
|
|
(commit-lookup repository oid)))
|
|
|
|
|
(old (and starting-commit
|
2020-06-07 16:14:56 -04:00
|
|
|
|
(false-if-git-not-found
|
|
|
|
|
(commit-lookup repository
|
|
|
|
|
(string->oid starting-commit)))))
|
2020-05-20 11:57:54 -04:00
|
|
|
|
(relation (and starting-commit
|
2020-06-07 16:14:56 -04:00
|
|
|
|
(if old
|
|
|
|
|
(commit-relation old new)
|
|
|
|
|
'unrelated))))
|
2018-04-02 17:11:07 -04:00
|
|
|
|
|
|
|
|
|
;; Reclaim file descriptors and memory mappings associated with
|
|
|
|
|
;; REPOSITORY as soon as possible.
|
2020-10-12 15:47:14 -04:00
|
|
|
|
(repository-close! repository)
|
2018-04-02 17:11:07 -04:00
|
|
|
|
|
2021-06-11 17:32:45 -04:00
|
|
|
|
;; Update CACHE-DIRECTORY's mtime to so the cache logic sees it.
|
|
|
|
|
(match (gettimeofday)
|
|
|
|
|
((seconds . microseconds)
|
|
|
|
|
(let ((nanoseconds (* 1000 microseconds)))
|
|
|
|
|
(utime cache-directory
|
|
|
|
|
seconds seconds
|
|
|
|
|
nanoseconds nanoseconds))))
|
|
|
|
|
|
2020-12-19 16:59:01 -05:00
|
|
|
|
;; When CACHE-DIRECTORY is a sub-directory of the default cache
|
|
|
|
|
;; directory, remove expired checkouts that are next to it.
|
|
|
|
|
(let ((parent (dirname cache-directory)))
|
|
|
|
|
(when (string=? parent (%repository-cache-directory))
|
|
|
|
|
(maybe-remove-expired-cache-entries parent cache-entries
|
|
|
|
|
#:entry-expiration
|
|
|
|
|
cached-checkout-expiration
|
|
|
|
|
#:delete-entry delete-checkout
|
|
|
|
|
#:cleanup-period
|
|
|
|
|
%checkout-cache-cleanup-period)))
|
|
|
|
|
|
2020-05-20 11:57:54 -04:00
|
|
|
|
(values cache-directory (oid->string oid) relation)))))
|
2017-05-05 05:01:50 -04:00
|
|
|
|
|
|
|
|
|
(define* (latest-repository-commit store url
|
|
|
|
|
#:key
|
2019-02-08 03:12:07 -05:00
|
|
|
|
recursive?
|
2018-11-27 08:48:32 -05:00
|
|
|
|
(log-port (%make-void-port "w"))
|
2017-05-05 05:01:50 -04:00
|
|
|
|
(cache-directory
|
|
|
|
|
(%repository-cache-directory))
|
2021-04-09 23:50:13 -04:00
|
|
|
|
(ref '()))
|
2017-05-05 05:01:50 -04:00
|
|
|
|
"Return two values: the content of the git repository at URL copied into a
|
|
|
|
|
store directory and the sha1 of the top level commit in this directory. The
|
|
|
|
|
reference to be checkout, once the repository is fetched, is specified by REF.
|
|
|
|
|
REF is pair whose key is [branch | commit | tag] and value the associated
|
2021-04-09 23:50:13 -04:00
|
|
|
|
data, respectively [<branch name> | <sha1> | <tag name>]. If REF is the empty
|
|
|
|
|
list, the remote HEAD is used.
|
2017-05-05 05:01:50 -04:00
|
|
|
|
|
2019-02-08 03:12:07 -05:00
|
|
|
|
When RECURSIVE? is true, check out submodules as well, if any.
|
|
|
|
|
|
2017-05-05 05:01:50 -04:00
|
|
|
|
Git repositories are kept in the cache directory specified by
|
2018-11-27 08:48:32 -05:00
|
|
|
|
%repository-cache-directory parameter.
|
|
|
|
|
|
|
|
|
|
Log progress and checkout info to LOG-PORT."
|
2018-04-02 17:11:07 -04:00
|
|
|
|
(define (dot-git? file stat)
|
|
|
|
|
(and (string=? (basename file) ".git")
|
2019-02-08 03:12:07 -05:00
|
|
|
|
(or (eq? 'directory (stat:type stat))
|
|
|
|
|
|
|
|
|
|
;; Submodule checkouts end up with a '.git' regular file that
|
|
|
|
|
;; contains metadata about where their actual '.git' directory
|
|
|
|
|
;; lives.
|
|
|
|
|
(and recursive?
|
|
|
|
|
(eq? 'regular (stat:type stat))))))
|
2018-03-25 18:12:52 -04:00
|
|
|
|
|
2018-11-27 08:48:32 -05:00
|
|
|
|
(format log-port "updating checkout of '~a'...~%" url)
|
2018-07-08 06:15:41 -04:00
|
|
|
|
(let*-values
|
2020-05-20 11:57:54 -04:00
|
|
|
|
(((checkout commit _)
|
2018-07-08 06:15:41 -04:00
|
|
|
|
(update-cached-checkout url
|
2019-02-08 03:12:07 -05:00
|
|
|
|
#:recursive? recursive?
|
2018-07-08 06:15:41 -04:00
|
|
|
|
#:ref ref
|
|
|
|
|
#:cache-directory
|
2019-02-08 03:12:07 -05:00
|
|
|
|
(url-cache-directory url cache-directory
|
|
|
|
|
#:recursive?
|
|
|
|
|
recursive?)
|
|
|
|
|
#:log-port log-port))
|
2018-07-08 06:15:41 -04:00
|
|
|
|
((name)
|
|
|
|
|
(url+commit->name url commit)))
|
2018-11-27 08:48:32 -05:00
|
|
|
|
(format log-port "retrieved commit ~a~%" commit)
|
2018-04-02 17:11:07 -04:00
|
|
|
|
(values (add-to-store store name #t "sha256" checkout
|
|
|
|
|
#:select? (negate dot-git?))
|
|
|
|
|
commit)))
|
2018-11-27 08:50:48 -05:00
|
|
|
|
|
2019-02-11 16:51:08 -05:00
|
|
|
|
(define (print-git-error port key args default-printer)
|
|
|
|
|
(match args
|
|
|
|
|
(((? git-error? error) . _)
|
|
|
|
|
(format port (G_ "Git error: ~a~%")
|
|
|
|
|
(git-error-message error)))))
|
|
|
|
|
|
|
|
|
|
(set-exception-printer! 'git-error print-git-error)
|
|
|
|
|
|
2019-09-14 11:54:06 -04:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Commit difference.
|
|
|
|
|
;;;
|
|
|
|
|
|
2019-12-27 07:15:00 -05:00
|
|
|
|
(define* (commit-closure commit #:optional (visited (setq)))
|
|
|
|
|
"Return the closure of COMMIT as a set. Skip commits contained in VISITED,
|
|
|
|
|
a set, and adjoin VISITED to the result."
|
2019-09-14 11:54:06 -04:00
|
|
|
|
(let loop ((commits (list commit))
|
2019-12-27 07:15:00 -05:00
|
|
|
|
(visited visited))
|
2019-09-14 11:54:06 -04:00
|
|
|
|
(match commits
|
|
|
|
|
(()
|
|
|
|
|
visited)
|
|
|
|
|
((head . tail)
|
|
|
|
|
(if (set-contains? visited head)
|
|
|
|
|
(loop tail visited)
|
|
|
|
|
(loop (append (commit-parents head) tail)
|
|
|
|
|
(set-insert head visited)))))))
|
|
|
|
|
|
2019-12-27 07:15:00 -05:00
|
|
|
|
(define* (commit-difference new old #:optional (excluded '()))
|
2019-09-14 11:54:06 -04:00
|
|
|
|
"Return the list of commits between NEW and OLD, where OLD is assumed to be
|
2019-12-27 07:15:00 -05:00
|
|
|
|
an ancestor of NEW. Exclude all the commits listed in EXCLUDED along with
|
|
|
|
|
their ancestors.
|
2019-09-14 11:54:06 -04:00
|
|
|
|
|
|
|
|
|
Essentially, this computes the set difference between the closure of NEW and
|
|
|
|
|
that of OLD."
|
|
|
|
|
(let loop ((commits (list new))
|
|
|
|
|
(result '())
|
2020-06-08 15:39:55 -04:00
|
|
|
|
(visited (fold commit-closure
|
|
|
|
|
(setq)
|
|
|
|
|
(cons old excluded))))
|
2019-09-14 11:54:06 -04:00
|
|
|
|
(match commits
|
|
|
|
|
(()
|
|
|
|
|
(reverse result))
|
|
|
|
|
((head . tail)
|
|
|
|
|
(if (set-contains? visited head)
|
|
|
|
|
(loop tail result visited)
|
|
|
|
|
(loop (append (commit-parents head) tail)
|
|
|
|
|
(cons head result)
|
|
|
|
|
(set-insert head visited)))))))
|
|
|
|
|
|
2020-05-20 07:01:26 -04:00
|
|
|
|
(define (commit-relation old new)
|
|
|
|
|
"Return a symbol denoting the relation between OLD and NEW, two commit
|
|
|
|
|
objects: 'ancestor (meaning that OLD is an ancestor of NEW), 'descendant, or
|
|
|
|
|
'unrelated, or 'self (OLD and NEW are the same commit)."
|
|
|
|
|
(if (eq? old new)
|
|
|
|
|
'self
|
|
|
|
|
(let ((newest (commit-closure new)))
|
|
|
|
|
(if (set-contains? newest old)
|
|
|
|
|
'ancestor
|
|
|
|
|
(let* ((seen (list->setq (commit-parents new)))
|
|
|
|
|
(oldest (commit-closure old seen)))
|
|
|
|
|
(if (set-contains? oldest new)
|
|
|
|
|
'descendant
|
|
|
|
|
'unrelated))))))
|
|
|
|
|
|
2018-11-27 08:50:48 -05:00
|
|
|
|
|
|
|
|
|
;;;
|
|
|
|
|
;;; Checkouts.
|
|
|
|
|
;;;
|
|
|
|
|
|
2018-11-30 07:24:48 -05:00
|
|
|
|
;; Representation of the "latest" checkout of a branch or a specific commit.
|
2018-11-27 08:50:48 -05:00
|
|
|
|
(define-record-type* <git-checkout>
|
|
|
|
|
git-checkout make-git-checkout
|
|
|
|
|
git-checkout?
|
|
|
|
|
(url git-checkout-url)
|
2021-04-09 23:50:13 -04:00
|
|
|
|
(branch git-checkout-branch (default #f))
|
2019-07-26 05:09:56 -04:00
|
|
|
|
(commit git-checkout-commit (default #f)) ;#f | tag | commit
|
2019-02-08 03:16:27 -05:00
|
|
|
|
(recursive? git-checkout-recursive? (default #f)))
|
2018-11-27 08:50:48 -05:00
|
|
|
|
|
2019-02-08 03:16:27 -05:00
|
|
|
|
(define* (latest-repository-commit* url #:key ref recursive? log-port)
|
2018-11-30 10:41:22 -05:00
|
|
|
|
;; Monadic variant of 'latest-repository-commit'.
|
|
|
|
|
(lambda (store)
|
|
|
|
|
;; The caller--e.g., (guix scripts build)--may not handle 'git-error' so
|
|
|
|
|
;; translate it into '&message' conditions that we know will be properly
|
|
|
|
|
;; handled.
|
|
|
|
|
(catch 'git-error
|
|
|
|
|
(lambda ()
|
|
|
|
|
(values (latest-repository-commit store url
|
2019-02-08 03:16:27 -05:00
|
|
|
|
#:ref ref
|
|
|
|
|
#:recursive? recursive?
|
|
|
|
|
#:log-port log-port)
|
2018-11-30 10:41:22 -05:00
|
|
|
|
store))
|
|
|
|
|
(lambda (key error . _)
|
|
|
|
|
(raise (condition
|
|
|
|
|
(&message
|
|
|
|
|
(message
|
|
|
|
|
(match ref
|
|
|
|
|
(('commit . commit)
|
|
|
|
|
(format #f (G_ "cannot fetch commit ~a from ~a: ~a")
|
|
|
|
|
commit url (git-error-message error)))
|
|
|
|
|
(('branch . branch)
|
|
|
|
|
(format #f (G_ "cannot fetch branch '~a' from ~a: ~a")
|
|
|
|
|
branch url (git-error-message error)))
|
|
|
|
|
(_
|
|
|
|
|
(format #f (G_ "Git failure while fetching ~a: ~a")
|
|
|
|
|
url (git-error-message error))))))))))))
|
2018-11-27 08:50:48 -05:00
|
|
|
|
|
|
|
|
|
(define-gexp-compiler (git-checkout-compiler (checkout <git-checkout>)
|
|
|
|
|
system target)
|
|
|
|
|
;; "Compile" CHECKOUT by updating the local checkout and adding it to the
|
|
|
|
|
;; store.
|
|
|
|
|
(match checkout
|
2019-02-08 03:16:27 -05:00
|
|
|
|
(($ <git-checkout> url branch commit recursive?)
|
2018-11-27 08:50:48 -05:00
|
|
|
|
(latest-repository-commit* url
|
2021-04-09 23:50:13 -04:00
|
|
|
|
#:ref (cond (commit
|
|
|
|
|
`(tag-or-commit . ,commit))
|
|
|
|
|
(branch
|
|
|
|
|
`(branch . ,branch))
|
|
|
|
|
(else '()))
|
2019-02-08 03:16:27 -05:00
|
|
|
|
#:recursive? recursive?
|
2018-11-27 08:50:48 -05:00
|
|
|
|
#:log-port (current-error-port)))))
|
2019-02-08 03:12:07 -05:00
|
|
|
|
|
|
|
|
|
;; Local Variables:
|
|
|
|
|
;; eval: (put 'with-repository 'scheme-indent-function 2)
|
|
|
|
|
;; End:
|