【问题标题】:Code fragment repository search on github.comgithub.com 上的代码片段存储库搜索
【发布时间】:2012-10-30 14:11:21
【问题描述】:

如何在github.com 上搜索代码片段?当我在存储库ErikZalm/Marlin 中搜索MSG_PREPARE 时,github 会显示nothing

我正在使用https://github.com/search 中描述的存储库代码搜索语法和

repo:ErikZalm/Marlin MSG_PREPARE

没有结果,但可以在此存储库 here 中找到 MSG_PREPARE。我错过了什么吗? github.com上没有代码搜索吗?

【问题讨论】:

标签: search github full-text-search code-search


【解决方案1】:

2013 年 7 月更新:“Preview the new Search API

GitHub search API on code 现在支持片段,通过text-match metadata

一些 API 使用者会希望在显示搜索结果时突出显示匹配的搜索词。 API 提供了额外的元数据来支持这个用例。要在搜索结果中获取此元数据,请在 Accept 标头中指定文本匹配媒体类型。例如,通过curl,上述查询将如下所示:

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

这会产生与上面相同的 JSON 有效负载,并带有一个名为 text_matches 的额外键,它是一个对象数组。这些对象提供信息,例如搜索词在文本中的位置,以及包含搜索词的属性。


原始答案(2012 年 11 月)

我认为您不会错过任何事情。

如果您搜索 for SdFile,您会在 .pde 文件中找到结果,但在 cpp 文件中却没有,例如在此 SdFile.cpp 文件中。

搜索的是introduced 4 years ago (November 2008),但是,如“Search a github repository for the file defining a given function”中所述,GitHub 存储库代码根本没有完全编入索引

【讨论】:

【解决方案2】:

在撰写此答案时,与提出此问题的时间(即大约 8 年前)相比,github 已经取得了不错的成绩,尽管还没有达到您正在查看的长度。

GitHub 代码搜索受限于以下规则:https://docs.github.com/en/github/searching-for-information-on-github/searching-code。引用相同的:

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."

所以我们可以使用fork:true 选项在分叉中搜索,尽管正如预期的那样,由于与父MarlinFirmware/Marlin 相比,repo ErikZalm/Marlin 的星数较低,因此分叉中的代码仍然没有被索引。因此advance search shows no good except a match to the repo.

但是,如果您对父项执行相同的搜索,它会在代码中显示匹配项。 Here are the matches for MSG_PREPARE in the parent repo MarlinFirmware/Marlin

幸运的是,我知道在这个领域工作的一家公司是 SourceGraph:https://about.sourcegraph.com/

因此,您可以使用 SourceGraph 轻松搜索您想要的内容: Here are the matches for MSG_PREPARE in the ErikZalm/Marlin using SourceGraph Cloud

【讨论】:

  • 我 2012 年答案的有趣更新。赞成。
  • 源图搜索功能的深层链接太棒了! https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/ErikZalm/Marlin%24%20MSG_PREPARE&patternType=literal
  • 这个 sourcegraph 甚至允许在 all repos 中搜索代码并使用正则表达式!
猜你喜欢
  • 2010-09-21
  • 2011-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多