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

2

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))))))
Bonedry answered 18/7, 2013 at 23:41 Comment(1)
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.Amathist
O
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" :-)

Onida answered 30/12, 2013 at 2:14 Comment(0)
M
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).

Mushy answered 19/7, 2013 at 6:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.