{"thread":{"id":"15287","subject":"[PATCH] git.el: Add \"git grep\" functionality in a grep-style compilation buffer","startedAt":"2008-08-30T18:55:23Z","lastAt":"2008-09-09T15:14:16Z","messageCount":4,"participants":["David Christensen","David Kågedal","Rémi Vanicat"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"89237","messageId":"1220122523-68561-1-git-send-email-david@endpoint.com","threadId":"15287","inReplyTo":null,"subject":"[PATCH] git.el: Add \"git grep\" functionality in a grep-style compilation buffer","fromName":"David Christensen","fromEmail":"david@endpoint.com","sentAt":"2008-08-30T18:55:23Z","receivedAt":"2008-08-30T18:55:23Z","isPatch":true,"sender":{"key":"david@endpoint.com","avatar":"https://gravatar.com/avatar/6089b35cc409d9d15ab439753a213d7528cd5e0a04f3917fa452d8dc45296612?d=mp&s=160"},"body":"\nSigned-off-by: David Christensen <david@endpoint.com>\n---\n contrib/emacs/git.el |   10 ++++++++++\n 1 files changed, 10 insertions(+), 0 deletions(-)\n\ndiff --git a/contrib/emacs/git.el b/contrib/emacs/git.el\nindex 279610f..46a9e49 100644\n--- a/contrib/emacs/git.el\n+++ b/contrib/emacs/git.el\n@@ -1395,6 +1395,15 @@ amended version of it.\"\n   (let ((info (ewoc-data (ewoc-locate git-status))))\n     (view-file (git-fileinfo->name info))))\n \n+(defun git-grep (pat)\n+  \"Run git grep on the repository with the provided pattern;\n+results are placed in a grep-compilation buffer.\"\n+  (interactive \"sgit grep regexp: \")\n+  (unless git-status (error \"Not in git-status buffer.\"))\n+  (let ((null-device nil)\n+        (git-grep-flags nil))           ; for future extension\n+    (grep (concat \"git --no-pager grep -n \" git-grep-flags \" -e \" pat))))\n+\n (defun git-refresh-status ()\n   \"Refresh the git status buffer.\"\n   (interactive)\n@@ -1446,6 +1455,7 @@ amended version of it.\"\n     (suppress-keymap map)\n     (define-key map \"?\"   'git-help)\n     (define-key map \"*\"    mark-map)\n+    (define-key map \"%\"   'git-grep)\n     (define-key map \"h\"   'git-help)\n     (define-key map \" \"   'git-next-file)\n     (define-key map \"a\"   'git-add-file)\n-- \n1.6.0.1.90.g27a6e.dirty\n"},{"id":"90195","messageId":"871vzt8ydp.fsf@lysator.liu.se","threadId":"15287","inReplyTo":"1220122523-68561-1-git-send-email-david@endpoint.com","subject":"Re: [PATCH] git.el: Add \"git grep\" functionality in a grep-style compilation buffer","fromName":"David Kågedal","fromEmail":"davidk@lysator.liu.se","sentAt":"2008-09-09T09:25:06Z","receivedAt":"2008-09-09T09:25:06Z","isPatch":true,"sender":{"key":"davidk@lysator.liu.se","avatar":"https://avatars.githubusercontent.com/u/60530?v=4"},"body":"David Christensen <david@endpoint.com> writes:\n\n> Signed-off-by: David Christensen <david@endpoint.com>\n\nI posted a longer version of git-grep that built on the grep commands\nin Emacs 22 a while ago. It gives you a better was to navigate to the\nhits etc. I'll dig it up again.\n\n-- \nDavid Kågedal\n"},{"id":"90226","messageId":"87d4jdcqyk.dlv@maison.homelinux.org","threadId":"15287","inReplyTo":"871vzt8ydp.fsf@lysator.liu.se","subject":"Re: [PATCH] git.el: Add \"git grep\" functionality in a grep-style compilation buffer","fromName":"Rémi Vanicat","fromEmail":"vanicat@debian.org","sentAt":"2008-09-09T14:51:47Z","receivedAt":"2008-09-09T14:51:47Z","isPatch":true,"sender":{"key":"vanicat@debian.org","avatar":"https://gravatar.com/avatar/cd491a7f4c221349809a60f88fc21326b97cce2a705e318898aa74851db92409?d=mp&s=160"},"body":"From: David Kågedal <davidk@lysator.liu.se>\nSubject: [PATCH] [PATCH] git.el: Add a git-grep command\n\nThis allows easy access to git grep from Emacs.\n\nSigned-off-by: David Kågedal <davidk@lysator.liu.se>\nTested-by: Rémi Vanicat <vanicat@debian.org>\n---\nDavid Kågedal <davidk@lysator.liu.se> writes:\n\n> David Christensen <david@endpoint.com> writes:\n>\n>> Signed-off-by: David Christensen <david@endpoint.com>\n>\n> I posted a longer version of git-grep that built on the grep commands\n> in Emacs 22 a while ago. It gives you a better was to navigate to the\n> hits etc. I'll dig it up again.\n\nWas it this one ?\n\n-- \nRémi Vanicat\n contrib/emacs/git.el |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++\n 1 files changed, 52 insertions(+), 0 deletions(-)\n\ndiff --git a/contrib/emacs/git.el b/contrib/emacs/git.el\nindex c1cf1cb..57351a5 100644\n--- a/contrib/emacs/git.el\n+++ b/contrib/emacs/git.el\n@@ -49,6 +49,7 @@\n (require 'ewoc)\n (require 'log-edit)\n (require 'easymenu)\n+(require 'grep () t)\n \n \n ;;;; Customizations\n@@ -1496,6 +1497,7 @@ amended version of it.\"\n       [\"Diff File\" git-diff-file t]\n       [\"Interactive Diff File\" git-diff-file-idiff t]\n       [\"Log\" git-log-file t]\n+      ,@(if (featurep 'grep) (list [\"Grep\" git-grep t]) ())\n       \"--------\"\n       [\"Mark\" git-mark-file t]\n       [\"Mark All\" git-mark-all t]\n@@ -1584,5 +1586,55 @@ Meant to be used in `after-save-hook'.\"\n   (interactive)\n   (describe-function 'git-status-mode))\n \n+(when (featurep 'grep)\n+  (defvar git-grep-history nil)\n+\n+  (defun git-grep (regexp &optional files dir)\n+    \"Recursively grep for REGEXP in FILES in directory tree rooted at DIR.\n+The search is limited to file names matching shell pattern FILES.\n+FILES may use abbreviations defined in `grep-files-aliases', e.g.\n+entering `ch' is equivalent to `*.[ch]'.\n+\n+With \\\\[universal-argument] prefix, you can edit the constructed shell command line\n+before it is executed.\n+With two \\\\[universal-argument] prefixes, directly edit and run `git-grep-find-command'.\n+\n+Collect output in a buffer.  While find runs asynchronously, you\n+can use \\\\[next-error] (M-x next-error), or \\\\<grep-mode-map>\\\\[compile-goto-error]\n+in the grep output buffer, to go to the lines where grep found matches.\"\n+    (interactive\n+     (cond\n+       ((equal current-prefix-arg '(16))\n+       (list (read-from-minibuffer \"Run: \" \"git grep \"\n+                                   nil nil 'git-grep-history)\n+             nil))\n+       (t (let* ((regexp (grep-read-regexp))\n+                (files (grep-read-files regexp))\n+                (dir (read-directory-name \"Base directory: \"\n+                                          nil default-directory t)))\n+           (list regexp files dir)))))\n+    (when (and (stringp regexp) (> (length regexp) 0))\n+      (if (null files)\n+         (if (not (string= regexp grep-find-command))\n+             (compilation-start regexp 'grep-mode))\n+         (setq dir (file-name-as-directory (expand-file-name dir)))\n+         (let ((command (concat\n+                         \"git grep -n \"\n+                         \"-e \" (shell-quote-argument regexp)\n+                         (if (string= files \"*\")\n+                             \"\"\n+                             (concat \" -- \" (shell-quote-argument files))))))\n+           (when command\n+             (if current-prefix-arg\n+                 (setq command\n+                       (read-from-minibuffer \"Confirm: \"\n+                                             command nil nil 'git-grep-history))\n+                 (add-to-history 'git-grep-history command))\n+             (let ((default-directory dir))\n+               (compilation-start (concat \"PAGER= \" command) 'grep-mode))\n+             ;; Set default-directory if we started rgrep in the *grep* buffer.\n+             (if (eq next-error-last-buffer (current-buffer))\n+                 (setq default-directory dir))))))))\n+\n (provide 'git)\n ;;; git.el ends here\n-- \n1.6.0.1.183.gb8d57\n"},{"id":"90228","messageId":"87sks973nb.fsf@lysator.liu.se","threadId":"15287","inReplyTo":"87d4jdcqyk.dlv@maison.homelinux.org","subject":"Re: [PATCH] git.el: Add \"git grep\" functionality in a grep-style compilation buffer","fromName":"David Kågedal","fromEmail":"davidk@lysator.liu.se","sentAt":"2008-09-09T15:14:16Z","receivedAt":"2008-09-09T15:14:16Z","isPatch":true,"sender":{"key":"davidk@lysator.liu.se","avatar":"https://avatars.githubusercontent.com/u/60530?v=4"},"body":"vanicat@debian.org (Rémi Vanicat) writes:\n\n> From: David Kågedal <davidk@lysator.liu.se>\n> Subject: [PATCH] [PATCH] git.el: Add a git-grep command\n>\n> This allows easy access to git grep from Emacs.\n>\n> Signed-off-by: David Kågedal <davidk@lysator.liu.se>\n> Tested-by: Rémi Vanicat <vanicat@debian.org>\n> ---\n> David Kågedal <davidk@lysator.liu.se> writes:\n>\n>> David Christensen <david@endpoint.com> writes:\n>>\n>>> Signed-off-by: David Christensen <david@endpoint.com>\n>>\n>> I posted a longer version of git-grep that built on the grep commands\n>> in Emacs 22 a while ago. It gives you a better was to navigate to the\n>> hits etc. I'll dig it up again.\n>\n> Was it this one ?\n\nKindof. This seems to be an improved version that doesn't fail on\nolder emacsen and that adds a menu. I'm not sure who contributed that,\nbut it's probably also in some mail somewhere. I use this every day\n(but without the improvements) and I think it is really useful.\n\n> -- \n> Rémi Vanicat\n>  contrib/emacs/git.el |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++\n>  1 files changed, 52 insertions(+), 0 deletions(-)\n>\n> diff --git a/contrib/emacs/git.el b/contrib/emacs/git.el\n> index c1cf1cb..57351a5 100644\n> --- a/contrib/emacs/git.el\n> +++ b/contrib/emacs/git.el\n> @@ -49,6 +49,7 @@\n>  (require 'ewoc)\n>  (require 'log-edit)\n>  (require 'easymenu)\n> +(require 'grep () t)\n>  \n>  \n>  ;;;; Customizations\n> @@ -1496,6 +1497,7 @@ amended version of it.\"\n>        [\"Diff File\" git-diff-file t]\n>        [\"Interactive Diff File\" git-diff-file-idiff t]\n>        [\"Log\" git-log-file t]\n> +      ,@(if (featurep 'grep) (list [\"Grep\" git-grep t]) ())\n>        \"--------\"\n>        [\"Mark\" git-mark-file t]\n>        [\"Mark All\" git-mark-all t]\n> @@ -1584,5 +1586,55 @@ Meant to be used in `after-save-hook'.\"\n>    (interactive)\n>    (describe-function 'git-status-mode))\n>  \n> +(when (featurep 'grep)\n> +  (defvar git-grep-history nil)\n> +\n> +  (defun git-grep (regexp &optional files dir)\n> +    \"Recursively grep for REGEXP in FILES in directory tree rooted at DIR.\n> +The search is limited to file names matching shell pattern FILES.\n> +FILES may use abbreviations defined in `grep-files-aliases', e.g.\n> +entering `ch' is equivalent to `*.[ch]'.\n> +\n> +With \\\\[universal-argument] prefix, you can edit the constructed shell command line\n> +before it is executed.\n> +With two \\\\[universal-argument] prefixes, directly edit and run `git-grep-find-command'.\n> +\n> +Collect output in a buffer.  While find runs asynchronously, you\n> +can use \\\\[next-error] (M-x next-error), or \\\\<grep-mode-map>\\\\[compile-goto-error]\n> +in the grep output buffer, to go to the lines where grep found matches.\"\n> +    (interactive\n> +     (cond\n> +       ((equal current-prefix-arg '(16))\n> +       (list (read-from-minibuffer \"Run: \" \"git grep \"\n> +                                   nil nil 'git-grep-history)\n> +             nil))\n> +       (t (let* ((regexp (grep-read-regexp))\n> +                (files (grep-read-files regexp))\n> +                (dir (read-directory-name \"Base directory: \"\n> +                                          nil default-directory t)))\n> +           (list regexp files dir)))))\n> +    (when (and (stringp regexp) (> (length regexp) 0))\n> +      (if (null files)\n> +         (if (not (string= regexp grep-find-command))\n> +             (compilation-start regexp 'grep-mode))\n> +         (setq dir (file-name-as-directory (expand-file-name dir)))\n> +         (let ((command (concat\n> +                         \"git grep -n \"\n> +                         \"-e \" (shell-quote-argument regexp)\n> +                         (if (string= files \"*\")\n> +                             \"\"\n> +                             (concat \" -- \" (shell-quote-argument files))))))\n> +           (when command\n> +             (if current-prefix-arg\n> +                 (setq command\n> +                       (read-from-minibuffer \"Confirm: \"\n> +                                             command nil nil 'git-grep-history))\n> +                 (add-to-history 'git-grep-history command))\n> +             (let ((default-directory dir))\n> +               (compilation-start (concat \"PAGER= \" command) 'grep-mode))\n> +             ;; Set default-directory if we started rgrep in the *grep* buffer.\n> +             (if (eq next-error-last-buffer (current-buffer))\n> +                 (setq default-directory dir))))))))\n> +\n>  (provide 'git)\n>  ;;; git.el ends here\n\n-- \nDavid Kågedal\n"}]}