refresh: Update the source code URL.
[jackhill/guix/guix.git] / guix / scripts / describe.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2019 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2018 Oleg Pykhalov <go.wigust@gmail.com>
4 ;;;
5 ;;; This file is part of GNU Guix.
6 ;;;
7 ;;; GNU Guix is free software; you can redistribute it and/or modify it
8 ;;; under the terms of the GNU General Public License as published by
9 ;;; the Free Software Foundation; either version 3 of the License, or (at
10 ;;; your option) any later version.
11 ;;;
12 ;;; GNU Guix is distributed in the hope that it will be useful, but
13 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;;; GNU General Public License for more details.
16 ;;;
17 ;;; You should have received a copy of the GNU General Public License
18 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20 (define-module (guix scripts describe)
21 #:use-module ((guix ui) #:hide (display-profile-content))
22 #:use-module (guix channels)
23 #:use-module (guix scripts)
24 #:use-module (guix describe)
25 #:use-module (guix profiles)
26 #:use-module ((guix scripts pull) #:select (display-profile-content))
27 #:use-module (git)
28 #:use-module (json)
29 #:use-module (srfi srfi-1)
30 #:use-module (srfi srfi-37)
31 #:use-module (ice-9 match)
32 #:autoload (ice-9 pretty-print) (pretty-print)
33 #:export (guix-describe))
34
35 \f
36 ;;;
37 ;;; Command-line options.
38 ;;;
39
40 (define %options
41 ;; Specifications of the command-line options.
42 (list (option '(#\f "format") #t #f
43 (lambda (opt name arg result)
44 (unless (member arg '("human" "channels" "json" "recutils"))
45 (leave (G_ "~a: unsupported output format~%") arg))
46 (alist-cons 'format (string->symbol arg) result)))
47 (option '(#\p "profile") #t #f
48 (lambda (opt name arg result)
49 (alist-cons 'profile (canonicalize-profile arg)
50 result)))
51 (option '(#\h "help") #f #f
52 (lambda args
53 (show-help)
54 (exit 0)))
55 (option '(#\V "version") #f #f
56 (lambda args
57 (show-version-and-exit "guix describe")))))
58
59 (define %default-options
60 ;; Alist of default option values.
61 '((format . human)))
62
63 (define (show-help)
64 (display (G_ "Usage: guix describe [OPTION]...
65 Display information about the channels currently in use.\n"))
66 (display (G_ "
67 -f, --format=FORMAT display information in the given FORMAT"))
68 (display (G_ "
69 -p, --profile=PROFILE display information about PROFILE"))
70 (newline)
71 (display (G_ "
72 -h, --help display this help and exit"))
73 (display (G_ "
74 -V, --version display version information and exit"))
75 (newline)
76 (show-bug-report-information))
77
78 (define (display-package-search-path fmt)
79 "Display GUIX_PACKAGE_PATH, if it is set, according to FMT."
80 (match (getenv "GUIX_PACKAGE_PATH")
81 (#f #t)
82 (string
83 (match fmt
84 ('human
85 (format #t "~%GUIX_PACKAGE_PATH=\"~a\"~%" string))
86 ('channels
87 (format #t (G_ "~%;; warning: GUIX_PACKAGE_PATH=\"~a\"~%")
88 string))
89 (_
90 (warning (G_ "'GUIX_PACKAGE_PATH' is set but it is not captured~%")))))))
91
92 (define (channel->sexp channel)
93 `(channel
94 (name ',(channel-name channel))
95 (url ,(channel-url channel))
96 (commit ,(channel-commit channel))))
97
98 (define (channel->json channel)
99 (scm->json-string `((name . ,(channel-name channel))
100 (url . ,(channel-url channel))
101 (commit . ,(channel-commit channel)))))
102
103 (define (channel->recutils channel port)
104 (format port "name: ~a~%" (channel-name channel))
105 (format port "url: ~a~%" (channel-url channel))
106 (format port "commit: ~a~%" (channel-commit channel)))
107
108 (define (display-checkout-info fmt)
109 "Display information about the current checkout according to FMT, a symbol
110 denoting the requested format. Exit if the current directory does not lie
111 within a Git checkout."
112 (let* ((program (car (command-line)))
113 (directory (catch 'git-error
114 (lambda ()
115 (repository-discover (dirname program)))
116 (lambda (key err)
117 (leave (G_ "failed to determine origin~%")))))
118 (repository (repository-open directory))
119 (head (repository-head repository))
120 (commit (oid->string (reference-target head))))
121 (match fmt
122 ('human
123 (format #t (G_ "Git checkout:~%"))
124 (format #t (G_ " repository: ~a~%") (dirname directory))
125 (format #t (G_ " branch: ~a~%") (reference-shorthand head))
126 (format #t (G_ " commit: ~a~%") commit))
127 ('channels
128 (pretty-print `(list ,(channel->sexp (channel (name 'guix)
129 (url (dirname directory))
130 (commit commit))))))
131 ('json
132 (display (channel->json (channel (name 'guix)
133 (url (dirname directory))
134 (commit commit))))
135 (newline))
136 ('recutils
137 (channel->recutils (channel (name 'guix)
138 (url (dirname directory))
139 (commit commit))
140 (current-output-port))))
141 (display-package-search-path fmt)))
142
143 (define (display-profile-info profile fmt)
144 "Display information about PROFILE, a profile as created by (guix channels),
145 in the format specified by FMT."
146 (define number
147 (generation-number profile))
148
149 (define channels
150 (map (lambda (entry)
151 (match (assq 'source (manifest-entry-properties entry))
152 (('source ('repository ('version 0)
153 ('url url)
154 ('branch branch)
155 ('commit commit)
156 _ ...))
157 (channel (name (string->symbol (manifest-entry-name entry)))
158 (url url)
159 (commit commit)))
160
161 ;; Pre-0.15.0 Guix does not provide that information,
162 ;; so there's not much we can do in that case.
163 (_ (channel (name 'guix)
164 (url "?")
165 (commit "?")))))
166
167 ;; Show most recently installed packages last.
168 (reverse
169 (manifest-entries
170 (profile-manifest
171 (if (zero? number)
172 profile
173 (generation-file-name profile number)))))))
174
175 (match fmt
176 ('human
177 (display-profile-content profile number))
178 ('channels
179 (pretty-print `(list ,@(map channel->sexp channels))))
180 ('json
181 (format #t "[~a]~%" (string-join (map channel->json channels) ",")))
182 ('recutils
183 (format #t "~{~a~%~}"
184 (map (lambda (channel)
185 (with-output-to-string
186 (lambda ()
187 (channel->recutils channel (current-output-port)))))
188 channels))))
189 (display-package-search-path fmt))
190
191 \f
192 ;;;
193 ;;; Entry point.
194 ;;;
195
196 (define (guix-describe . args)
197 (let* ((opts (args-fold* args %options
198 (lambda (opt name arg result)
199 (leave (G_ "~A: unrecognized option~%")
200 name))
201 cons
202 %default-options))
203 (format (assq-ref opts 'format))
204 (profile (or (assq-ref opts 'profile) (current-profile))))
205 (with-error-handling
206 (match profile
207 (#f
208 (display-checkout-info format))
209 (profile
210 (display-profile-info (canonicalize-profile profile) format))))))