From patchwork Thu Nov 18 00:20:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Simon Tournier X-Patchwork-Id: 34582 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id C6F4B27BBE3; Thu, 18 Nov 2021 00:21:20 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2, SPF_HELO_PASS,T_DKIM_INVALID,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 58C4127BBE1 for ; Thu, 18 Nov 2021 00:21:20 +0000 (GMT) Received: from localhost ([::1]:51120 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mnVB1-0003hi-HM for patchwork@mira.cbaines.net; Wed, 17 Nov 2021 19:21:19 -0500 Received: from eggs.gnu.org ([209.51.188.92]:45192) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mnVAk-0003gg-6j for guix-patches@gnu.org; Wed, 17 Nov 2021 19:21:03 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:50830) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mnVAj-00088Y-Ua for guix-patches@gnu.org; Wed, 17 Nov 2021 19:21:01 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mnVAj-00070T-R2 for guix-patches@gnu.org; Wed, 17 Nov 2021 19:21:01 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#51307] [PATCH v2 1/3] scripts: hash: Support several files. Resent-From: zimoun Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 18 Nov 2021 00:21:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 51307 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 51307@debbugs.gnu.org Cc: ludo@gnu.org, zimoun Received: via spool by 51307-submit@debbugs.gnu.org id=B51307.163719484726865 (code B ref 51307); Thu, 18 Nov 2021 00:21:01 +0000 Received: (at 51307) by debbugs.gnu.org; 18 Nov 2021 00:20:47 +0000 Received: from localhost ([127.0.0.1]:34137 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mnVAU-0006zA-Rs for submit@debbugs.gnu.org; Wed, 17 Nov 2021 19:20:47 -0500 Received: from mail-wm1-f44.google.com ([209.85.128.44]:43932) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mnVAS-0006yf-1q for 51307@debbugs.gnu.org; Wed, 17 Nov 2021 19:20:45 -0500 Received: by mail-wm1-f44.google.com with SMTP id 67-20020a1c1946000000b0030d4c90fa87so3433710wmz.2 for <51307@debbugs.gnu.org>; Wed, 17 Nov 2021 16:20:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=AsdNvgj5R7rdm45aQhwuhx082GGMq4PMmw6rHAjB99k=; b=YDc5ZNT6ah/qU09SQKpRm/41HdBk7yUxQjgy/CKze0ZJWpYY7SCeYdh30SpkSHbm+2 zRIJWucwgC/cIuUN2Py+fBBxk/enPhJ3uvWKBs1UpTPpSU2wr2wNrk7tGd/wPF/WRBOJ mhX+jfDVdSEe0dSl6jYJCNoOr4WVKkrNjC6alVVMbv6IAobx2UbdsRBdZoceGBy3FbsW ScdypyJU5kbsvK7FN8oAkCNZSBAMCjlOmHuaTw7TwWY+XLIM0iRf40+wldFNXg6HXoEs RZH1W26Pf3hhQssH3rzpH9LK6JGt+9CwLktaCCK67OYedzfI7x4YuJaCjFG800xU+J92 rPjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=AsdNvgj5R7rdm45aQhwuhx082GGMq4PMmw6rHAjB99k=; b=1ZejHykEV5Q4fRK3gZgupyMszC8q6nFNHGzsIj8JSLewDb4Ydb8AAm512+XfUO4UXH ddzBVttRZTJs+ZT1BsVQEcfOIS5tWbxcialpVCMZoh4unFHSJaIbZ8Z8inKHwteAVvz5 y1EfPmhOB1RiqixLVPxYtlXyCpllH5Hb/3xLWXFGae5WkDSDyDbwqk463l5nhVRX36Au dXTgAZq4MrFgUEuKNfTyTqcDr45rGTewTlk8Y2GjCPg1QmoVN+Wi9AcLK0LgVdoHzx3d U+iAptfWCft3WQ83piivR+uRUHONNsJk0fXoxc05B0pgmw92rbLh3m1GwDO4LNhLLkPp IgLA== X-Gm-Message-State: AOAM530dEdGPwhK2jmCYixEw/hGHoCHrfnpT2/wcsALydOq+RnYgSgCA m8cSBDJq5/uUtrhH3WsN4xlXA6I4ap0= X-Google-Smtp-Source: ABdhPJxyVzW/cGvqNK9Fs9CSmhEzi8p2kXCAH9j+4FKwbTn3qWUm5w22eOUkPhkqbxUizvL6Te4gOQ== X-Received: by 2002:a05:600c:2dc1:: with SMTP id e1mr4652486wmh.170.1637194838264; Wed, 17 Nov 2021 16:20:38 -0800 (PST) Received: from localhost.localdomain ([193.48.40.117]) by smtp.gmail.com with ESMTPSA id y7sm1255927wrw.55.2021.11.17.16.20.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Nov 2021 16:20:37 -0800 (PST) From: zimoun Date: Thu, 18 Nov 2021 01:20:21 +0100 Message-Id: <20211118002023.3323307-2-zimon.toutoune@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211118002023.3323307-1-zimon.toutoune@gmail.com> References: <20211118002023.3323307-1-zimon.toutoune@gmail.com> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * guix/scripts/hash.scm (guix-hash): Allow several files. [file-hash]: Catch system-error. [formatted-hash]: New procedure. --- guix/scripts/hash.scm | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/guix/scripts/hash.scm b/guix/scripts/hash.scm index b8622373cc..12f542929b 100644 --- a/guix/scripts/hash.scm +++ b/guix/scripts/hash.scm @@ -3,6 +3,7 @@ ;;; Copyright © 2013 Nikita Karetnikov ;;; Copyright © 2016 Jan Nieuwenhuizen ;;; Copyright © 2018 Tim Gesthuizen +;;; Copyright © 2021 Simon Tournier ;;; ;;; This file is part of GNU Guix. ;;; @@ -149,27 +150,31 @@ (define (vcs-file? file stat) (define (file-hash file) ;; Compute the hash of FILE. ;; Catch and gracefully report possible '&nar-error' conditions. - (with-error-handling - (if (assoc-ref opts 'recursive?) + (if (assoc-ref opts 'recursive?) + (with-error-handling (let-values (((port get-hash) (open-hash-port (assoc-ref opts 'hash-algorithm)))) (write-file file port #:select? select?) (force-output port) - (get-hash)) - (match file - ("-" (port-hash (assoc-ref opts 'hash-algorithm) - (current-input-port))) - (_ (call-with-input-file file - (cute port-hash (assoc-ref opts 'hash-algorithm) - <>))))))) + (get-hash))) + (catch 'system-error + (lambda _ + (call-with-input-file file + (cute port-hash (assoc-ref opts 'hash-algorithm) + <>))) + (lambda args + (leave (G_ "~a ~a~%") + file + (strerror (system-error-errno args))))))) - (match args - ((file) - (catch 'system-error - (lambda () - (format #t "~a~%" (fmt (file-hash file)))) - (lambda args - (leave (G_ "~a~%") - (strerror (system-error-errno args)))))) - (x - (leave (G_ "wrong number of arguments~%")))))) + (define (formatted-hash thing) + (match thing + ("-" (with-error-handling + (fmt (port-hash (assoc-ref opts 'hash-algorithm) + (current-input-port))))) + (_ + (fmt (file-hash thing))))) + + (for-each + (compose (cute format #t "~a~%" <>) formatted-hash) + args))) From patchwork Thu Nov 18 00:20:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Simon Tournier X-Patchwork-Id: 34583 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 66E9F27BBE3; Thu, 18 Nov 2021 00:21:24 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2, SPF_HELO_PASS,T_DKIM_INVALID,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id C8FB227BBE1 for ; Thu, 18 Nov 2021 00:21:23 +0000 (GMT) Received: from localhost ([::1]:51214 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mnVB4-0003lX-Ut for patchwork@mira.cbaines.net; Wed, 17 Nov 2021 19:21:23 -0500 Received: from eggs.gnu.org ([209.51.188.92]:45194) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mnVAm-0003gr-5N for guix-patches@gnu.org; Wed, 17 Nov 2021 19:21:04 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:50832) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mnVAl-00088e-2R for guix-patches@gnu.org; Wed, 17 Nov 2021 19:21:03 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mnVAk-00070h-LO for guix-patches@gnu.org; Wed, 17 Nov 2021 19:21:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#51307] [PATCH v2 2/3] scripts: hash: Add 'serializer' option. Resent-From: zimoun Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 18 Nov 2021 00:21:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 51307 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 51307@debbugs.gnu.org Cc: ludo@gnu.org, zimoun Received: via spool by 51307-submit@debbugs.gnu.org id=B51307.163719485026885 (code B ref 51307); Thu, 18 Nov 2021 00:21:02 +0000 Received: (at 51307) by debbugs.gnu.org; 18 Nov 2021 00:20:50 +0000 Received: from localhost ([127.0.0.1]:34141 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mnVAX-0006zS-KU for submit@debbugs.gnu.org; Wed, 17 Nov 2021 19:20:50 -0500 Received: from mail-wm1-f47.google.com ([209.85.128.47]:40566) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mnVAS-0006yg-Aw for 51307@debbugs.gnu.org; Wed, 17 Nov 2021 19:20:46 -0500 Received: by mail-wm1-f47.google.com with SMTP id az33-20020a05600c602100b00333472fef04so6184721wmb.5 for <51307@debbugs.gnu.org>; Wed, 17 Nov 2021 16:20:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=cY2sj5YyVxW04ul2pRGYBDOUoW+p8Rb6I9dQaIubgiU=; b=mtFnMqTC5dLvlS7sLv464bD26ruJGgx+twwB9ufpPDolUi6K8xfUTXeRxOU0+Af0AB Tb30MnAuEO2tx0FmPjcQOn559pjlN+ih+DDplJB2JVYk1iLzzsPpCyYVRIMFVHiA2aIp wQRx/yCoqhzALKMZubHl12YSqOt8UnSCE0VDFHICxlEF0w1LbY9Yu54XD0zW8skXDfT2 OYdPO9GQcuGx8ASXfbBk58k4fXICL+312/HiOnShHkfcEMwdFvpcoc1E70+VBPIZUqtg jM+iRcNTkliop3DCnpXhTsvjDxsvgXonsv70/c5RhmZofRoeD2IMwR4Ki0DSXwp0gTc3 5ipw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cY2sj5YyVxW04ul2pRGYBDOUoW+p8Rb6I9dQaIubgiU=; b=t7P1Br90aDaD6297ArJtSofiCMzvrOKUyFbl7LAIIZT1FdxTlWC8r4SCiAjGhllejE fZG5nSLCFdz2ipU8hsZbfv94/QZ2oFImlkVj4C1WGG5RwWcEnx1Jnnx9p1uYySB4PhHd /SyMVxTcOWTNlgH+LmsKvtteD8P+iHaTL8utAu63BXjuPIDUsC3GnioksPFgdIMR1uHG QEhuvp+ZLbqeBdV4kXshLexHj/PV6NA/+EyytXWlGBcUZdyXiPRdzbC2B4tmiBvuE0kt wqvHwRmeejWssWKi7KWLkuNoHnXTXIGd9CpSFzRXQCUJ+bTZqbTd3BFwWdQaEb2yDFy0 eMqw== X-Gm-Message-State: AOAM5306IwEXUUB2I42ekeJPMfTt3KinFTdsLbR5nDukMczU59zT9Aok hBDgSPlhYUO2uMWMJKt5Lw5zaJ+3KQY= X-Google-Smtp-Source: ABdhPJwmmJck3m8o3lg0Sgmz/VUMJ7lF8Czwi9PjAniJEZtR2AwGLaolwWHIVTmFB2QuRAzRq6aZ7w== X-Received: by 2002:a7b:c2f7:: with SMTP id e23mr4766735wmk.92.1637194838831; Wed, 17 Nov 2021 16:20:38 -0800 (PST) Received: from localhost.localdomain ([193.48.40.117]) by smtp.gmail.com with ESMTPSA id y7sm1255927wrw.55.2021.11.17.16.20.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Nov 2021 16:20:38 -0800 (PST) From: zimoun Date: Thu, 18 Nov 2021 01:20:22 +0100 Message-Id: <20211118002023.3323307-3-zimon.toutoune@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211118002023.3323307-1-zimon.toutoune@gmail.com> References: <20211118002023.3323307-1-zimon.toutoune@gmail.com> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * guix/scripts/hash.scm (%options): Deprecate 'recursive', add 'serializer'. (%default-options): Add 'serializer'. (nar-hash): New procedure. (default-hash): New procedure. (guix-hash)[file-hash]: Use them. (show-help): Adjust. * tests/guix-hash.scm: Adjust. * doc/guix.texi: Update. --- doc/guix.texi | 25 ++++++++----- guix/scripts/hash.scm | 82 +++++++++++++++++++++++++++++-------------- tests/guix-hash.sh | 14 +++++--- 3 files changed, 81 insertions(+), 40 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index 89a970908d..20041c20b7 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -71,7 +71,7 @@ Copyright @copyright{} 2019 Kyle Andrews@* Copyright @copyright{} 2019 Alex Griffin@* Copyright @copyright{} 2019, 2020, 2021 Guillaume Le Vaillant@* Copyright @copyright{} 2020 Liliana Marie Prikler@* -Copyright @copyright{} 2019, 2020 Simon Tournier@* +Copyright @copyright{} 2019, 2020, 2021 Simon Tournier@* Copyright @copyright{} 2020 Wiktor Żelazny@* Copyright @copyright{} 2020 Damien Cassou@* Copyright @copyright{} 2020 Jakub Kądziołka@* @@ -11631,14 +11631,21 @@ in the definitions of packages. @item --recursive @itemx -r -Compute the hash on @var{file} recursively. - -In this case, the hash is computed on an archive containing @var{file}, -including its children if it is a directory. Some of the metadata of -@var{file} is part of the archive; for instance, when @var{file} is a -regular file, the hash is different depending on whether @var{file} is -executable or not. Metadata such as time stamps has no impact on the -hash (@pxref{Invoking guix archive}). +This option is deprecated in favor of @option{--serializer}. It is a +legacy alias for @var{type} sets to @code{nar}. + +@item --serializer=@var{type} +@itemx -S +Compute the hash on @var{file} using @var{type} serialization. + +Supported types: @code{none} and @code{nar}. + +When using @code{nar}, the hash is computed on an archive containing +@var{file}, including its children if it is a directory. Some of the +metadata of @var{file} is part of the archive; for instance, when +@var{file} is a regular file, the hash is different depending on whether +@var{file} is executable or not. Metadata such as time stamps has no +impact on the hash (@pxref{Invoking guix archive}). @c FIXME: Replace xref above with xref to an ``Archive'' section when @c it exists. diff --git a/guix/scripts/hash.scm b/guix/scripts/hash.scm index 12f542929b..d05ecb80ba 100644 --- a/guix/scripts/hash.scm +++ b/guix/scripts/hash.scm @@ -37,6 +37,29 @@ (define-module (guix scripts hash) #:use-module (srfi srfi-37) #:export (guix-hash)) + +;;; +;;; Serializers +;;; + +(define* (nar-hash file #:optional + (algorithm (assoc-ref %default-options 'hash-algorithm)) + select?) + (let-values (((port get-hash) + (open-hash-port algorithm))) + (write-file file port #:select? select?) + (force-output port) + (get-hash))) + +(define* (default-hash file #:optional + (algorithm (assoc-ref %default-options 'hash-algorithm)) + select?) + (match file + ("-" (port-hash algorithm (current-input-port))) + (_ + (call-with-input-file file + (cute port-hash algorithm <>))))) + ;;; ;;; Command-line options. @@ -45,7 +68,8 @@ (define-module (guix scripts hash) (define %default-options ;; Alist of default option values. `((format . ,bytevector->nix-base32-string) - (hash-algorithm . ,(hash-algorithm sha256)))) + (hash-algorithm . ,(hash-algorithm sha256)) + (serializer . ,default-hash))) (define (show-help) (display (G_ "Usage: guix hash [OPTION] FILE @@ -61,7 +85,7 @@ (define (show-help) (format #t (G_ " -f, --format=FMT write the hash in the given format")) (format #t (G_ " - -r, --recursive compute the hash on FILE recursively")) + -S, --serializer=TYPE compute the hash on FILE according to TYPE serialization")) (newline) (display (G_ " -h, --help display this help and exit")) @@ -102,7 +126,24 @@ (define fmt-proc (alist-delete 'format result)))) (option '(#\r "recursive") #f #f (lambda (opt name arg result) - (alist-cons 'recursive? #t result))) + (warning (G_ "'--recursive' is deprecated, \ +use '--serializer' instead~%")) + (alist-cons 'serializer nar-hash + (alist-delete 'serializer result)))) + (option '(#\S "serializer") #t #f + (lambda (opt name arg result) + (define serializer-proc + (match arg + ("none" + default-hash) + ("nar" + nar-hash) + (x + (leave (G_ "unsupported serializer type: ~a~%") + arg)))) + + (alist-cons 'serializer serializer-proc + (alist-delete 'serializer result)))) (option '(#\h "help") #f #f (lambda args (show-help) @@ -145,35 +186,24 @@ (define (vcs-file? file stat) (fmt (assq-ref opts 'format)) (select? (if (assq-ref opts 'exclude-vcs?) (negate vcs-file?) - (const #t)))) + (const #t))) + (algorithm (assoc-ref opts 'hash-algorithm)) + (serializer (assoc-ref opts 'serializer))) (define (file-hash file) ;; Compute the hash of FILE. - ;; Catch and gracefully report possible '&nar-error' conditions. - (if (assoc-ref opts 'recursive?) + ;; Catch and gracefully report possible error + (catch 'system-error + (lambda _ (with-error-handling - (let-values (((port get-hash) - (open-hash-port (assoc-ref opts 'hash-algorithm)))) - (write-file file port #:select? select?) - (force-output port) - (get-hash))) - (catch 'system-error - (lambda _ - (call-with-input-file file - (cute port-hash (assoc-ref opts 'hash-algorithm) - <>))) - (lambda args - (leave (G_ "~a ~a~%") - file - (strerror (system-error-errno args))))))) + (serializer file algorithm select?))) + (lambda args + (leave (G_ "~a ~a~%") + file + (strerror (system-error-errno args)))))) (define (formatted-hash thing) - (match thing - ("-" (with-error-handling - (fmt (port-hash (assoc-ref opts 'hash-algorithm) - (current-input-port))))) - (_ - (fmt (file-hash thing))))) + (fmt (file-hash thing))) (for-each (compose (cute format #t "~a~%" <>) formatted-hash) diff --git a/tests/guix-hash.sh b/tests/guix-hash.sh index c4461fa955..cdcfac19bc 100644 --- a/tests/guix-hash.sh +++ b/tests/guix-hash.sh @@ -42,25 +42,29 @@ chmod +x "$tmpdir/exe" ( cd "$tmpdir" ; ln -s exe symlink ) mkdir "$tmpdir/subdir" -test `guix hash -r "$tmpdir"` = 10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p -test `guix hash -r "$tmpdir" -H sha512` = 301ra58c2vahczzxiyfin41mpyb0ljh4dh9zn3ijvwviaw1j40sfzw5skh9x945da88n3785ggifzig7acd6k72h0mpsc20m1f66m9n +test `guix hash -S nar "$tmpdir"` = 10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p +test `guix hash -S nar "$tmpdir" -H sha512` = 301ra58c2vahczzxiyfin41mpyb0ljh4dh9zn3ijvwviaw1j40sfzw5skh9x945da88n3785ggifzig7acd6k72h0mpsc20m1f66m9n + +# Deprecated --recursive option +test `guix hash -r "$tmpdir" 2>/dev/null` = 10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p +test `guix hash -r "$tmpdir" -H sha512 2>/dev/null` = 301ra58c2vahczzxiyfin41mpyb0ljh4dh9zn3ijvwviaw1j40sfzw5skh9x945da88n3785ggifzig7acd6k72h0mpsc20m1f66m9n # Without '-r', this should fail. ! guix hash "$tmpdir" # This should fail because /dev/null is a character device, which # the archive format doesn't support. -! guix hash -r /dev/null +! guix hash -S nar /dev/null # Adding a .git directory mkdir "$tmpdir/.git" touch "$tmpdir/.git/foo" # ...changes the hash -test `guix hash -r $tmpdir` = 0a50z04zyzf7pidwxv0nwbj82pgzbrhdy9562kncnvkcfvb48m59 +test `guix hash -S nar $tmpdir` = 0a50z04zyzf7pidwxv0nwbj82pgzbrhdy9562kncnvkcfvb48m59 # ...but remains the same when using `-x' -test `guix hash -r $tmpdir -x` = 10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p +test `guix hash -S nar $tmpdir -x` = 10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p # Without '-r', this should fail. ! guix hash "$tmpdir" From patchwork Thu Nov 18 00:20:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Tournier X-Patchwork-Id: 34584 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 1139D27BBE3; Thu, 18 Nov 2021 00:21:30 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2, SPF_HELO_PASS,T_DKIM_INVALID,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 8F81527BBE1 for ; Thu, 18 Nov 2021 00:21:29 +0000 (GMT) Received: from localhost ([::1]:51720 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mnVBA-0004Ct-Lb for patchwork@mira.cbaines.net; Wed, 17 Nov 2021 19:21:28 -0500 Received: from eggs.gnu.org ([209.51.188.92]:45196) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mnVAm-0003gs-5b for guix-patches@gnu.org; Wed, 17 Nov 2021 19:21:04 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:50831) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mnVAl-00088d-0R for guix-patches@gnu.org; Wed, 17 Nov 2021 19:21:03 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mnVAk-00070a-8V for guix-patches@gnu.org; Wed, 17 Nov 2021 19:21:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#51307] [PATCH v2 3/3] scripts: hash: Add git serializer. Resent-From: zimoun Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 18 Nov 2021 00:21:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 51307 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 51307@debbugs.gnu.org Cc: ludo@gnu.org, zimoun Received: via spool by 51307-submit@debbugs.gnu.org id=B51307.163719484926878 (code B ref 51307); Thu, 18 Nov 2021 00:21:02 +0000 Received: (at 51307) by debbugs.gnu.org; 18 Nov 2021 00:20:49 +0000 Received: from localhost ([127.0.0.1]:34139 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mnVAX-0006zN-8Q for submit@debbugs.gnu.org; Wed, 17 Nov 2021 19:20:49 -0500 Received: from mail-wm1-f49.google.com ([209.85.128.49]:51171) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mnVAT-0006yl-0y for 51307@debbugs.gnu.org; Wed, 17 Nov 2021 19:20:46 -0500 Received: by mail-wm1-f49.google.com with SMTP id 133so3777508wme.0 for <51307@debbugs.gnu.org>; Wed, 17 Nov 2021 16:20:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SL/gIdANgdxZAgXsJxq4XvVCS8NHj/f/oZy9QdFOxNw=; b=PQjCP73Ed1W9i9OmbdPttpddetuhr3hpImdm2A7o8MzN/LvL9Kx0wFj2ZN4H2BlV8n QnWOvC8moio0UjXT9Uh/mEyLMHAkc1eiolb/nnEDEXuqstHoMcjrJwirWP7mfa+UCMx5 3f5LoyY57OTuBwd4k18Ja6YuUGe92BbXxsquZglXr+ZCN3AFYuG+aM3DJrztc8tN9VtL 6d8i+Er4jjki/HNgFJkPPeVFNmocy3YRvB9aCklUq2jOLMM8Rwa8HxJilV16lE0gCKOz KQF78MPcyNslR365vzeuEmqZgq0kDs8/TfgwPFJQwURJBzgjFVJ4+Y2zl17dTBGTGckQ YPKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=SL/gIdANgdxZAgXsJxq4XvVCS8NHj/f/oZy9QdFOxNw=; b=TAVCI8fTSuMl7WPRsXysanLAUt40V9HcjxyZC4CFo1n2dj2Oph2aDrkQ9TqMuiH91g 4YYe1oH8QLdYowRt6Anc3Xvbegyh3CT8smZRsXLzum1xGTxO+p+qPnPXKaISuO0Tv/IS ir/jS3gHoN/9b3K1CSNWCam31Hd4hdk8dkyzqpQv1/ZYdcN8Egf/jMe4825I9NdrggIR TfZjCU2lQi+es3kVjTuJmW1/EF83DJZqL531oA4aZCVN1uxne92DJqDn1ttDj7lmwng/ 0hjRDqs4pisq/cOrpsa0hGq/fmouGDrr+k6OFRQ0gRxkQMv3V1351LAKLm12EPEoUbZd 9kcQ== X-Gm-Message-State: AOAM530g5dtHEbVFCCxr+p3jUdeaYzmmnEr7Cs7CL380lzyLmutlGwXz xk7EjSLkbdqhIAZaXDdXBkfsC98n0AU= X-Google-Smtp-Source: ABdhPJyiaxVZkZ386v+PAb71IJ0p3QaQNYqRIi+N+0flJxemc6NEVjRWT4CaSFp6TIq67JmYHR6UPQ== X-Received: by 2002:a1c:9a89:: with SMTP id c131mr4615594wme.80.1637194839356; Wed, 17 Nov 2021 16:20:39 -0800 (PST) Received: from localhost.localdomain ([193.48.40.117]) by smtp.gmail.com with ESMTPSA id y7sm1255927wrw.55.2021.11.17.16.20.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Nov 2021 16:20:39 -0800 (PST) From: zimoun Date: Thu, 18 Nov 2021 01:20:23 +0100 Message-Id: <20211118002023.3323307-4-zimon.toutoune@gmail.com> X-Mailer: git-send-email 2.33.1 In-Reply-To: <20211118002023.3323307-1-zimon.toutoune@gmail.com> References: <20211118002023.3323307-1-zimon.toutoune@gmail.com> MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches * guix/scripts/hash.scm (git-hash): New procedure. (%options): Use it. * tests/guix-hash.sh: Test it. * doc/guix.texi: Update. --- doc/guix.texi | 4 +++- guix/scripts/hash.scm | 15 +++++++++++++++ tests/guix-hash.sh | 6 ++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/doc/guix.texi b/doc/guix.texi index 20041c20b7..af2e99903b 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -11638,7 +11638,7 @@ legacy alias for @var{type} sets to @code{nar}. @itemx -S Compute the hash on @var{file} using @var{type} serialization. -Supported types: @code{none} and @code{nar}. +Supported types: @code{none}, @code{nar} and @code{git}. When using @code{nar}, the hash is computed on an archive containing @var{file}, including its children if it is a directory. Some of the @@ -11649,6 +11649,8 @@ impact on the hash (@pxref{Invoking guix archive}). @c FIXME: Replace xref above with xref to an ``Archive'' section when @c it exists. +Using @code{git} serializes the file or directory as a Git tree. + @item --exclude-vcs @itemx -x When combined with @option{--recursive}, exclude version control system diff --git a/guix/scripts/hash.scm b/guix/scripts/hash.scm index d05ecb80ba..80581f2340 100644 --- a/guix/scripts/hash.scm +++ b/guix/scripts/hash.scm @@ -35,6 +35,8 @@ (define-module (guix scripts hash) #:use-module (srfi srfi-11) #:use-module (srfi srfi-26) #:use-module (srfi srfi-37) + #:use-module ((disarchive git-hash) #:select (git-hash-file + git-hash-directory)) #:export (guix-hash)) @@ -60,6 +62,17 @@ (define* (default-hash file #:optional (call-with-input-file file (cute port-hash algorithm <>))))) +(define* (git-hash file #:optional + (algorithm (assoc-ref %default-options 'hash-algorithm)) + select?) + (define directory? + (case (stat:type (stat file)) + ((directory) #t) + (else #f))) + (if directory? + (git-hash-directory file algorithm) + (git-hash-file file algorithm))) + ;;; ;;; Command-line options. @@ -138,6 +151,8 @@ (define serializer-proc default-hash) ("nar" nar-hash) + ("git" + git-hash) (x (leave (G_ "unsupported serializer type: ~a~%") arg)))) diff --git a/tests/guix-hash.sh b/tests/guix-hash.sh index cdcfac19bc..bb3973771f 100644 --- a/tests/guix-hash.sh +++ b/tests/guix-hash.sh @@ -34,6 +34,9 @@ test `guix hash -f base32 /dev/null` = 4oymiquy7qobjgx36tejs35zeqt24qpemsnzgtfes test `guix hash -H sha512 -f hex /dev/null` = cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e test `guix hash -H sha1 -f base64 /dev/null` = "2jmj7l5rSw0yVb/vlWAYkK/YBwk=" +# idem as `cat /dev/null | git hash-object --stdin` +test `guix hash -S git -H sha1 -f hex /dev/null` = e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 + ! guix hash -H abcd1234 /dev/null mkdir "$tmpdir" @@ -44,6 +47,7 @@ mkdir "$tmpdir/subdir" test `guix hash -S nar "$tmpdir"` = 10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p test `guix hash -S nar "$tmpdir" -H sha512` = 301ra58c2vahczzxiyfin41mpyb0ljh4dh9zn3ijvwviaw1j40sfzw5skh9x945da88n3785ggifzig7acd6k72h0mpsc20m1f66m9n +test `guix hash -S git "$tmpdir" -H sha512` = 158b10d1bsdk4pm8ym9cg9ckfak1b0cgpw7365cl6s341ir380mh2f4ylicyh8khyrfnwq5cn9766d7m8fbfwwl94ndkv456v6a8knr # Deprecated --recursive option test `guix hash -r "$tmpdir" 2>/dev/null` = 10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p @@ -62,9 +66,11 @@ touch "$tmpdir/.git/foo" # ...changes the hash test `guix hash -S nar $tmpdir` = 0a50z04zyzf7pidwxv0nwbj82pgzbrhdy9562kncnvkcfvb48m59 +test `guix hash -S git $tmpdir` = 0ghlpca9xaswa1ay1g55dknwd9q899mi3ahfr43pq083v8wisjc7 # ...but remains the same when using `-x' test `guix hash -S nar $tmpdir -x` = 10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p +test `guix hash -S git $tmpdir -x` = 0ghlpca9xaswa1ay1g55dknwd9q899mi3ahfr43pq083v8wisjc7 # Without '-r', this should fail. ! guix hash "$tmpdir"