Code fragment repository search on github.com
Asked Answered
B

2

9

How can I search for code fragments on github.com? When I search for MSG_PREPARE in the repository ErikZalm/Marlin github shows up nothing.

I'm using the repository code search syntax described on https://github.com/search with

repo:ErikZalm/Marlin MSG_PREPARE

No results, but MSG_PREPARE can be found inside this repository here. Am I missing something? Is there no code search on github.com?

Blindage answered 10/11, 2012 at 12:12 Comment(2)
Update July 2013: text match metadata allows for an easier way to find text fragments. See my edited answer belowCola
Possible duplicate: #53795277 (has an answer) Sorry for the bounty, did not notice you wanted to just search in context of single repoWoodsum
T
4

At the time of writing this answer, compared to time this question was asked i.e. about 8 years ago, github has come a good way, though still not to the length which you are looking at.

GitHub code searches are limited on the following rules: https://docs.github.com/en/github/searching-for-information-on-github/searching-code . Quoting the same:

Code in forks is only searchable if the fork has more stars than the parent repository.
Forks with fewer stars than the parent repository are not indexed for code search.
To include forks with more stars than their parent in the search results, you will need to add fork:true or fork:only to your query.
For more information, see "Searching in forks."

So we can search within the fork using the fork:true option, though as expected, since the repo ErikZalm/Marlin is low on star count as compared to parent MarlinFirmware/Marlin, the code in the fork is still not indexed. Hence the advance search shows no good except a match to the repo.

Though, if you perform the same search on the parent, it would show the matches on the code. Here are the matches for MSG_PREPARE in the parent repo MarlinFirmware/Marlin

Fortunately, one company which I know working on this domain is SourceGraph: https://about.sourcegraph.com/

Hence, you can easily search what you intended with SourceGraph: Here are the matches for MSG_PREPARE in the ErikZalm/Marlin using SourceGraph Cloud

Technicality answered 29/1, 2021 at 8:55 Comment(2)
The deeplink to sourcgraph's search function is awsome! https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/ErikZalm/Marlin%24%20MSG_PREPARE&patternType=literalBlindage
This sourcegraph thing even allows searching code in all repos and to use regex!Woodsum
C
1

Update July 2013: "Preview the new Search API"

The GitHub search API on code now supports fragments, through text-match metadata.

text match metadata

Some API consumers will want to highlight the matching search terms when displaying search results. The API offers additional metadata to support this use case. To get this metadata in your search results, specify the text-match media type in your Accept header. For example, via curl, the above query would look like this:

curl -H 'Accept: application/vnd.github.preview.text-match+json' \
  https://api.github.com/search/code?q=octokit+in:file+extension:gemspec+-repo:octokit/octokit.rb&sort=indexed

This produces the same JSON payload as above, with an extra key called text_matches, an array of objects. These objects provide information such as the position of your search terms within the text, as well as the property that included the search term.


Original answer (November 2012)

I don't think there is anything that you would have missed.

If you search for SdFile, you would find results in .pde file, but none in cpp files like in this SdFile.cpp file.

The search was introduced 4 years ago (November 2008), but, as mentioned in "Search a github repository for the file defining a given function", GitHub repository code is simply not fully indexed.

Cola answered 11/11, 2012 at 11:27 Comment(2)
I read the blog article several times to make sure I did not missed something.Blindage
It now is more completely indexed with github.com/blog/1381-a-whole-new-code-searchCola

© 2022 - 2024 — McMap. All rights reserved.