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
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.
- 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.
- 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.
- 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.
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:
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.
Follow these steps:
Sign up for a free account on Sourcegraph.com. You can’t create a search context without an account.
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.
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.
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.
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.