git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] git.el: Add a git-grep command

From
David Kågedal <davidk@lysator.liu.se>
Date
Feb 22, 2008, 11:43 UTC
Message-ID
<877igx9pxu.fsf@lysator.liu.se>
In-Reply-To
<87d4qpgs9y.dlv@maison.homelinux.org>
Remi Vanicat <vanicat@debian.org> writes:
> Here is a modification with inclusion of git-grep only when the grep
> library is available. 

Nice. I didn't even know that require had more parameters. But I'd write nil instead of () in this case.

Show 97 quoted lines
> To put it in another file might be a good idea to, but in this case,
> may be we could break this huge 1786 file in several smaller file.
>
> From 403143a61bf8f77d042893765b19cf7cc7062e59 Mon Sep 17 00:00:00 2001
> From: David Kågedal <davidk@lysator.liu.se>
> Date: Fri, 22 Feb 2008 11:57:25 +0100
> Subject: [PATCH] git.el: Add a git-grep command
> MIME-Version: 1.0
> Content-Type: text/plain; charset=utf-8
> Content-Transfer-Encoding: 8bit
>
> This allows easy access to git grep from Emacs.
>
> Signed-off-by: David Kågedal <davidk@lysator.liu.se>
> Signed-off-by: Rémi Vanicat <vanicat@debian.org>
> ---
>  contrib/emacs/git.el |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 files changed, 52 insertions(+), 0 deletions(-)
>
> diff --git a/contrib/emacs/git.el b/contrib/emacs/git.el
> index f69b697..afaf187 100644
> --- a/contrib/emacs/git.el
> +++ b/contrib/emacs/git.el
> @@ -49,6 +49,7 @@
>  (require 'ewoc)
>  (require 'log-edit)
>  (require 'easymenu)
> +(require 'grep () t)
>  
>  
>  ;;;; Customizations
> @@ -1496,6 +1497,7 @@ amended version of it."
>        ["Diff File" git-diff-file t]
>        ["Interactive Diff File" git-diff-file-idiff t]
>        ["Log" git-log-file t]
> +      ,@(if (featurep 'grep) (list ["Grep" git-grep t]) ())
>        "--------"
>        ["Mark" git-mark-file t]
>        ["Mark All" git-mark-all t]
> @@ -1584,5 +1586,55 @@ Meant to be used in `after-save-hook'."
>    (interactive)
>    (describe-function 'git-status-mode))
>  
> +(when (featurep 'grep)
> +  (defvar git-grep-history nil)
> +
> +  (defun git-grep (regexp &optional files dir)
> +    "Recursively grep for REGEXP in FILES in directory tree rooted at DIR.
> +The search is limited to file names matching shell pattern FILES.
> +FILES may use abbreviations defined in `grep-files-aliases', e.g.
> +entering `ch' is equivalent to `*.[ch]'.
> +
> +With \\[universal-argument] prefix, you can edit the constructed shell command line
> +before it is executed.
> +With two \\[universal-argument] prefixes, directly edit and run `git-grep-find-command'.
> +
> +Collect output in a buffer.  While find runs asynchronously, you
> +can use \\[next-error] (M-x next-error), or \\<grep-mode-map>\\[compile-goto-error]
> +in the grep output buffer, to go to the lines where grep found matches."
> +    (interactive
> +     (cond
> +       ((equal current-prefix-arg '(16))
> +	(list (read-from-minibuffer "Run: " "git grep "
> +				    nil nil 'git-grep-history)
> +	      nil))
> +       (t (let* ((regexp (grep-read-regexp))
> +		 (files (grep-read-files regexp))
> +		 (dir (read-directory-name "Base directory: "
> +					   nil default-directory t)))
> +	    (list regexp files dir)))))
> +    (when (and (stringp regexp) (> (length regexp) 0))
> +      (if (null files)
> +	  (if (not (string= regexp grep-find-command))
> +	      (compilation-start regexp 'grep-mode))
> +	  (setq dir (file-name-as-directory (expand-file-name dir)))
> +	  (let ((command (concat
> +			  "git grep -n "
> +			  "-e " (shell-quote-argument regexp)
> +			  (if (string= files "*")
> +			      ""
> +			      (concat " -- " (shell-quote-argument files))))))
> +	    (when command
> +	      (if current-prefix-arg
> +		  (setq command
> +			(read-from-minibuffer "Confirm: "
> +					      command nil nil 'git-grep-history))
> +		  (add-to-history 'git-grep-history command))
> +	      (let ((default-directory dir))
> +		(compilation-start (concat "PAGER= " command) 'grep-mode))
> +	      ;; Set default-directory if we started rgrep in the *grep* buffer.
> +	      (if (eq next-error-last-buffer (current-buffer))
> +		  (setq default-directory dir))))))))
> +
>  (provide 'git)
>  ;;; git.el ends here
> -- 
> 1.5.4.2.191.g7b407
-- 
David Kågedal
Previous: Remi VanicatNext: Xavier Maillard
Message 13 of 17 in “git.el: Add a git-grep command”
  1. git.el: Add a git-grep commandDavid Kågedal, Feb 19, 2008
  2. Remi VanicatFeb 21, 2008
  3. David KågedalFeb 21, 2008
  4. Xavier MaillardFeb 22, 2008
  5. Karl HasselströmFeb 22, 2008
  6. David KågedalFeb 22, 2008
  7. Karl HasselströmFeb 22, 2008
  8. Jakub NarebskiFeb 22, 2008
  9. David KågedalFeb 22, 2008
  10. Jakub NarebskiFeb 22, 2008
  11. David KågedalFeb 22, 2008
  12. Remi VanicatFeb 22, 2008
  13. David KågedalFeb 22, 2008
  14. Xavier MaillardFeb 23, 2008
  15. Remi VanicatFeb 23, 2008
  16. Xavier MaillardFeb 23, 2008
  17. Xavier MaillardFeb 24, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.