Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • How to search for code in GitHub with GitHub API?
  • How to search for code in github, with github API?
  • https://developer.github.com/v3/search/#search-code

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.

  • 1 result (obvious): repo:repo/redacted
  • 0 results: repo:repo/redacted search-string
  • 0 results: search-string repo:repo/redacted filename:Jenkinsfile
  • 0 results: search-string repo:repo/redacted in:Jenkinsfile

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!

like image 737
Kyle Carlson Avatar asked Sep 13 '25 03:09

Kyle Carlson


1 Answers

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"?

like image 93
Jon Schneider Avatar answered Sep 16 '25 09:09

Jon Schneider