guix-hash.el 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. ;;; guix-hash.el --- Calculate hashes of files -*- lexical-binding: t -*-
  2. ;; Copyright © 2017–2018 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. (require 'guix-utils)
  25. (define-obsolete-variable-alias 'guix-hash-support-dired
  26. 'guix-support-dired "0.4.1")
  27. ;;;###autoload
  28. (defun guix-hash (file &optional format)
  29. "Calculate and copy (put into `kill-ring') the hash of FILE.
  30. If FILE is a directory, calculate its hash recursively excluding
  31. version-controlled files.
  32. Interactively, prompt for FILE (see also `guix-support-dired').
  33. With prefix argument, prompt for FORMAT as well.
  34. See also Info node `(guix) Invoking guix hash'."
  35. (interactive
  36. (list (guix-read-file-name-maybe)
  37. (and current-prefix-arg
  38. (guix-read-hash-format))))
  39. (let* ((file (guix-file-name file))
  40. (args (list :format (or format guix-default-hash-format)))
  41. (args (if (file-directory-p file)
  42. (append '(:recursive? t) args)
  43. args))
  44. (hash (guix-eval-read
  45. (apply #'guix-make-guile-expression
  46. 'file-hash file args))))
  47. (kill-new hash)
  48. (message "Hash of \"%s\" (%s) has been added to the kill ring."
  49. (file-name-nondirectory file)
  50. hash)))
  51. (provide 'guix-hash)
  52. ;;; guix-hash.el ends here