Skip to content

Support searching gists #9704

Closed
Closed

Description

@heaths

Describe the feature or problem you’d like to solve

I use gists for a lot of one-offs: showing people examples of something, storing my own https://play.rust-lang.org gists (so you can edit them), and so much more. The number is untenable, but GitHub offers no way to search them while limiting to some scope e.g., user. I would like a way to search them, and I think others might as well (hence not going straight to an extension).

Proposed solution

Add gh gist search with options to search all (default), --public, or --secret (mirroring gh gist list), and to "grep" just the description, file names, or file contents. A GraphQL query will be built to pull only what is needed, but search will page through all gists.

Examples:

gh gist search 'foo' # find all gists with "foo" in the description
gh gist search 'foo?' --public --filename # find all public gists with "fo" or "foo" in the description or filenames
gh gist search 'foo|bar' --secret --filename --code # find all secret gists with "foo" or "bar" in the description, filenames, or file content

Here I use --filename and --code to mirror some other command parameters in gh already. I think we should support regex from the start using Go's package (not great - no look-arounds, IIRC - but decent) so people aren't hampered when searching for code and don't request it later, creating a possible compatibility issue down the road.

Because the web site already has a search, I'm initially thinking this is scoped to a user. By default, it's effectively @me but you could pass --author {name} to scope to a different author's gists. IMO, we shouldn't error if --secret is specified since we tightly couple the code but could. The service won't return any results.

As for rendered output, this gets trickier. Ideally, gist search would show highlights of the search pattern; however, with the current table format of gist list we can't show multiple file names and certainly not content. Showing multiple file/names for possibly multiple gists would be hard to read. We could mimic the output of gh search code but add another level like:

{gist ID}  {gist description}
  {filename}
    {first or all found search pattern with a line or two above and below for context}

Of course, we'd have to handle highlighting the matches and context lines. Not hard, but starts to bloat the CLI even more.

Instead, we could just list the gists that contain the pattern found in whatever options they specify. They could use existing gist commands to view more. At least in my use cases, I generally know what I'm looking for - and always put a decent description in to help find things - to showing the gist ID and description would satisfy my use case. Curious about others'.

Alternative

Alternatively, we could add a gist subcommand to gh search but I know from experience in the code that all the plumbing for search is very different from what this command would do, so it may not be worth it. At the very least, it would completely bifurcate the code path.

Additional context

Currently, gh gist list | grep 'something' is about as close as you can get and that will only search the view fields you return including the ID (repo name, which is a GUID; and useless to search), the description, and other fields that are useless to search.

Activity

  1. added
    enhancement
    on Oct 4, 2024
  2. added
    needs-triage
    on Oct 4, 2024
  3. added
    gh-gist
    gh-search
    on Oct 7, 2024
  4. added a commit that references this issue on Oct 8, 2024
    3696914
  5. andyfeller commented on Oct 8, 2024

    @andyfeller
    Contributor

    As always, I appreciate you taking the time to actively solve the challenges you face, @heaths! ❤

    I find myself somewhat torn given the technical limitations involved. As you stated, this isn't like gh search commandset that leverages GitHub's search API. In fact, GitHub doesn't officially have an API for search gists though the GitHub gist UI allows various search qualifiers for interactive usage. In actuality, this is less "search" and more "filter". 🤔

    All of that said, I understand the value it presents; just wondering how not to mislead people if accepted.

    Would someone expect this to include gists beyond their own?

    Because the web site already has a search, I'm initially thinking this is scoped to a user. By default, it's effectively @me but you could pass --author {name} to scope to a different author's gists. IMO, we shouldn't error if --secret is specified since we tightly couple the code but could. The service won't return any results.

    gh gist is built around GraphQL queries, which only show the authenticated user's gists. If we want to get gists from other users, we would need to leverage REST API endpoints:

    Is it important that this is a new command versus new functionality within gh gist list?

    Again, I worry that this isn't truly searching and would be confusing in context to other commands.

  6. heaths commented on Oct 8, 2024

    @heaths
    ContributorAuthor

    I never realized there were filters: the web UI should show a link like it does when you're focused in the search on the main site.

    That said, the goal here is only to search your own as the deception says, and as the existing gist list command says. So what confusion might there be given the command help text? I could elaborate in the Long text that it's fetching all gists to search locally if that helps, but commands don't usually go into implementation details.

    I'd love to change the page handling though. The limit is used for the page size as well as max number of results. That doesn't utilize the benefits of paging. I made some changes a few years ago to use channels to fetch smaller pages and process asynchronously - even better for this scenario - and would like to do that here as well if you're all good with it. We'd keep limit as the max still for compatibility.

  7. jtmcg commented on Oct 8, 2024

    @jtmcg
    Contributor

    Hey @heaths, thanks for the detailed submission! All the thoughts and context were really helpful for us to dig into this 🙌

    There are two proposals that I see in the issue and the responses, so I'm going to separate them out so we can discuss them individually.

    Proposal 1: Add gh gist search subcommand

    Upon looking into the existing CLI patterns and a bit of the historical decision making around commands/subcommands, I think this is ultimately what gh gist list is designed to do. It sounds like that subcommand has some feature gaps that aren't meeting your needs that we could address.

    But what about the existing --search flag in commands like gh pr list?

    This is an entirely different query and leverages gh search. There are limitations to this (see below).

    Proposal 2: Add gist subcommand to gh search

    While I think this is probably the correct choice to make for the full "grep" like functionality you've requested, the gh search command is built on top of github's search apis. Currently, there's no type:gist in search, so we can't support it in the CLI. If the gist-owning team builds out search functionality for type:gist, then it should be fairly straightforward to bring that functionality natively to the CLI. I'll reach out to them and let them know about this as a feature request.

    Summary

    Overall, I think @andyfeller asked the right question: Is it important that this is a new command versus new functionality within gh gist list?

    IMO, we should expand gh gist list to encompass your needs rather than creating a new command. To do so, could you enumerate the exact functionality you'd like to see, @heaths, perhaps with respect to the following:

    1. Filters
    2. Ability to search for gists that aren't yours (this is an api limitation, so we can't currently do this without api support)
    3. More fields returned by the command
    4. An alternative way to present the information
    5. An alternative way to handle pagination

    What did I miss?

    For reference, here's the schema for gists, which I got from our API explorer in the docs. We can use the schema to understand better what we can present and filter on to scope the design to what's available would be helpful.

    gist graphql schema
    A Gist.
    
    Implements
    Node
    Starrable
    UniformResourceLocatable
    Fields
    comments(
    after: String
    before: String
    first: Int
    last: Int
    ): GistCommentConnection!
    A list of comments associated with the gist
    
    createdAt: DateTime!
    Identifies the date and time when the object was created.
    
    description: String
    The gist description.
    
    files(
    limit: Int = 10
    oid: GitObjectID
    ): [GistFile]
    The files in this gist.
    
    forks(
    after: String
    before: String
    first: Int
    last: Int
    orderBy: GistOrder
    ): GistConnection!
    A list of forks associated with the gist
    
    id: ID!
    The Node ID of the Gist object
    
    isFork: Boolean!
    Identifies if the gist is a fork.
    
    isPublic: Boolean!
    Whether the gist is public or not.
    
    name: String!
    The gist name.
    
    owner: RepositoryOwner
    The gist owner.
    
    pushedAt: DateTime
    Identifies when the gist was last pushed to.
    
    resourcePath: URI!
    The HTML path to this resource.
    
    stargazerCount: Int!
    Returns a count of how many stargazers there are on this object
    
    stargazers(
    after: String
    before: String
    first: Int
    last: Int
    orderBy: StarOrder
    ): StargazerConnection!
    A list of users who have starred this starrable.
    
    updatedAt: DateTime!
    Identifies the date and time when the object was last updated.
    
    url: URI!
    The HTTP URL for this Gist.
    
    viewerHasStarred: Boolean!
    Returns a boolean indicating whether the viewing user has starred this starrable.
  8. added
    more-info-needed
    on Oct 8, 2024
  9. self-assigned this
    on Oct 8, 2024
  10. heaths commented on Oct 8, 2024

    @heaths
    ContributorAuthor

    I'm fine with making this some options on gh gist list, but I'd want it to be able to fetch text from the GraphQL schema (which I've used a lot over the years) conditionally because they can be large payloads. That's more bandwidth on you and consumers.

    But if we wanted to support filters as @andyfeller called out, that's different. I could add the functionality certainly, but grep'ing over results is local only since, as I already knew, there's no search API. Thus, the most recent commit I just pushed improves that loop a bit. Still, I think fetching and processing should be in separate go routines as I've helped do in other commands I've added or improved on over the years. That could be done in a separate PR or this one.

  11. jtmcg commented on Oct 8, 2024

    @jtmcg
    Contributor

    For sure, but I think you're a little early on your PR, as we haven't agreed upon an approach to this, yet.

    As I stated above I don't think we want to add a gh gist search subcommand and should instead modify gh gist list. I've taken deeper look at the PR to try to understand exactly what you're asking for and how we can solve the problem leveraging the gh gist list subcommand. It should be relatively easy to change what you've done to flags, but here's a rough outline of the design I imagine:

    Added to the gh gist list subcommand:

    • A --filename=<filename> flag that allows filtering on a filename
    • A --include-content boolean flag that toggles whether a gist's text is fetched
    • A --filter=<REGEX> flag that checks ALL returned fields on gists to match the supplied regex (I chose --filter as the name because --search does something different as described in my comment above. I'd prefer to leave the --search flag name available in case we get API support for searching gists).

    examples:

    $ gh gist list --filename=foo
    ... lists all gists that contain a file named "foo"
    
    $ gh gist list --include-content --filter="foo"
    ... lists all gists that include "foo" in their content or description. text is included in the output table
    
    $ gh gist list --filter="bar"
    ... lists all gist that include "bar" in their descriptions
    

    I think you can achieve your desired behavior this way. If not, what did I miss and how can we improve it?

  12. heaths commented on Oct 8, 2024

    @heaths
    ContributorAuthor

    I'm fine with that. I did the PR earlier because I had time and wanted to this weekend. I can refactor, as I think you'll find my many PRs in the past have been. 😉

    Most of the supporting code would still work. It's mostly just a matter of refactoring the command-related code.

    Still, in a separate PR (since you squash merge, and I'm a firm believer in focused PRs that can be backed out if needed), I think I should better utilize go routines to convert (your GraphQL response structs get deep-copied to older REST response structs) and filter (running regexes isn't exactly cheap) separately from fetches. No doubt that is still faster then network IO, but why not parallelize them? Plenty other commands in the CLI already do that.

  13. jtmcg commented on Oct 8, 2024

    @jtmcg
    Contributor

    Sweet! Sounds great. I'll keep an eye out for the PR updates 🙌

  14. added
    help wanted
    and removed
    more-info-needed
    needs-triage
    on Oct 8, 2024
  15. removed their assignment
    on Oct 8, 2024
  16. heaths commented on Oct 9, 2024

    @heaths
    ContributorAuthor

    I won't be able to filter the filename - at least, not with GraphQL or REST API support. I checked both and there's no way to pass a server-processed filter. I could, of course, support that but if we're already going to search descriptions, file names, and optionally content, I see no point A regex pattern could just as easily be a simple string that is a filename e.g., "main.rs", which will still match "main.rs", loosely speaking, but also "mainers". Seems like a very low risk, though, and people could always use "main.rs" instead.

  17. heaths commented on Oct 15, 2024

    @heaths
    ContributorAuthor

    Thanks both for pushing the PR through. I also commented on an existing bug about gist searching: https://github.com/orgs/community/discussions/38820#discussioncomment-10953321

    At the very least, it would be nice to see a link to the qualifier help somewhere obvious. I spend hours a day on github.com (and docs., gists., etc.) nearly every day and have poured through your (mostly API) docs countless hours and, to be honest, I've never ran across those qualifiers.

  18. mattheworiordan commented on Apr 15, 2026

    @mattheworiordan

    Hit the same wall. 500 gists over the years and no way to find anything. GitHub search doesn't index gists, and gh gist list --include-content --filter is ~30s on my account, because it's serial API calls.

    I scratched it: https://github.com/mattheworiordan/gistgrep -> Local mirror, incremental sync, fzf TUI with ranked previews, and on-device AI summaries via Apple Intelligence.

    Search is now sub-100ms once synced

    Image

    brew install mattheworiordan/tap/gistgrep. macOS only right now (the summary bit uses FoundationModels). Happy to add a Linux port via Ollama if there's interest, please do raise an issue. I hope my little project helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementgh-gistgh-searchhelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions