Skip to main content

gh-search

Last release Python versions Unittests status

GitHub code search with full text regex filtering, from your cli.

Features

  • Filters search results (eg. ignore archived repositories or search for specific text in matched content)
  • Displays results grouped by organisation/repository
  • GitHub API rate limit aware (prevent accidentally consuming your entire core API quota)
  • Uses GitHub's Rest API (and therefore works with GitHub Enterprise)

Installation

gh-search is available as a python package via pypi.org and requires Python 3.12+

uv tool install gh-search

Authentication

A valid GitHub personal access token, with the repo scope, is required to retrieve search results. It can be set on a GITHUB_TOKEN envvar or passed to the script via the --github-token option.

Enterprise

To search GitHub Enterprise set the GITHUB_API_URL envvar to your organisation's GitHub v3 API endpoint. eg. GITHUB_API_URL=https://github.mycompany.net/api/v3. You can also use the --github-api-url option for this.

Usage

Invoke with gh-search and pass a query string as the first argument. For example, to search for the word "usage" in this repo:

gh-search usage repo:janeklb/gh-search

Note that repo: is a search qualifier natively supported by the GitHub Search API. See GitHub's searching code documentation to see what other qualifiers are available.

Example: regex content filtering

If you are searching for a specific non-alphanumeric string you can use the --regex-content-filter (or --content-filter) options. This must be combined with a valid GitHub Search API query (which will produce the result set that will subsequently be filtered).

For example if you're looking for a special_var variable being set to a value of characters beginning with 10 you could do something like:

gh-search special_var -e "special_var\\s*=\\s*10"

Archived repositories

GitHub code search does not index archived repositories, so gh-search cannot search them. Clone or download an archived repository and search it locally instead.

All available options

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 does not match this regular expression.
  -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.
  -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.

Saving default configuration values to disk

Default values for options can specified via a config file. Location of this file is based on click.get_app_dir, with gh-search as the app_name (eg. ~/Library/Application\ Support/gh-search/config on MacOS). You'll see the exact file location printed out next to the help text of the --config of gh-search --help (or if you run with the --verbose flag).

The option names must be converted to snake_case as per click's parameter naming.

For example, in order set a default --github-token and --github-api-url you would write the following to your config file (replacing <PLACE HOLDERS> accordingly):

github_token="<YOUR TOKEN>"
github_api_url="<THE API URL>"

Rate Limiting

gh-search checks your rate limits and will prompt you to continue if your search might:

  • perform more than 500 core API requests
  • leave you with less than 10% of your core API quota

Only the core API quota is checked because gh-search's filters can make heavy use it. The search API quota is not checked.

GitHub can return incomplete code-search results. When this happens, gh-search prints a warning; narrow the query and retry.

Developing

This project uses uv for dependency management.

  • make install-dev install dev dependencies
  • make unit run unit tests
  • make lint run linters

Release files for gh-search 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for gh-search 1.0.0
File Size Uploaded
gh_search-1.0.0.tar.gz 60.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for gh-search 1.0.0
File Interpreter ABI Platform
gh_search-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 70.7 kB

Release files / gh_search-1.0.0.tar.gz

Download URL gh_search-1.0.0.tar.gz
Size 60.1 kB
Tags Source
SHA-256 checksum
How to use checksums
8fe1f0d8f46bba25e7325fa2d982641378b3daea84a407fa8dc5d5acf65b60af
BLAKE2b-256 checksum
How to use checksums
d407ec594756ad096c40ae7f85334955ec1083c04f2fe689e71d627b5418e424
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.10 {"installer":{"name":"uv","version":"0.12.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / gh_search-1.0.0-py3-none-any.whl

Download URL gh_search-1.0.0-py3-none-any.whl
Size 10.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
69407a3032a833b37639c5ec4470652667fa39eb0ce437983022f30117a656c9
BLAKE2b-256 checksum
How to use checksums
36c2f7e93ed92dfe32152fb86b5f389ce7b14612951baae86b31d164c85fbccb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.10 {"installer":{"name":"uv","version":"0.12.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.3

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page