Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More granular helm-grep-do-git-grep #1252

Closed
hinrik opened this issue Oct 30, 2015 · 12 comments

Comments

Projects
None yet
5 participants
@hinrik
Copy link

commented Oct 30, 2015

Can you change helm-grep-do-git-grep (and helm-grep-git-1 I suppose) so that they can be used to perform a git grep in exactly the directory I want? Now it always passes the value of default-directory to vc-find-root, so I cannot do a grep in a subdirectory of a repository.

I would really like to, since the repository I'm working with is huge (so searching all of it is slow and unnecessary), and there are two top-level directories in it that I frequently call git grep in, for different tasks.

@thierryvolpiatto

This comment has been minimized.

Copy link
Member

commented Oct 30, 2015

Hinrik Örn Sigurðsson notifications@github.com writes:

Can you change helm-grep-do-git-grep (and helm-grep-git-1 I suppose)
so that they can be used to perform a git grep in exactly the
directory I want?

Use helm-find-files to navigate to your directory and start git-grep
from there (M-g g).

NOTE: You could have had this information by using C-h m.

Otherwise you may have also a look at helm-grep-ag, and the git-grep
that comes with helm-ls-git.

Thierry
https://emacs-helm.github.io/helm/

@hinrik

This comment has been minimized.

Copy link
Author

commented Oct 31, 2015

Use helm-find-files to navigate to your directory and start git-grep from there (M-g g).

M-g g still searches the entire repository, because helm-grep-git-1 always passes the supplied directory to vc-find-root first: https://github.com/emacs-helm/helm/blob/master/helm-grep.el#L1301.

M-g a (ag) and M-g s (grep) both do the right thing of course, but they don't have the benefits of git grep.

helm-ls-git only allows marking specific files for searching in, not directories.

The only thing I've found that will do a git grep on a directory of my choosing is (vc-git-grep "someregex" "*" "/some/repo/subdir"), but that's not helm-enabled.

thierryvolpiatto added a commit that referenced this issue Oct 31, 2015

