Last active
June 20, 2023 19:50
-
-
Save indifferentcats/9b6c4b9df9ca25b4c8c12d02427b102b to your computer and use it in GitHub Desktop.
Search through all branches and history of a repository. Can be sourced or put in a bin folder and run as a script. All passed parameters go straight to `git grep` but it searches through the last 300 commits unless you use `-y`. See git-grep(1) for valid options.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
gitgrep () | |
{ | |
ref_max=300; | |
if [ -z "$1" ]; then | |
echo "Usage: gitgrep [-y num] <search_options> pattern"; | |
echo " -y num is max number of back refs to reverse-order search."; | |
return 1; | |
fi; | |
if [ "$1" == "-y" ]; then | |
shift; | |
ref_max="$1"; | |
shift; | |
fi; | |
sed_pat=$(for ref in $(git rev-list --all | head -$ref_max); do | |
branch=$(git branch --contains $ref | head -1 | cut -c3-); | |
echo "s/^${ref}:/${branch}:${ref}:/;"; | |
done ); | |
git grep "$@" $(git rev-list --all | head -$ref_max) | sed -e "${sed_pat}" | |
} | |
# test to see if we're sourced and if not, execute the method with passed params | |
(return 0 2>/dev/null) || gitgrep "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment