git 在 Github 项目中搜索代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3616221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:00:10  来源:igfitidea点击:

Search code inside a Github project

gitsearchgithub

提问by Ben Humphreys

Is there a way to grep for something inside a Github project's code?

有没有办法在 Github 项目代码中查找某些内容?

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.

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

Ideas?

想法?

采纳答案by VonC

Update January 2013: a brand new search has arrived!, based on elasticsearch.org:

2013 年 1 月更新:全新搜索已到来!,基于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/wordpressreturns the same as test repo:Wordpress/Wordpress)

在 ruby​​ 存储库中搜索 stat 将被表示为stat repo:ruby/ruby,现在将只工作TM
(回购名称不区分大小写:test repo:wordpress/wordpress返回与 相同test repo:Wordpress/Wordpress

enter image description here

在此处输入图片说明

Will give:

会给:

enter image description here

在此处输入图片说明

And you have many other examples of search, based on followers, or on forks, or...

您还有许多其他搜索示例,基于followerfork,或...



Update July 2012(old days of Lucene search and poor code indexing, combined with broken GUI, kept here for archive):

2012 年 7 月更新(Lucene 搜索和糟糕的代码索引的旧时代,加上损坏的 GUI,保存在这里以供存档):

The search (based on SolrQuerySyntax) is now more permissive and the dreaded "Invalid search query. Try quoting it." is gone when using the defaultsearch selector "Everything":)

搜索(基于SolrQuerySyntax)现在更加宽松,Invalid search query. Try quoting it.使用默认搜索选择器“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 questionat the time ;) )

(我想我们都可以,而不是Tim Pease,他的目标之一是“改进所有 GitHub 属性的搜索体验”,我当时确实提到了这个 Stack Overflow 问题;))

Here is an illustration of a grep within the ruby code: it will looks for repos and users, but alsofor what I wanted to search in the first place: the code!

这是 ruby​​ 代码中的 grep 的说明:它将查找存储库和用户,但也会查找我首先想要搜索的内容:代码!

GitHub more permissive search results

GitHub 更宽松的搜索结果



Initial answer and illustration of the former issue (Sept. 2012 => March 2012)

前一期的初步回答和说明(2012 年 9 月 => 2012 年 3 月)

You can use the advanced search GitHub form:

您可以使用高级搜索 GitHub 表单

  • Choose Code, Repositoriesor Usersfrom the drop-down and
  • use the corresponding prefixes listed for that search type.
  • 选择CodeRepositoriesUsers从下拉菜单中选择
  • 使用为该搜索类型列出相应前缀

For instance, Use the repo:username/repo-namedirective to limit the search to a coderepository.
The initial "Advanced Search" page includes the section:

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

CodeSearch:

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:

代码搜索:

代码搜索将查看 GitHub 上公开托管的所有代码。您还可以按以下条件过滤:

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

So if you select the "Code" search selector, then your query grepping for a text within a repo will work:

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

Good Search selector

好的搜索选择器



What is incrediblyunhelpfulfrom GitHub is that:

什么是令人难以置信的无益从GitHub是:

  • if you forget to put the right search selector (here "Code"), you will get an error message:
    "Invalid search query. Try quoting it."
  • 如果您忘记放置正确的搜索选择器(此处为“ Code”),您将收到一条错误消息:
    Invalid search query. Try quoting it.

Wrong selector for the code filer

代码文件管理器的错误选择器

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

  • 错误消息根本没有帮助你。
    再多的“ quoting it”也无法让你摆脱这个错误。

  • 一旦您收到该错误消息,您就不会看到提醒您搜索选择器(“ Repositories”、“ Users” 或“ Language”)和(正确的)搜索过滤器(此处为“ repo:”)之间的正确关联的部分。
    您所做的任何进一步尝试都不会显示这些关联(选择器-过滤器)。只有您在上面看到的错误消息......
    取回这些数组的唯一方法是单击“ Advance Search”图标:

Advance Search Icon on GitHub

GitHub 上的高级搜索图标

  • the "Everything" search selector, which is the default, is actually the wrongone for allof 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...)

  • " Everything" 搜索选择器,这是默认的,实际上是所有搜索过滤器的错误选择!除了“ ”... (您可以想象/假设“ ”会帮助您选择与搜索过滤器“ ”实际配合使用的任何搜索选择器,但不会。那太容易了)language:
    Everythingrepo:

  • 您不能Advance Search单独通过“ ”字段指定您想要的搜索选择器!
    (但您可以输入“ language:”,即使“ Search Language”是“ Search for”“类型”下方的另一个组合框...)

Wrong search selector

