Merge branch 'master' into core-updates
[jackhill/guix/guix.git] / guix / scripts / hash.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2012, 2013, 2014, 2016 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 utils)
28 #:use-module (rnrs io 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 (_ "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 (_ "
53 -x, --exclude-vcs exclude version control directories"))
54 (format #t (_ "
55 -f, --format=FMT write the hash in the given format"))
56 (format #t (_ "
57 -r, --recursive compute the hash on FILE recursively"))
58 (newline)
59 (display (_ "
60 -h, --help display this help and exit"))
61 (display (_ "
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 (_ "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 (_ "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 (else
120 #f)))
121
122 (let* ((opts (parse-options))
123 (args (filter-map (match-lambda
124 (('argument . value)
125 value)
126 (_ #f))
127 (reverse opts)))
128 (fmt (assq-ref opts 'format))
129 (select? (if (assq-ref opts 'exclude-vcs?)
130 (negate vcs-file?)
131 (const #t))))
132
133 (define (file-hash file)
134 ;; Compute the hash of FILE.
135 ;; Catch and gracefully report possible '&nar-error' conditions.
136 (with-error-handling
137 (if (assoc-ref opts 'recursive?)
138 (let-values (((port get-hash) (open-sha256-port)))
139 (write-file file port #:select? select?)
140 (flush-output-port port)
141 (get-hash))
142 (call-with-input-file file port-sha256))))
143
144 (match args
145 ((file)
146 (catch 'system-error
147 (lambda ()
148 (format #t "~a~%" (fmt (file-hash file))))
149 (lambda args
150 (leave (_ "~a~%")
151 (strerror (system-error-errno args))))))
152 (x
153 (leave (_ "wrong number of arguments~%"))))))