phcerdan
phcerdan

Reputation: 812

git grep showing absolute paths

I would like to use git grep from outside the repository, for example editing a file in vim. This works:

git -C /path/to/gitrepo/ grep 'my search'

The problem is that the results show relative path from /path/to/gitrepo. So, I cannot open them directly.

Is there any way to generate the full file path to the matches? From git would be great, but a hacky solution to prepend and build the absolute path will do too.

Upvotes: 4

Views: 1389

Answers (2)

apang1992 pang
apang1992 pang

Reputation: 11

[alias]
        egrep = "!f1() { git grep --line-number \"$1\" | sed \"s,^,$PWD/,g\"  | sed \"s/^\\(.*:[0-9]\\{1,10\\}\\):/\\1 /g\" ; }; f1"

This is my git alias config.
Usage: git egrep 'my search' . This would give you filenames with fullpath and line number.

Upvotes: 1

Armali
Armali

Reputation: 19395

The usual output is just one line per match (though this does depend on options). In this case, a simple git -C $path grep pattern | sed s,^,$path, does the trick (assuming no "," in paths). – torek

Upvotes: 2

Related Questions