Tool for searching across branches and through history in a Git repository

gitsearchsoftware-rec

I'm looking for a tool, or for suggestions towards a script, that would be able to search a Git repository for files based on both filenames and file contents (find/grep-like). It would need to be able to search not just in the currently checked out branch, but through that branch's history as well as in other branches.

The very specific example of a Git repository that I have is a checkout of dspinellis' unix-history-repo. I often look for historical implementations of things in there, but it's a pain to track down files as I often need to guess what branch I need to be looking at (there are 165 branches in that repository).

Example of thing I would like to do with this tool is to find the wow command, which may have been an external command or a built-in command in sh or csh at some point, if it existed as part of some early BSD Unix (if it existed at all). To do this, I would want to search for wow.* as a filename pattern, and also for files that may at some point have included a wow C function, across the 165 branches of dspinellis' Git repository.

Best Answer

Heh, guess what I’ve been doing too...

To look for a file name across all branches, I use

git log --all --name-only --pretty=format:%H -- wow\*

wow can be replaced by any glob. This runs quite quickly on the Unix history repository. The format shows the hash leading to the creation of the matching file, so you can then check the tree out at that point and explore further.

To search file contents across all branches, I use

git rev-list --all | xargs git grep "excited too"

which lists all commit objects and searches through them. This is very, very slow on the Unix history repository; listing all the branches and grepping there is quicker:

git grep "excited too" $(git branch -r | awk '{print $1}')
Related Question