git: 'update-cached-checkout' returns the commit relation.
[jackhill/guix/guix.git] / guix / channels.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
4 ;;; Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
5 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
12 ;;;
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21 (define-module (guix channels)
22 #:use-module (git)
23 #:use-module (guix git)
24 #:use-module (guix records)
25 #:use-module (guix gexp)
26 #:use-module (guix modules)
27 #:use-module (guix discovery)
28 #:use-module (guix monads)
29 #:use-module (guix profiles)
30 #:use-module (guix packages)
31 #:use-module (guix derivations)
32 #:use-module (guix combinators)
33 #:use-module (guix diagnostics)
34 #:use-module (guix sets)
35 #:use-module (guix store)
36 #:use-module (guix i18n)
37 #:use-module ((guix utils)
38 #:select (source-properties->location
39 &error-location
40 &fix-hint))
41 #:use-module (srfi srfi-1)
42 #:use-module (srfi srfi-2)
43 #:use-module (srfi srfi-9)
44 #:use-module (srfi srfi-11)
45 #:use-module (srfi srfi-26)
46 #:use-module (srfi srfi-34)
47 #:use-module (srfi srfi-35)
48 #:autoload (guix self) (whole-package make-config.scm)
49 #:autoload (guix inferior) (gexp->derivation-in-inferior) ;FIXME: circular dep
50 #:autoload (guix quirks) (%quirks %patches applicable-patch? apply-patch)
51 #:use-module (ice-9 match)
52 #:use-module (ice-9 vlist)
53 #:use-module ((ice-9 rdelim) #:select (read-string))
54 #:export (channel
55 channel?
56 channel-name
57 channel-url
58 channel-branch
59 channel-commit
60 channel-location
61
62 %default-channels
63 guix-channel?
64
65 channel-instance?
66 channel-instance-channel
67 channel-instance-commit
68 channel-instance-checkout
69
70 latest-channel-instances
71 checkout->channel-instance
72 latest-channel-derivation
73 channel-instances->manifest
74 %channel-profile-hooks
75 channel-instances->derivation
76
77 profile-channels
78
79 channel-news-entry?
80 channel-news-entry-commit
81 channel-news-entry-tag
82 channel-news-entry-title
83 channel-news-entry-body
84
85 channel-news-for-commit))
86
87 ;;; Commentary:
88 ;;;
89 ;;; This module implements "channels." A channel is usually a source of
90 ;;; package definitions. There's a special channel, the 'guix' channel, that
91 ;;; provides all of Guix, including its commands and its documentation.
92 ;;; User-defined channels are expected to typically provide a bunch of .scm
93 ;;; files meant to be added to the '%package-search-path'.
94 ;;;
95 ;;; This module provides tools to fetch and update channels from a Git
96 ;;; repository and to build them.
97 ;;;
98 ;;; Code:
99
100 (define-record-type* <channel> channel make-channel
101 channel?
102 (name channel-name)
103 (url channel-url)
104 (branch channel-branch (default "master"))
105 (commit channel-commit (default #f))
106 (location channel-location
107 (default (current-source-location)) (innate)))
108
109 (define %default-channels
110 ;; Default list of channels.
111 (list (channel
112 (name 'guix)
113 (branch "master")
114 (url "https://git.savannah.gnu.org/git/guix.git"))))
115
116 (define (guix-channel? channel)
117 "Return true if CHANNEL is the 'guix' channel."
118 (eq? 'guix (channel-name channel)))
119
120 (define-record-type <channel-instance>
121 (channel-instance channel commit checkout)
122 channel-instance?
123 (channel channel-instance-channel)
124 (commit channel-instance-commit)
125 (checkout channel-instance-checkout))
126
127 (define-record-type <channel-metadata>
128 (channel-metadata directory dependencies news-file)
129 channel-metadata?
130 (directory channel-metadata-directory) ;string with leading slash
131 (dependencies channel-metadata-dependencies) ;list of <channel>
132 (news-file channel-metadata-news-file)) ;string | #f
133
134 (define (channel-reference channel)
135 "Return the \"reference\" for CHANNEL, an sexp suitable for
136 'latest-repository-commit'."
137 (match (channel-commit channel)
138 (#f `(branch . ,(channel-branch channel)))
139 (commit `(commit . ,(channel-commit channel)))))
140
141 (define (read-channel-metadata port)
142 "Read from PORT channel metadata in the format expected for the
143 '.guix-channel' file. Return a <channel-metadata> record, or raise an error
144 if valid metadata could not be read from PORT."
145 (match (read port)
146 (('channel ('version 0) properties ...)
147 (let ((directory (and=> (assoc-ref properties 'directory) first))
148 (dependencies (or (assoc-ref properties 'dependencies) '()))
149 (news-file (and=> (assoc-ref properties 'news-file) first)))
150 (channel-metadata
151 (cond ((not directory) "/") ;directory
152 ((string-prefix? "/" directory) directory)
153 (else (string-append "/" directory)))
154 (map (lambda (item) ;dependencies
155 (let ((get (lambda* (key #:optional default)
156 (or (and=> (assoc-ref item key) first) default))))
157 (and-let* ((name (get 'name))
158 (url (get 'url))
159 (branch (get 'branch "master")))
160 (channel
161 (name name)
162 (branch branch)
163 (url url)
164 (commit (get 'commit))))))
165 dependencies)
166 news-file))) ;news-file
167 ((and ('channel ('version version) _ ...) sexp)
168 (raise (condition
169 (&message (message "unsupported '.guix-channel' version"))
170 (&error-location
171 (location (source-properties->location
172 (source-properties sexp)))))))
173 (sexp
174 (raise (condition
175 (&message (message "invalid '.guix-channel' file"))
176 (&error-location
177 (location (source-properties->location
178 (source-properties sexp)))))))))
179
180 (define (read-channel-metadata-from-source source)
181 "Return a channel-metadata record read from channel's SOURCE/.guix-channel
182 description file, or return the default channel-metadata record if that file
183 doesn't exist."
184 (catch 'system-error
185 (lambda ()
186 (call-with-input-file (string-append source "/.guix-channel")
187 read-channel-metadata))
188 (lambda args
189 (if (= ENOENT (system-error-errno args))
190 (channel-metadata "/" '() #f)
191 (apply throw args)))))
192
193 (define (channel-instance-metadata instance)
194 "Return a channel-metadata record read from the channel INSTANCE's
195 description file or its default value."
196 (read-channel-metadata-from-source (channel-instance-checkout instance)))
197
198 (define (channel-instance-dependencies instance)
199 "Return the list of channels that are declared as dependencies for the given
200 channel INSTANCE."
201 (channel-metadata-dependencies (channel-instance-metadata instance)))
202
203 (define (apply-patches checkout commit patches)
204 "Apply the matching PATCHES to CHECKOUT, modifying files in place. The
205 result is unspecified."
206 (let loop ((patches patches))
207 (match patches
208 (() #t)
209 ((patch rest ...)
210 (when (applicable-patch? patch checkout commit)
211 (apply-patch patch checkout))
212 (loop rest)))))
213
214 (define* (latest-channel-instance store channel
215 #:key (patches %patches))
216 "Return the latest channel instance for CHANNEL."
217 (define (dot-git? file stat)
218 (and (string=? (basename file) ".git")
219 (eq? 'directory (stat:type stat))))
220
221 (let-values (((checkout commit relation)
222 (update-cached-checkout (channel-url channel)
223 #:ref (channel-reference channel))))
224 (when (guix-channel? channel)
225 ;; Apply the relevant subset of PATCHES directly in CHECKOUT. This is
226 ;; safe to do because 'switch-to-ref' eventually does a hard reset.
227 (apply-patches checkout commit patches))
228
229 (let* ((name (url+commit->name (channel-url channel) commit))
230 (checkout (add-to-store store name #t "sha256" checkout
231 #:select? (negate dot-git?))))
232 (channel-instance channel commit checkout))))
233
234 (define* (latest-channel-instances store channels)
235 "Return a list of channel instances corresponding to the latest checkouts of
236 CHANNELS and the channels on which they depend."
237 ;; Only process channels that are unique, or that are more specific than a
238 ;; previous channel specification.
239 (define (ignore? channel others)
240 (member channel others
241 (lambda (a b)
242 (and (eq? (channel-name a) (channel-name b))
243 (or (channel-commit b)
244 (not (or (channel-commit a)
245 (channel-commit b))))))))
246
247 (let loop ((channels channels)
248 (previous-channels '()))
249 ;; Accumulate a list of instances. A list of processed channels is also
250 ;; accumulated to decide on duplicate channel specifications.
251 (define-values (resulting-channels instances)
252 (fold2 (lambda (channel previous-channels instances)
253 (if (ignore? channel previous-channels)
254 (values previous-channels instances)
255 (begin
256 (format (current-error-port)
257 (G_ "Updating channel '~a' from Git repository at '~a'...~%")
258 (channel-name channel)
259 (channel-url channel))
260 (let ((instance (latest-channel-instance store channel)))
261 (let-values (((new-instances new-channels)
262 (loop (channel-instance-dependencies instance)
263 previous-channels)))
264 (values (append (cons channel new-channels)
265 previous-channels)
266 (append (cons instance new-instances)
267 instances)))))))
268 previous-channels
269 '() ;instances
270 channels))
271
272 (let ((instance-name (compose channel-name channel-instance-channel)))
273 ;; Remove all earlier channel specifications if they are followed by a
274 ;; more specific one.
275 (values (delete-duplicates instances
276 (lambda (a b)
277 (eq? (instance-name a) (instance-name b))))
278 resulting-channels))))
279
280 (define* (checkout->channel-instance checkout
281 #:key commit
282 (url checkout) (name 'guix))
283 "Return a channel instance for CHECKOUT, which is assumed to be a checkout
284 of COMMIT at URL. Use NAME as the channel name."
285 (let* ((commit (or commit (make-string 40 #\0)))
286 (channel (channel (name name)
287 (commit commit)
288 (url url))))
289 (channel-instance channel commit checkout)))
290
291 (define %self-build-file
292 ;; The file containing code to build Guix. This serves the same purpose as
293 ;; a makefile, and, similarly, is intended to always keep this name.
294 "build-aux/build-self.scm")
295
296 (define %pull-version
297 ;; This is the version of the 'guix pull' protocol. It specifies what's
298 ;; expected from %SELF-BUILD-FILE. The initial version ("0") was when we'd
299 ;; place a set of compiled Guile modules in ~/.config/guix/latest.
300 1)
301
302 (define (standard-module-derivation name source core dependencies)
303 "Return a derivation that builds with CORE, a Guix instance, the Scheme
304 modules in SOURCE and that depend on DEPENDENCIES, a list of lowerable
305 objects. The assumption is that SOURCE contains package modules to be added
306 to '%package-module-path'."
307
308 (let* ((metadata (read-channel-metadata-from-source source))
309 (directory (channel-metadata-directory metadata)))
310
311 (define build
312 ;; This is code that we'll run in CORE, a Guix instance, with its own
313 ;; modules and so on. That way, we make sure these modules are built for
314 ;; the right Guile version, with the right dependencies, and that they get
315 ;; to see the right (gnu packages …) modules.
316 (with-extensions dependencies
317 #~(begin
318 (use-modules (guix build compile)
319 (guix build utils)
320 (srfi srfi-26))
321
322 (define go
323 (string-append #$output "/lib/guile/" (effective-version)
324 "/site-ccache"))
325 (define scm
326 (string-append #$output "/share/guile/site/"
327 (effective-version)))
328
329 (let* ((subdir #$directory)
330 (source (string-append #$source subdir)))
331 (compile-files source go (find-files source "\\.scm$"))
332 (mkdir-p (dirname scm))
333 (symlink (string-append #$source subdir) scm))
334
335 scm)))
336
337 (gexp->derivation-in-inferior name build core)))
338
339 (define* (guile-for-source source #:optional (quirks %quirks))
340 "Return the Guile package to use when building SOURCE or #f if the default
341 '%guile-for-build' should be good enough."
342 (let loop ((quirks quirks))
343 (match quirks
344 (()
345 #f)
346 (((predicate . guile) rest ...)
347 (if (predicate source) (guile) (loop rest))))))
348
349 (define (call-with-guile guile thunk)
350 (lambda (store)
351 (values (parameterize ((%guile-for-build
352 (if guile
353 (package-derivation store guile)
354 (%guile-for-build))))
355 (run-with-store store (thunk)))
356 store)))
357
358 (define-syntax-rule (with-guile guile exp ...)
359 "Set GUILE as the '%guile-for-build' parameter for the dynamic extent of
360 EXP, a series of monadic expressions."
361 (call-with-guile guile (lambda ()
362 (mbegin %store-monad exp ...))))
363
364 (define (with-trivial-build-handler mvalue)
365 "Run MVALUE, a monadic value, with a \"trivial\" build handler installed
366 that unconditionally resumes the continuation."
367 (lambda (store)
368 (with-build-handler (lambda (continue . _)
369 (continue #t))
370 (values (run-with-store store mvalue)
371 store))))
372
373 (define* (build-from-source name source
374 #:key core verbose? commit
375 (dependencies '()))
376 "Return a derivation to build Guix from SOURCE, using the self-build script
377 contained therein; use COMMIT as the version string. When CORE is true, build
378 package modules under SOURCE using CORE, an instance of Guix."
379 ;; Running the self-build script makes it easier to update the build
380 ;; procedure: the self-build script of the Guix-to-be-installed contains the
381 ;; right dependencies, build procedure, etc., which the Guix-in-use may not
382 ;; be know.
383 (define script
384 (string-append source "/" %self-build-file))
385
386 (if (file-exists? script)
387 (let ((build (save-module-excursion
388 (lambda ()
389 ;; Disable deprecation warnings; it's OK for SCRIPT to
390 ;; use deprecated APIs and the user doesn't have to know
391 ;; about it.
392 (parameterize ((guix-warning-port
393 (%make-void-port "w")))
394 (primitive-load script)))))
395 (guile (guile-for-source source)))
396 ;; BUILD must be a monadic procedure of at least one argument: the
397 ;; source tree.
398 ;;
399 ;; Note: BUILD can return #f if it does not support %PULL-VERSION. In
400 ;; the future we'll fall back to a previous version of the protocol
401 ;; when that happens.
402 (with-guile guile
403 ;; BUILD is usually quite costly. Install a "trivial" build handler
404 ;; so we don't bounce an outer build-accumulator handler that could
405 ;; cause us to redo half of the BUILD computation several times just
406 ;; to realize it gives the same result.
407 (with-trivial-build-handler
408 (build source #:verbose? verbose? #:version commit
409 #:pull-version %pull-version))))
410
411 ;; Build a set of modules that extend Guix using the standard method.
412 (standard-module-derivation name source core dependencies)))
413
414 (define* (build-channel-instance instance
415 #:optional core (dependencies '()))
416 "Return, as a monadic value, the derivation for INSTANCE, a channel
417 instance. DEPENDENCIES is a list of extensions providing Guile modules that
418 INSTANCE depends on."
419 (build-from-source (symbol->string
420 (channel-name (channel-instance-channel instance)))
421 (channel-instance-checkout instance)
422 #:commit (channel-instance-commit instance)
423 #:core core
424 #:dependencies dependencies))
425
426 (define (resolve-dependencies instances)
427 "Return a procedure that, given one of the elements of INSTANCES, returns
428 list of instances it depends on."
429 (define channel-instance-name
430 (compose channel-name channel-instance-channel))
431
432 (define table ;map a name to an instance
433 (fold (lambda (instance table)
434 (vhash-consq (channel-instance-name instance)
435 instance table))
436 vlist-null
437 instances))
438
439 (define edges
440 (fold (lambda (instance edges)
441 (fold (lambda (channel edges)
442 (let ((name (channel-name channel)))
443 (match (vhash-assq name table)
444 ((_ . target)
445 (vhash-consq instance target edges)))))
446 edges
447 (channel-instance-dependencies instance)))
448 vlist-null
449 instances))
450
451 (lambda (instance)
452 (vhash-foldq* cons '() instance edges)))
453
454 (define (channel-instance-derivations instances)
455 "Return the list of derivations to build INSTANCES, in the same order as
456 INSTANCES."
457 (define core-instance
458 ;; The 'guix' channel is treated specially: it's an implicit dependency of
459 ;; all the other channels.
460 (find (lambda (instance)
461 (guix-channel? (channel-instance-channel instance)))
462 instances))
463
464 (define edges
465 (resolve-dependencies instances))
466
467 (define (instance->derivation instance)
468 (mlet %store-monad ((system (current-system)))
469 (mcached (if (eq? instance core-instance)
470 (build-channel-instance instance)
471 (mlet %store-monad ((core (instance->derivation core-instance))
472 (deps (mapm %store-monad instance->derivation
473 (edges instance))))
474 (build-channel-instance instance core deps)))
475 instance
476 system)))
477
478 (unless core-instance
479 (let ((loc (and=> (any (compose channel-location channel-instance-channel)
480 instances)
481 source-properties->location)))
482 (raise (apply make-compound-condition
483 (condition
484 (&message (message "'guix' channel is lacking")))
485 (condition
486 (&fix-hint (hint (G_ "Make sure your list of channels
487 contains one channel named @code{guix} providing the core of Guix."))))
488 (if loc
489 (list (condition (&error-location (location loc))))
490 '())))))
491
492 (mapm %store-monad instance->derivation instances))
493
494 (define (whole-package-for-legacy name modules)
495 "Return a full-blown Guix package for MODULES, a derivation that builds Guix
496 modules in the old ~/.config/guix/latest style."
497 (define packages
498 (resolve-interface '(gnu packages guile)))
499
500 (define modules+compiled
501 ;; Since MODULES contains both .scm and .go files at its root, re-bundle
502 ;; it so that it has share/guile/site and lib/guile, which is what
503 ;; 'whole-package' expects.
504 (computed-file (derivation-name modules)
505 (with-imported-modules '((guix build utils))
506 #~(begin
507 (use-modules (guix build utils))
508
509 (define version
510 (effective-version))
511 (define share
512 (string-append #$output "/share/guile/site"))
513 (define lib
514 (string-append #$output "/lib/guile/" version))
515
516 (mkdir-p share) (mkdir-p lib)
517 (symlink #$modules (string-append share "/" version))
518 (symlink #$modules (string-append lib "/site-ccache"))))))
519
520 (letrec-syntax ((list (syntax-rules (->)
521 ((_)
522 '())
523 ((_ (module -> variable) rest ...)
524 (cons (module-ref (resolve-interface
525 '(gnu packages module))
526 'variable)
527 (list rest ...)))
528 ((_ variable rest ...)
529 (cons (module-ref packages 'variable)
530 (list rest ...))))))
531 (whole-package name modules+compiled
532
533 ;; In the "old style", %SELF-BUILD-FILE would simply return a
534 ;; derivation that builds modules. We have to infer what the
535 ;; dependencies of these modules were.
536 (list guile-json-3 guile-git guile-bytestructures
537 (ssh -> guile-ssh) (tls -> gnutls)))))
538
539 (define (old-style-guix? drv)
540 "Return true if DRV corresponds to a ~/.config/guix/latest style of
541 derivation."
542 ;; Here we rely on a gross historical fact: that derivations produced by the
543 ;; "old style" (before commit 8a0d9bc8a3f153159d9e239a151c0fa98f1e12d8,
544 ;; dated May 30, 2018) did not depend on "guix-command.drv".
545 (not (find (lambda (input)
546 (string=? "guix-command"
547 (derivation-name
548 (derivation-input-derivation input))))
549 (derivation-inputs drv))))
550
551 (define (channel-instances->manifest instances)
552 "Return a profile manifest with entries for all of INSTANCES, a list of
553 channel instances."
554 (define (instance->entry instance drv)
555 (let ((commit (channel-instance-commit instance))
556 (channel (channel-instance-channel instance)))
557 (manifest-entry
558 (name (symbol->string (channel-name channel)))
559 (version (string-take commit 7))
560 (item (if (guix-channel? channel)
561 (if (old-style-guix? drv)
562 (whole-package-for-legacy (string-append name "-" version)
563 drv)
564 drv)
565 drv))
566 (properties
567 `((source (repository
568 (version 0)
569 (url ,(channel-url channel))
570 (branch ,(channel-branch channel))
571 (commit ,commit))))))))
572
573 (mlet* %store-monad ((derivations (channel-instance-derivations instances))
574 (entries -> (map instance->entry instances derivations)))
575 (return (manifest entries))))
576
577 (define (package-cache-file manifest)
578 "Build a package cache file for the instance in MANIFEST. This is meant to
579 be used as a profile hook."
580 (let ((profile (profile (content manifest) (hooks '()))))
581 (define build
582 #~(begin
583 (use-modules (gnu packages))
584
585 (if (defined? 'generate-package-cache)
586 (begin
587 ;; Delegate package cache generation to the inferior.
588 (format (current-error-port)
589 "Generating package cache for '~a'...~%"
590 #$profile)
591 (generate-package-cache #$output))
592 (mkdir #$output))))
593
594 (gexp->derivation-in-inferior "guix-package-cache" build
595 profile
596
597 ;; If the Guix in PROFILE is too old and
598 ;; lacks 'guix repl', don't build the cache
599 ;; instead of failing.
600 #:silent-failure? #t
601
602 #:properties '((type . profile-hook)
603 (hook . package-cache))
604 #:local-build? #t)))
605
606 (define %channel-profile-hooks
607 ;; The default channel profile hooks.
608 (cons package-cache-file %default-profile-hooks))
609
610 (define (channel-instances->derivation instances)
611 "Return the derivation of the profile containing INSTANCES, a list of
612 channel instances."
613 (mlet %store-monad ((manifest (channel-instances->manifest instances)))
614 (profile-derivation manifest
615 #:hooks %channel-profile-hooks)))
616
617 (define latest-channel-instances*
618 (store-lift latest-channel-instances))
619
620 (define* (latest-channel-derivation #:optional (channels %default-channels))
621 "Return as a monadic value the derivation that builds the profile for the
622 latest instances of CHANNELS."
623 (mlet %store-monad ((instances (latest-channel-instances* channels)))
624 (channel-instances->derivation instances)))
625
626 (define (profile-channels profile)
627 "Return the list of channels corresponding to entries in PROFILE. If
628 PROFILE is not a profile created by 'guix pull', return the empty list."
629 (filter-map (lambda (entry)
630 (match (assq 'source (manifest-entry-properties entry))
631 (('source ('repository ('version 0)
632 ('url url)
633 ('branch branch)
634 ('commit commit)
635 _ ...))
636 (channel (name (string->symbol
637 (manifest-entry-name entry)))
638 (url url)
639 (commit commit)))
640
641 ;; No channel information for this manifest entry.
642 ;; XXX: Pre-0.15.0 Guix did not provide that information,
643 ;; but there's not much we can do in that case.
644 (_ #f)))
645
646 ;; Show most recently installed packages last.
647 (reverse
648 (manifest-entries (profile-manifest profile)))))
649
650 \f
651 ;;;
652 ;;; News.
653 ;;;
654
655 ;; Channel news.
656 (define-record-type <channel-news>
657 (channel-news entries)
658 channel-news?
659 (entries channel-news-entries)) ;list of <channel-news-entry>
660
661 ;; News entry, associated with a specific commit of the channel.
662 (define-record-type <channel-news-entry>
663 (channel-news-entry commit tag title body)
664 channel-news-entry?
665 (commit channel-news-entry-commit) ;hex string | #f
666 (tag channel-news-entry-tag) ;#f | string
667 (title channel-news-entry-title) ;list of language tag/string pairs
668 (body channel-news-entry-body)) ;list of language tag/string pairs
669
670 (define (sexp->channel-news-entry entry)
671 "Return the <channel-news-entry> record corresponding to ENTRY, an sexp."
672 (define (pair language message)
673 (cons (symbol->string language) message))
674
675 (match entry
676 (('entry ((and (or 'commit 'tag) type) commit-or-tag)
677 ('title ((? symbol? title-tags) (? string? titles)) ...)
678 ('body ((? symbol? body-tags) (? string? bodies)) ...)
679 _ ...)
680 (channel-news-entry (and (eq? type 'commit) commit-or-tag)
681 (and (eq? type 'tag) commit-or-tag)
682 (map pair title-tags titles)
683 (map pair body-tags bodies)))
684 (_
685 (raise (condition
686 (&message (message "invalid channel news entry"))
687 (&error-location
688 (location (source-properties->location
689 (source-properties entry)))))))))
690
691 (define (read-channel-news port)
692 "Read a channel news feed from PORT and return it as a <channel-news>
693 record."
694 (match (false-if-exception (read port))
695 (('channel-news ('version 0) entries ...)
696 (channel-news (map sexp->channel-news-entry entries)))
697 (('channel-news ('version version) _ ...)
698 ;; This is an unsupported version from the future. There's nothing wrong
699 ;; with that (the user may simply need to upgrade the 'guix' channel to
700 ;; be able to read it), so silently ignore it.
701 (channel-news '()))
702 (#f
703 (raise (condition
704 (&message (message "syntactically invalid channel news file")))))
705 (sexp
706 (raise (condition
707 (&message (message "invalid channel news file"))
708 (&error-location
709 (location (source-properties->location
710 (source-properties sexp)))))))))
711
712 (define (resolve-channel-news-entry-tag repository entry)
713 "If ENTRY has its 'commit' field set, return ENTRY. Otherwise, lookup
714 ENTRY's 'tag' in REPOSITORY and return ENTRY with its 'commit' field set to
715 the field its 'tag' refers to. A 'git-error' exception is raised if the tag
716 cannot be found."
717 (if (channel-news-entry-commit entry)
718 entry
719 (let* ((tag (channel-news-entry-tag entry))
720 (reference (string-append "refs/tags/" tag))
721 (oid (reference-name->oid repository reference)))
722 (channel-news-entry (oid->string oid) tag
723 (channel-news-entry-title entry)
724 (channel-news-entry-body entry)))))
725
726 (define* (channel-news-for-commit channel new #:optional old)
727 "Return a list of <channel-news-entry> for CHANNEL between commits OLD and
728 NEW. When OLD is omitted or is #f, return all the news entries of CHANNEL."
729 (catch 'git-error
730 (lambda ()
731 (let* ((checkout (update-cached-checkout (channel-url channel)
732 #:ref `(commit . ,new)))
733 (metadata (read-channel-metadata-from-source checkout))
734 (news-file (channel-metadata-news-file metadata))
735 (news-file (and news-file
736 (string-append checkout "/" news-file))))
737 (if (and news-file (file-exists? news-file))
738 (with-repository checkout repository
739 (let* ((news (call-with-input-file news-file
740 read-channel-news))
741 (entries (map (lambda (entry)
742 (resolve-channel-news-entry-tag repository
743 entry))
744 (channel-news-entries news))))
745 (if old
746 (let* ((new (commit-lookup repository (string->oid new)))
747 (old (commit-lookup repository (string->oid old)))
748 (commits (list->set
749 (map (compose oid->string commit-id)
750 (commit-difference new old)))))
751 (filter (lambda (entry)
752 (set-contains? commits
753 (channel-news-entry-commit entry)))
754 entries))
755 entries)))
756 '())))
757 (lambda (key error . rest)
758 ;; If commit NEW or commit OLD cannot be found, then something must be
759 ;; wrong (for example, the history of CHANNEL was rewritten and these
760 ;; commits no longer exist upstream), so quietly return the empty list.
761 (if (= GIT_ENOTFOUND (git-error-code error))
762 '()
763 (apply throw key error rest)))))
764
765 ;;; Local Variables:
766 ;;; eval: (put 'with-guile 'scheme-indent-function 1)
767 ;;; End: