branch: elpa/magit
commit 74a06b23602317d9bc58c3d952cc882701922f84
Author: Jonas Bernoulli <[email protected]>
Commit: Jonas Bernoulli <[email protected]>

    magit-insert-nested-repositories: New function
    
    Closes #5336.
---
 docs/CHANGELOG.4        |  2 ++
 lisp/magit-extras.el    | 30 ++++++++++++++++++++++++++++++
 lisp/magit-submodule.el |  4 ++--
 3 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/docs/CHANGELOG.4 b/docs/CHANGELOG.4
index 87e463d5fb..558f488d81 100644
--- a/docs/CHANGELOG.4
+++ b/docs/CHANGELOG.4
@@ -65,6 +65,8 @@
 
 - Added new option ~magit-revision-use-dedicated-buffers~.  #5330, #5248
 
+- Added new function ~magit-insert-nested-repositories~.  #5336
+
 Bugfixes:
 
 - ~magit-ignore-submodules-p~ didn't return ~nil~ for ~none~.
diff --git a/lisp/magit-extras.el b/lisp/magit-extras.el
index 3a79d73875..19d6dce117 100644
--- a/lisp/magit-extras.el
+++ b/lisp/magit-extras.el
@@ -834,6 +834,36 @@ In Magit diffs, also skip over - and + at the beginning of 
the line."
   (skip-syntax-forward " " (line-end-position))
   (backward-prefix-chars))
 
+(defun magit-insert-nested-repositories ()
+  "Insert repositories below the current repository.
+
+If the current repository has submodules, then do nothing (in this case
+the expectation is that all nested repositories are submodules and that
+they are being listed using a function such as `magit-insert-modules').
+Otherwise look for nested repositories once, using \"find\", and cache
+the result.  That cache can be invalidated using `magit-zap-caches'.
+
+Note that \"find\" will be run once in every repository, including in
+huge repositories, which don't actually have any nested repositories.
+This may be highly inefficient and you might have to implement your own
+version of this function, which is more suited to you situation.  This
+function is mostly intended as a proof-of-concept."
+  (unless (magit-list-module-names)
+    (unless (magit-repository-local-exists-p 'nested-repos)
+      (magit-repository-local-set
+       'nested-repos
+       (mapcar (##file-name-directory (substring % 2))
+               (process-lines "find" "-name" ".git" "-mindepth" "2"))))
+    (when-let ((repos (magit-repository-local-get 'nested-repos)))
+      (magit-insert-section (repos repos t)
+        (magit-insert-heading
+          (format "%s (%s)"
+                  (propertize "Nested repositories"
+                              'font-lock-face 'magit-section-heading)
+                  (length repos)))
+        (magit-insert-section-body
+          (magit--insert-modules-overview nil repos))))))
+
 ;;; _
 (provide 'magit-extras)
 ;; Local Variables:
diff --git a/lisp/magit-submodule.el b/lisp/magit-submodule.el
index c69bbcf6df..bb2a18cc11 100644
--- a/lisp/magit-submodule.el
+++ b/lisp/magit-submodule.el
@@ -486,9 +486,9 @@ or, failing that, the abbreviated HEAD commit hash."
 
 (defvar magit-modules-overview-align-numbers t)
 
-(defun magit--insert-modules-overview (&optional _section)
+(defun magit--insert-modules-overview (&optional _section repos)
   (magit-with-toplevel
-    (let* ((modules (magit-list-module-paths))
+    (let* ((modules (or repos (magit-list-module-paths)))
            (path-format (format "%%-%ds "
                                 (min (apply #'max (mapcar #'length modules))
                                      (/ (window-width) 2))))

Reply via email to