Skip to contentSkip to conversation

git.el: Add a git-grep command

PATCH In review David Kågedal 5 participants 17 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 git.el: Add a git-grep command

This allows easy access to git grep from Emacs.

---
 contrib/emacs/git.el |   50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 50 insertions(+), 0 deletions(-)

This works for me, but before including it someone else should try it. It might only work in Emacs 22, for instance.

But when it works, it is really useful.

contrib/emacs/git.el +50 -0
index f69b697..898e70a 100644
@@ -49,6 +49,7 @@
 (require 'ewoc)
 (require 'log-edit)
 (require 'easymenu)
+(require 'grep)
 
 
 ;;;; Customizations
@@ -1584,5 +1585,54 @@ Meant to be used in `after-save-hook'."
   (interactive)
   (describe-function 'git-status-mode))
 
+(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
Signature · 5 lines
1.5.4.2.148.g410dc


-- 
David Kågedal
#1 lore raw

This allows easy access to git grep from Emacs.

Very useful. Would it be possible to save the history from sessions to sessions ?

Xavier

Signature · 3 lines
http://www.gnu.org
http://www.april.org
http://www.lolica.org
#2 lore raw
In reply to David Kågedal
Quoted 2 lines · David Kågedal

This works for me, but before including it someone else should try it. It might only work in Emacs 22, for instance.

It works for me (also using emacs 22).

I was a bit confused about the filename pattern selection, though: the promt suggested "ch", which, when I accepted it, was converted to "*.[ch]". However, manually specifying "*.c" works as well. I'm not sure if this is specific to your patch or something affecting all grep stuff in emacs (though I can't recall seeing it before).

Signature · 2 lines
Karl Hasselström, [email protected]
      www.treskal.com/kalle
#3 lore raw
Quoted 5 lines · Karl Hasselström
Quoted 2 lines · David Kågedal

This works for me, but before including it someone else should try it. It might only work in Emacs 22, for instance.

Quoted 7 lines

It works for me (also using emacs 22).

I was a bit confused about the filename pattern selection, though: the promt suggested "ch", which, when I accepted it, was converted to "*.[ch]". However, manually specifying "*.c" works as well. I'm not sure if this is specific to your patch or something affecting all grep stuff in emacs (though I can't recall seeing it before).

This isn't invented by me, but is "standard" in the grep commands in Emacs 22. See the grep-files-aliases variable and the documentation of rgrep.

Signature · 1 line
David Kågedal
#4 lore raw
Quoted 11 lines · David Kågedal
Quoted 5 lines · Karl Hasselström

I was a bit confused about the filename pattern selection, though:
the promt suggested "ch", which, when I accepted it, was converted to "*.[ch]". However, manually specifying "*.c" works as well. I'm not sure if this is specific to your patch or something affecting all grep stuff in emacs (though I can't recall seeing it before).

This isn't invented by me, but is "standard" in the grep commands in Emacs 22. See the grep-files-aliases variable and the documentation of rgrep.

Oh. OK. Thanks.

Signature · 2 lines
Karl Hasselström, [email protected]
      www.treskal.com/kalle
#5 lore raw
In reply to David Kågedal
Quoted 2 lines · David Kågedal

This allows easy access to git grep from Emacs.

You might want to add a menu:

@@ -1497,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]
+ ["Grep" git-grep t]
"--------"
["Mark" git-mark-file t]
["Mark All" git-mark-all t]

Signature · 1 line
Rémi Vanicat
#6 lore raw
Quoted 7 lines · Remi Vanicat
Quoted 2 lines · David Kågedal

This allows easy access to git grep from Emacs.

You might want to add a menu:

Note that git-grep is completely independent of git-status. But a menu entry in git-status might still be a good idea.

Quoted 8 lines
(quoted diff) +1 -0
@@ -1497,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]
+      ["Grep" git-grep t]
       "--------"
       ["Mark" git-mark-file t]
       ["Mark All" git-mark-all t]
Signature · 1 line
David Kågedal
#7 lore raw
In reply to David Kågedal
Quoted 2 lines · David Kågedal

This works for me, but before including it someone else should try it. It might only work in Emacs 22, for instance.

1077:[emacs@git/contrib/emacs]# LC_ALL=en_EN make
emacs -batch -f batch-byte-compile git.el
Loading /usr/share/emacs/site-lisp/site-start.d/php-mode-init.el (source)...
[...]
While compiling toplevel forms in file /home/jnareb/git/contrib/emacs/git.el:
  !! File error (("Cannot open load file" "grep"))
Done
make: *** [git.elc] Error 1
1078:[emacs@git/contrib/emacs]# emacs --version
GNU Emacs 21.4.1
Signature · 3 lines
Jakub Narebski
Poland
ShadeHawk on #git
#8 lore raw
Quoted 14 lines · Jakub Narebski
Quoted 2 lines · David Kågedal

This works for me, but before including it someone else should try it. It might only work in Emacs 22, for instance.

1077:[emacs@git/contrib/emacs]# LC_ALL=en_EN make
emacs -batch -f batch-byte-compile git.el
Loading /usr/share/emacs/site-lisp/site-start.d/php-mode-init.el (source)...
[...]
While compiling toplevel forms in file /home/jnareb/git/contrib/emacs/git.el:
  !! File error (("Cannot open load file" "grep"))
Done
make: *** [git.elc] Error 1
Quoted 2 lines
1078:[emacs@git/contrib/emacs]# emacs --version
GNU Emacs 21.4.1

As I suspected. The problem is that the grep commands were rewritten for Emacs 22, and lots of it can be reused. So to write a similar command for Emacs 21 probably requires a bit more work. And since I use Emacs 22 I'm not sure I will feel motivated enough...

Signature · 1 line
David Kågedal
#9 lore raw
Quoted 13 lines · David Kågedal
Quoted 12 lines · Jakub Narebski
Quoted 2 lines · David Kågedal

This works for me, but before including it someone else should try it. It might only work in Emacs 22, for instance.

emacs -batch -f batch-byte-compile git.el
[...]
While compiling toplevel forms in file git/contrib/emacs/git.el:
!! File error (("Cannot open load file" "grep"))
Done
make: *** [git.elc] Error 1

Quoted 7 lines
Quoted 2 lines
1078:[emacs@git/contrib/emacs]# emacs --version
GNU Emacs 21.4.1

As I suspected. The problem is that the grep commands were rewritten for Emacs 22, and lots of it can be reused. So to write a similar command for Emacs 21 probably requires a bit more work. And since I use Emacs 22 I'm not sure I will feel motivated enough...

Could you make it at least conditional on the Emacs version used, or even better depending on the fact if "grep" 'library' is available?

I'd like to be able to use git.el with my Emacs, even without some functionality. TIA.

Signature · 2 lines
Jakub Narebski
Poland
#10 lore raw
Quoted 17 lines · Jakub Narebski
Quoted 16 lines · David Kågedal
Quoted 14 lines · Jakub Narebski
Quoted 2 lines · David Kågedal

This works for me, but before including it someone else should try it. It might only work in Emacs 22, for instance.

emacs -batch -f batch-byte-compile git.el
[...]
While compiling toplevel forms in file git/contrib/emacs/git.el:
!! File error (("Cannot open load file" "grep"))
Done
make: *** [git.elc] Error 1

1078:[emacs@git/contrib/emacs]# emacs --version
GNU Emacs 21.4.1
Quoted 7 lines
Quoted 4 lines

As I suspected. The problem is that the grep commands were rewritten for Emacs 22, and lots of it can be reused. So to write a similar command for Emacs 21 probably requires a bit more work. And since I use Emacs 22 I'm not sure I will feel motivated enough...

Could you make it at least conditional on the Emacs version used, or even better depending on the fact if "grep" 'library' is available?

Sure. We could e.g. put it in its own file, git-grep.el.

Quoted 2 lines

I'd like to be able to use git.el with my Emacs, even without some functionality. TIA.

Absolutely, I hope this doesn't get included until this is resolved.

Signature · 1 line
David Kågedal
#11 lore raw

Here is a modification with inclusion of git-grep only when the grep library is available.

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 <[email protected]>
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.

---
 contrib/emacs/git.el |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 52 insertions(+), 0 deletions(-)
contrib/emacs/git.el +52 -0
index f69b697..afaf187 100644
@@ -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
Signature · 6 lines
1.5.4.2.191.g7b407



-- 
Rémi Vanicat
#12 lore raw
Quoted 2 lines · Remi Vanicat

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.

Quoted 97 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 <[email protected]>
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.

---
 contrib/emacs/git.el |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 52 insertions(+), 0 deletions(-)
contrib/emacs/git.el +52 -0
index f69b697..afaf187 100644
@@ -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
Signature · 1 line
1.5.4.2.191.g7b407
Signature · 1 line
David Kågedal
#13 lore raw
In reply to Remi Vanicat

Hi,

Here is a modification with inclusion of git-grep only when the grep library is available.

+(require 'grep () t)

+(when (featurep 'grep)
+ (defvar git-grep-history nil)
+
+ (defun git-grep (regexp &optional files dir)

Why not just do something like this ?

(when (require 'grep () t)

      (defvar ...)
      (defun git-grep ...))

Regards,

Xavier

Signature · 3 lines
http://www.gnu.org
http://www.april.org
http://www.lolica.org
#14 lore raw
Quoted 12 lines · Xavier Maillard

Hi,

Here is a modification with inclusion of git-grep only when the grep library is available.

+(require 'grep () t)

+(when (featurep 'grep)
+ (defvar git-grep-history nil)
+
+ (defun git-grep (regexp &optional files dir)

Quoted 5 lines

Why not just do something like this ?

(when (require 'grep () t)

      (defvar ...)
      (defun git-grep ...))

Because I wanted require to stay on top of the file, but I didn't want to put the rest of the git-grep stuff there.

(the real reason is because I did not check in the doc for the return value of require, and so I didn't knew that I could do that, but still, I believe that my after the fact argument are good.)

Signature · 1 line
Rémi Vanicat
#15 lore raw
Quoted 12 lines · Xavier Maillard

Hi,

Here is a modification with inclusion of git-grep only when the grep library is available.

+(require 'grep () t)

+(when (featurep 'grep)
+ (defvar git-grep-history nil)
+
+ (defun git-grep (regexp &optional files dir)

Quoted 5 lines

Why not just do something like this ?

(when (require 'grep () t)

      (defvar ...)
      (defun git-grep ...))

Because I wanted require to stay on top of the file, but I didn't want to put the rest of the git-grep stuff there.

Good point. Though, you can still "embed" the require form directly into the defun. This is thing I have already seen in the past. Dunno if it is a convention or a coding style but something like:

(defun git-grep ()
"Docstring"
(interactive)
(when (require 'grep nil t)
here the rest
))

is doable too. Maybe the if-else form would be better though with an else clause to (error "No grep package foud.").

Well just kidding, your patch is okay ;)

Xavier

Signature · 3 lines
http://www.gnu.org
http://www.april.org
http://www.lolica.org
#16 lore raw
In reply to Remi Vanicat
Quoted 12 lines · Xavier Maillard

Hi,

Here is a modification with inclusion of git-grep only when the grep library is available.

+(require 'grep () t)

+(when (featurep 'grep)
+ (defvar git-grep-history nil)
+
+ (defun git-grep (regexp &optional files dir)

Quoted 5 lines

Why not just do something like this ?

(when (require 'grep () t)

      (defvar ...)
      (defun git-grep ...))

Because I wanted require to stay on top of the file, but I didn't want to put the rest of the git-grep stuff there.

Good point. Though, you can still "embed" the require form directly into the defun. This is thing I have already seen in the past. Dunno if it is a convention or a coding style but something like:

(defun git-grep ()
"Docstring"
(interactive)
(when (require 'grep nil t)
here the rest
))

is doable too. Maybe the if-else form would be better though with an else clause to (error "No grep package foud.").

Well just kidding, your patch is okay ;)

Xavier

Signature · 3 lines
http://www.gnu.org
http://www.april.org
http://www.lolica.org