从您的 cli 进行带有全文正则表达式过滤的 GitHub 代码搜索。
organisation/repository
分组的结果gh-search
可通过 pypi.org 作为 python 包使用,并且需要 Python 3.12+
pipx install gh-search
检索搜索结果需要有效的 GitHub 个人访问令牌以及repo
范围。它可以在GITHUB_TOKEN
envvar 上设置或通过--github-token
选项传递给脚本。
要搜索 GitHub Enterprise,请将GITHUB_API_URL
envvar 设置为您组织的 GitHub v3 API 端点。例如。 GITHUB_API_URL=https://github.mycompany.net/api/v3
。您还可以使用--github-api-url
选项来实现此目的。
使用gh-search
调用并传递查询字符串作为第一个参数。例如,要在此存储库中搜索单词“usage”:
gh-search usage repo:janeklb/gh-search
请注意, repo:
是 GitHub 搜索 API 本身支持的搜索限定符。请参阅 GitHub 的搜索代码文档以了解其他可用的限定符。
如果您要搜索特定的非字母数字字符串,可以使用--regex-content-filter
(或--content-filter
)选项。这必须与有效的 GitHub Search API 查询结合使用(这将生成随后将被过滤的结果集)。
例如,如果您正在寻找一个设置为以10
开头的字符值的special_var
变量,您可以执行以下操作:
gh-search special_var -e " special_var \ s*= \ s*10 "
Usage: gh-search [OPTIONS] QUERY...
QUERY must contain at least one search term, but may also contain search qualifiers
(https://docs.github.com/en/github/searching-for-information-on-github/searching-code)
Options:
--github-token TEXT GitHub Auth Token. Will fall back on GITHUB_TOKEN envvar.
--github-api-url TEXT Override default GitHub API URL. Can also specify via GITHUB_API_URL envvar.
-p, --path-filter TEXT Exclude results whose path (or part of path) does not match this.
-c, --content-filter TEXT Exclude results whose content does not match this.
-e, --regex-content-filter TEXT
Exclude results whose content does not match this regex.
-a, --include-archived Include results from archived repos.
-l, --repos-with-matches Only the names of repos are printed. Equivalent to --output=repo-list
-o, --output TEXT Output style; one of: default, repo-list, json, yaml
-v, --verbose Verbose output.
--help Show this message and exit.
选项的默认值可以通过配置文件指定。该文件的位置基于click.get_app_dir
,其中gh-search
作为app_name
(例如,MacOS 上的~/Library/Application Support/gh-search/config
)。您将看到在gh-search --help
的--config
的帮助文本旁边打印出确切的文件位置(或者如果您使用--verbose
标志运行)。
选项名称必须根据click
的参数命名转换为 Snake_case。
例如,为了设置默认的--github-token
和--github-api-url
您可以将以下内容写入您的配置文件(相应地替换<PLACE HOLDERS>
):
github_token="<YOUR TOKEN>"
github_api_url="<THE API URL>"
gh-search
检查您的速率限制,如果您的搜索可能出现以下情况,则会提示您继续:
500
核心 API 请求10%
仅检查核心API 配额,因为gh-search
的过滤器会大量使用它。不检查搜索API 配额。
make install-dev
安装dev依赖项(先设置自己的虚拟环境)make unit
运行单元测试make lint
运行 linter