gnu: nvi: Build with support for Unicode text.
[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>.
61 (clone url directory (clone-init-options)))
6b7b3ca9
MO
62 (lambda _
63 (false-if-exception (rmdir directory)))))
64
65(define (repository->head-sha1 repo)
66 "Return the sha1 of the HEAD commit in REPOSITORY as a string."
67 (let ((oid (reference-target (repository-head repo))))
68 (oid->string (commit-id (commit-lookup repo oid)))))
69
70(define (url+commit->name url sha1)
71 "Return the string \"<REPO-NAME>-<SHA1:7>\" where REPO-NAME is the name of
72the git repository, extracted from URL and SHA1:7 the seven first digits
73of SHA1 string."
74 (string-append
75 (string-replace-substring
76 (last (string-split url #\/)) ".git" "")
77 "-" (string-take sha1 7)))
78
79(define* (copy-to-store store cache-directory #:key url repository)
80 "Copy items in cache-directory to store. URL and REPOSITORY are used
81to forge store directory name."
82 (let* ((commit (repository->head-sha1 repository))
83 (name (url+commit->name url commit)))
84 (values (add-to-store store name #t "sha256" cache-directory) commit)))
85
86(define (switch-to-ref repository ref)
87 "Switch to REPOSITORY's branch, commit or tag specified by REF."
88 (let* ((oid (match ref
89 (('branch . branch)
90 (reference-target
91 (branch-lookup repository branch BRANCH-REMOTE)))
92 (('commit . commit)
93 (string->oid commit))
94 (('tag . tag)
95 (reference-name->oid repository
96 (string-append "refs/tags/" tag)))))
97 (obj (object-lookup repository oid)))
98 (reset repository obj RESET_HARD)))
99
100(define* (latest-repository-commit store url
101 #:key
102 (cache-directory
103 (%repository-cache-directory))
104 (ref '(branch . "origin/master")))
105 "Return two values: the content of the git repository at URL copied into a
106store directory and the sha1 of the top level commit in this directory. The
107reference to be checkout, once the repository is fetched, is specified by REF.
108REF is pair whose key is [branch | commit | tag] and value the associated
109data, respectively [<branch name> | <sha1> | <tag name>].
110
111Git repositories are kept in the cache directory specified by
112%repository-cache-directory parameter."
113 (with-libgit2
114 (let* ((cache-dir (url-cache-directory url cache-directory))
115 (cache-exists? (openable-repository? cache-dir))
116 (repository (if cache-exists?
117 (repository-open cache-dir)
118 (clone* url cache-dir))))
119 ;; Only fetch remote if it has not been cloned just before.
120 (when cache-exists?
121 (remote-fetch (remote-lookup repository "origin")))
122 (switch-to-ref repository ref)
123 (copy-to-store store cache-dir
124 #:url url
125 #:repository repository))))