Subject: bug#66806: 30.0.50; [PATCH] 'project-find-regexp' passes Git submodules to the search program

From: Jim Porter <[email-masked]>
Date: Sun, 29 Oct 2023 17:58:01 -0700

On 10/29/2023 2:41 PM, Dmitry Gutov wrote:
> And that's not to mention usage over Tramp (which would be affected by 
> the +1 process call that you mentioned as well, but that seems 
> unavoidable).

Yeah, I don't see a way around that, unless we constructed a complex 
command to run via Tramp that does it all in one go.

(I looked into using the "--stage" argument for "git ls-files", which 
gets most of the way to fixing this, but you could break that logic with 
evil file names not in the tree...)

> Anyway, after recent experience micro-optimizing list operations, I came 
> up with this version where the impact seems minimal.
> 
> WDYT?

Thanks, that helped form the basis for the attached patch. It moves the 
'member' call into the lambda for the original 'mapcar', and then we can 
just 'delq' all the nil elements. Benchmarking against the Emacs repo, 
this still seems to have a minimal perf impact.

There might also be a benefit to using "git ls-files 
--recurse-submodules" when we can (i.e. when not using "-o"), but maybe 
we can leave that optimization for another day.
From b0e01806e36c8671279e6ffeb6ba2397655a3f5a Mon Sep 17 00:00:00 2001
From: Jim Porter <[email-masked]>
Date: Sat, 28 Oct 2023 22:20:41 -0700
Subject: [PATCH] Exclude Git submodules from 'project-files'

* lisp/progmodes/project.el (project--vc-list-files): Exclude Git
submodules.
(project-search, project-query-replace-regexp): Remove now-unneeded
workaround.
---
 lisp/progmodes/project.el | 41 +++++++++++++++++++--------------------
 1 file changed, 20 insertions(+), 21 deletions(-)

diff --git a/lisp/progmodes/project.el b/lisp/progmodes/project.el
index fda1081eb62..bb44cfefa54 100644
--- a/lisp/progmodes/project.el
+++ b/lisp/progmodes/project.el
@@ -647,6 +647,7 @@ project--vc-list-files
             (include-untracked (project--value-in-dir
                                 'project-vc-include-untracked
                                 dir))
+            (submodules (project--git-submodules))
             files)
        (setq args (append args
                           '("-c" "--exclude-standard")
@@ -678,23 +679,25 @@ project--vc-list-files
                                         i)))
                                    extra-ignores)))))
        (setq files
-             (mapcar
-              (lambda (file) (concat default-directory file))
-              (split-string
-               (apply #'vc-git--run-command-string nil "ls-files" args)
-               "\0" t)))
+             (delq nil
+                   (mapcar
+                    (lambda (file)
+                      (unless (member file submodules)
+                        (concat default-directory file)))
+                    (split-string
+                     (apply #'vc-git--run-command-string nil "ls-files" args)
+                     "\0" t))))
        (when (project--vc-merge-submodules-p default-directory)
          ;; Unfortunately, 'ls-files --recurse-submodules' conflicts with '-o'.
-         (let* ((submodules (project--git-submodules))
-                (sub-files
-                 (mapcar
-                  (lambda (module)
-                    (when (file-directory-p module)
-                      (project--vc-list-files
-                       (concat default-directory module)
-                       backend
-                       extra-ignores)))
-                  submodules)))
+         (let ((sub-files
+                (mapcar
+                 (lambda (module)
+                   (when (file-directory-p module)
+                     (project--vc-list-files
+                      (concat default-directory module)
+                      backend
+                      extra-ignores)))
+                 submodules)))
            (setq files
                  (apply #'nconc files sub-files))))
        ;; 'git ls-files' returns duplicate entries for merge conflicts.
@@ -1326,8 +1329,7 @@ project-search
   (interactive "sSearch (regexp): ")
   (fileloop-initialize-search
    regexp
-   ;; XXX: See the comment in project-query-replace-regexp.
-   (cl-delete-if-not #'file-regular-p (project-files (project-current t)))
+   (project-files (project-current t))
    'default)
   (fileloop-continue))
 
@@ -1348,10 +1350,7 @@ project-query-replace-regexp
        (list from to))))
   (fileloop-initialize-replace
    from to
-   ;; XXX: Filter out Git submodules, which are not regular files.
-   ;; `project-files' can return those, which is arguably suboptimal,
-   ;; but removing them eagerly has performance cost.
-   (cl-delete-if-not #'file-regular-p (project-files (project-current t)))
+   (project-files (project-current t))
    'default)
   (fileloop-continue))
 
-- 
2.25.1