Sourcegraph prides itself in the ability to search millions of open source and private repositories.
Many features enrich the developer's experience of using Sourcegraph code search. One of such features is Search Contexts.
In this article, you'll learn how to use search contexts on Sourcegraph Cloud
What are Search Contexts?
Search contexts are a set of repositories at specific revisions that search queries on Sourcegraph will target. In other words, search contexts enable you to narrow, group and search only the code repositories you care about.
By default, Sourcegraph uses a global search context (namely, context:global
) for all search queries, when no search context is specified by the actor (me, you, or a programmable robot) performing the search.
Use cases for Search Contexts
First
- In a day, I search many repositories for code. I have identified about 15 code repos that often come up repeatedly and return the desired search results. I want to group them so that my search query targets only this set of repos.
Second
- Sequel to the first case, Sourcegraph allows me to search code repo branches. Instead of specifying the branch names in my query every time, I want to group specific branches of certain repos once to focus more on what I need to find than the semantics of finding it.
Third
- My company has hundreds of repos, thousands of developers and several teams. Sourcegraph allows us to search all of these repos. However, we'd also like each team to be able to search only the code repos they actively manage.
How to use Search Contexts
The first step is to go to sourcegraph.com.
You’ll see a context:global selector at the left side of the search textbox.
Every open-source repository that is added to Sourcegraph Cloud is automatically assigned to the global search context.
You can explore other available public search contexts by clicking the search contexts selector. A dropdown menu should appear as shown below:
Yaaaay!! More public search contexts. Here we have: chakraui, cncf, kubernetes, o3de, stackstorm, stanford, and temporalio search contexts that anyone can use to explore public code.
Given a scenario where you want to search for code within the Kubernetes community, you can select the kubernetes search context and input your query syntax in the search textbox like so:
It only searches for code within the Kubernetes search context. One major benefit of search contexts is the swift response time you get while searching for anything.
My Recommendation: Explore the Stanford search context. A treasure of open source code from Stanford students, academics and professors!
You’re probably wondering how to create your search context. So let’s get into it!
Note: By the way, if you have added repositories to Sourcegraph Cloud, you already have a search context in this format: context:@username.
How to create Search Contexts
Follow these steps:
Step 1
Sign up for a free account on Sourcegraph.com. You can’t create a search context without an account.
Step 2
Go to Code Search in the top left navigation bar. On Hover, you’ll see a dropdown.
Click on the Contexts menu item. It'll direct you to a page that shows all your existing search contexts (if you have others apart from the global search context).
In the image above, you can see that I already have a context:@unicodeveloper search context because I added some private repositories to Sourcegraph a few months ago.
Step 3
Click on the Create search context button by the right hand side.
It will direct you to https://sourcegraph.com/contexts/new
- Add a name and description.
- Choose the visibility of the context.
Now, add the repositories you want to be part of this context in the format above.
An example is this:
Note: Remove the “https://” when adding the repository URL.
- Test the configuration
- Go ahead and create the search context.
You will see your recently created search context. Mine is below:
Now, you can explore only the code you care about in this newly minted search context like so:
Note: Don’t forget to press the context selector to switch to the global search context if you need to explore all the public code on Sourcegraph.
Conclusion
As developers, our knowledge of refactoring code and identifying patterns come in handy in automating repeatable processes. Leverage your search context knowledge in taking your code search prowess to the next level.
Create as many contexts as possible to match your use case. If you are part of a team, invite them to enjoy the benefits of code search.
Sourcegraph.com is not the only option to add, explore private code and use search contexts. Sourcegraph can be installed in different ways to set up a deployment for your private code.
You can run Sourcegraph locally or opt-in for us to set up a managed Sourcegraph instance for your team. You'll be able to use a self-hosted Sourcegraph instance for free (up to 10-users).
Have suggestions or questions? Leave a comment, or join our Community Slack Space where our team will be happy to answer any questions you may have about Sourcegraph.
Top comments (0)