diff mbox series

[bug#66961,v3] scripts: hash: Handle repository with different VCS folders.

Message ID 6a560e37ba23e43d35c0b62c1ee860b4341c263c.1701290895.git.zimon.toutoune@gmail.com
State New
Headers show
Series [bug#66961,v3] scripts: hash: Handle repository with different VCS folders. | expand

Commit Message

Simon Tournier Nov. 29, 2023, 8:54 p.m. UTC
Fixes <https://issues.guix.gnu.org/issue/65979>.
Reported by Simon Tournier <zimon.toutoune@gmail.com>

* guix/hash.scm (%vcs-directories): New variable.
(vcs-file?): Add optional argument for passing VCS kind of the
file/repository.
(file-hash*): Adjust accordingly.
(vcs-exclude?): New procedure and export it.
* guix/scripts/hash.scm (guix-hash)[file-hash]: Use it.

Change-Id: I8e286c3426ddefd664dc3a471d5a09e309824faa
---
 guix/hash.scm         | 34 ++++++++++++++++++++++++++++------
 guix/scripts/hash.scm | 10 +++++-----
 2 files changed, 33 insertions(+), 11 deletions(-)


base-commit: cd46757c1a0f886848fbb6828c028dd2a2532767

Comments

Ludovic Courtès Dec. 2, 2023, 10:39 a.m. UTC | #1
Hi,

Simon Tournier <zimon.toutoune@gmail.com> skribis:

> Fixes <https://issues.guix.gnu.org/issue/65979>.
> Reported by Simon Tournier <zimon.toutoune@gmail.com>
>
> * guix/hash.scm (%vcs-directories): New variable.
> (vcs-file?): Add optional argument for passing VCS kind of the
> file/repository.
> (file-hash*): Adjust accordingly.
> (vcs-exclude?): New procedure and export it.
> * guix/scripts/hash.scm (guix-hash)[file-hash]: Use it.
>
> Change-Id: I8e286c3426ddefd664dc3a471d5a09e309824faa

[...]

> +(define (vcs-exclude? directory)
> +  "Return a procedure excluding content if DIRECTORY contains supported VCS."

Nitpick: I would call it ‘vcs-file-predicate’ for consistency, with
a docstring along these lines:

  Return a two-argument procedure that returns true for version-control
  metadata directories such as '.git' found in DIRECTORY.

> +  (lambda (file stat)
> +    (not (vcs-file? file stat vcs-directories))))

… and thus removing ‘not’ here

> +            (let ((select? (if (assq-ref opts 'exclude-vcs?)
> +                               (vcs-exclude? file)

… and changing the above to (negate (vcs-file-predicate file)).

(“Return #t for VCS files” sounds clearer to me than “return true except
for VCS files” in terms of API design.)

OK with these changes!

Thanks,
Ludo’.
Simon Tournier Dec. 2, 2023, 1:10 p.m. UTC | #2
Hi,

On sam., 02 déc. 2023 at 11:39, Ludovic Courtès <ludo@gnu.org> wrote:

> Nitpick: I would call it ‘vcs-file-predicate’ for consistency,

Hum, I thought the convention for ’predicate’ was ending by ’?’.
Therefore, personally I do not find this name consistent.  Anyway…

>                                                                with
> a docstring along these lines:
>
>   Return a two-argument procedure that returns true for version-control
>   metadata directories such as '.git' found in DIRECTORY.

…did that way in v4.

Thanks for your comments.


Cheers,
simon
diff mbox series

Patch

diff --git a/guix/hash.scm b/guix/hash.scm
index 3cb68e5c44..650def0696 100644
--- a/guix/hash.scm
+++ b/guix/hash.scm
@@ -1,6 +1,7 @@ 
 ;;; GNU Guix --- Functional package management for GNU
 ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
 ;;; Copyright © 2022 Maxime Devos <maximedevos@telenet.be>
+;;; Copyright © 2023 Simon Tournier <zimon.toutoune@gmail.com>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -23,23 +24,44 @@  (define-module (guix hash)
   #:use-module (srfi srfi-1)
   #:use-module (srfi srfi-11)
   #:export (vcs-file?
+            vcs-exclude?
             file-hash*))
 
-(define (vcs-file? file stat)
-  "Returns true if FILE is a version control system file."
+(define %vcs-directories
+  ;; Directory used for determining the kind of VCS.
+  (list ".bzr" ".git" ".hg" ".svn" "CVS"))
+
+(define* (vcs-file? file stat
+                    #:optional
+                    (vcs-directories %vcs-directories))
+  "Return true if FILE matches a version control system from the list
+VCSES-DIRECTORIES."
   (case (stat:type stat)
     ((directory)
-     (member (basename file) '(".bzr" ".git" ".hg" ".svn" "CVS")))
+     (member (basename file) vcs-directories))
     ((regular)
-     ;; Git sub-modules have a '.git' file that is a regular text file.
-     (string=? (basename file) ".git"))
+     (if (member ".git" vcs-directories)
+         ;; Git sub-modules have a '.git' file that is a regular text file.
+         (string=? (basename file) ".git")
+         #f))
     (else
      #f)))
 
+(define (vcs-exclude? directory)
+  "Return a procedure excluding content if DIRECTORY contains supported VCS."
+  (define vcs-directories
+    (filter (lambda (vcs)
+              (file-exists? (in-vicinity directory vcs)))
+            %vcs-directories))
+
+  (lambda (file stat)
+    (not (vcs-file? file stat vcs-directories))))
+
 (define* (file-hash* file #:key
                      (algorithm (hash-algorithm sha256))
                      (recursive? 'auto)
-                     (select? (negate vcs-file?)))
+                     (select? (negate (lambda (file stat)
+                                        (vcs-file? file stat)))))
   "Compute the hash of FILE with ALGORITHM.
 
 Symbolic links are only dereferenced if RECURSIVE? is false.
diff --git a/guix/scripts/hash.scm b/guix/scripts/hash.scm
index 7197d3965c..8982bb37b9 100644
--- a/guix/scripts/hash.scm
+++ b/guix/scripts/hash.scm
@@ -3,7 +3,7 @@ 
 ;;; Copyright © 2013 Nikita Karetnikov <nikita@karetnikov.org>
 ;;; Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
 ;;; Copyright © 2018 Tim Gesthuizen <tim.gesthuizen@yahoo.de>
-;;; Copyright © 2021 Simon Tournier <zimon.toutoune@gmail.com>
+;;; Copyright © 2021, 2023 Simon Tournier <zimon.toutoune@gmail.com>
 ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
 ;;;
 ;;; This file is part of GNU Guix.
@@ -181,9 +181,6 @@  (define-command (guix-hash . args)
                             (_ #f))
                            (reverse opts)))
          (fmt  (assq-ref opts 'format))
-         (select? (if (assq-ref opts 'exclude-vcs?)
-                      (negate vcs-file?)
-                      (const #t)))
          (algorithm (assoc-ref opts 'hash-algorithm))
          (serializer (assoc-ref opts 'serializer)))
 
@@ -193,7 +190,10 @@  (define-command (guix-hash . args)
       (catch 'system-error
         (lambda _
           (with-error-handling
-            (serializer file algorithm select?)))
+            (let ((select? (if (assq-ref opts 'exclude-vcs?)
+                               (vcs-exclude? file)
+                               (const #t))))
+              (serializer file algorithm select?))))
         (lambda args
           (leave (G_ "~a ~a~%")
                  file