You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

755 lines
32 KiB

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