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 https://github.com/jenkinsci - try cloning http://git.jenkins-ci.org/all.git 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)
do
  git ls-tree -r --name-only $b | grep some/path/name | sed -e "s|^|$b:|g"
done

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

print "https://github.com/jenkinsci/$repo/blob/master/$fname\n"

http://ci.jenkins-ci.org/view/Infrastructure/job/infra_backend-merge-all-repo/ updates this repo weekly.

You can also:

git clone git@github.com:jenkinsci/backend-merge-all-repo.git
./backend-merge-all-repo/clone.groovy

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:

https://github.com/search?type=Code&q=user%3Ajenkinsci+SomeClass.staticMethod

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

https://github.com/search?ref=simplesearch&type=Code&q=user%3Ajenkinsci+%s

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