repl: Add "-q".
[jackhill/guix/guix.git] / guix / scripts / hash.scm
CommitLineData
6c365eca 1;;; GNU Guix --- Functional package management for GNU
eeb05e87 2;;; Copyright © 2012, 2013, 2014, 2016, 2017 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)
72626a71 23 #:use-module (guix base32)
ca719424 24 #:use-module (gcrypt hash)
0363991a 25 #:use-module (guix serialization)
72626a71 26 #:use-module (guix ui)
88981dd3 27 #:use-module (guix scripts)
4c0c4db0 28 #:use-module (guix base16)
2535635f 29 #:use-module (ice-9 binary-ports)
72626a71
LC
30 #:use-module (rnrs files)
31 #:use-module (ice-9 match)
32 #:use-module (srfi srfi-1)
3140f2df 33 #:use-module (srfi srfi-11)
72626a71
LC
34 #:use-module (srfi srfi-26)
35 #:use-module (srfi srfi-37)
36 #:export (guix-hash))
6c365eca
NK
37
38\f
39;;;
40;;; Command-line options.
41;;;
42
43(define %default-options
44 ;; Alist of default option values.
45 `((format . ,bytevector->nix-base32-string)))
46
47(define (show-help)
e91152e9 48 (display (G_ "Usage: guix hash [OPTION] FILE
6c365eca
NK
49Return the cryptographic hash of FILE.
50
3140f2df
LC
51Supported formats: 'nix-base32' (default), 'base32', and 'base16' ('hex'
52and 'hexadecimal' can be used as well).\n"))
69daee23 53 (format #t (G_ "
392a4e12 54 -x, --exclude-vcs exclude version control directories"))
69daee23 55 (format #t (G_ "
6c365eca 56 -f, --format=FMT write the hash in the given format"))
69daee23 57 (format #t (G_ "
3140f2df 58 -r, --recursive compute the hash on FILE recursively"))
6c365eca 59 (newline)
69daee23 60 (display (G_ "
6c365eca 61 -h, --help display this help and exit"))
69daee23 62 (display (G_ "
6c365eca
NK
63 -V, --version display version information and exit"))
64 (newline)
65 (show-bug-report-information))
66
67(define %options
68 ;; Specification of the command-line options.
392a4e12
JN
69 (list (option '(#\x "exclude-vcs") #f #f
70 (lambda (opt name arg result)
71 (alist-cons 'exclude-vcs? #t result)))
72 (option '(#\f "format") #t #f
6c365eca
NK
73 (lambda (opt name arg result)
74 (define fmt-proc
75 (match arg
76 ("nix-base32"
77 bytevector->nix-base32-string)
78 ("base32"
79 bytevector->base32-string)
80 ((or "base16" "hex" "hexadecimal")
81 bytevector->base16-string)
82 (x
69daee23 83 (leave (G_ "unsupported hash format: ~a~%")
6c365eca
NK
84 arg))))
85
86 (alist-cons 'format fmt-proc
87 (alist-delete 'format result))))
3140f2df
LC
88 (option '(#\r "recursive") #f #f
89 (lambda (opt name arg result)
90 (alist-cons 'recursive? #t result)))
6c365eca
NK
91 (option '(#\h "help") #f #f
92 (lambda args
93 (show-help)
94 (exit 0)))
95 (option '(#\V "version") #f #f
96 (lambda args
e91152e9 97 (show-version-and-exit "guix hash")))))
6c365eca
NK
98
99
100\f
101;;;
102;;; Entry point.
103;;;
104
105(define (guix-hash . args)
106 (define (parse-options)
107 ;; Return the alist of option values.
a1ff7e1d
LC
108 (parse-command-line args %options (list %default-options)
109 #:build-options? #f))
6c365eca 110
392a4e12
JN
111 (define (vcs-file? file stat)
112 (case (stat:type stat)
113 ((directory)
114 (member (basename file) '(".bzr" ".git" ".hg" ".svn" "CVS")))
eeb05e87
LC
115 ((regular)
116 ;; Git sub-modules have a '.git' file that is a regular text file.
117 (string=? (basename file) ".git"))
392a4e12
JN
118 (else
119 #f)))
120
ccbce848
LC
121 (let* ((opts (parse-options))
122 (args (filter-map (match-lambda
123 (('argument . value)
124 value)
125 (_ #f))
126 (reverse opts)))
392a4e12
JN
127 (fmt (assq-ref opts 'format))
128 (select? (if (assq-ref opts 'exclude-vcs?)
129 (negate vcs-file?)
130 (const #t))))
ccbce848 131
3140f2df
LC
132 (define (file-hash file)
133 ;; Compute the hash of FILE.
134 ;; Catch and gracefully report possible '&nar-error' conditions.
135 (with-error-handling
136 (if (assoc-ref opts 'recursive?)
137 (let-values (((port get-hash) (open-sha256-port)))
392a4e12 138 (write-file file port #:select? select?)
2535635f 139 (force-output port)
3140f2df 140 (get-hash))
343dc117
LC
141 (match file
142 ("-" (port-sha256 (current-input-port)))
143 (_ (call-with-input-file file port-sha256))))))
3140f2df 144
ccbce848
LC
145 (match args
146 ((file)
147 (catch 'system-error
148 (lambda ()
3140f2df 149 (format #t "~a~%" (fmt (file-hash file))))
ccbce848 150 (lambda args
69daee23 151 (leave (G_ "~a~%")
ccbce848 152 (strerror (system-error-errno args))))))
e465d9e1 153 (x
69daee23 154 (leave (G_ "wrong number of arguments~%"))))))