從您的 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