错误的搜索选择器



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 firstchoice "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...)
  • 您单击“ Advanced Search”,浏览过滤器的那些部分,并注意您要使用的一个:“ repo:
  • 您进行第一个高级搜索“ repo:jruby/jruby stat”,但使用默认搜索选择器“ Everything
    => FAIL!(并且显示关联“Selectors-Filters”的数组消失了
  • 你注意到“搜索”选择器的东西,选择第一个选择“ Repositories”(“Dah!我想在存储库中搜索......”)
    => FAIL
  • 沮丧地,你选择了下一个选择器(这里是“ Users”),甚至没有看那个选择器,只是再试一次......
    => FAIL
  • “该死,GitHub 搜索失败了!我要离开这里了!”
    ...
    (GitHub 高级搜索实际上并没有被破坏。只有他们的 GUI 是...)


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 项目代码中的某些内容”,作为 OP Ben Humphreys,不要忘记选择“ Code”搜索选择器......

回答by Patrick Linskey

Recent private repositories have a search field for searching through that repo.

最近的私有存储库有一个搜索字段,用于搜索该存储库。

enter image description here

在此处输入图片说明

Bafflingly, it looks like this functionality is not available to public repositories, though.

令人费解的是,这个功能似乎不适用于公共存储库。

回答by fny

UPDATE

更新

The bookmarklet hack below is broken due to XHR issues and API changes.

由于 XHR 问题和 API 更改,下面的 bookmarklet hack 被破坏。

Thankfully Github now has "A Whole New Code Search"which does the job superbly.

谢天谢地,Github 现在有了“全新的代码搜索”,可以出色地完成这项工作。



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://github.com/skratchdot/github-code-search.user.js/raw/master/github-code-search.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.

CAVEATGithub must index a repo before you can search it.

Before the Bookmarklet

Abracadabra...

After - Look in the second menubar after the leftmost tabs: Files, Commits, Branches...

Here's a sample search from the annotated ECMAScript 5.1 specification repository:

Sample search in the annotated ECMAScript 5.1 specification repository

签出这个伏都Github 代码搜索用户脚本

按照那里的说明进行操作,或者如果您讨厌使用脚本和扩展程序使浏览器膨胀,请使用我的书签化用户脚本包:

javascript:(function(){var s='https://github.com/skratchdot/github-code-search.user.js/raw/master/github-code-search.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('');

将上面的源保存为新书签的 URL。浏览到任何 Github 存储库,单击书签,然后bam: in-page, ajaxified code search

CAVEATGithub 必须先索引一个 repo,然后才能搜索它。

在书签之前

胡言乱语...

之后 - 查看最左侧选项卡后的第二个菜单栏:文件、提交、分支...

这是来自带注释的 ECMAScript 5.1 规范存储库的示例搜索:

在带注释的 ECMAScript 5.1 规范存储库中搜索示例

回答by studgeek

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:

虽然@VonC 的答案适用于某些存储库,但不幸的是,对于许多存储库,您现在不能。Github 根本没有索引它们(正如@emddudley 最初评论的那样)。他们没有在网站上的任何地方说明这一点,但如果您寻求支持,他们会告诉您:

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

来自:Tim Pease
我们已经停止将新推送的代码添加到我们的代码搜索索引中。代码量已经超出了我们当前的搜索索引,我们正在努力转向更具可扩展性的搜索架构。很抱歉给您带来麻烦。我们没有估计这个新的搜索索引何时会启动并运行,但是当它准备好时,将发布一篇博文 (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).

除了等待他们写博客(或在 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.

来自:Tim Pease
恐怕我们的问题跟踪器是内部的,但我们会在新搜索索引启动并运行后立即通知您。

回答by crizCraig

Google allows you to search in the project, but not the code :(

Google 允许您在项目中进行搜索,但不允许在代码中进行搜索 :(

site:https://github.com/CreateJS/EaselJS test

站点:https://github.com/CreateJS/EaselJS 测试

回答by Tyler Curtis Jowers

I search the source code inside of Github Repositories with the free Sourcegraph Chrome Extension... But I Downloaded Chrome First, I knew other browsers support it though, such as - and maybe just only - Firefox.

我使用免费的Sourcegraph Chrome 扩展程序在 Github 存储库中搜索源代码 ......但我首先下载了 Chrome,我知道其他浏览器支持它,例如 - 也许只是 - Firefox。

I skimmed through SourceForge's Chrome Extension Docsand then also I 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

我浏览了SourceForge 的 Chrome 扩展文档,然后通过阅读一些Github 的代码库搜索文档,查看了使用 Github 的搜索引擎本身搜索目录名称所需的内容