Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

magit-grep, how to include or exlude file extensions?

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))))))
like image 588
eugene Avatar asked Jul 18 '13 23:07

eugene


2 Answers

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" :-)

like image 54
tarsius Avatar answered Nov 06 '22 13:11

tarsius


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).

like image 37
Slaven Rezic Avatar answered Nov 06 '22 15:11

Slaven Rezic