summaryrefslogtreecommitdiff
path: root/guix/scripts/describe.scm
blob: 6d451dc902ca5bd46988ea6239f473188167fdc4 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2018, 2019, 2020, 2021, 2023 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2018 Oleg Pykhalov <go.wigust@gmail.com>
;;; Copyright © 2020 Ekaitz Zarraga <ekaitz@elenq.tech>
;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
;;; Copyright © 2022 jgart <jgart@dismail.de>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (guix scripts describe)
  #:use-module ((guix config) #:select (%guix-version))
  #:use-module ((guix ui) #:hide (display-profile-content))
  #:use-module ((guix utils) #:select (string-replace-substring))
  #:use-module (guix channels)
  #:use-module (guix scripts)
  #:use-module (guix describe)
  #:use-module (guix profiles)
  #:autoload   (guix colors) (supports-hyperlinks? hyperlink)
  #:autoload   (guix openpgp) (openpgp-format-fingerprint)
  #:autoload   (json builder) (scm->json-string)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-37)
  #:use-module (ice-9 match)
  #:use-module (ice-9 format)
  #:autoload   (ice-9 pretty-print) (pretty-print)
  #:use-module (web uri)
  #:export (display-profile-content
            channel-commit-hyperlink

            guix-describe))


;;;
;;; Command-line options.
;;;
(define %available-formats
  '("human" "channels" "channels-sans-intro" "json" "recutils"))

(define (list-formats)
  (display (G_ "The available formats are:\n"))
  (newline)
  (for-each (lambda (f)
              (format #t "  - ~a~%" f))
            %available-formats))

(define %options
  ;; Specifications of the command-line options.
  (list (option '(#\f "format") #t #f
                (lambda (opt name arg result)
                  (unless (member arg %available-formats)
                    (leave (G_ "~a: unsupported output format~%") arg))
                  (alist-cons 'format (string->symbol arg) result)))
        (option '("list-formats") #f #f
                (lambda (opt name arg result)
                  (list-formats)
                  (exit 0)))
        (option '(#\p "profile") #t #f
                (lambda (opt name arg result)
                  (alist-cons 'profile (canonicalize-profile arg)
                              result)))
        (option '(#\h "help") #f #f
                (lambda args
                  (leave-on-EPIPE (show-help))
                  (exit 0)))
        (option '(#\V "version") #f #f
                (lambda args
                  (show-version-and-exit "guix describe")))))

(define %default-options
  ;; Alist of default option values.
  '((format . human)))

(define (show-help)
  (display (G_ "Usage: guix describe [OPTION]...
Display information about the channels currently in use.\n"))
  (display (G_ "
  -f, --format=FORMAT    display information in the given FORMAT"))
  (display (G_ "
      --list-formats     display available formats"))
  (display (G_ "
  -p, --profile=PROFILE  display information about PROFILE"))
  (newline)
  (display (G_ "
  -h, --help             display this help and exit"))
  (display (G_ "
  -V, --version          display version information and exit"))
  (newline)
  (show-bug-report-information))

(define (display-package-search-path fmt)
  "Display GUIX_PACKAGE_PATH, if it is set, according to FMT."
  (match (getenv "GUIX_PACKAGE_PATH")
    (#f #t)
    (string
     (match fmt
       ('human
        (format #t "~%GUIX_PACKAGE_PATH=\"~a\"~%" string))
       ('channels
        (format #t (G_ "~%;; warning: GUIX_PACKAGE_PATH=\"~a\"~%")
                string))
       (_
        (warning (G_ "'GUIX_PACKAGE_PATH' is set but it is not captured~%")))))))

(define (channel->json channel)
  (scm->json-string
   (let ((intro (channel-introduction channel)))
     `((name . ,(channel-name channel))
       (url . ,(channel-url channel))
       (branch . ,(channel-branch channel))
       (commit . ,(channel-commit channel))
       ,@(if intro
             `((introduction
                . ((commit . ,(channel-introduction-first-signed-commit
                               intro))
                   (signer . ,(openpgp-format-fingerprint
                               (channel-introduction-first-commit-signer
                                intro))))))
             '())))))

(define (channel->recutils channel port)
  (define intro
    (channel-introduction channel))

  (format port "name: ~a~%" (channel-name channel))
  (format port "url: ~a~%" (channel-url channel))
  (format port "branch: ~a~%" (channel-branch channel))
  (format port "commit: ~a~%" (channel-commit channel))
  (when intro
    (format port "introductioncommit: ~a~%"
            (channel-introduction-first-signed-commit intro))
    (format port "introductionsigner: ~a~%"
            (openpgp-format-fingerprint
             (channel-introduction-first-commit-signer intro)))))

(define (display-checkout-info fmt)
  "Display information about the current checkout according to FMT, a symbol
denoting the requested format.  Exit if the current directory does not lie
within a Git checkout."
  (let* ((program (car (command-line)))
         (channel (repository->guix-channel (dirname program))))
    (unless channel
      (report-error (G_ "failed to determine origin~%"))
      (display-hint (G_ "Perhaps this
@command{guix} command was not obtained with @command{guix pull}?  Its version
string is ~a.~%")
                    %guix-version)
      (exit 1))

    (match fmt
      ('human
       (format #t (G_ "Git checkout:~%"))
       (format #t (G_ "  repository: ~a~%") (channel-url channel))
       (format #t (G_ "  branch: ~a~%") (channel-branch channel))
       (format #t (G_ "  commit: ~a~%") (channel-commit channel)))
      ('channels
       (pretty-print `(list ,(channel->code channel))))
      ('json
       (display (channel->json channel))
       (newline))
      ('recutils
       (channel->recutils channel (current-output-port))))
    (display-package-search-path fmt)))

(define* (display-profile-info profile fmt
                               #:optional
                               (channels (profile-channels profile)))
  "Display information about PROFILE, a profile as created by (guix channels),
in the format specified by FMT.  PROFILE can be #f, in which case CHANNELS is
what matters."
  (define number
    (and profile (generation-number profile)))

  (match fmt
    ('human
     (display-profile-content profile number channels))
    ('channels
     (pretty-print `(list ,@(map channel->code channels))))
    ('channels-sans-intro
     (pretty-print `(list ,@(map (cut channel->code <>
                                      #:include-introduction? #f)
                                 channels))))
    ('json
     (format #t "[~a]~%" (string-join (map channel->json channels) ",")))
    ('recutils
     (format #t "~{~a~%~}"
             (map (lambda (channel)
                    (with-output-to-string
                      (lambda ()
                        (channel->recutils channel (current-output-port)))))
                  channels))))
  (display-package-search-path fmt))

(define (profile-generation-channels profile number)
  "Return the list of channels for generation NUMBER of PROFILE."
  (profile-channels (if (zero? number)
                        profile
                        (generation-file-name profile number))))

(define* (display-profile-content profile number
                                  #:optional
                                  (channels
                                   (profile-generation-channels profile
                                                                number)))
  "Display CHANNELS along with PROFILE info, generation NUMBER, in a
human-readable way and displaying details about the channel's source code.
PROFILE and NUMBER "
  (when (and number profile)
    (display-generation profile number))

  (for-each (lambda (channel)
              (format #t "  ~a ~a~%"
                      (channel-name channel)
                      (string-take (channel-commit channel) 7))
              (format #t (G_ "    repository URL: ~a~%")
                      (channel-url channel))
              (when (channel-branch channel)
                (format #t (G_ "    branch: ~a~%")
                        (channel-branch channel)))
              (format #t (G_ "    commit: ~a~%")
                      (if (supports-hyperlinks?)
                          (channel-commit-hyperlink channel)
                          (channel-commit channel))))
            channels))

(define %vcs-web-views
  ;; Hard-coded list of host names and corresponding web view URL templates.
  ;; TODO: Allow '.guix-channel' files to specify a URL template.
  (let ((labhub-url (lambda (repository-url commit)
                      (string-append
                       (if (string-suffix? ".git" repository-url)
                           (string-drop-right repository-url 4)
                           repository-url)
                       "/commit/" commit))))
    `(("git.savannah.gnu.org"
       ,(lambda (repository-url commit)
          (string-append (string-replace-substring repository-url
                                                   "/git/" "/cgit/")
                         "/commit/?id=" commit)))
      ("notabug.org" ,labhub-url)
      ("framagit.org" ,labhub-url)
      ("gitlab.com" ,labhub-url)
      ("gitlab.inria.fr" ,labhub-url)
      ("github.com" ,labhub-url))))

(define* (channel-commit-hyperlink channel
                                   #:optional
                                   (commit (channel-commit channel)))
  "Return a hyperlink for COMMIT in CHANNEL, using COMMIT as the hyperlink's
text.  The hyperlink links to a web view of COMMIT, when available."
  (let* ((url  (channel-url channel))
         (uri  (string->uri url))
         (host (and uri (uri-host uri))))
    (if host
        (match (assoc host %vcs-web-views)
          (#f
           commit)
          ((_ template)
           (hyperlink (template url commit) commit)))
        commit)))


;;;
;;; Entry point.
;;;

(define-command (guix-describe . args)
  (synopsis "describe the channel revisions currently used")
  (let* ((opts    (parse-command-line args %options (list %default-options)
                              #:build-options? #f
                              #:argument-handler cons))
         (format  (assq-ref opts 'format))
         (profile (or (assq-ref opts 'profile) (current-profile))))
    (with-error-handling
      (match profile
        (#f
         (match (current-channels)
           (()
            (display-checkout-info format))
           (channels
            (display-profile-info #f format channels))))
        (profile
         ;; For the current profile, resort to 'current-channels', which has a
         ;; fallback to metadata from (guix config) in case PROFILE lacks it.
         (let ((channels (if (and (current-profile)
                                  (string=? profile (current-profile)))
                             (current-channels)
                             (profile-channels profile))))
           (display-profile-info (canonicalize-profile profile)
                                 format channels)))))))