gnu: ikiwiki: Add missing input.
[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)
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 #:optional (previous-channels '()))
235 "Return a list of channel instances corresponding to the latest checkouts of
236 CHANNELS and the channels on which they depend. PREVIOUS-CHANNELS is a list
237 of previously processed channels."
238 ;; Only process channels that are unique, or that are more specific than a
239 ;; previous channel specification.
240 (define (ignore? channel others)
241 (member channel others
242 (lambda (a b)
243 (and (eq? (channel-name a) (channel-name b))
244 (or (channel-commit b)
245 (not (or (channel-commit a)
246 (channel-commit b))))))))
247
248 ;; Accumulate a list of instances. A list of processed channels is also
249 ;; accumulated to decide on duplicate channel specifications.
250 (define-values (resulting-channels instances)
251 (fold2 (lambda (channel previous-channels instances)
252 (if (ignore? channel previous-channels)
253 (values previous-channels instances)
254 (begin
255 (format (current-error-port)
256 (G_ "Updating channel '~a' from Git repository at '~a'...~%")
257 (channel-name channel)
258 (channel-url channel))
259 (let ((instance (latest-channel-instance store channel)))
260 (let-values (((new-instances new-channels)
261 (latest-channel-instances
262 store
263 (channel-instance-dependencies instance)
264 previous-channels)))
265 (values (append (cons channel new-channels)
266 previous-channels)
267 (append (cons instance new-instances)
268 instances)))))))
269 previous-channels
270 '() ;instances
271 channels))
272
273 (let ((instance-name (compose channel-name channel-instance-channel)))
274 ;; Remove all earlier channel specifications if they are followed by a
275 ;; more specific one.
276 (values (delete-duplicates instances
277 (lambda (a b)
278 (eq? (instance-name a) (instance-name b))))
279 resulting-channels)))
280
281 (define* (checkout->channel-instance checkout
282 #:key commit
283 (url checkout) (name 'guix))
284 "Return a channel instance for CHECKOUT, which is assumed to be a checkout
285 of COMMIT at URL. Use NAME as the channel name."
286 (let* ((commit (or commit (make-string 40 #\0)))
287 (channel (channel (name name)
288 (commit commit)
289 (url url))))
290 (channel-instance channel commit checkout)))
291
292 (define %self-build-file
293 ;; The file containing code to build Guix. This serves the same purpose as
294 ;; a makefile, and, similarly, is intended to always keep this name.
295 "build-aux/build-self.scm")
296
297 (define %pull-version
298 ;; This is the version of the 'guix pull' protocol. It specifies what's
299 ;; expected from %SELF-BUILD-FILE. The initial version ("0") was when we'd
300 ;; place a set of compiled Guile modules in ~/.config/guix/latest.
301 1)
302
303 (define (standard-module-derivation name source core dependencies)
304 "Return a derivation that builds with CORE, a Guix instance, the Scheme
305 modules in SOURCE and that depend on DEPENDENCIES, a list of lowerable
306 objects. The assumption is that SOURCE contains package modules to be added
307 to '%package-module-path'."
308
309 (let* ((metadata (read-channel-metadata-from-source source))
310 (directory (channel-metadata-directory metadata)))
311
312 (define build
313 ;; This is code that we'll run in CORE, a Guix instance, with its own
314 ;; modules and so on. That way, we make sure these modules are built for
315 ;; the right Guile version, with the right dependencies, and that they get
316 ;; to see the right (gnu packages …) modules.
317 (with-extensions dependencies
318 #~(begin
319 (use-modules (guix build compile)
320 (guix build utils)
321 (srfi srfi-26))
322
323 (define go
324 (string-append #$output "/lib/guile/" (effective-version)
325 "/site-ccache"))
326 (define scm
327 (string-append #$output "/share/guile/site/"
328 (effective-version)))
329
330 (let* ((subdir #$directory)
331 (source (string-append #$source subdir)))
332 (compile-files source go (find-files source "\\.scm$"))
333 (mkdir-p (dirname scm))
334 (symlink (string-append #$source subdir) scm))
335
336 scm)))
337
338 (gexp->derivation-in-inferior name build core)))
339
340 (define* (guile-for-source source #:optional (quirks %quirks))
341 "Return the Guile package to use when building SOURCE or #f if the default
342 '%guile-for-build' should be good enough."
343 (let loop ((quirks quirks))
344 (match quirks
345 (()
346 #f)
347 (((predicate . guile) rest ...)
348 (if (predicate source) (guile) (loop rest))))))
349
350 (define (call-with-guile guile thunk)
351 (lambda (store)
352 (values (parameterize ((%guile-for-build
353 (if guile
354 (package-derivation store guile)
355 (%guile-for-build))))
356 (run-with-store store (thunk)))
357 store)))
358
359 (define-syntax-rule (with-guile guile exp ...)
360 "Set GUILE as the '%guile-for-build' parameter for the dynamic extent of
361 EXP, a series of monadic expressions."
362 (call-with-guile guile (lambda ()
363 (mbegin %store-monad exp ...))))
364
365 (define (with-trivial-build-handler mvalue)
366 "Run MVALUE, a monadic value, with a \"trivial\" build handler installed
367 that unconditionally resumes the continuation."
368 (lambda (store)
369 (with-build-handler (lambda (continue . _)
370 (continue #t))
371 (values (run-with-store store mvalue)
372 store))))
373
374 (define* (build-from-source name source
375 #:key core verbose? commit
376 (dependencies '()))
377 "Return a derivation to build Guix from SOURCE, using the self-build script
378 contained therein; use COMMIT as the version string. When CORE is true, build
379 package modules under SOURCE using CORE, an instance of Guix."
380 ;; Running the self-build script makes it easier to update the build
381 ;; procedure: the self-build script of the Guix-to-be-installed contains the
382 ;; right dependencies, build procedure, etc., which the Guix-in-use may not
383 ;; be know.
384 (define script
385 (string-append source "/" %self-build-file))
386
387 (if (file-exists? script)
388 (let ((build (save-module-excursion
389 (lambda ()
390 ;; Disable deprecation warnings; it's OK for SCRIPT to
391 ;; use deprecated APIs and the user doesn't have to know
392 ;; about it.
393 (parameterize ((guix-warning-port
394 (%make-void-port "w")))
395 (primitive-load script)))))
396 (guile (guile-for-source source)))
397 ;; BUILD must be a monadic procedure of at least one argument: the
398 ;; source tree.
399 ;;
400 ;; Note: BUILD can return #f if it does not support %PULL-VERSION. In
401 ;; the future we'll fall back to a previous version of the protocol
402 ;; when that happens.
403 (with-guile guile
404 ;; BUILD is usually quite costly. Install a "trivial" build handler
405 ;; so we don't bounce an outer build-accumulator handler that could
406 ;; cause us to redo half of the BUILD computation several times just
407 ;; to realize it gives the same result.
408 (with-trivial-build-handler
409 (build source #:verbose? verbose? #:version commit
410 #:pull-version %pull-version))))
411
412 ;; Build a set of modules that extend Guix using the standard method.
413 (standard-module-derivation name source core dependencies)))
414
415 (define* (build-channel-instance instance
416 #:optional core (dependencies '()))
417 "Return, as a monadic value, the derivation for INSTANCE, a channel
418 instance. DEPENDENCIES is a list of extensions providing Guile modules that
419 INSTANCE depends on."
420 (build-from-source (symbol->string
421 (channel-name (channel-instance-channel instance)))
422 (channel-instance-checkout instance)
423 #:commit (channel-instance-commit instance)
424 #:core core
425 #:dependencies dependencies))
426
427 (define (resolve-dependencies instances)
428 "Return a procedure that, given one of the elements of INSTANCES, returns
429 list of instances it depends on."
430 (define channel-instance-name
431 (compose channel-name channel-instance-channel))
432
433 (define table ;map a name to an instance
434 (fold (lambda (instance table)
435 (vhash-consq (channel-instance-name instance)
436 instance table))
437 vlist-null
438 instances))
439
440 (define edges
441 (fold (lambda (instance edges)
442 (fold (lambda (channel edges)
443 (let ((name (channel-name channel)))
444 (match (vhash-assq name table)
445 ((_ . target)
446 (vhash-consq instance target edges)))))
447 edges
448 (channel-instance-dependencies instance)))
449 vlist-null
450 instances))
451
452 (lambda (instance)
453 (vhash-foldq* cons '() instance edges)))
454
455 (define (channel-instance-derivations instances)
456 "Return the list of derivations to build INSTANCES, in the same order as
457 INSTANCES."
458 (define core-instance
459 ;; The 'guix' channel is treated specially: it's an implicit dependency of
460 ;; all the other channels.
461 (find (lambda (instance)
462 (guix-channel? (channel-instance-channel instance)))
463 instances))
464
465 (define edges
466 (resolve-dependencies instances))
467
468 (define (instance->derivation instance)
469 (mlet %store-monad ((system (current-system)))
470 (mcached (if (eq? instance core-instance)
471 (build-channel-instance instance)
472 (mlet %store-monad ((core (instance->derivation core-instance))
473 (deps (mapm %store-monad instance->derivation
474 (edges instance))))
475 (build-channel-instance instance core deps)))
476 instance
477 system)))
478
479 (unless core-instance
480 (let ((loc (and=> (any (compose channel-location channel-instance-channel)
481 instances)
482 source-properties->location)))
483 (raise (apply make-compound-condition
484 (condition
485 (&message (message "'guix' channel is lacking")))
486 (condition
487 (&fix-hint (hint (G_ "Make sure your list of channels
488 contains one channel named @code{guix} providing the core of Guix."))))
489 (if loc
490 (list (condition (&error-location (location loc))))
491 '())))))
492
493 (mapm %store-monad instance->derivation instances))
494
495 (define (whole-package-for-legacy name modules)
496 "Return a full-blown Guix package for MODULES, a derivation that builds Guix
497 modules in the old ~/.config/guix/latest style."
498 (define packages
499 (resolve-interface '(gnu packages guile)))
500
501 (define modules+compiled
502 ;; Since MODULES contains both .scm and .go files at its root, re-bundle
503 ;; it so that it has share/guile/site and lib/guile, which is what
504 ;; 'whole-package' expects.
505 (computed-file (derivation-name modules)
506 (with-imported-modules '((guix build utils))
507 #~(begin
508 (use-modules (guix build utils))
509
510 (define version
511 (effective-version))
512 (define share
513 (string-append #$output "/share/guile/site"))
514 (define lib
515 (string-append #$output "/lib/guile/" version))
516
517 (mkdir-p share) (mkdir-p lib)
518 (symlink #$modules (string-append share "/" version))
519 (symlink #$modules (string-append lib "/site-ccache"))))))
520
521 (letrec-syntax ((list (syntax-rules (->)
522 ((_)
523 '())
524 ((_ (module -> variable) rest ...)
525 (cons (module-ref (resolve-interface
526 '(gnu packages module))
527 'variable)
528 (list rest ...)))
529 ((_ variable rest ...)
530 (cons (module-ref packages 'variable)
531 (list rest ...))))))
532 (whole-package name modules+compiled
533
534 ;; In the "old style", %SELF-BUILD-FILE would simply return a
535 ;; derivation that builds modules. We have to infer what the
536 ;; dependencies of these modules were.
537 (list guile-json-3 guile-git guile-bytestructures
538 (ssh -> guile-ssh) (tls -> gnutls)))))
539
540 (define (old-style-guix? drv)
541 "Return true if DRV corresponds to a ~/.config/guix/latest style of
542 derivation."
543 ;; Here we rely on a gross historical fact: that derivations produced by the
544 ;; "old style" (before commit 8a0d9bc8a3f153159d9e239a151c0fa98f1e12d8,
545 ;; dated May 30, 2018) did not depend on "guix-command.drv".
546 (not (find (lambda (input)
547 (string=? "guix-command"
548 (derivation-name
549 (derivation-input-derivation input))))
550 (derivation-inputs drv))))
551
552 (define (channel-instances->manifest instances)
553 "Return a profile manifest with entries for all of INSTANCES, a list of
554 channel instances."
555 (define (instance->entry instance drv)
556 (let ((commit (channel-instance-commit instance))
557 (channel (channel-instance-channel instance)))
558 (manifest-entry
559 (name (symbol->string (channel-name channel)))
560 (version (string-take commit 7))
561 (item (if (guix-channel? channel)
562 (if (old-style-guix? drv)
563 (whole-package-for-legacy (string-append name "-" version)
564 drv)
565 drv)
566 drv))
567 (properties
568 `((source (repository
569 (version 0)
570 (url ,(channel-url channel))
571 (branch ,(channel-branch channel))
572 (commit ,commit))))))))
573
574 (mlet* %store-monad ((derivations (channel-instance-derivations instances))
575 (entries -> (map instance->entry instances derivations)))
576 (return (manifest entries))))
577
578 (define (package-cache-file manifest)
579 "Build a package cache file for the instance in MANIFEST. This is meant to
580 be used as a profile hook."
581 (let ((profile (profile (content manifest) (hooks '()))))
582 (define build
583 #~(begin
584 (use-modules (gnu packages))
585
586 (if (defined? 'generate-package-cache)
587 (begin
588 ;; Delegate package cache generation to the inferior.
589 (format (current-error-port)
590 "Generating package cache for '~a'...~%"
591 #$profile)
592 (generate-package-cache #$output))
593 (mkdir #$output))))
594
595 (gexp->derivation-in-inferior "guix-package-cache" build
596 profile
597
598 ;; If the Guix in PROFILE is too old and
599 ;; lacks 'guix repl', don't build the cache
600 ;; instead of failing.
601 #:silent-failure? #t
602
603 #:properties '((type . profile-hook)
604 (hook . package-cache))
605 #:local-build? #t)))
606
607 (define %channel-profile-hooks
608 ;; The default channel profile hooks.
609 (cons package-cache-file %default-profile-hooks))
610
611 (define (channel-instances->derivation instances)
612 "Return the derivation of the profile containing INSTANCES, a list of
613 channel instances."
614 (mlet %store-monad ((manifest (channel-instances->manifest instances)))
615 (profile-derivation manifest
616 #:hooks %channel-profile-hooks)))
617
618 (define latest-channel-instances*
619 (store-lift latest-channel-instances))
620
621 (define* (latest-channel-derivation #:optional (channels %default-channels))
622 "Return as a monadic value the derivation that builds the profile for the
623 latest instances of CHANNELS."
624 (mlet %store-monad ((instances (latest-channel-instances* channels)))
625 (channel-instances->derivation instances)))
626
627 (define (profile-channels profile)
628 "Return the list of channels corresponding to entries in PROFILE. If
629 PROFILE is not a profile created by 'guix pull', return the empty list."
630 (filter-map (lambda (entry)
631 (match (assq 'source (manifest-entry-properties entry))
632 (('source ('repository ('version 0)
633 ('url url)
634 ('branch branch)
635 ('commit commit)
636 _ ...))
637 (channel (name (string->symbol
638 (manifest-entry-name entry)))
639 (url url)
640 (commit commit)))
641
642 ;; No channel information for this manifest entry.
643 ;; XXX: Pre-0.15.0 Guix did not provide that information,
644 ;; but there's not much we can do in that case.
645 (_ #f)))
646
647 ;; Show most recently installed packages last.
648 (reverse
649 (manifest-entries (profile-manifest profile)))))
650
651 \f
652 ;;;
653 ;;; News.
654 ;;;
655
656 ;; Channel news.
657 (define-record-type <channel-news>
658 (channel-news entries)
659 channel-news?
660 (entries channel-news-entries)) ;list of <channel-news-entry>
661
662 ;; News entry, associated with a specific commit of the channel.
663 (define-record-type <channel-news-entry>
664 (channel-news-entry commit tag title body)
665 channel-news-entry?
666 (commit channel-news-entry-commit) ;hex string | #f
667 (tag channel-news-entry-tag) ;#f | string
668 (title channel-news-entry-title) ;list of language tag/string pairs
669 (body channel-news-entry-body)) ;list of language tag/string pairs
670
671 (define (sexp->channel-news-entry entry)
672 "Return the <channel-news-entry> record corresponding to ENTRY, an sexp."
673 (define (pair language message)
674 (cons (symbol->string language) message))
675
676 (match entry
677 (('entry ((and (or 'commit 'tag) type) commit-or-tag)
678 ('title ((? symbol? title-tags) (? string? titles)) ...)
679 ('body ((? symbol? body-tags) (? string? bodies)) ...)
680 _ ...)
681 (channel-news-entry (and (eq? type 'commit) commit-or-tag)
682 (and (eq? type 'tag) commit-or-tag)
683 (map pair title-tags titles)
684 (map pair body-tags bodies)))
685 (_
686 (raise (condition
687 (&message (message "invalid channel news entry"))
688 (&error-location
689 (location (source-properties->location
690 (source-properties entry)))))))))
691
692 (define (read-channel-news port)
693 "Read a channel news feed from PORT and return it as a <channel-news>
694 record."
695 (match (false-if-exception (read port))
696 (('channel-news ('version 0) entries ...)
697 (channel-news (map sexp->channel-news-entry entries)))
698 (('channel-news ('version version) _ ...)
699 ;; This is an unsupported version from the future. There's nothing wrong
700 ;; with that (the user may simply need to upgrade the 'guix' channel to
701 ;; be able to read it), so silently ignore it.
702 (channel-news '()))
703 (#f
704 (raise (condition
705 (&message (message "syntactically invalid channel news file")))))
706 (sexp
707 (raise (condition
708 (&message (message "invalid channel news file"))
709 (&error-location
710 (location (source-properties->location
711 (source-properties sexp)))))))))
712
713 (define (resolve-channel-news-entry-tag repository entry)
714 "If ENTRY has its 'commit' field set, return ENTRY. Otherwise, lookup
715 ENTRY's 'tag' in REPOSITORY and return ENTRY with its 'commit' field set to
716 the field its 'tag' refers to. A 'git-error' exception is raised if the tag
717 cannot be found."
718 (if (channel-news-entry-commit entry)
719 entry
720 (let* ((tag (channel-news-entry-tag entry))
721 (reference (string-append "refs/tags/" tag))
722 (oid (reference-name->oid repository reference)))
723 (channel-news-entry (oid->string oid) tag
724 (channel-news-entry-title entry)
725 (channel-news-entry-body entry)))))
726
727 (define* (channel-news-for-commit channel new #:optional old)
728 "Return a list of <channel-news-entry> for CHANNEL between commits OLD and
729 NEW. When OLD is omitted or is #f, return all the news entries of CHANNEL."
730 (catch 'git-error
731 (lambda ()
732 (let* ((checkout (update-cached-checkout (channel-url channel)
733 #:ref `(commit . ,new)))
734 (metadata (read-channel-metadata-from-source checkout))
735 (news-file (channel-metadata-news-file metadata))
736 (news-file (and news-file
737 (string-append checkout "/" news-file))))
738 (if (and news-file (file-exists? news-file))
739 (with-repository checkout repository
740 (let* ((news (call-with-input-file news-file
741 read-channel-news))
742 (entries (map (lambda (entry)
743 (resolve-channel-news-entry-tag repository
744 entry))
745 (channel-news-entries news))))
746 (if old
747 (let* ((new (commit-lookup repository (string->oid new)))
748 (old (commit-lookup repository (string->oid old)))
749 (commits (list->set
750 (map (compose oid->string commit-id)
751 (commit-difference new old)))))
752 (filter (lambda (entry)
753 (set-contains? commits
754 (channel-news-entry-commit entry)))
755 entries))
756 entries)))
757 '())))
758 (lambda (key error . rest)
759 ;; If commit NEW or commit OLD cannot be found, then something must be
760 ;; wrong (for example, the history of CHANNEL was rewritten and these
761 ;; commits no longer exist upstream), so quietly return the empty list.
762 (if (= GIT_ENOTFOUND (git-error-code error))
763 '()
764 (apply throw key error rest)))))
765
766 ;;; Local Variables:
767 ;;; eval: (put 'with-guile 'scheme-indent-function 1)
768 ;;; End: