Kyle Carlson
Kyle Carlson

Reputation: 8163

GitHub: Search for String Inside of a File in a Repo

TLDR: I want to use the GitHub search API to find a list of all repos with a "search-string" inside of the top-level Jenkinsfile. Does GitHub allow that?

I've read numerous SO posts and GitHub search/API docs including:

And can't find the answer to my issue.

I'm first trying to use the GitHub code search API to search for a specific substring (only two words with a dash joining them: "search-string") in a specific file, but I can't figure out how to do it. I've tried numerous combinations of simple & advanced searches, but usually get zero results. I.e.

And quite a few more combos.

Once I get it working on the GitHub website, I'll convert it to an API call, which shouldn't be an issue.

Thanks in advance!

Upvotes: 4

Views: 2118

Answers (1)

Jon Schneider
Jon Schneider

Reputation: 26973

I was having a similar problem where I was trying to use the GitHub search web interface to find instances of a particular filename in my code, which had a name including underscore characters and a number, like my_image_asset_2.svg.

Searching on that string within my repository (or organization) unexpectedly returned zero results (in the "Code" results type), using a search term like:

  • repo:repo/redacted my_image_asset_2.svg

Even trimming out the number and extension from my search term still unexpectedly returned zero results:

  • repo:repo/redacted my_image_asset

A workaround that finally stumbled on that got GitHub to return the code I was looking for was to (1) drop all punctuation characters from my filename, and (2) enclose the filename in quotes:

  • repo:repo/redacted "my image asset 2 svg"

This might not be a perfect solution in all cases; I imagine it might also match filenames like my-image-asset-2.svg. But depending on your use case, it might be "good enough"?

Upvotes: 2

Related Questions