在 Github 项目中搜索代码 [英] Search code inside a Github project

查看:64
本文介绍了在 Github 项目中搜索代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法对 Github 项目代码中的某些内容进行 grep?

我可以提取源代码并在本地进行 grep,但我想知道是否可以通过 Web 界面或第三方替代方案.

想法?

解决方案

2013 年 1 月更新:,或:

  • 从下拉列表中选择CodeRepositoriesUsers
  • 使用为该搜索类型列出的相应前缀.

例如,使用repo:username/repo-name 指令将搜索限制为代码 存储库.
最初的Advanced Search"页面包括以下部分:

<块引用>

代码搜索:

代码搜索将查看公开托管在 GitHub 上的所有代码.您还可以过滤:

  • 语言语言:
  • 存储库名称(包括用户名)repo:
  • 文件路径path:

因此,如果您选择Code"搜索选择器,那么您对 ​​repo 中文本的查询 grepping 将起作用:

<小时>

来自 GitHub 的令人难以置信的无用是:

  • 如果您忘记放置正确的搜索选择器(此处为Code"),您将收到一条错误消息:
    "无效的搜索查询.尝试引用它."

  • 错误信息对您没有任何帮助.
    再多的引用它"都不会让你摆脱这个错误.

  • 一旦您收到该错误消息,您就不会收到提醒您搜索选择器(存储库"、"Users" 或 "Language") 和(右)搜索过滤器(这里是repo:").
    您所做的任何进一步尝试都不会显示这些关联(选择器-过滤器).只有您在上面看到的错误消息...
    取回这些数组的唯一方法是单击Advance Search"图标:

  • 默认的Everything"搜索选择器实际上是错误所有搜索过滤器!除了语言:"...
    (您可以想象/假设Everything"将帮助您选择实际与搜索过滤器repo:"一起使用的任何搜索选择器,但不.那太简单)

  • 您无法单独通过Advance Search"字段指定所需的搜索选择器!
    (但您可以输入language:",即使Search Language"是位于Search for"下方的另一个组合框,输入'一个...)

<小时>

所以,用户的体验通常是这样的:

  • 您点击Advanced Search",浏览过滤器的那些部分,并注意您要使用的一个:repo:"
  • 您进行第一个高级搜索repo:jruby/jruby stat",但使用默认搜索选择器Everything"
    => 失败!(并且显示关联选择器-过滤器"的数组消失了)
  • 您注意到搜索"选择器很重要,选择第一选项Repositories"(Dah!我想在存储库中搜索...")
    => 失败
  • 沮丧,你选择了下一个选择器(这里是Users"),甚至没有看那个选择器,只是再试一次...
    => 失败
  • 搞砸了,GitHub 搜索坏了!我走了!"
    ...
    (GitHub 高级搜索实际上并没有坏.只有他们的 GUI 是...)
<小时>

所以,总结一下,如果您想在 Github 项目代码中查找某些内容",作为 OP Ben Humphreys,不要忘记选择Code"搜索选择器...

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 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:

  • Choose Code, Repositories or Users from the drop-down and
  • use the corresponding prefixes listed for that search type.

For 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:

  • if you forget to put the right search selector (here "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:

  • you click "Advanced Search", glance over those sections of filters, and notice one you want to use: "repo:"
  • you make a first advanced search "repo:jruby/jruby stat", but with the default Search selector "Everything"
    => FAIL! (and the arrays displaying the association "Selectors-Filters" is gone)
  • you notice that "Search for" selector thingy, select the first choice "Repositories" ("Dah! I want to search within repositories...")
    => FAIL!
  • dejected, you select the next choice of selectors (here, "Users"), without even looking at said selector, just to give it one more try...
    => FAIL!
  • "Screw this, GitHub search is broken! I'm outta here!"
    ...
    (GitHub advanced search is actually not broken. Only their GUI is...)

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

这篇关于在 Github 项目中搜索代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