Subject: [bug#66961] [PATCH v2] scripts: hash: Handle repository with different VCS folders.

From: Simon Tournier <[email-masked]>
Date: Wed, 29 Nov 2023 21:31:00 +0100

Fixes <https://issues.guix.gnu.org/issue/65979>.
Reported by Simon Tournier <[email-masked]>

* 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): Use it.

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

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 <[email-masked]>
 ;;; Copyright © 2022 Maxime Devos <[email-masked]>
+;;; Copyright © 2023 Simon Tournier <[email-masked]>
 ;;;
 ;;; 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..b6af9d5649 100644
--- a/guix/scripts/hash.scm
+++ b/guix/scripts/hash.scm
@@ -182,7 +182,7 @@ (define-command (guix-hash . args)
                            (reverse opts)))
          (fmt  (assq-ref opts 'format))
          (select? (if (assq-ref opts 'exclude-vcs?)
-                      (negate vcs-file?)
+                      (vcs-exclude? file)
                       (const #t)))
          (algorithm (assoc-ref opts 'hash-algorithm))
          (serializer (assoc-ref opts 'serializer)))

base-commit: cd46757c1a0f886848fbb6828c028dd2a2532767
-- 
2.41.0