I was aware of Sourcegraph since a few months back during the Kubernetes meetup here in Singapore. However, it was a paid service back then.
Last month Sourcegraph announced that its code will be open source. This is a great decision since I think it will be very useful for developers to try it out first before recommending it for use by the companies or organisations they are working with. Source code is now available in its github repo.
So what is Sourcegraph?
Sourcegraph is a tool to search and understand code since it has built-in intelligence similar to popular IDEs like IntelliJ, Eclipse, and VSCode. Sourcegraph is web-based and is used to search source code. Sourcegraph supports multiple programming languages so it is really very powerful. Here are the languages it supports. Some languages are also available in experimental stage.
For code intelligence, Sourcegraph uses LSP(Language Server Protocol) and runs language servers to help map out relationships within your source code and external dependencies. I believe this is where it stands out since it is a pluggable architecture.
I primarily use Sourcegraph if I want to search and understand source code of a project I'm trying to learn. Initially, I tried it for open source projects in github and a few weeks back we installed it at work and used it with our projects at work. We configured it for our Java, Angular, and Nodejs projects. Also, we were able to use it with Altassian's bitbucket, our internal git repository.
Sourcegraph is intuitive and makes it easier to search code. Rather than cloning the source code in your local machine and loading it up in an IDE, you can just login to Sourcegraph and search right away. This saves a lot of time which can be spent for more important things. Hovers over source code are pretty useful, you can easily find declarations, references, implementations, or usages of your variables, objects, or methods.
How do you start using Sourcegraph? That's pretty easy! If you have docker installed, Sourcegraph has a docker image which you can use right away. No need to install anything else. Here is the docker command to run Sourcegraph.
docker run \ --publish 7080:7080 --rm \ --volume ~/.sourcegraph/config:/etc/sourcegraph \ --volume ~/.sourcegraph/data:/var/opt/sourcegraph \ --volume /var/run/docker.sock:/var/run/docker.sock \ sourcegraph/server:2.13.0
Once you see this in your terminal, you're good! You can now access Sourcegraph using your browser at http://localhost:7080
I've pre-configured my github repositories beforehand.
So let's try to use to search code in my donvito/learngo github project. The project is a Go (golang) project. I suggest you try it out since screenshots don't make justice to the intuitiveness of the tool.
Here are some interesting screenshots of the useful features of Sourcegraph.
Find References of the Job struct
Read inline documentation
See when this line of code was committed to the repository.
Go to definition of a particular variable
Intelligent Code Search
Search repositories. This is useful if you already have a lot configured.
View commit diff
Search symbols (functions, classes, constants, structs, fields, etc.)
You can also search Java projects
Built-in saved searches
So those are some features of Sourcegraph, I'm sure you'll be able to discover more.
I've mentioned earlier that Sourcegraph uses Language Servers which is implementing LSP(Language Server Protocol). When you are running Sourcegraph in docker you will see these language servers spawned as new containers. You don't have to do this manually, the Sourcegraph engine spawns this automatically for you.
There is also a Chrome and Firefox browser plugin for Sourcegraph which makes it easier to search a repo.
Be sure to check out the Sourcegraph official documentation for more information. :)
That's it! Hope you learned something from this article! :)
For more updates, you can follow my blog and Twitter @donvito. Most of my tweets are about Docker, Kubernetes, GoLang, Cloud, DevOps, Agile and Startups. I also share code in my GitHub. If you want to know more about what I do, please add me in LinkedIn. I recently started a new youtube channel - I upload some tutorials there. Check it out!