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

    magit-list-submodules: Support including unpopulated modules
    
    Closes #5077.
---
 docs/CHANGELOG.4        |  3 +++
 lisp/magit-repos.el     | 10 ++++++----
 lisp/magit-submodule.el | 43 ++++++++++++++++++++++++++++++++-----------
 3 files changed, 41 insertions(+), 15 deletions(-)

diff --git a/docs/CHANGELOG.4 b/docs/CHANGELOG.4
index 049d6934df..054a148fe1 100644
--- a/docs/CHANGELOG.4
+++ b/docs/CHANGELOG.4
@@ -44,6 +44,9 @@
   invoked with a prefix argument, the user can instead select the
   commit from a log.  #5602
 
+- Added new option ~magit-submodule-list-predicate~ and made it taught
+  to ~magit-submodule-list~ to support unpopulated modules.  #5077
+
 Bugfixes:
 
 - ~magit-ignore-submodules-p~ didn't return ~nil~ for ~none~.
diff --git a/lisp/magit-repos.el b/lisp/magit-repos.el
index 39a5da76a2..5f6dbaf49e 100644
--- a/lisp/magit-repos.el
+++ b/lisp/magit-repos.el
@@ -408,10 +408,12 @@ Usually this is just its basename."
 
 (defun magit-repolist-column-branch (_)
   "Insert the current branch."
-  (let ((branch (magit-get-current-branch)))
-    (if (member branch magit-main-branch-names)
-        (magit--propertize-face branch 'shadow)
-      branch)))
+  (if (file-exists-p ".git")
+      (let ((branch (magit-get-current-branch)))
+        (if (member branch magit-main-branch-names)
+            (magit--propertize-face branch 'shadow)
+          branch))
+    (magit--propertize-face "(unpopulated)" 'warning)))
 
 (defun magit-repolist-column-upstream (_)
   "Insert the upstream branch of the current branch."
diff --git a/lisp/magit-submodule.el b/lisp/magit-submodule.el
index 6f2e8b4d7b..c69bbcf6df 100644
--- a/lisp/magit-submodule.el
+++ b/lisp/magit-submodule.el
@@ -140,6 +140,15 @@ if non-nil, means to invert the resulting sort."
                  (cons (string :tag "Column name")
                        (boolean :tag "Flip order"))))
 
+(defcustom magit-submodule-list-predicate #'always
+  "Predicate used to determine whether to include a module in the list.
+Suitable values incluce `always' and 
`magit-submodule-list-module-populated-p'."
+  :package-version '(magit . "4.6.1")
+  :group 'magit-repolist
+  :type '(choice (function-item always)
+                 (function-item magit-submodule-list-module-populated-p)
+                 (function)))
+
 (defvar magit-submodule-list-format-path-functions nil)
 
 (defcustom magit-submodule-remove-trash-gitdirs nil
@@ -640,8 +649,6 @@ These sections can be expanded to show the respective 
commits."
   (setq-local tabulated-list-revert-hook
               (list #'magit-submodule-list-refresh t)))
 
-(defvar-local magit-submodule-list-predicate nil)
-
 (defun magit-submodule-list-setup (columns &optional predicate)
   (magit-display-buffer
    (or (magit-get-mode-buffer 'magit-submodule-list-mode)
@@ -649,7 +656,8 @@ These sections can be expanded to show the respective 
commits."
   (magit-submodule-list-mode)
   (setq-local magit-repolist-columns columns)
   (setq-local magit-repolist-sort-key magit-submodule-list-sort-key)
-  (setq-local magit-submodule-list-predicate predicate)
+  (when predicate
+    (setq-local magit-submodule-list-predicate predicate))
   (magit-repolist-setup-1)
   (magit-submodule-list-refresh))
 
@@ -662,21 +670,34 @@ These sections can be expanded to show the respective 
commits."
   (message "Listing submodules...done"))
 
 (defun magit-submodule--format-module (module)
-  (let ((default-directory (expand-file-name (file-name-as-directory module))))
-    (and (file-exists-p ".git")
-         (or (not magit-submodule-list-predicate)
-             (funcall magit-submodule-list-predicate module))
+  (and (funcall magit-submodule-list-predicate module)
+       (let ((default-directory
+              (expand-file-name (file-name-as-directory module))))
          (list default-directory
                (vconcat
                 (mapcar
                  (pcase-lambda (`(,title ,width ,fn ,props))
-                   (or (funcall fn `((:path  ,module)
-                                     (:title ,title)
-                                     (:width ,width)
-                                     ,@props))
+                   (or (and (or (file-exists-p ".git")
+                                (and
+                                 (symbolp fn)
+                                 (get fn 
'magit-submodule-support-unpopulated)))
+                            (funcall fn `((:path  ,module)
+                                          (:title ,title)
+                                          (:width ,width)
+                                          ,@props)))
                        ""))
                  magit-repolist-columns))))))
 
+(put 'magit-modulelist-column-path 'magit-submodule-support-unpopulated t)
+(put 'magit-repolist-column-path   'magit-submodule-support-unpopulated t)
+(put 'magit-repolist-column-branch 'magit-submodule-support-unpopulated t)
+
+(defun magit-submodule-list-module-populated-p (module)
+  "Return t if MODULE is populated, nil otherwise.
+This is only suitable as a value of `magit-submodule-list-predicate'."
+  (file-exists-p
+   (expand-file-name (concat (file-name-as-directory module) ".git"))))
+
 (defun magit-modulelist-column-path (spec)
   "Insert the relative path of the submodule."
   (let ((path (cadr (assq :path spec))))

Reply via email to