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