processes: Allow 'less' to properly estimate line length.
[jackhill/guix/guix.git] / guix / git.scm
CommitLineData
6b7b3ca9 1;;; GNU Guix --- Functional package management for GNU
c3574749 2;;; Copyright © 2017, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
6a7c4636 3;;; Copyright © 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
6b7b3ca9
MO
4;;;
5;;; This file is part of GNU Guix.
6;;;
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.
11;;;
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.
16;;;
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/>.
19
20(define-module (guix git)
21 #:use-module (git)
22 #:use-module (git object)
a3d77c51 23 #:use-module (guix i18n)
6b7b3ca9 24 #:use-module (guix base32)
ca719424 25 #:use-module (gcrypt hash)
0ad5f809 26 #:use-module ((guix build utils) #:select (mkdir-p))
6b7b3ca9
MO
27 #:use-module (guix store)
28 #:use-module (guix utils)
49ae3f6d
LC
29 #:use-module (guix records)
30 #:use-module (guix gexp)
873f6f13 31 #:use-module (guix sets)
69db2993 32 #:use-module ((guix diagnostics) #:select (leave))
6b7b3ca9
MO
33 #:use-module (rnrs bytevectors)
34 #:use-module (ice-9 match)
35 #:use-module (srfi srfi-1)
91881986 36 #:use-module (srfi srfi-11)
95bd9f65
LC
37 #:use-module (srfi srfi-34)
38 #:use-module (srfi srfi-35)
6b7b3ca9 39 #:export (%repository-cache-directory
bc041b3e
LC
40 honor-system-x509-certificates!
41
873f6f13 42 with-repository
69db2993 43 with-git-error-handling
e7827560 44 false-if-git-not-found
91881986 45 update-cached-checkout
053b10c3 46 url+commit->name
49ae3f6d 47 latest-repository-commit
873f6f13 48 commit-difference
c098c11b 49 commit-relation
49ae3f6d
LC
50
51 git-checkout
52 git-checkout?
53 git-checkout-url
a063bac6
LC
54 git-checkout-branch
55 git-checkout-commit
56 git-checkout-recursive?))
6b7b3ca9
MO
57
58(define %repository-cache-directory
e83b2b0f
LC
59 (make-parameter (string-append (cache-directory #:ensure? #f)
60 "/checkouts")))
6b7b3ca9 61
bc041b3e
LC
62(define (honor-system-x509-certificates!)
63 "Use the system's X.509 certificates for Git checkouts over HTTPS. Honor
64the '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
69 ;; one.
70 (let ((file (letrec-syntax ((choose
71 (syntax-rules ()
72 ((_ file rest ...)
73 (let ((f file))
74 (if (and f (file-exists? f))
75 f
76 (choose rest ...))))
77 ((_)
78 #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")))
83 (and (or file
84 (and=> (stat directory #f)
85 (lambda (st)
86 (> (stat:nlink st) 2))))
87 (begin
88 (set-tls-certificate-locations! directory file)
89 #t))))
90
91(define %certificates-initialized?
92 ;; Whether 'honor-system-x509-certificates!' has already been called.
93 #f)
94
6b7b3ca9 95(define-syntax-rule (with-libgit2 thunk ...)
b02469d2
MO
96 (begin
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.
100 (libgit2-init!)
bc041b3e
LC
101 (unless %certificates-initialized?
102 (honor-system-x509-certificates!)
103 (set! %certificates-initialized? #t))
b02469d2 104 thunk ...))
6b7b3ca9
MO
105
106(define* (url-cache-directory url
107 #:optional (cache-directory
60cbc6a8
LC
108 (%repository-cache-directory))
109 #:key recursive?)
6b7b3ca9
MO
110 "Return the directory associated to URL in %repository-cache-directory."
111 (string-append
112 cache-directory "/"
60cbc6a8
LC
113 (bytevector->base32-string
114 (sha256 (string->utf8 (if recursive?
115 (string-append "R:" url)
116 url))))))
6b7b3ca9 117
c3574749
MO
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))))
121
6b7b3ca9
MO
122(define (clone* url directory)
123 "Clone git repository at URL into DIRECTORY. Upon failure,
124make sure no empty directory is left behind."
125 (with-throw-handler #t
126 (lambda ()
127 (mkdir-p directory)
195f0d05
LC
128
129 ;; Note: Explicitly pass options to work around the invalid default
130 ;; value in Guile-Git: <https://bugs.gnu.org/29238>.
b1488c76
LC
131 (if (module-defined? (resolve-interface '(git))
132 'clone-init-options)
c3574749
MO
133 (let ((auth-method (and auth-supported?
134 (%make-auth-ssh-agent))))
135 (clone url directory
136 (if auth-supported?
137 (make-clone-options
138 #:fetch-options (make-fetch-options auth-method))
139 (clone-init-options))))
b1488c76 140 (clone url directory)))
6b7b3ca9
MO
141 (lambda _
142 (false-if-exception (rmdir directory)))))
143
6b7b3ca9
MO
144(define (url+commit->name url sha1)
145 "Return the string \"<REPO-NAME>-<SHA1:7>\" where REPO-NAME is the name of
146the git repository, extracted from URL and SHA1:7 the seven first digits
147of SHA1 string."
148 (string-append
149 (string-replace-substring
150 (last (string-split url #\/)) ".git" "")
151 "-" (string-take sha1 7)))
152
1c058c38
LC
153(define (resolve-reference repository ref)
154 "Resolve the branch, commit or tag specified by REF, and return the
155corresponding Git object."
156 (let resolve ((ref ref))
157 (match ref
158 (('branch . branch)
159 (let ((oid (reference-target
160 (branch-lookup repository branch BRANCH-REMOTE))))
161 (object-lookup repository oid)))
162 (('commit . commit)
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.
168 (if (< len 40)
169 (if (module-defined? (resolve-interface '(git object))
170 'object-lookup-prefix)
171 (object-lookup-prefix repository (string->oid commit) len)
172 (raise (condition
173 (&message
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
180 (catch 'git-error
181 (lambda ()
182 (resolve `(tag . ,str)))
183 (lambda _
184 ;; There's no such tag, so it must be a commit ID.
185 (resolve `(commit . ,str))))))
186 (('tag . tag)
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))))))
193
6b7b3ca9 194(define (switch-to-ref repository ref)
91881986
LC
195 "Switch to REPOSITORY's branch, commit or tag specified by REF. Return the
196OID (roughly the commit hash) corresponding to REF."
95bd9f65 197 (define obj
1c058c38 198 (resolve-reference repository ref))
95bd9f65 199
91881986
LC
200 (reset repository obj RESET_HARD)
201 (object-id obj))
202
60cbc6a8
LC
203(define (call-with-repository directory proc)
204 (let ((repository #f))
205 (dynamic-wind
206 (lambda ()
207 (set! repository (repository-open directory)))
208 (lambda ()
209 (proc repository))
210 (lambda ()
211 (repository-close! repository)))))
212
213(define-syntax-rule (with-repository directory repository exp ...)
214 "Open the repository at DIRECTORY and bind REPOSITORY to it within the
215dynamic extent of EXP."
216 (call-with-repository directory
217 (lambda (repository) exp ...)))
218
69db2993
LC
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.
223 (match error
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)))))
228
229(define-syntax-rule (with-git-error-handling body ...)
230 (catch 'git-error
231 (lambda ()
232 body ...)
233 (lambda (key err)
234 (report-git-error err))))
235
6a7c4636
LC
236(define (load-git-submodules)
237 "Attempt to load (git submodules), which was missing until Guile-Git 0.2.0.
238Return true on success, false on failure."
239 (match (false-if-exception (resolve-interface '(git submodule)))
240 (#f
241 (set! load-git-submodules (const #f))
242 #f)
243 (iface
058d0251 244 (module-use! (resolve-module '(guix git)) iface)
6a7c4636
LC
245 (set! load-git-submodules (const #t))
246 #t)))
247
60cbc6a8
LC
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).
6a7c4636 252 (if (load-git-submodules)
60cbc6a8
LC
253 (for-each (lambda (name)
254 (let ((submodule (submodule-lookup repository name)))
255 (format log-port (G_ "updating submodule '~a'...~%")
256 name)
257 (submodule-update submodule)
258
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; \
269please upgrade Guile-Git.~%"))))
270
1fd7de45
LC
271(define-syntax-rule (false-if-git-not-found exp)
272 "Evaluate EXP, returning #false if a GIT_ENOTFOUND error is raised."
273 (catch 'git-error
274 (lambda ()
275 exp)
276 (lambda (key error . rest)
277 (if (= GIT_ENOTFOUND (git-error-code error))
278 #f
279 (apply throw key error rest)))))
280
a78dcb3d
LC
281(define (reference-available? repository ref)
282 "Return true if REF, a reference such as '(commit . \"cabba9e\"), is
283definitely available in REPOSITORY, false otherwise."
284 (match ref
285 (('commit . commit)
1fd7de45
LC
286 (false-if-git-not-found
287 (->bool (commit-lookup repository (string->oid commit)))))
a78dcb3d
LC
288 (_
289 #f)))
290
91881986
LC
291(define* (update-cached-checkout url
292 #:key
37a6cdbf 293 (ref '(branch . "master"))
60cbc6a8 294 recursive?
a620c9d5 295 (check-out? #t)
8d1d5657 296 starting-commit
60cbc6a8 297 (log-port (%make-void-port "w"))
91881986 298 (cache-directory
ffc3fcad 299 (url-cache-directory
60cbc6a8
LC
300 url (%repository-cache-directory)
301 #:recursive? recursive?)))
8d1d5657 302 "Update the cached checkout of URL to REF in CACHE-DIRECTORY. Return three
91881986 303values: the cache directory name, and the SHA1 commit (a string) corresponding
8d1d5657
LC
304to REF, and the relation of the new commit relative to STARTING-COMMIT (if
305provided) as returned by 'commit-relation'.
91881986 306
c4c2449f
LC
307REF is pair whose key is [branch | commit | tag | tag-or-commit ] and value
308the associated data: [<branch name> | <sha1> | <tag name> | <string>].
60cbc6a8 309
a620c9d5
LC
310When RECURSIVE? is true, check out submodules as well, if any.
311
312When CHECK-OUT? is true, reset the cached working tree to REF; otherwise leave
313it unchanged."
37a6cdbf
LC
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.
318 (match ref
319 (('branch . branch)
320 `(branch . ,(if (string-prefix? "origin/" branch)
321 branch
322 (string-append "origin/" branch))))
323 (_ ref)))
324
91881986 325 (with-libgit2
ffc3fcad 326 (let* ((cache-exists? (openable-repository? cache-directory))
91881986 327 (repository (if cache-exists?
e3e1a7ba 328 (repository-open cache-directory)
ffc3fcad 329 (clone* url cache-directory))))
91881986 330 ;; Only fetch remote if it has not been cloned just before.
a78dcb3d
LC
331 (when (and cache-exists?
332 (not (reference-available? repository ref)))
c3574749
MO
333 (if auth-supported?
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"))))
60cbc6a8
LC
339 (when recursive?
340 (update-submodules repository #:log-port log-port))
8d1d5657
LC
341
342 ;; Note: call 'commit-relation' from here because it's more efficient
343 ;; than letting users re-open the checkout later on.
a620c9d5
LC
344 (let* ((oid (if check-out?
345 (switch-to-ref repository canonical-ref)
346 (object-id
347 (resolve-reference repository canonical-ref))))
8d1d5657
LC
348 (new (and starting-commit
349 (commit-lookup repository oid)))
350 (old (and starting-commit
1fd7de45
LC
351 (false-if-git-not-found
352 (commit-lookup repository
353 (string->oid starting-commit)))))
8d1d5657 354 (relation (and starting-commit
1fd7de45
LC
355 (if old
356 (commit-relation old new)
357 'unrelated))))
91881986
LC
358
359 ;; Reclaim file descriptors and memory mappings associated with
360 ;; REPOSITORY as soon as possible.
361 (when (module-defined? (resolve-interface '(git repository))
362 'repository-close!)
363 (repository-close! repository))
364
8d1d5657 365 (values cache-directory (oid->string oid) relation)))))
6b7b3ca9
MO
366
367(define* (latest-repository-commit store url
368 #:key
60cbc6a8 369 recursive?
35cb37ea 370 (log-port (%make-void-port "w"))
6b7b3ca9
MO
371 (cache-directory
372 (%repository-cache-directory))
37a6cdbf 373 (ref '(branch . "master")))
6b7b3ca9
MO
374 "Return two values: the content of the git repository at URL copied into a
375store directory and the sha1 of the top level commit in this directory. The
376reference to be checkout, once the repository is fetched, is specified by REF.
377REF is pair whose key is [branch | commit | tag] and value the associated
378data, respectively [<branch name> | <sha1> | <tag name>].
379
60cbc6a8
LC
380When RECURSIVE? is true, check out submodules as well, if any.
381
6b7b3ca9 382Git repositories are kept in the cache directory specified by
35cb37ea
LC
383%repository-cache-directory parameter.
384
385Log progress and checkout info to LOG-PORT."
91881986
LC
386 (define (dot-git? file stat)
387 (and (string=? (basename file) ".git")
60cbc6a8
LC
388 (or (eq? 'directory (stat:type stat))
389
390 ;; Submodule checkouts end up with a '.git' regular file that
391 ;; contains metadata about where their actual '.git' directory
392 ;; lives.
393 (and recursive?
394 (eq? 'regular (stat:type stat))))))
dfca2418 395
35cb37ea 396 (format log-port "updating checkout of '~a'...~%" url)
ffc3fcad 397 (let*-values
8d1d5657 398 (((checkout commit _)
ffc3fcad 399 (update-cached-checkout url
60cbc6a8 400 #:recursive? recursive?
ffc3fcad
OP
401 #:ref ref
402 #:cache-directory
60cbc6a8
LC
403 (url-cache-directory url cache-directory
404 #:recursive?
405 recursive?)
406 #:log-port log-port))
ffc3fcad
OP
407 ((name)
408 (url+commit->name url commit)))
35cb37ea 409 (format log-port "retrieved commit ~a~%" commit)
91881986
LC
410 (values (add-to-store store name #t "sha256" checkout
411 #:select? (negate dot-git?))
412 commit)))
49ae3f6d 413
1d8b10d0
LC
414(define (print-git-error port key args default-printer)
415 (match args
416 (((? git-error? error) . _)
417 (format port (G_ "Git error: ~a~%")
418 (git-error-message error)))))
419
420(set-exception-printer! 'git-error print-git-error)
421
49ae3f6d 422\f
873f6f13
LC
423;;;
424;;; Commit difference.
425;;;
426
785af04a
LC
427(define* (commit-closure commit #:optional (visited (setq)))
428 "Return the closure of COMMIT as a set. Skip commits contained in VISITED,
429a set, and adjoin VISITED to the result."
873f6f13 430 (let loop ((commits (list commit))
785af04a 431 (visited visited))
873f6f13
LC
432 (match commits
433 (()
434 visited)
435 ((head . tail)
436 (if (set-contains? visited head)
437 (loop tail visited)
438 (loop (append (commit-parents head) tail)
439 (set-insert head visited)))))))
440
785af04a 441(define* (commit-difference new old #:optional (excluded '()))
873f6f13 442 "Return the list of commits between NEW and OLD, where OLD is assumed to be
785af04a
LC
443an ancestor of NEW. Exclude all the commits listed in EXCLUDED along with
444their ancestors.
873f6f13
LC
445
446Essentially, this computes the set difference between the closure of NEW and
447that of OLD."
448 (let loop ((commits (list new))
449 (result '())
72357e21
LC
450 (visited (fold commit-closure
451 (setq)
452 (cons old excluded))))
873f6f13
LC
453 (match commits
454 (()
455 (reverse result))
456 ((head . tail)
457 (if (set-contains? visited head)
458 (loop tail result visited)
459 (loop (append (commit-parents head) tail)
460 (cons head result)
461 (set-insert head visited)))))))
462
c098c11b
LC
463(define (commit-relation old new)
464 "Return a symbol denoting the relation between OLD and NEW, two commit
465objects: 'ancestor (meaning that OLD is an ancestor of NEW), 'descendant, or
466'unrelated, or 'self (OLD and NEW are the same commit)."
467 (if (eq? old new)
468 'self
469 (let ((newest (commit-closure new)))
470 (if (set-contains? newest old)
471 'ancestor
472 (let* ((seen (list->setq (commit-parents new)))
473 (oldest (commit-closure old seen)))
474 (if (set-contains? oldest new)
475 'descendant
476 'unrelated))))))
477
873f6f13 478\f
49ae3f6d
LC
479;;;
480;;; Checkouts.
481;;;
482
b18f7234 483;; Representation of the "latest" checkout of a branch or a specific commit.
49ae3f6d
LC
484(define-record-type* <git-checkout>
485 git-checkout make-git-checkout
486 git-checkout?
487 (url git-checkout-url)
b18f7234 488 (branch git-checkout-branch (default "master"))
177fecb5 489 (commit git-checkout-commit (default #f)) ;#f | tag | commit
06fff484 490 (recursive? git-checkout-recursive? (default #f)))
49ae3f6d 491
06fff484 492(define* (latest-repository-commit* url #:key ref recursive? log-port)
a3d77c51
LC
493 ;; Monadic variant of 'latest-repository-commit'.
494 (lambda (store)
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
497 ;; handled.
498 (catch 'git-error
499 (lambda ()
500 (values (latest-repository-commit store url
06fff484
LC
501 #:ref ref
502 #:recursive? recursive?
503 #:log-port log-port)
a3d77c51
LC
504 store))
505 (lambda (key error . _)
506 (raise (condition
507 (&message
508 (message
509 (match ref
510 (('commit . commit)
511 (format #f (G_ "cannot fetch commit ~a from ~a: ~a")
512 commit url (git-error-message error)))
513 (('branch . branch)
514 (format #f (G_ "cannot fetch branch '~a' from ~a: ~a")
515 branch url (git-error-message error)))
516 (_
517 (format #f (G_ "Git failure while fetching ~a: ~a")
518 url (git-error-message error))))))))))))
49ae3f6d
LC
519
520(define-gexp-compiler (git-checkout-compiler (checkout <git-checkout>)
521 system target)
522 ;; "Compile" CHECKOUT by updating the local checkout and adding it to the
523 ;; store.
524 (match checkout
06fff484 525 (($ <git-checkout> url branch commit recursive?)
49ae3f6d 526 (latest-repository-commit* url
b18f7234 527 #:ref (if commit
177fecb5 528 `(tag-or-commit . ,commit)
b18f7234 529 `(branch . ,branch))
06fff484 530 #:recursive? recursive?
49ae3f6d 531 #:log-port (current-error-port)))))
60cbc6a8
LC
532
533;; Local Variables:
534;; eval: (put 'with-repository 'scheme-indent-function 2)
535;; End: