A Helpful Command For Searching Git Submodules

Sometimes you need to search through a git repository containing sixty directories. Each directory is a separate git submodule, meaning that they're fully-fleshed-out git repositories in their own right, unconcerned about their siblings or parent repositories.

I was filing a pull request affecting a CSS file in one of the repositories, so I needed to check all the other repositories. They're WordPress child themes; the pull request is to their parent. Past changes to the parent have conflicted with CSS in some of the other repositories.

The easiest way to check for clashing styles was to search for selectors in each theme, and the easiest way to do that is with a tool like grep.

git-grep is specifically built to search within git repositories, but it doesn't search within submodules, and it doesn't have a way to limit the search by filename or filetype.

ack and ag are popular grep replacements, but I'm not familiar with either.

Here's my solution:

#! /bin/bash

if [ -z $1 ] || [ -z $2 ];
	echo "Usage: deepgrep <filetype> <string to search for>"

find . -type f -name "*.$1" -print0 | xargs -0 grep --color=auto -0 $2

Save the file as 'deepgrep' somewhere in your $PATH. Mine's in a folder in my dotfiles.

With  deepgrep css .is-video I was able to see that there were indeed no conflicts, and it was safe to submit the pull request.

If you don't want to bother with saving the file, you could set an alias in your terminal, or run this in the terminal directly:

find . -type f -name "*.css" -print0 | xargs -0 grep --color=auto -0 "The string you want to search for"

If you prefer ag, try ag -UG <filetype> <string to search for>

Happy searching!