unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
* [bug#48699] [PATCH] git-download: Support submodules in 'git-predicate'.
@ 2021-05-27 14:18 Andrew Whatson
  2021-05-28 10:10 ` bug#48699: " Ludovic Courtès
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew Whatson @ 2021-05-27 14:18 UTC (permalink / raw)
  To: 48699; +Cc: Andrew Whatson

* guix/git-download.scm (git-file-list): Add prefix and recursive?
arguments.  Recurse into submodules when requested.
(git-predicate): Add recursive? argument.
---
 guix/git-download.scm | 67 ++++++++++++++++++++++++++++++++++---------
 1 file changed, 53 insertions(+), 14 deletions(-)

diff --git a/guix/git-download.scm b/guix/git-download.scm
index 8d8e1c865f..8094e5e5c7 100644
--- a/guix/git-download.scm
+++ b/guix/git-download.scm
@@ -33,6 +33,9 @@
                                  repository-discover
                                  repository-head
                                  repository-working-directory)
+  #:autoload   (git submodule)  (repository-submodules
+                                 submodule-lookup
+                                 submodule-path)
   #:autoload   (git commit)     (commit-lookup commit-tree)
   #:autoload   (git reference)  (reference-target)
   #:autoload   (git tree)       (tree-list)
@@ -194,11 +197,17 @@ HASH-ALGO (a symbol).  Use NAME as the file name, or a generic name if #f."
 ;;; 'git-predicate'.
 ;;;
 
-(define (git-file-list directory)
+(define* (git-file-list directory #:optional prefix #:key (recursive? #t))
   "Return the list of files checked in in the Git repository at DIRECTORY.
 The result is similar to that of the 'git ls-files' command, except that it
-also includes directories, not just regular files.  The returned file names
-are relative to DIRECTORY, which is not necessarily the root of the checkout."
+also includes directories, not just regular files.
+
+When RECURSIVE? is true, also list files in submodules, similar to the 'git
+ls-files --recurse-submodules' command.  This is enabled by default.
+
+The returned file names are relative to DIRECTORY, which is not necessarily
+the root of the checkout.  If a PREFIX is provided, it is prepended to each
+file name."
   (let* (;; 'repository-working-directory' always returns a trailing "/",
          ;; so add one here to ease the comparisons below.
          (directory  (string-append (canonicalize-path directory) "/"))
@@ -209,27 +218,57 @@ are relative to DIRECTORY, which is not necessarily the root of the checkout."
          (oid        (reference-target head))
          (commit     (commit-lookup repository oid))
          (tree       (commit-tree commit))
-         (files      (tree-list tree)))
+         (files      (tree-list tree))
+         (submodules (if recursive?
+                         (map (lambda (name)
+                                (submodule-path
+                                 (submodule-lookup repository name)))
+                              (repository-submodules repository))
+                         '()))
+         (relative      (and (not (string=? workdir directory))
+                             (string-drop directory (string-length workdir))))
+         (included?     (lambda (path)
+                          (or (not relative)
+                              (string-prefix? relative path))))
+         (make-relative (lambda (path)
+                          (if relative
+                              (string-drop path (string-length relative))
+                              path)))
+         (add-prefix    (lambda (path)
+                          (if prefix
+                              (string-append prefix "/" path)
+                              path)))
+         (rectify       (compose add-prefix make-relative)))
     (repository-close! repository)
-    (if (string=? workdir directory)
-        files
-        (let ((relative (string-drop directory (string-length workdir))))
-          (filter-map (lambda (file)
-                        (and (string-prefix? relative file)
-                             (string-drop file (string-length relative))))
-                      files)))))
-
-(define (git-predicate directory)
+    (append
+     (if (or relative prefix)
+         (filter-map (lambda (file)
+                       (and (included? file)
+                            (rectify file)))
+                     files)
+         files)
+     (append-map (lambda (submodule)
+                   (if (included? submodule)
+                       (git-file-list
+                        (string-append workdir submodule)
+                        (rectify submodule))
+                       '()))
+                 submodules))))
+
+(define* (git-predicate directory #:key (recursive? #t))
   "Return a predicate that returns true if a file is part of the Git checkout
 living at DIRECTORY.  If DIRECTORY does not lie within a Git checkout, and
 upon Git errors, return #f instead of a predicate.
 
+When RECURSIVE? is true, the predicate also returns true if a file is part of
+any Git submodule under DIRECTORY.  This is enabled by default.
+
 The returned predicate takes two arguments FILE and STAT where FILE is an
 absolute file name and STAT is the result of 'lstat'."
   (libgit2-init!)
   (catch 'git-error
     (lambda ()
-      (let* ((files  (git-file-list directory))
+      (let* ((files  (git-file-list directory #:recursive? recursive?))
              (inodes (fold (lambda (file result)
                              (let* ((path (string-append directory "/" file))
                                     (stat (and (file-exists? path)
-- 
2.31.1





^ permalink raw reply related	[flat|nested] 2+ messages in thread

* bug#48699: [PATCH] git-download: Support submodules in 'git-predicate'.
  2021-05-27 14:18 [bug#48699] [PATCH] git-download: Support submodules in 'git-predicate' Andrew Whatson
@ 2021-05-28 10:10 ` Ludovic Courtès
  0 siblings, 0 replies; 2+ messages in thread
From: Ludovic Courtès @ 2021-05-28 10:10 UTC (permalink / raw)
  To: Andrew Whatson; +Cc: 48699-done

Andrew Whatson <whatson@gmail.com> skribis:

> * guix/git-download.scm (git-file-list): Add prefix and recursive?
> arguments.  Recurse into submodules when requested.
> (git-predicate): Add recursive? argument.

Pushed as ebbfee880c1def28b77aeb2eee640998b9fa7d5f, thanks!

Ludo’.




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-05-28 10:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-27 14:18 [bug#48699] [PATCH] git-download: Support submodules in 'git-predicate' Andrew Whatson
2021-05-28 10:10 ` bug#48699: " Ludovic Courtès

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).