6

I'd like to give some options to magit-grep

running it with option foo results in

git --no-pager grep -n foo

I'd like to give options to it

git --no-pager grep -n foo (options to include *.html and exclude *.py, etc)

It doesn't have to be magit-grep, what's the best git grep solution in emacs?


magit-grep

(magit-define-command grep (&optional pattern)
  (interactive)
  (let ((pattern (or pattern
                     (read-string "git grep: "
                                  (shell-quote-argument (grep-tag-default))))))
    (with-current-buffer (generate-new-buffer "*Magit Grep*")
      (let ((default-directory (magit-get-top-dir)))
        (insert magit-git-executable " "
                (mapconcat 'identity magit-git-standard-options " ")
                " grep -n "
                (shell-quote-argument pattern) "\n\n")
        (magit-git-insert (list "grep" "--line-number" pattern))
        (grep-mode)
        (pop-to-buffer (current-buffer))))))
elemakil
  • 3,681
  • 28
  • 53
eugene
  • 39,839
  • 68
  • 255
  • 489
  • I think just `git grep 'expression' -- '*.html'` should do. Maybe you could extend this function to ask you for the file masks if given universal argument. –  Jul 19 '13 at 13:50

2 Answers2

14

I have removed magit-grep, as it was just a severely crippled version of rgrep. vc-git-grep is actually an improvement, so you should use that. (Nowadays magit-grep is defined as an alias for the vc command, to avoid disturbing anyone who previously used the magit variant. A classic case of "add features by looking what already exists elsewhere and then remove our code" :-)

tarsius
  • 8,147
  • 5
  • 32
  • 46
2

One possibility is a combination og git-grep and git-ls-files:

git grep ... `git ls-files | grep -- '\.html$'`

But this works only if the output of git-ls-files does not exceed the maximum command line size on your system (which is a couple of 100K on modern systems).

Slaven Rezic
  • 4,571
  • 14
  • 12