gnu: guile-git: Update to 2bb9fbb.
[jackhill/guix/guix.git] / guix / git.scm
CommitLineData
6b7b3ca9
MO
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
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)
20 #:use-module (git)
21 #:use-module (git object)
22 #:use-module (guix base32)
23 #:use-module (guix hash)
0ad5f809 24 #:use-module ((guix build utils) #:select (mkdir-p))
6b7b3ca9
MO
25 #:use-module (guix store)
26 #:use-module (guix utils)
27 #:use-module (rnrs bytevectors)
28 #:use-module (ice-9 match)
29 #:use-module (srfi srfi-1)
30 #:export (%repository-cache-directory
31 latest-repository-commit))
32
33(define %repository-cache-directory
34 (make-parameter "/var/cache/guix/checkouts"))
35
36(define-syntax-rule (with-libgit2 thunk ...)
b02469d2
MO
37 (begin
38 ;; XXX: The right thing to do would be to call (libgit2-shutdown) here,
39 ;; but pointer finalizers used in guile-git may be called after shutdown,
40 ;; resulting in a segfault. Hence, let's skip shutdown call for now.
41 (libgit2-init!)
42 thunk ...))
6b7b3ca9
MO
43
44(define* (url-cache-directory url
45 #:optional (cache-directory
46 (%repository-cache-directory)))
47 "Return the directory associated to URL in %repository-cache-directory."
48 (string-append
49 cache-directory "/"
50 (bytevector->base32-string (sha256 (string->utf8 url)))))
51
52(define (clone* url directory)
53 "Clone git repository at URL into DIRECTORY. Upon failure,
54make sure no empty directory is left behind."
55 (with-throw-handler #t
56 (lambda ()
57 (mkdir-p directory)
195f0d05
LC
58
59 ;; Note: Explicitly pass options to work around the invalid default
60 ;; value in Guile-Git: <https://bugs.gnu.org/29238>.
b1488c76
LC
61 (if (module-defined? (resolve-interface '(git))
62 'clone-init-options)
63 (clone url directory (clone-init-options))
64 (clone url directory)))
6b7b3ca9
MO
65 (lambda _
66 (false-if-exception (rmdir directory)))))
67
68(define (repository->head-sha1 repo)
69 "Return the sha1 of the HEAD commit in REPOSITORY as a string."
70 (let ((oid (reference-target (repository-head repo))))
71 (oid->string (commit-id (commit-lookup repo oid)))))
72
73(define (url+commit->name url sha1)
74 "Return the string \"<REPO-NAME>-<SHA1:7>\" where REPO-NAME is the name of
75the git repository, extracted from URL and SHA1:7 the seven first digits
76of SHA1 string."
77 (string-append
78 (string-replace-substring
79 (last (string-split url #\/)) ".git" "")
80 "-" (string-take sha1 7)))
81
82(define* (copy-to-store store cache-directory #:key url repository)
fe9b3ec3
LC
83 "Copy CACHE-DIRECTORY recursively to STORE. URL and REPOSITORY are used to
84create the store directory name."
85 (define (dot-git? file stat)
86 (and (string=? (basename file) ".git")
87 (eq? 'directory (stat:type stat))))
88
6b7b3ca9
MO
89 (let* ((commit (repository->head-sha1 repository))
90 (name (url+commit->name url commit)))
fe9b3ec3
LC
91 (values (add-to-store store name #t "sha256" cache-directory
92 #:select? (negate dot-git?))
93 commit)))
6b7b3ca9
MO
94
95(define (switch-to-ref repository ref)
96 "Switch to REPOSITORY's branch, commit or tag specified by REF."
97 (let* ((oid (match ref
98 (('branch . branch)
99 (reference-target
100 (branch-lookup repository branch BRANCH-REMOTE)))
101 (('commit . commit)
102 (string->oid commit))
103 (('tag . tag)
104 (reference-name->oid repository
105 (string-append "refs/tags/" tag)))))
106 (obj (object-lookup repository oid)))
107 (reset repository obj RESET_HARD)))
108
109(define* (latest-repository-commit store url
110 #:key
111 (cache-directory
112 (%repository-cache-directory))
113 (ref '(branch . "origin/master")))
114 "Return two values: the content of the git repository at URL copied into a
115store directory and the sha1 of the top level commit in this directory. The
116reference to be checkout, once the repository is fetched, is specified by REF.
117REF is pair whose key is [branch | commit | tag] and value the associated
118data, respectively [<branch name> | <sha1> | <tag name>].
119
120Git repositories are kept in the cache directory specified by
121%repository-cache-directory parameter."
122 (with-libgit2
123 (let* ((cache-dir (url-cache-directory url cache-directory))
124 (cache-exists? (openable-repository? cache-dir))
125 (repository (if cache-exists?
126 (repository-open cache-dir)
127 (clone* url cache-dir))))
128 ;; Only fetch remote if it has not been cloned just before.
129 (when cache-exists?
130 (remote-fetch (remote-lookup repository "origin")))
131 (switch-to-ref repository ref)
132 (copy-to-store store cache-dir
133 #:url url
134 #:repository repository))))