gnu: surgescript: Update to 0.5.4.4.
[jackhill/guix/guix.git] / guix / scripts / hash.scm
CommitLineData
6c365eca 1;;; GNU Guix --- Functional package management for GNU
18ae1ec3 2;;; Copyright © 2012, 2013, 2014, 2016, 2017, 2020 Ludovic Courtès <ludo@gnu.org>
6c365eca 3;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
392a4e12 4;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
e91152e9 5;;; Copyright © 2018 Tim Gesthuizen <tim.gesthuizen@yahoo.de>
6c365eca
NK
6;;;
7;;; This file is part of GNU Guix.
8;;;
9;;; GNU Guix is free software; you can redistribute it and/or modify it
10;;; under the terms of the GNU General Public License as published by
11;;; the Free Software Foundation; either version 3 of the License, or (at
12;;; your option) any later version.
13;;;
14;;; GNU Guix is distributed in the hope that it will be useful, but
15;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;;; GNU General Public License for more details.
18;;;
19;;; You should have received a copy of the GNU General Public License
20;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21
22(define-module (guix scripts hash)
ca719424 23 #:use-module (gcrypt hash)
0363991a 24 #:use-module (guix serialization)
72626a71 25 #:use-module (guix ui)
88981dd3 26 #:use-module (guix scripts)
4c0c4db0 27 #:use-module (guix base16)
0e4e9c8e
LC
28 #:use-module (guix base32)
29 #:autoload (guix base64) (base64-encode)
2535635f 30 #:use-module (ice-9 binary-ports)
72626a71
LC
31 #:use-module (rnrs files)
32 #:use-module (ice-9 match)
33 #:use-module (srfi srfi-1)
3140f2df 34 #:use-module (srfi srfi-11)
72626a71
LC
35 #:use-module (srfi srfi-26)
36 #:use-module (srfi srfi-37)
37 #:export (guix-hash))
6c365eca
NK
38
39\f
40;;;
41;;; Command-line options.
42;;;
43
44(define %default-options
45 ;; Alist of default option values.
18ae1ec3
LC
46 `((format . ,bytevector->nix-base32-string)
47 (hash-algorithm . ,(hash-algorithm sha256))))
6c365eca
NK
48
49(define (show-help)
e91152e9 50 (display (G_ "Usage: guix hash [OPTION] FILE
0e4e9c8e
LC
51Return the cryptographic hash of FILE.\n"))
52 (newline)
53 (display (G_ "\
54Supported formats: 'base64', 'nix-base32' (default), 'base32',
55and 'base16' ('hex' and 'hexadecimal' can be used as well).\n"))
69daee23 56 (format #t (G_ "
392a4e12 57 -x, --exclude-vcs exclude version control directories"))
69daee23 58 (format #t (G_ "
18ae1ec3
LC
59 -H, --hash=ALGORITHM use the given hash ALGORITHM"))
60 (format #t (G_ "
6c365eca 61 -f, --format=FMT write the hash in the given format"))
69daee23 62 (format #t (G_ "
3140f2df 63 -r, --recursive compute the hash on FILE recursively"))
6c365eca 64 (newline)
69daee23 65 (display (G_ "
6c365eca 66 -h, --help display this help and exit"))
69daee23 67 (display (G_ "
6c365eca
NK
68 -V, --version display version information and exit"))
69 (newline)
70 (show-bug-report-information))
71
72(define %options
73 ;; Specification of the command-line options.
392a4e12
JN
74 (list (option '(#\x "exclude-vcs") #f #f
75 (lambda (opt name arg result)
76 (alist-cons 'exclude-vcs? #t result)))
18ae1ec3
LC
77 (option '(#\H "hash") #t #f
78 (lambda (opt name arg result)
79 (match (lookup-hash-algorithm (string->symbol arg))
80 (#f
81 (leave (G_ "~a: unknown hash algorithm~%") arg))
82 (algo
83 (alist-cons 'hash-algorithm algo result)))))
392a4e12 84 (option '(#\f "format") #t #f
6c365eca
NK
85 (lambda (opt name arg result)
86 (define fmt-proc
87 (match arg
0e4e9c8e
LC
88 ("base64"
89 base64-encode)
6c365eca
NK
90 ("nix-base32"
91 bytevector->nix-base32-string)
92 ("base32"
93 bytevector->base32-string)
94 ((or "base16" "hex" "hexadecimal")
95 bytevector->base16-string)
96 (x
69daee23 97 (leave (G_ "unsupported hash format: ~a~%")
6c365eca
NK
98 arg))))
99
100 (alist-cons 'format fmt-proc
101 (alist-delete 'format result))))
3140f2df
LC
102 (option '(#\r "recursive") #f #f
103 (lambda (opt name arg result)
104 (alist-cons 'recursive? #t result)))
6c365eca
NK
105 (option '(#\h "help") #f #f
106 (lambda args
107 (show-help)
108 (exit 0)))
109 (option '(#\V "version") #f #f
110 (lambda args
e91152e9 111 (show-version-and-exit "guix hash")))))
6c365eca
NK
112
113
114\f
115;;;
116;;; Entry point.
117;;;
118
3794ce93
LC
119(define-command (guix-hash . args)
120 (category packaging)
121 (synopsis "compute the cryptographic hash of a file")
122
6c365eca
NK
123 (define (parse-options)
124 ;; Return the alist of option values.
a1ff7e1d
LC
125 (parse-command-line args %options (list %default-options)
126 #:build-options? #f))
6c365eca 127
392a4e12
JN
128 (define (vcs-file? file stat)
129 (case (stat:type stat)
130 ((directory)
131 (member (basename file) '(".bzr" ".git" ".hg" ".svn" "CVS")))
eeb05e87
LC
132 ((regular)
133 ;; Git sub-modules have a '.git' file that is a regular text file.
134 (string=? (basename file) ".git"))
392a4e12
JN
135 (else
136 #f)))
137
ccbce848
LC
138 (let* ((opts (parse-options))
139 (args (filter-map (match-lambda
140 (('argument . value)
141 value)
142 (_ #f))
143 (reverse opts)))
392a4e12
JN
144 (fmt (assq-ref opts 'format))
145 (select? (if (assq-ref opts 'exclude-vcs?)
146 (negate vcs-file?)
147 (const #t))))
ccbce848 148
3140f2df
LC
149 (define (file-hash file)
150 ;; Compute the hash of FILE.
151 ;; Catch and gracefully report possible '&nar-error' conditions.
152 (with-error-handling
153 (if (assoc-ref opts 'recursive?)
154 (let-values (((port get-hash) (open-sha256-port)))
392a4e12 155 (write-file file port #:select? select?)
2535635f 156 (force-output port)
3140f2df 157 (get-hash))
343dc117 158 (match file
18ae1ec3
LC
159 ("-" (port-hash (assoc-ref opts 'hash-algorithm)
160 (current-input-port)))
161 (_ (call-with-input-file file
162 (cute port-hash (assoc-ref opts 'hash-algorithm)
163 <>)))))))
3140f2df 164
ccbce848
LC
165 (match args
166 ((file)
167 (catch 'system-error
168 (lambda ()
3140f2df 169 (format #t "~a~%" (fmt (file-hash file))))
ccbce848 170 (lambda args
69daee23 171 (leave (G_ "~a~%")
ccbce848 172 (strerror (system-error-errno args))))))
e465d9e1 173 (x
69daee23 174 (leave (G_ "wrong number of arguments~%"))))))