]> git.eshelyaron.com Git - emacs.git/commitdiff
(file-expand-wildcards): Handle patterns ending in "/"
authorStefan Monnier <monnier@iro.umontreal.ca>
Sun, 10 Dec 2023 00:46:07 +0000 (19:46 -0500)
committerStefan Monnier <monnier@iro.umontreal.ca>
Sun, 10 Dec 2023 00:49:33 +0000 (19:49 -0500)
The bug was encountered via the ls-lisp advice on Dired but
it actually affects all uses of `file-expand-wildcards`,
so better fix it there.

* lisp/files.el (file-expand-wildcards): Fix bug#60819.
* lisp/ls-lisp.el (ls-lisp--dired): Undo commit b365a7cc32e2.
* test/lisp/files-tests.el (files-tests--expand-wildcards): New test.

lisp/files.el
lisp/ls-lisp.el
test/lisp/files-tests.el

index 047854d393942a84c464c330a0cb8521f56296c6..3c1d0c30e67386ebe4f5b284cdd6903260ae25cf 100644 (file)
@@ -7547,27 +7547,34 @@ default directory.  However, if FULL is non-nil, they are absolute."
       (dolist (dir (nreverse dirs))
        (when (or (null dir)    ; Possible if DIRPART is not wild.
                  (file-accessible-directory-p dir))
-         (let ((this-dir-contents
-                ;; Filter out "." and ".."
-                (delq nil
-                       (mapcar (lambda (name)
-                                 (unless (string-match "\\`\\.\\.?\\'"
-                                                       (file-name-nondirectory name))
-                                   name))
-                              (directory-files
-                                (or dir ".") full
-                                (if regexp
-                                    ;; We're matching each file name
-                                    ;; element separately.
-                                    (concat "\\`" nondir "\\'")
-                                 (wildcard-to-regexp nondir)))))))
-           (setq contents
-                 (nconc
-                  (if (and dir (not full))
-                       (mapcar (lambda (name) (concat dir name))
-                              this-dir-contents)
-                    this-dir-contents)
-                  contents)))))
+          (if (equal "" nondir)
+              ;; `nondir' is "" when the pattern ends in "/".  Basically ""
+              ;; refers to the directory itself, like ".", but it's not
+              ;; among the names returned by `directory-files', so we have
+              ;; to special-case it.
+              (push (or dir nondir) contents)
+           (let ((this-dir-contents
+                  ;; Filter out "." and ".."
+                  (delq nil
+                         (mapcar (lambda (name)
+                                   (unless (string-match "\\`\\.\\.?\\'"
+                                                         (file-name-nondirectory
+                                                          name))
+                                     name))
+                                (directory-files
+                                  (or dir ".") full
+                                  (if regexp
+                                      ;; We're matching each file name
+                                      ;; element separately.
+                                      (concat "\\`" nondir "\\'")
+                                  (wildcard-to-regexp nondir)))))))
+             (setq contents
+                   (nconc
+                    (if (and dir (not full))
+                        (mapcar (lambda (name) (concat dir name))
+                                this-dir-contents)
+                      this-dir-contents)
+                    contents))))))
       contents)))
 
 (defcustom find-sibling-rules nil
@@ -7757,7 +7764,7 @@ need to be passed verbatim to shell commands."
     (purecopy "ls"))
   "Absolute or relative name of the `ls'-like program.
 This is used by `insert-directory' and `dired-insert-directory'
-(thus, also by `dired').  For Dired, this should ideally point to
+\(thus, also by `dired').  For Dired, this should ideally point to
 GNU ls, or another version of ls that supports the \"--dired\"
 flag.  See `dired-use-ls-dired'.
 
index c576819c5d0a4e719f4a2ada6161f1ac40f17240..1066f38c050550da99ce1cf7357845e09479eba0 100644 (file)
@@ -483,22 +483,8 @@ not contain `d', so that a full listing is expected."
       (if (not dir-wildcard)
           (funcall orig-fun dir-or-list switches)
         (let* ((default-directory (car dir-wildcard))
-               (wildcard (cdr dir-wildcard))
-               (files (file-expand-wildcards wildcard))
+               (files (file-expand-wildcards (cdr dir-wildcard)))
                (dir (car dir-wildcard)))
-          ;; When the wildcard ends in a slash, file-expand-wildcards
-          ;; returns nil; fix that by treating the wildcards as
-          ;; specifying only directories whose names match the
-          ;; widlcard.
-          (if (and (null files)
-                   (directory-name-p wildcard))
-              (setq files
-                    (delq nil
-                          (mapcar (lambda (fname)
-                                   (if (file-accessible-directory-p fname)
-                                        fname))
-                                 (file-expand-wildcards
-                                   (directory-file-name wildcard))))))
           (if files
               (let ((inhibit-read-only t)
                     (buf
index 3e499fff46892540f5a008ae5d12601204fb6562..24b144c424787d97b420d571dbbbdb9c9065d1ea 100644 (file)
@@ -2101,5 +2101,9 @@ Prompt users for any modified buffer with `buffer-offer-save' non-nil."
       (should (documentation 'bar))
       (should (documentation 'zot)))))
 
+(ert-deftest files-tests--expand-wildcards ()
+  (should (file-expand-wildcards
+           (concat (directory-file-name default-directory) "*/"))))
+
 (provide 'files-tests)
 ;;; files-tests.el ends here