Is there a way to grep for something inside a Github project's code?
I could pull the source and grep it locally, but I was wondering if it's possible through the web interface or a 3rd-party alternative.
Ideas?
Is there a way to grep for something inside a Github project's code?
I could pull the source and grep it locally, but I was wondering if it's possible through the web interface or a 3rd-party alternative.
Ideas?
Update May 2023:
The new code search and code view is now generally available (May. 2023)
(As noted by holydragon in the comments, that does require to sign in to GitHub first)
At GitHub Universe last year, we announced a total redesign of GitHub's code search and navigation experience, powered by our all-new code search engine that we built from scratch.
And in February, we announced our public beta.Today, we are rolling out this feature to all GitHub users.
Check out our blog post to learn more about how GitHub's new code search and code view can help you search, navigate, and understand your code.
And if you have feedback, please share it with us in our feedback discussion.
Update Dec. 2021: search has been improved again, with Search for an exact string, with support for substring matches and special characters, or regexps.
But only on cs.github.com, and still in beta (waitlist applies)
Update January 2013: a brand new search has arrived!, based on elasticsearch.org:
A search for stat within the ruby repo will be expressed as stat repo:ruby/ruby
, and will now just workTM.
(the repo name is not case sensitive: test repo:wordpress/wordpress
returns the same as test repo:Wordpress/Wordpress
)
Will give:
And you have many other examples of search, based on followers, or on forks, or...
Update July 2012 (old days of Lucene search and poor code indexing, combined with broken GUI, kept here for archive):
The search (based on SolrQuerySyntax) is now more permissive and the dreaded "Invalid search query. Try quoting it.
" is gone when using the default search selector "Everything":)
(I suppose we can all than Tim Pease, which had in one of his objectives "hacking on improved search experiences for all GitHub properties", and I did mention this Stack Overflow question at the time ;) )
Here is an illustration of a grep within the ruby code: it will looks for repos and users, but also for what I wanted to search in the first place: the code!
Initial answer and illustration of the former issue (Sept. 2012 => March 2012)
You can use the advanced search GitHub form:
Code
, Repositories
or Users
from the drop-down andFor instance, Use the repo:username/repo-name
directive to limit the search to a code repository.
The initial "Advanced Search
" page includes the section:
Code Search:
The Code search will look through all of the code publicly hosted on GitHub. You can also filter by :
- the language
language:
- the repository name (including the username)
repo:
- the file path
path:
So if you select the "Code
" search selector, then your query grepping for a text within a repo will work:
What is incredibly unhelpful from GitHub is that:
Code
"), you will get an error message:Invalid search query. Try quoting it.
"the error message doesn't help you at all.
No amount of "quoting it
" will get you out of this error.
once you get that error message, you don't get the sections reminding you of the right association between the search selectors ("Repositories
", "Users
" or "Language
") and the (right) search filters (here "repo:
").
Any further attempt you do won't display those associations (selectors-filters) back. Only the error message you see above...
The only way to get back those arrays is by clicking the "Advance Search
" icon:
the "Everything
" search selector, which is the default, is actually the wrong one for all of the search filters! Except "language:
"...
(You could imagine/assume that "Everything
" would help you to pick whatever search selector actually works with the search filter "repo:
", but nope. That would be too easy)
you cannot specify the search selector you want through the "Advance Search
" field alone!
(but you can for "language:
", even though "Search Language
" is another combo box just below the "Search for
" 'type' one...)
So, the user's experience usually is as follows:
Advanced Search
", glance over those sections of filters, and notice one you want to use: "repo:
"repo:jruby/jruby stat
", but with the default Search selector "Everything
"FAIL
! (and the arrays displaying the association "Selectors-Filters" is gone)Repositories
" ("Dah! I want to search within repositories...")FAIL
!Users
"), without even looking at said selector, just to give it one more try...FAIL
!So, to recap, if you want to "grep for something inside a Github project's code", as the OP Ben Humphreys, don't forget to select the "Code
" search selector...
repo:"jruby/jruby" stat
repo:"jruby" stat
jruby: stat
none of which works –
Viv repo:jruby/jruby stat
(no need for double-quotes here) can work. So it is not a bug, after all. –
Pol @user:repo
rather than repo:user/repo
–
Maples repo:git/git path:/Documentation/RelNotes/1.7.2.txt 'orphan'
is an exact match but repo:git/git path:/Documentation/RelNotes/1.7.2.txt 'orpha'
fails :( –
Yong To seach within a repository, add the URL parametes /search?q=search_terms
at the root of the repo, for example:
https://github.com/bmewburn/vscode-intelephense/search?q=phpstorm
In the above example, it returns 2 results in Code and 160 results in Issues.
Recent private
repositories have a search field for searching through that repo.
Bafflingly, it looks like this functionality is not available to public
repositories, though.
UPDATE
The bookmarklet hack below is broken due to XHR issues and API changes.
Thankfully Github now has "A Whole New Code Search" which does the job superbly.
Checkout this voodoo: Github code search userscript.
Follow the directions there, or if you hate bloating your browser with scripts and extensions, use my bookmarkified bundle of the userscript:
javascript:(function(){var s='https://raw.githubusercontent.com/skratchdot/github-enhancement-suite/master/build/github-enhancement-suite.user.js',t='text/javascript',d=document,n=navigator,e;(e=d.createElement('script')).src=s;e.type=t;d.getElementsByTagName('head')[0].appendChild(e)})();doIt('');void('');
Save the source above as the URL of a new bookmark. Browse to any Github repo, click the bookmark, and bam: in-page, ajaxified code search.
CAVEAT Github must index a repo before you can search it.
Abracadabra...
Here's a sample search from the annotated ECMAScript 5.1 specification repository:
While @VonC's answer works for some repositories, unfortunately for many repositories you can't right now. Github is simply not indexing them (as commented originally by @emddudley). They haven't stated this anywhere on their website, but they will tell you if you ask support:
From: Tim Pease
We have stopped adding newly pushed code into our codesearch index. The volume of code has outgrown our current search index, and we are working on moving to a more scalable search architecture. I'm sorry for the annoyance. We do not have an estimate for when this new search index will be up and running, but when it is ready a blog post will be published (https://github.com/blog).
Annoyingly there is no way to tell which repositories are not indexed other than the lack of results (which also could be from a bad query).
There also is no way to track this issue other than waiting for them to blog it (or watching here on SO).
From: Tim Pease
I am afraid our issue tracker is internal, but we can notify you as soon as the new search index is up and running.
There is a really nice project called github1s @ https://github.com/conwnet/github1s.
Basically you change the url of any repo https://github.com/microsoft/vscode
to https://github1s.com/microsoft/vscode
and then you can use the built in search function in the editor, to search for anything in a repository.
github.dev
. So you can view the code at https://github.dev/microsoft/vscode
. Read more here. –
Claritaclarity Go here: https://github.com/search and enter "pattern repo:user_name/repo_name".
For example, to search for cnn_learner
in the fastai
repo of user fastai
, enter this:
cnn_learner repo:fastai/fastai
That's it. The only annoyance is you'll need an extra click. It will tell you:
We couldn’t find any repositories matching 'cnn_learner repo:fastai/fastai'
because by default it searches for repositories matching that search string...
So just click on the left on "Code" and it will display what you want.
Or get the code search results directly with a URL like this:
https://github.com/search?q=cnn_learner+repo%3Afastai%2Ffastai&type=code
Use your search term(s) and in:file
in the Search box.
The Search will create a dropdown that asks you where you want to search.
Simply use SourceGraph.com instead to search in GitHub, or install its extension:
I search the source code inside of Github Repositories with the free Sourcegraph Chrome Extension
I skimmed through SourceForge's Chrome Extension Docs and looked at just what I needed for searching for directory names with Github's Search Engine itself by reading some of Github's Codebase Searching Doc
Visit that repo's homepage, then type your search term into the search box on the top left.
The first suggested result will be tagged "in this repository". Click on that, or hit enter.
Google allows you to search in the project, but not the code :(
GitHub documentation says -
octocat in:file
matches code where "octocat" appears in the file contents.
octocat in:path
matches code where "octocat" appears in the file > path.
octocat in:file,path
matches code where "octocat" appears in the > file contents or the file path.
© 2022 - 2024 — McMap. All rights reserved.