github-cli

GitHub search a path in a repository via the GitHub CLI


I have this search via the code browser:

It searches for a path inside a repository and it does returns results.

No matter how I try it to translate it to GitHub CLI I can not succeed. The following one (Windows):

gh api --method=GET "search/code?q=repo:RapidCircle/vscode+path:.vscode/"

returns empty.

So does the following one (Ubuntu) too:

gh api -X GET search/code -f 'q=repo:RapidCircle/vscode+path:.vscode/'

Did some extra investigation and these queries do not return result either:

gh api -X GET search/code -f q='repo:RapidCircle/vscode' -F per_page=100
gh api --method=GET "search/code?q=repo:RapidCircle/vscode"

But the repo (RapidCircle/vscode) seems there. Ok. Fine. It might not be indexed (Search for the repo in an other way).

Let us look at another repo. This Github CLI does returns values:

gh api -X GET search/code -f q='repo:cli/cli' -F per_page=100

If I try to add a path variable (works via web) but not with GitHub CLI:

gh api -X GET search/code -f q='repo:cli/cli+path:/^internal\//' -F per_page=100

Same but Windows:

gh api --method=GET "search/code?q=repo:cli/cli+path:/^internal\//"

What is wrong?

A more general question would be:

I am guessing there is limitation on what you can do with GitHub CLI API vs real WebSearch (on the site).

Submitted the issue here too: GitHub CLI Issues/Discussions


Solution

  • Someone answered my question regarding why the first query did not work and that was because this repository: RapidCircle/vscode is a fork of microsoft/vscode.

    So you need the "+fork:true" to the CLI to be able to search.

    Windows full command:

    gh api --method=GET "search/code?q=repo:RapidCircle/vscode+path:.vscode/+fork:true"
    

    And here is a syntax that worked for me in Linux (Ubuntu):

    gh api -X GET search/code -f 'q=repo:RapidCircle/vscode path:.vscode/ fork:true'
    

    Please pay attention that instead of "+" there is space there.

    As for these commands:

    Ubuntu:

    gh api -X GET search/code -f q='repo:cli/cli+path:/^internal\//' -F per_page=100
    gh api -X GET search/code -f q='repo:cli/cli path:/^internal\//' -F per_page=100
    

    Windows:

    gh api --method=GET "search/code?q=repo:cli/cli+path:/^internal\//"
    

    They are not working because this search pattern: /^internal// is Regexp (regex) and it seams GitHub CLI does not know that one yet.

    Commands like these (regexp (regex) free) work:

    Ubuntu:

    gh api -X GET search/code -f q='repo:cli/cli path:internal/' -F per_page=100
    

    Windows:

    gh api --method=GET "search/code?q=repo:cli/cli+path:internal/"