guix-hash.el 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ;;; guix-hash.el --- Calculate hashes of files -*- lexical-binding: t -*-
  2. ;; Copyright © 2017 Alex Kost <alezost@gmail.com>
  3. ;; This file is part of Emacs-Guix.
  4. ;; Emacs-Guix is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;;
  9. ;; Emacs-Guix is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with Emacs-Guix. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This file provides the code to determine SHA256 hashes of files (these
  18. ;; hashes are used in Guix packages).
  19. ;;; Code:
  20. (require 'guix-help-vars)
  21. (require 'guix-read)
  22. (require 'guix-repl)
  23. (require 'guix-guile)
  24. (defcustom guix-hash-support-dired t
  25. "Whether '\\[guix-hash]' supports `dired-mode' or not.
  26. If non-nil, do not prompt for a file name in `dired-mode' and use
  27. the file on the current line instead.
  28. If nil, always prompt for a file name."
  29. :type 'boolean
  30. :group 'guix)
  31. (declare-function dired-get-filename "dired" t)
  32. ;;;###autoload
  33. (defun guix-hash (file &optional format)
  34. "Calculate and copy (put into `kill-ring') the hash of FILE.
  35. If FILE is a directory, calculate its hash recursively excluding
  36. version-controlled files.
  37. Interactively, prompt for FILE (see also
  38. `guix-hash-support-dired' variable). With prefix argument,
  39. prompt for FORMAT as well.
  40. See also Info node `(guix) Invoking guix hash'."
  41. (interactive
  42. (list (if (and guix-hash-support-dired
  43. (derived-mode-p 'dired-mode))
  44. (dired-get-filename)
  45. (read-file-name "File: "))
  46. (and current-prefix-arg
  47. (guix-read-hash-format))))
  48. (let* ((file (expand-file-name file))
  49. (args (list :format (or format guix-default-hash-format)))
  50. (args (if (file-directory-p file)
  51. (append '(:recursive? t) args)
  52. args))
  53. (hash (guix-eval-read
  54. (apply #'guix-make-guile-expression
  55. 'file-hash file args))))
  56. (kill-new hash)
  57. (message "Hash of \"%s\" (%s) has been added to the kill ring."
  58. (file-name-nondirectory (directory-file-name file))
  59. hash)))
  60. (provide 'guix-hash)
  61. ;;; guix-hash.el ends here