emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/urgrep 03a4687866 3/3: Make sure that search directorie


From: ELPA Syncer
Subject: [elpa] externals/urgrep 03a4687866 3/3: Make sure that search directories are properly expanded before building command
Date: Sun, 19 Nov 2023 03:58:44 -0500 (EST)

branch: externals/urgrep
commit 03a46878664ae7281dd89bfaa18873c388868236
Author: Jim Porter <jporterbugs@gmail.com>
Commit: Jim Porter <jporterbugs@gmail.com>

    Make sure that search directories are properly expanded before building 
command
    
    This prevents home directories using "~" from getting inadvertently escaped.
---
 urgrep-tests.el | 36 ++++++++++++++++++++++++++++++------
 urgrep.el       | 21 +++++++++++++++++++--
 2 files changed, 49 insertions(+), 8 deletions(-)

diff --git a/urgrep-tests.el b/urgrep-tests.el
index ac2824c371..11290f1cb6 100644
--- a/urgrep-tests.el
+++ b/urgrep-tests.el
@@ -206,7 +206,11 @@ joined to compare against COMMAND."
       (urgrep-tests/check-command
        (urgrep-command "foo" :tool tool :directory '("dir1" "dir2"))
        `(,@common-args "--heading" "--break" "-i" "-F" "-e" "foo" "dir1"
-                       "dir2")))
+                       "dir2"))
+      (urgrep-tests/check-command
+       (urgrep-command "foo" :tool tool :directory "~/dir")
+       `(,@common-args "--heading" "--break" "-i" "-F" "-e" "foo"
+                       ,(expand-file-name "~/dir"))))
     (ert-info ("Color")
       (urgrep-tests/check-command
        (urgrep-command "foo" :tool tool :color nil)
@@ -286,7 +290,11 @@ joined to compare against COMMAND."
        `(,@common-args "--heading" "-i" "-F" "--" "foo" "dir"))
       (urgrep-tests/check-command
        (urgrep-command "foo" :tool tool :directory '("dir1" "dir2"))
-       `(,@common-args "--heading" "-i" "-F" "--" "foo" "dir1" "dir2")))
+       `(,@common-args "--heading" "-i" "-F" "--" "foo" "dir1" "dir2"))
+      (urgrep-tests/check-command
+       (urgrep-command "foo" :tool tool :directory "~/dir")
+       `(,@common-args "--heading" "-i" "-F" "--" "foo"
+                       ,(expand-file-name "~/dir"))))
     (ert-info ("Color")
       (urgrep-tests/check-command
        (urgrep-command "foo" :tool tool :color nil)
@@ -360,7 +368,11 @@ joined to compare against COMMAND."
        `(,@common-args "--group" "-i" "-Q" "--" "foo" "dir"))
       (urgrep-tests/check-command
        (urgrep-command "foo" :tool tool :directory '("dir1" "dir2"))
-       `(,@common-args "--group" "-i" "-Q" "--" "foo" "dir1" "dir2")))
+       `(,@common-args "--group" "-i" "-Q" "--" "foo" "dir1" "dir2"))
+      (urgrep-tests/check-command
+       (urgrep-command "foo" :tool tool :directory "~/dir")
+       `(,@common-args "--group" "-i" "-Q" "--" "foo"
+                       ,(expand-file-name "~/dir"))))
     (ert-info ("Color")
       (urgrep-tests/check-command
        (urgrep-command "foo" :tool tool :color nil)
@@ -444,7 +456,11 @@ joined to compare against COMMAND."
       (urgrep-tests/check-command
        (urgrep-command "foo" :tool tool :directory '("dir1" "dir2"))
        `(,@common-args ,@no-hidden-args "--group" "-i" "-Q" "--" "foo" "dir1"
-                       "dir2")))
+                       "dir2"))
+      (urgrep-tests/check-command
+       (urgrep-command "foo" :tool tool :directory "~/dir")
+       `(,@common-args ,@no-hidden-args "--group" "-i" "-Q" "--" "foo"
+                       ,(expand-file-name "~/dir"))))
     (ert-info ("Color")
       (urgrep-tests/check-command
        (urgrep-command "foo" :tool tool :color nil)
@@ -538,7 +554,11 @@ joined to compare against COMMAND."
       (urgrep-tests/check-command
        (urgrep-command "foo" :tool tool :directory '("dir1" "dir2"))
        `(,@common-args ,@group-args "-i" "-F" "-e" "foo" "--" ,@no-hidden-args
-                       "dir1" "dir2")))
+                       "dir1" "dir2"))
+      (urgrep-tests/check-command
+       (urgrep-command "foo" :tool tool :directory "~/dir")
+       `(,@common-args ,@group-args "-i" "-F" "-e" "foo" "--" ,@no-hidden-args
+                       ,(expand-file-name "~/dir"))))
     (ert-info ("File wildcard + Directory")
       (urgrep-tests/check-command
        (urgrep-command "foo" :tool tool :file-wildcard "*.el" :directory "dir")
@@ -629,7 +649,11 @@ joined to compare against COMMAND."
                (urgrep-command "foo" :tool tool :directory "dir")))
       (should (string-match
                (format template "dir1 dir2" "" "--color=always -i -F" "foo")
-               (urgrep-command "foo" :tool tool :directory '("dir1" "dir2")))))
+               (urgrep-command "foo" :tool tool :directory '("dir1" "dir2"))))
+      (should (string-match
+               (format template (expand-file-name "~/dir") ""
+                       "--color=always -i -F" "foo")
+               (urgrep-command "foo" :tool tool :directory "~/dir"))))
     (ert-info ("Color")
       (should (string-match (format template "." "" "+-i -F" "foo")
                             (urgrep-command "foo" :tool tool :color nil))))))
diff --git a/urgrep.el b/urgrep.el
index 495026bba9..df86c219fb 100644
--- a/urgrep.el
+++ b/urgrep.el
@@ -191,6 +191,21 @@ for MS shells."
   "Join ARGUMENTS by spaces, quoting each argument as needed for the shell."
   (mapconcat #'urgrep--maybe-shell-quote-argument arguments " "))
 
+(defun urgrep--safe-file-name (file)
+  "Get a safe form of FILE ready to pass to an external process.
+This expands tildes and removes any remote host identifiers or quoting."
+  (when-let ((remote (file-remote-p file)))
+    (unless (equal remote (file-remote-p default-directory))
+      (error "remote file doesn't match host for `default-directory'"))
+    (setq file (file-local-name file)))
+  (setq file (file-name-unquote file))
+  (if (not (string-prefix-p "~" file))
+      file
+    (setq file (expand-file-name file))
+    (if (file-in-directory-p file default-directory)
+        (file-relative-name file)
+      (file-local-name file))))
+
 (defun urgrep--flatten-arguments (tree &optional abbrs)
   "Flatten a TREE of arguments into a single shell-quoted string.
 This also finds sublists with the `:abbreviate' key and adds the
@@ -229,7 +244,6 @@ properties defined in the `urgrep-tools' entry for TOOL."
                             props))))
     (urgrep--flatten-arguments (cl-sublis props arguments) abbrev)))
 
-
 (rx-define urgrep-regular-number (seq (any "1-9") (* digit)))
 
 
@@ -620,6 +634,9 @@ HIDDEN: non-nil to search in hidden files; defaults to nil.
 FILE-WILDCARD: a wildcard (or list of wildcards) to limit the
 files searched.
 
+DIRECTORY: the directory (or list of directories) to search in;
+if nil (the default), search in `default-directory'.
+
 GROUP: show results grouped by filename (t, the default), or if
 nil, prefix the filename on each result line.
 
@@ -632,7 +649,7 @@ COLOR: non-nil (the default) if the output should use 
color."
   (with-connection-local-variables
    (let* ((regexp-syntax (if (eq regexp t) urgrep-regexp-syntax regexp))
           (file-wildcard (ensure-list file-wildcard))
-          (directory (ensure-list directory))
+          (directory (mapcar #'urgrep--safe-file-name (ensure-list directory)))
           (tool (or (urgrep-get-tool tool)
                     (error "unknown tool %s" tool)))
           (tool-re-syntax (urgrep--get-best-syntax regexp-syntax tool))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]