[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
05/06: guix hash: Add git serializer.
From: |
guix-commits |
Subject: |
05/06: guix hash: Add git serializer. |
Date: |
Fri, 17 Dec 2021 11:31:12 -0500 (EST) |
civodul pushed a commit to branch master
in repository guix.
commit 28ffdc5cc815e981281e2aa67a81280666985f0f
Author: zimoun <zimon.toutoune@gmail.com>
AuthorDate: Thu Nov 18 01:20:23 2021 +0100
guix hash: Add git serializer.
* guix/scripts/hash.scm (git-hash): New procedure.
(%options): Use it.
* tests/guix-hash.sh: Test it.
* doc/guix.texi: Update.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
---
doc/guix.texi | 4 +++-
guix/scripts/hash.scm | 14 ++++++++++++++
tests/guix-hash.sh | 6 ++++++
3 files changed, 23 insertions(+), 1 deletion(-)
diff --git a/doc/guix.texi b/doc/guix.texi
index 7f579f8..31a2387 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -11758,7 +11758,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
@@ -11769,6 +11769,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 debe8b4..d73e3d1 100644
--- a/guix/scripts/hash.scm
+++ b/guix/scripts/hash.scm
@@ -35,6 +35,7 @@
#:use-module (srfi srfi-11)
#:use-module (srfi srfi-26)
#:use-module (srfi srfi-37)
+ #:autoload (disarchive git-hash) (git-hash-file git-hash-directory)
#:export (guix-hash))
@@ -60,6 +61,17 @@
(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 +150,8 @@ use '--serializer' instead~%"))
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 7d186c9..854c493 100644
--- a/tests/guix-hash.sh
+++ b/tests/guix-hash.sh
@@ -40,6 +40,9 @@ test "`guix hash /dev/null "$abs_top_srcdir/README"`" =
"`guix hash /dev/null ;
# Zero files.
! guix hash
+# 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"
@@ -50,6 +53,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
@@ -68,9 +72,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"
- branch master updated (d6abbfd -> 4204156), guix-commits, 2021/12/17
- 01/06: import: utils: Wrap lines in description., guix-commits, 2021/12/17
- 02/06: import: Beautify descriptions when appropriate., guix-commits, 2021/12/17
- 03/06: guix hash: Support several files., guix-commits, 2021/12/17
- 04/06: guix hash: Add 'serializer' option., guix-commits, 2021/12/17
- 05/06: guix hash: Add git serializer.,
guix-commits <=
- 06/06: doc: Format 'guix hash -S' values as a table., guix-commits, 2021/12/17