ui: Rename '_' to 'G_'.
[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>
6c365eca
NK
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)
72626a71
LC
22 #:use-module (guix base32)
23 #:use-module (guix 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)
2535635f 28 #:use-module (ice-9 binary-ports)
72626a71
LC
29 #:use-module (rnrs files)
30 #:use-module (ice-9 match)
31 #:use-module (srfi srfi-1)
3140f2df 32 #:use-module (srfi srfi-11)
72626a71
LC
33 #:use-module (srfi srfi-26)
34 #:use-module (srfi srfi-37)
35 #:export (guix-hash))
6c365eca
NK
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)
69daee23 47 (display (G_ "Usage: guix hash [OPTION] FILE
6c365eca
NK
48Return the cryptographic hash of FILE.
49
3140f2df
LC
50Supported formats: 'nix-base32' (default), 'base32', and 'base16' ('hex'
51and 'hexadecimal' can be used as well).\n"))
69daee23 52 (format #t (G_ "
392a4e12 53 -x, --exclude-vcs exclude version control directories"))
69daee23 54 (format #t (G_ "
6c365eca 55 -f, --format=FMT write the hash in the given format"))
69daee23 56 (format #t (G_ "
3140f2df 57 -r, --recursive compute the hash on FILE recursively"))
6c365eca 58 (newline)
69daee23 59 (display (G_ "
6c365eca 60 -h, --help display this help and exit"))
69daee23 61 (display (G_ "
6c365eca
NK
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.
392a4e12
JN
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
6c365eca
NK
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
69daee23 82 (leave (G_ "unsupported hash format: ~a~%")
6c365eca
NK
83 arg))))
84
85 (alist-cons 'format fmt-proc
86 (alist-delete 'format result))))
3140f2df
LC
87 (option '(#\r "recursive") #f #f
88 (lambda (opt name arg result)
89 (alist-cons 'recursive? #t result)))
6c365eca
NK
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.
a5975ced
LC
107 (args-fold* args %options
108 (lambda (opt name arg result)
69daee23 109 (leave (G_ "unrecognized option: ~a~%")
a5975ced
LC
110 name))
111 (lambda (arg result)
112 (alist-cons 'argument arg result))
113 %default-options))
6c365eca 114
392a4e12
JN
115 (define (vcs-file? file stat)
116 (case (stat:type stat)
117 ((directory)
118 (member (basename file) '(".bzr" ".git" ".hg" ".svn" "CVS")))
eeb05e87
LC
119 ((regular)
120 ;; Git sub-modules have a '.git' file that is a regular text file.
121 (string=? (basename file) ".git"))
392a4e12
JN
122 (else
123 #f)))
124
ccbce848
LC
125 (let* ((opts (parse-options))
126 (args (filter-map (match-lambda
127 (('argument . value)
128 value)
129 (_ #f))
130 (reverse opts)))
392a4e12
JN
131 (fmt (assq-ref opts 'format))
132 (select? (if (assq-ref opts 'exclude-vcs?)
133 (negate vcs-file?)
134 (const #t))))
ccbce848 135
3140f2df
LC
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)))
392a4e12 142 (write-file file port #:select? select?)
2535635f 143 (force-output port)
3140f2df 144 (get-hash))
343dc117
LC
145 (match file
146 ("-" (port-sha256 (current-input-port)))
147 (_ (call-with-input-file file port-sha256))))))
3140f2df 148
ccbce848
LC
149 (match args
150 ((file)
151 (catch 'system-error
152 (lambda ()
3140f2df 153 (format #t "~a~%" (fmt (file-hash file))))
ccbce848 154 (lambda args
69daee23 155 (leave (G_ "~a~%")
ccbce848 156 (strerror (system-error-errno args))))))
e465d9e1 157 (x
69daee23 158 (leave (G_ "wrong number of arguments~%"))))))