Allow git grepping subdirectories only (#1252).
* helm-grep.el (helm-grep-git-grep-command): Pass path spec at end after "--".
(helm-grep-git-1): Do it.
(helm-grep-do-git-grep): With a prefix arg grep whole repo.

thierryvolpiatto added a commit that referenced this issue Oct 31, 2015

Allow also git grepping whole repo from hff (#1252).
* helm-files.el (helm-ff-git-grep): Pass the pref arg to helm-grep-git-1.
@thierryvolpiatto

This comment has been minimized.

Copy link
Member

commented Oct 31, 2015

Hinrik Örn Sigurðsson notifications@github.com writes:

Use helm-find-files to navigate to your directory and start git-grep from there (M-g g).

M-g g still searches the entire repository,

Yes, sorry I misunderstood what you wanted.
So now helm-grep-do-git-grep with a prefix arg grep whole repo and
without grep only current dir.
Same from hff.

I have fixed helm-ls-git also, but it behave differently (as before).

Thierry
https://emacs-helm.github.io/helm/

@hinrik

This comment has been minimized.

Copy link
Author

commented Oct 31, 2015

Thanks!

@pcompassion

This comment has been minimized.

Copy link

commented Oct 2, 2016

what if I want the behavior reversed?

helm-grep-do-git-grep without a prefix arg grep whole repo and
with grep only current dir.

Because I most often need to grep from whole repository

I tried looking at the source of helm-grep-do-git-grep but couldn't understand.

@thierryvolpiatto

This comment has been minimized.

Copy link
Member

commented Oct 2, 2016

pcompassion notifications@github.com writes:

what if I want the behavior reversed?

helm-grep-do-git-grep without a prefix arg grep whole repo and
with grep only current dir.

Because I most often need to grep from whole repository

I tried looking at the source of helm-grep-do-git-grep but couldn't understand.

You can write your own command calling helm-grep-git-1 with its ALL
argument non--nil:

(helm-grep-git-1 default-directory t)

If you want to be able to grep current-dir only with prefix arg, modify
helm-grep-do-git-grep like this.

(helm-grep-git-1 default-directory (null arg))

Note: I don't want this to be the default, I prefer the actual behavior.

Thierry

@pcompassion

This comment has been minimized.

Copy link

commented Oct 2, 2016

Sorry but I don't get how to get

C-g (or any keybinding) to do grep in repository root with (helm-grep-git-1 default-directory t)
and C-u C-g to do grep in current directory with (helm-grep-git-1 default-directory (null arg))

   (defun grep-whole-git ()
     (interactive)
     (helm-do-grep-1 default-directory t))
   (global-set-key (kbd "C-c g") 'grep-whole-git)


   (defun grep-subdirectory ()
     (interactive)
     (helm-grep-git-1 default-directory (null arg)))
   (global-set-key (kbd "C-u C-c g") 'grep-subdirectory)

I get error

Error (use-package): helm :init: Key sequence C-u C-c g starts with non-prefix key C-u

If you want to be able to grep current-dir only with prefix arg, modify
helm-grep-do-git-grep like this. How do I do that? I don't think you mean, modify helm-grep.el file.

I also get Wrong type argument: stringp, 47 when I do C-g

@xuchunyang

This comment has been minimized.

Copy link
Member

commented Oct 2, 2016

@pcompassion give the following a try, C-c g to search whole repo and C-u C-c g to search current directory.

(defun my-helm-grep-do-git-grep (not-all)
  (interactive "P")
  (helm-grep-git-1 default-directory (null not-all)))

(global-set-key (kbd "C-c g") 'my-helm-grep-do-git-grep)
@pcompassion

This comment has been minimized.

Copy link

commented Oct 2, 2016

Actually I managed to solve it, not sure if this is best approach though.

I copied helm-grep-git-1 from helm-grep.el to my init.el and changed if all to if (not all) when calling helm-do-grep-1

(defun helm-grep-git-1 (directory &optional all default input)
(require 'vc)
(let* (helm-grep-default-recurse-command
;; Expand filename of each candidate with the git root dir.
;; The filename will be in the help-echo prop.
(helm-grep-default-directory-fn (lambda ()
(vc-find-root directory ".git")))
(helm-ff-default-directory (funcall helm-grep-default-directory-fn)))
(cl-assert helm-ff-default-directory nil "Not inside a Git repository")
(helm-do-grep-1 (if (not all) '("") `(,(expand-file-name directory)))
nil 'git nil default input)))

Oh. xuchnyang I see that is better way of doing the same thing.

@thierryvolpiatto

This comment has been minimized.

Copy link
Member

commented Oct 2, 2016

pcompassion notifications@github.com writes:

Sorry but I don't get how to get

C-g (or any keybinding)

Never (I mean NEVER!) bind something to C-g in emacs.

and C-u C-g

And never use C-u C-g to bind something in emacs.

to do grep in current directory with (helm-grep-git-1
default-directory (null arg))

(defun grep-whole-git ()
(interactive)
(helm-do-grep-1 default-directory t))
(global-set-key (kbd "C-c g") 'grep-whole-git)

This will grep whole directory only.

(defun grep-subdirectory ()
(interactive)
(helm-grep-git-1 default-directory (null arg)))
(global-set-key (kbd "C-u C-c g") 'grep-subdirectory)

Argument ARG is unknown in your function, see how interactive specs
work in elisp manual, what you want is something like this:

(defun grep-subdirectory (arg)
  (interactive "P")
  (helm-grep-git-1 default-directory (null arg)))

Here if you call this command with C-u, ARG will be bound to '(4).
If you don't prefix your command with C-u ARG will be == to nil.

I get error

Error (use-package): helm :init: Key sequence C-u C-c g starts with non-prefix key C-u

Of course, use instead:

(global-set-key (kbd "C-c g") 'grep-subdirectory)

More generally try to see emacs manual about prefix args and how to use
them.

Thierry

@thierryvolpiatto

This comment has been minimized.

Copy link
Member

commented Oct 2, 2016

pcompassion notifications@github.com writes:

Actually I managed to solve it, not sure if this is best approach though.

It is not, but I see Chunyang already showed you the best approach to use.

Thierry

@aguynamedben

This comment has been minimized.

Copy link

commented Jul 10, 2018

@xuchunyang Thank you! This helped me switch away from Projectile. 🙏

It doesn't have to be a default, but making this a config option (helm-ls-git-search-root?) that's easy to enable would make switching from Projectile more friendly.

I love helm, thanks for the work you all put into it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.