Skip to content

Instantly share code, notes, and snippets.

@atoa
Created January 5, 2024 17:50
Show Gist options
  • Save atoa/6bb01aa4193c77229f62c98f8b372b21 to your computer and use it in GitHub Desktop.
Save atoa/6bb01aa4193c77229f62c98f8b372b21 to your computer and use it in GitHub Desktop.
script to recursively git grep multiple repos in subdirectories
#!/usr/bin/env sh
#
# Git grep on multiple repos in descendant directories from cwd
set -euo pipefail
IFS=$'\n\t'
# how many directories to recurse - helps with performance to keep it low
# but may miss subdirectories if too low
# it can be overriden by setting this variable in the calling shell
MAXDEPTH=${MAXDEPTH:-3}
find . -maxdepth "$MAXDEPTH" -type d -name .git -print0 | \
xargs -0 -I@@ sh -c \
'cd "@@/.." ; GIT_PAGER="" git grep --color "$@" | sed -e "s#^#${PWD}/#"' \
-- "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment