Jenkins : Grepping all sources

If you find yourself needing to look for a certain pattern among all sources files in Jenkins and its plugins - anything in - try cloning and then running this handy one-liner in it (replace the "basic"-syntax regexp with whatever you need, or use -E for extended regexp):

git branch -a | fgrep -v '*' | xargs git grep 'SomeClass\.staticMethod' | perl -n -e 'print "$repo/$fname: $text\n" if ($repo, $fname, $text) = /^remotes\/origin\/(.+)\/master:([^:]+):(?:[ \t]*)(.+)$/'

(This greps files as of the tip of the master branch only. Checking historical versions is much slower.)

To limit the search to particular paths:

(git branch -a | fgrep -v '*'; echo -- pom.xml) | xargs git grep 'credentials' | perl -n -e 'print "$repo/$fname: $text\n" if ($repo, $fname, $text) = /^remotes\/origin\/(.+)\/master:([^:]+):(?:[ \t]*)(.+)$/'

Another useful trick is to collect the full text of all source files matching the pattern, so you evaluate matches in context:

git show `git branch -a | fgrep /master | xargs git grep -l 'SomeClass\.staticMethod'`

Similarly, to grep file names and not from file contents:

for b in $(git branch -a | fgrep /master)
  git ls-tree -r --name-only $b | grep some/path/name | sed -e "s|^|$b:|g"

To show hyperlinks to GitHub sources, change the Perl print statement to:

print "$repo/blob/master/$fname\n" updates this repo weekly.

You can also:

git clone

to clone all repositories separately, then use regular shell tools to search them:

find */src/main/java -type f -name \*.java -exec fgrep -q 'SomeClass.staticMethod' {} \; -print

But the easiest method is to skip checkouts entirely and use GitHub:

Or right-click your browser search bar, add a new provider with name github and URL pattern:

Now just: Ctrl-K github TAB SomeClass.staticMethod ENTER