Patchwork scripts: hash: Add --exclude-.git option.

login
register
mail settings
Submitter Jan Nieuwenhuizen
Date Sept. 5, 2016, 9:04 p.m.
Message ID <87fupexdmc.fsf@gnu.org>
Download mbox | patch
Permalink /patch/15346/
State New
Headers show

Comments

Jan Nieuwenhuizen - Sept. 5, 2016, 9:04 p.m.
Efraim Flashner writes:

> Can this be generalized? `grep \\-download\) gnu/packages/*scm' shows also
> svn-download, cvs-download and a lone hg-download.

Okay.  What about simply having non-default

   --exclude=FILE

See attached.  (Missing test included this time).

Greetings,
Jan
Leo Famulari - Sept. 5, 2016, 9:14 p.m.
On Mon, Sep 05, 2016 at 11:04:11PM +0200, Jan Nieuwenhuizen wrote:
> Efraim Flashner writes:
> 
> > Can this be generalized? `grep \\-download\) gnu/packages/*scm' shows also
> > svn-download, cvs-download and a lone hg-download.
> 
> Okay.  What about simply having non-default
> 
>    --exclude=FILE

I like this way the best. Hashing source code checkouts is still rare
enough in my experience that it makes sense to take a generalized
approach like this, in my opinion.

> 
> See attached.  (Missing test included this time).
> 
> Greetings,
> Jan
> 

> From ae738ad845f6d9e7576c42c53db6f7036b006e6c Mon Sep 17 00:00:00 2001
> From: Jan Nieuwenhuizen <janneke@gnu.org>
> Date: Mon, 5 Sep 2016 10:27:19 +0200
> Subject: [PATCH] scripts: hash: Add --exclude option.
> 
> * guix/scripts/hash.scm (show-help): Add help text for --exclude option.
> (%options): Add --exclude option.
> (guix-hash): Handle exclude option.
> * doc/guix.texi ("invoking guix hash"): Update doc.
> * tests/guix-hash.sh: Add test.
> ---
>  doc/guix.texi         | 14 +++++++++++++-
>  guix/scripts/hash.scm | 18 +++++++++++++++---
>  tests/guix-hash.sh    | 17 +++++++++++++++++
>  3 files changed, 45 insertions(+), 4 deletions(-)
> 
> diff --git a/doc/guix.texi b/doc/guix.texi
> index 5330238..b5c0a7d 100644
> --- a/doc/guix.texi
> +++ b/doc/guix.texi
> @@ -4616,10 +4616,17 @@ The general syntax is:
>  guix hash @var{option} @var{file}
>  @end example
>  
> -@command{guix hash} has the following option:
> +@command{guix hash} has the following options:
>  
>  @table @code
>  
> +@item --exclude=@var{file}
> +@itemx -e @var{file}
> +Exclude @var{file} when computing a recursive hash, e.g.:
> +@example
> +$ guix hash -r -e .git .
> +@end example
> +
>  @item --format=@var{fmt}
>  @itemx -f @var{fmt}
>  Write the hash in the format specified by @var{fmt}.
> @@ -4655,6 +4662,11 @@ $ cd foo
>  $ rm -rf .git
>  $ guix hash -r .
>  @end example
> +@noindent
> +or simply use the -e (--exclude) option
> +@example
> +$ guix hash -r -e .git .
> +@end example
>  @end table
>  
>  @node Invoking guix import
> diff --git a/guix/scripts/hash.scm b/guix/scripts/hash.scm
> index d440953..642e1c0 100644
> --- a/guix/scripts/hash.scm
> +++ b/guix/scripts/hash.scm
> @@ -1,6 +1,7 @@
>  ;;; GNU Guix --- Functional package management for GNU
>  ;;; Copyright © 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org>
>  ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
> +;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
>  ;;;
>  ;;; This file is part of GNU Guix.
>  ;;;
> @@ -49,6 +50,8 @@ Return the cryptographic hash of FILE.
>  Supported formats: 'nix-base32' (default), 'base32', and 'base16' ('hex'
>  and 'hexadecimal' can be used as well).\n"))
>    (format #t (_ "
> +  -e, --exclude=FILE     exclude FILE when computing hash"))
> +  (format #t (_ "
>    -f, --format=FMT       write the hash in the given format"))
>    (format #t (_ "
>    -r, --recursive        compute the hash on FILE recursively"))
> @@ -62,7 +65,10 @@ and 'hexadecimal' can be used as well).\n"))
>  
>  (define %options
>    ;; Specification of the command-line options.
> -  (list (option '(#\f "format") #t #f
> +  (list (option '(#\e "exclude") #t #f
> +                (lambda (opt name arg result)
> +                  (alist-cons 'exclude arg result)))
> +        (option '(#\f "format") #t #f
>                  (lambda (opt name arg result)
>                    (define fmt-proc
>                      (match arg
> @@ -78,6 +84,7 @@ and 'hexadecimal' can be used as well).\n"))
>  
>                    (alist-cons 'format fmt-proc
>                                (alist-delete 'format result))))
> +
>          (option '(#\r "recursive") #f #f
>                  (lambda (opt name arg result)
>                    (alist-cons 'recursive? #t result)))
> @@ -113,7 +120,12 @@ and 'hexadecimal' can be used as well).\n"))
>                               value)
>                              (_ #f))
>                             (reverse opts)))
> -         (fmt  (assq-ref opts 'format)))
> +         (fmt  (assq-ref opts 'format))
> +         (exclude (assq-ref opts 'exclude))
> +         (select? (if exclude
> +                      (lambda (f s)
> +                        (not (string= f (string-append (car args) "/" exclude))))
> +                      (const #t))))
>  
>      (define (file-hash file)
>        ;; Compute the hash of FILE.
> @@ -121,7 +133,7 @@ and 'hexadecimal' can be used as well).\n"))
>        (with-error-handling
>          (if (assoc-ref opts 'recursive?)
>              (let-values (((port get-hash) (open-sha256-port)))
> -              (write-file file port)
> +              (write-file file port #:select? select?)
>                (flush-output-port port)
>                (get-hash))
>              (call-with-input-file file port-sha256))))
> diff --git a/tests/guix-hash.sh b/tests/guix-hash.sh
> index 23df01d..b59939e 100644
> --- a/tests/guix-hash.sh
> +++ b/tests/guix-hash.sh
> @@ -1,5 +1,6 @@
>  # GNU Guix --- Functional package management for GNU
>  # Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
> +# Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
>  #
>  # This file is part of GNU Guix.
>  #
> @@ -20,6 +21,7 @@
>  # Test the `guix hash' command-line utility.
>  #
>  
> +set -x
>  guix hash --version
>  
>  tmpdir="guix-hash-$$"
> @@ -46,4 +48,18 @@ then false; else true; fi
>  # the archive format doesn't support.
>  if guix hash -r /dev/null
>  then false; else true; fi
> +
> +# Adding a .git directory
> +mkdir "$tmpdir/.git"
> +touch "$tmpdir/.git/foo"
> +
> +# ...changes the hash
> +test `guix hash -r $tmpdir` = 0a50z04zyzf7pidwxv0nwbj82pgzbrhdy9562kncnvkcfvb48m59
> +
> +# ...but remains the same when using `-e .git'
> +test `guix hash -r $tmpdir -e .git` = 10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p
> +
> +# Without '-r', this should fail.
> +if guix hash "$tmpdir"
> +then false; else true; fi
> +
> -- 
> 2.9.3
> 

> 
> -- 
> Jan Nieuwenhuizen <janneke@gnu.org> | GNU LilyPond http://lilypond.org
> Freelance IT http://JoyofSource.com | Avatar®  http://AvatarAcademy.nl

Patch

From ae738ad845f6d9e7576c42c53db6f7036b006e6c Mon Sep 17 00:00:00 2001
From: Jan Nieuwenhuizen <janneke@gnu.org>
Date: Mon, 5 Sep 2016 10:27:19 +0200
Subject: [PATCH] scripts: hash: Add --exclude option.

* guix/scripts/hash.scm (show-help): Add help text for --exclude option.
(%options): Add --exclude option.
(guix-hash): Handle exclude option.
* doc/guix.texi ("invoking guix hash"): Update doc.
* tests/guix-hash.sh: Add test.
---
 doc/guix.texi         | 14 +++++++++++++-
 guix/scripts/hash.scm | 18 +++++++++++++++---
 tests/guix-hash.sh    | 17 +++++++++++++++++
 3 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/doc/guix.texi b/doc/guix.texi
index 5330238..b5c0a7d 100644
--- a/doc/guix.texi
+++ b/doc/guix.texi
@@ -4616,10 +4616,17 @@  The general syntax is:
 guix hash @var{option} @var{file}
 @end example
 
-@command{guix hash} has the following option:
+@command{guix hash} has the following options:
 
 @table @code
 
+@item --exclude=@var{file}
+@itemx -e @var{file}
+Exclude @var{file} when computing a recursive hash, e.g.:
+@example
+$ guix hash -r -e .git .
+@end example
+
 @item --format=@var{fmt}
 @itemx -f @var{fmt}
 Write the hash in the format specified by @var{fmt}.
@@ -4655,6 +4662,11 @@  $ cd foo
 $ rm -rf .git
 $ guix hash -r .
 @end example
+@noindent
+or simply use the -e (--exclude) option
+@example
+$ guix hash -r -e .git .
+@end example
 @end table
 
 @node Invoking guix import
diff --git a/guix/scripts/hash.scm b/guix/scripts/hash.scm
index d440953..642e1c0 100644
--- a/guix/scripts/hash.scm
+++ b/guix/scripts/hash.scm
@@ -1,6 +1,7 @@ 
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2012, 2013, 2014 Ludovic Courtès <ludo@gnu.org>
 ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
+;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -49,6 +50,8 @@  Return the cryptographic hash of FILE.
 Supported formats: 'nix-base32' (default), 'base32', and 'base16' ('hex'
 and 'hexadecimal' can be used as well).\n"))
   (format #t (_ "
+  -e, --exclude=FILE     exclude FILE when computing hash"))
+  (format #t (_ "
   -f, --format=FMT       write the hash in the given format"))
   (format #t (_ "
   -r, --recursive        compute the hash on FILE recursively"))
@@ -62,7 +65,10 @@  and 'hexadecimal' can be used as well).\n"))
 
 (define %options
   ;; Specification of the command-line options.
-  (list (option '(#\f "format") #t #f
+  (list (option '(#\e "exclude") #t #f
+                (lambda (opt name arg result)
+                  (alist-cons 'exclude arg result)))
+        (option '(#\f "format") #t #f
                 (lambda (opt name arg result)
                   (define fmt-proc
                     (match arg
@@ -78,6 +84,7 @@  and 'hexadecimal' can be used as well).\n"))
 
                   (alist-cons 'format fmt-proc
                               (alist-delete 'format result))))
+
         (option '(#\r "recursive") #f #f
                 (lambda (opt name arg result)
                   (alist-cons 'recursive? #t result)))
@@ -113,7 +120,12 @@  and 'hexadecimal' can be used as well).\n"))
                              value)
                             (_ #f))
                            (reverse opts)))
-         (fmt  (assq-ref opts 'format)))
+         (fmt  (assq-ref opts 'format))
+         (exclude (assq-ref opts 'exclude))
+         (select? (if exclude
+                      (lambda (f s)
+                        (not (string= f (string-append (car args) "/" exclude))))
+                      (const #t))))
 
     (define (file-hash file)
       ;; Compute the hash of FILE.
@@ -121,7 +133,7 @@  and 'hexadecimal' can be used as well).\n"))
       (with-error-handling
         (if (assoc-ref opts 'recursive?)
             (let-values (((port get-hash) (open-sha256-port)))
-              (write-file file port)
+              (write-file file port #:select? select?)
               (flush-output-port port)
               (get-hash))
             (call-with-input-file file port-sha256))))
diff --git a/tests/guix-hash.sh b/tests/guix-hash.sh
index 23df01d..b59939e 100644
--- a/tests/guix-hash.sh
+++ b/tests/guix-hash.sh
@@ -1,5 +1,6 @@ 
 # GNU Guix --- Functional package management for GNU
 # Copyright © 2013, 2014 Ludovic Courtès <ludo@gnu.org>
+# Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
 #
 # This file is part of GNU Guix.
 #
@@ -20,6 +21,7 @@ 
 # Test the `guix hash' command-line utility.
 #
 
+set -x
 guix hash --version
 
 tmpdir="guix-hash-$$"
@@ -46,4 +48,18 @@  then false; else true; fi
 # the archive format doesn't support.
 if guix hash -r /dev/null
 then false; else true; fi
+
+# Adding a .git directory
+mkdir "$tmpdir/.git"
+touch "$tmpdir/.git/foo"
+
+# ...changes the hash
+test `guix hash -r $tmpdir` = 0a50z04zyzf7pidwxv0nwbj82pgzbrhdy9562kncnvkcfvb48m59
+
+# ...but remains the same when using `-e .git'
+test `guix hash -r $tmpdir -e .git` = 10k1lw41wyrjf9mxydi0is5nkpynlsvgslinics4ppir13g7d74p
+
+# Without '-r', this should fail.
+if guix hash "$tmpdir"
+then false; else true; fi
+
-- 
2.9.3