unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
From: zimoun <zimon.toutoune@gmail.com>
To: 51307@debbugs.gnu.org
Cc: zimoun <zimon.toutoune@gmail.com>
Subject: [bug#51307] [PATCH 2/2] scripts: hash: Support file or package.
Date: Wed, 20 Oct 2021 18:54:35 +0200	[thread overview]
Message-ID: <20211020165435.3358398-2-zimon.toutoune@gmail.com> (raw)
In-Reply-To: <20211020165435.3358398-1-zimon.toutoune@gmail.com>

* guix/scripts/hash.scm (guix-hash)[package?]: New procedure.
[hash-to-display]: Use it.
* tests/guix-hash.scm: New test.
---
 guix/scripts/hash.scm | 19 +++++++++++++++++--
 tests/guix-hash.sh    | 10 ++++++++++
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/guix/scripts/hash.scm b/guix/scripts/hash.scm
index f3363549d3..4f0d41629f 100644
--- a/guix/scripts/hash.scm
+++ b/guix/scripts/hash.scm
@@ -22,6 +22,9 @@
 
 (define-module (guix scripts hash)
   #:use-module (gcrypt hash)
+  #:use-module ((gnu packages) #:select (find-best-packages-by-name))
+  #:use-module (guix packages)
+  #:use-module ((guix utils) #:select (package-name->name+version))
   #:use-module (guix serialization)
   #:use-module (guix ui)
   #:use-module (guix scripts)
@@ -48,8 +51,8 @@ (define %default-options
     (hash-algorithm . ,(hash-algorithm sha256))))
 
 (define (show-help)
-  (display (G_ "Usage: guix hash [OPTION] FILE
-Return the cryptographic hash of FILE.\n"))
+  (display (G_ "Usage: guix hash [OPTION] FILE-OR-PACKAGE
+Return the cryptographic hash of FILE-OR-PACKAGE.\n"))
   (newline)
   (display (G_ "\
 Supported formats: 'base64', 'nix-base32' (default), 'base32',
@@ -141,6 +144,12 @@ (define (directory? file)
       ((directory) #t)
       (else #f)))
 
+  (define (package? spec)
+    (let-values (((name version) (package-name->name+version spec)))
+      (match (find-best-packages-by-name name version)
+        ((package) package)
+        (_ #f))))
+
   (let* ((opts (parse-options))
          (args (filter-map (match-lambda
                             (('argument . value)
@@ -182,6 +191,12 @@ (define (hash-to-display thing)
         ("-" (with-error-handling
                (fmt (port-hash (assoc-ref opts 'hash-algorithm)
                                (current-input-port)))))
+        ((? package? spec)
+         (let* ((package (package? spec))
+                (origin (package-source package))
+                (content-hash (origin-hash origin))
+                (hash (content-hash-value content-hash)))
+           (fmt hash)))
         (x
          (leave (G_ "wrong argument~%")))))
 
diff --git a/tests/guix-hash.sh b/tests/guix-hash.sh
index c4461fa955..41bd2b1588 100644
--- a/tests/guix-hash.sh
+++ b/tests/guix-hash.sh
@@ -1,6 +1,7 @@
 # GNU Guix --- Functional package management for GNU
 # Copyright © 2013, 2014, 2016, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
 # Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
+# Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
 #
 # This file is part of GNU Guix.
 #
@@ -65,3 +66,12 @@ test `guix hash -r $tmpdir -x` = 10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g
 # Without '-r', this should fail.
 ! guix hash "$tmpdir"
 
+cat > "$tmpdir/foo.scm"<<EOF
+(use-modules (guix packages)
+             (gnu packages base)
+             (guix base16))
+(format #t "~a~%"
+        (bytevector->base16-string
+         (content-hash-value (origin-hash (package-source hello)))))
+EOF
+test `guix hash hello -f base16` = `guix repl -- $tmpdir/foo.scm`
-- 
2.32.0





  reply	other threads:[~2021-10-20 16:55 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-20 16:50 [bug#51307] [PATCH 0/2] guix hash: eases conversion zimoun
2021-10-20 16:54 ` [bug#51307] [PATCH 1/2] scripts: hash: Improve error handling zimoun
2021-10-20 16:54   ` zimoun [this message]
2021-10-30 14:48     ` [bug#51307] [PATCH 0/2] guix hash: eases conversion Ludovic Courtès
2021-10-30 15:34       ` zimoun
2021-10-30 14:46   ` Ludovic Courtès
2021-10-30 15:40     ` zimoun
2021-10-31 13:43       ` Ludovic Courtès
2021-10-30 14:53 ` Ludovic Courtès
2021-10-30 15:19   ` zimoun
2021-10-30 15:24     ` zimoun
2021-10-31 14:03     ` Ludovic Courtès
2021-11-09  9:18       ` zimoun
2021-10-31 14:48     ` [bug#51307] Content hashes and file tree serialization methods Ludovic Courtès
2021-11-18  0:29       ` zimoun
2021-11-18  0:20 ` [bug#51307] [PATCH v2 0/3] scripts: hash: Several files and serializer zimoun
2021-11-18  0:20   ` [bug#51307] [PATCH v2 1/3] scripts: hash: Support several files zimoun
2021-12-17 16:17     ` Ludovic Courtès
2021-11-18  0:20   ` [bug#51307] [PATCH v2 2/3] scripts: hash: Add 'serializer' option zimoun
2021-12-17 16:17     ` Ludovic Courtès
2021-11-18  0:20   ` [bug#51307] [PATCH v2 3/3] scripts: hash: Add git serializer zimoun
     [not found]     ` <87bl1bjsxf.fsf@gnu.org>
     [not found]       ` <CAJ3okZ00+BSw=nuhGP8NTwU8ZmitzrfFzTw0WecsOB9rZ+hG_g@mail.gmail.com>
2021-12-21  9:09         ` Ludovic Courtès
2021-12-15  8:06   ` [bug#51307] [PATCH v2 0/3] scripts: hash: Several files and serializer zimoun
2021-12-15 10:05     ` Ludovic Courtès

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211020165435.3358398-2-zimon.toutoune@gmail.com \
    --to=zimon.toutoune@gmail.com \
    --cc=51307@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).