Sourcegraph Code Search - The Developers’ Treasure Trove

Tags: search, code, codereview, codesearch
Published: 2021-09-21 09:42:03

Software engineers, programmers, and researchers use and refurbish code daily. 

A quick scan through published code packages on npm, packagist and GitHub will show you similarities in implementing certain concepts in the same language or across different languages and frameworks.

As an experienced engineer that has worked on several products & built curriculums for boot camps, I have used code search extensively in my career to find code resources, API documentation and optimal solutions to hairy problems.

But how do we find code easily, quickly and seamlessly?

## GitHub Code Search

GitHub is a vast fountain of code knowledge. It is the largest and most used code host in the world. 

In addition, tons of developers view GitHub as the home for their everyday private and open source projects because of the tons of features it provides for collaboration, version control and deployment. 

[Github Advanced Search](https://github.com/search/advanced) searches the entire open source repositories on GitHub to fetch you code results. But, many developers find [GitHub’s search feature limited in functionality and usability.](https://twitter.com/bernhardsson/status/1432876977862610945)

**Example:** I want to search for how the common  `$this->generate_response()` method is used in Laravel projects via GitHub code search.

**Search Query:** [https://github.com/search?q=%24this-%3Egenerate_response%28%29+language%3APHP&type=code](https://github.com/search?q=%24this-%3Egenerate_response%28%29+language%3APHP&type=code)

![GitHub Search](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0e03ys8jc1kvdt3fjtl8.gif)
The results shown above are excellent. However, the results I want are occurrences of `$this->generate_response()` method call with no arguments passed to the method while GitHub returns everything instead of the exact results. 

Well, there’s an alternative to fetch better results. Let’s try this example with Sourcegraph!

## Sourcegraph Code Search

Sourcegraph is a code search engine that enables developers to explore and better understand all code, faster, with contextual code intelligence. 

**Example 1:** I want to search for how `$this->generate_response()` method is used in Laravel projects.

**Search Query:** [https://sourcegraph.com/search?q=context:global+%24this-%3Egenerate_response%28%29+lang:PHP&patternType=literal](https://sourcegraph.com/search?q=context:global+%24this-%3Egenerate_response%28%29+lang:PHP&patternType=literal)


![Sourcegraph](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mhzqn4dihfmgubcgetah.gif)
The results returned are very accurate results of the usage of `$this->generate_response()`.

I love GitHub so much and use it daily for many things other than code search. I’m probably biased but I’ll show you how Sourcegraph’s code search is a better goldmine for developers than Github code search or any tool out there!

**Note:** As of this writing, Sourcegraph has [indexed over 2.1M of the most starred repositories](https://about.sourcegraph.com/blog/why-index-the-oss-universe/) on GitHub (every repository with more than 5 stars). It aims to index every open source repository with more than 1 star on GitHub and GitLab by the end of the year.

Please grab a cup of coffee, stay calm, and let’s walk through these code search challenges!

## 10 Practical Code Search Challenges & Solutions

***Problem 1:*** I want to search for a literal like `“foo(“` or `“.foo”`. Find exact string matches including coding punctuation.

`“foo(“` results:

**GitHub:** [https://github.com/search?q=foo%28&type=code](https://github.com/search?q=foo%28&type=code)

**Sourcegraph:** [https://sourcegraph.com/search?q=context:global+foo%28&patternType=literal](https://sourcegraph.com/search?q=context:global+foo%28&patternType=literal)

`“.foo”` results:

**GitHub:** [https://github.com/search?q=.foo&type=code](https://github.com/search?q=.foo&type=code)

**Sourcegraph:** [https://sourcegraph.com/search?q=context:global+.foo&patternType=literal](https://sourcegraph.com/search?q=context:global+.foo&patternType=literal)

![GitHub](https://res.cloudinary.com/unicodeveloper/image/upload/q_50/v1632215953/image10_wt60z3.gif)

![Sourcegraph](https://res.cloudinary.com/unicodeveloper/image/upload/v1632216188/image7_eot0ih.gif)

***Problem 2:*** I want to search or find code projects that use specific libraries/dependencies.


**GitHub:** I haven’t found a way to search for this yet.
 
**Sourcegraph:** [https://sourcegraph.com/search?q=context:global+file:go.mod+cockroachdb/errors+select:repo+&patternType=literal](https://sourcegraph.com/search?q=context:global+file:go.mod+cockroachdb/errors+select:repo+&patternType=literal)

![Sourcegraph](https://res.cloudinary.com/unicodeveloper/image/upload/v1632216406/image2_qqtya0.gif)
**Note:** I used go.mod because this is present in all go projects. For Node.js & PHP projects, you can use package.json and composer.json, respectively. For other projects, you can use specific files that are mandatory in these projects.




***Problem 3:*** Regular expression matching for code and file names.

**GitHub:** I haven’t found a way to do this yet.

**Sourcegraph:** [https://sourcegraph.com/search?q=context:global+foo%28.*%3F%29bar&patternType=regexp](https://sourcegraph.com/search?q=context:global+foo%28.*%3F%29bar&patternType=regexp)


![Sourcegraph](https://res.cloudinary.com/unicodeveloper/image/upload/v1632216510/image8_k1xpxw.gif)
***Problem 4:*** Finding repositories that contain files with specific filenames and sorting the results according to the repositories with the most stars or activity.

For instance, I want to find all the repositories in my organization that contain a _bundlewatch.config.js_ file.

**GitHub:** I haven’t found a way to do this yet.

**Sourcegraph:** [https://sourcegraph.com/search?q=context:global+repo:contains.file%28bundlewatch.config.js%29+repo:%5Egithub%5C.com/cloudinary/.*++&patternType=regexp](https://sourcegraph.com/search?q=context:global+repo:contains.file%28bundlewatch.config.js%29+repo:%5Egithub%5C.com/cloudinary/.*++&patternType=regexp)

![Sourcegraph](https://res.cloudinary.com/unicodeveloper/image/upload/v1632216585/image1_uckpp1.gif)

***Problem 5:*** When searching for function or class names in the codebase, fetch me only results with the function definition, not files or parts of the codebase using and making function calls.

**GitHub:** I haven’t found a way to do this yet.

**Sourcegraph:** [https://sourcegraph.com/search?q=context:global+type:symbol+newrouter&patternType=literal](https://sourcegraph.com/search?q=context:global+type:symbol+newrouter&patternType=literal)


![Sourcegraph](https://res.cloudinary.com/unicodeveloper/image/upload/v1632217695/image4_clsgk3.gif)
***Problem 6:*** Exclude test files from my code search results. I don’t want to deal with test files when searching for function names and classes.

**GitHub:** I haven’t found a way to do this yet.

**Sourcegraph:** [https://sourcegraph.com/search?q=context:global+type:symbol+newrouter+-file:_test%5C.go%24+&patternType=literal](https://sourcegraph.com/search?q=context:global+type:symbol+newrouter+-file:_test%5C.go%24+&patternType=literal)

![Sourcegraph](https://res.cloudinary.com/unicodeveloper/image/upload/v1632217591/image3_oo1ups.gif)
***Problem 7:*** Ability to search only forked repositories.

**GitHub:** [https://github.com/search?l=&q=foobar+fork%3Aonly&type=code](https://github.com/search?l=&q=foobar+fork%3Aonly&type=code)
I tried to search forked repositories but I got no result.

**Sourcegraph:** [https://sourcegraph.com/search?q=context:global+github+fork:only+&patternType=literal](https://sourcegraph.com/search?q=context:global+github+fork:only+&patternType=literal)



![GitHub](https://res.cloudinary.com/unicodeveloper/image/upload/v1632216905/image5_y2iu9o.gif)

![Sourcegraph](https://res.cloudinary.com/unicodeveloper/image/upload/v1632216983/image9_kl1pzf.gif)

***Problem 8:*** I want to be able to specify a branch while searching for code.

**GitHub:** I haven’t found a way to do this yet.

**Sourcegraph:** [https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/kubernetes/kubernetes%24%40release-1.21+foobar&patternType=literal](https://sourcegraph.com/search?q=context:global+repo:%5Egithub%5C.com/kubernetes/kubernetes%24%40release-1.21+foobar&patternType=literal)


Once you have specified the repository in the search bar, add an `@` and specify the branch just after the `@` symbol. That’s all you need to search within specific branches of a code repo.

***Problem 9:*** I want to exclude archive repositories from code search results.

**GitHub:** I haven’t found a way to do this yet. I found this [GitHub forum thread](https://github.community/t/feature-request-search-code-for-not-archived-repositories/2135) and went down a rabbit hole trying to figure out the latest about the feature request.

**Sourcegraph:** Archived repositories are excluded from code search results by default.

However, if you want to include archived repositories in your Sourcegraph search results, you can make a search query like:

***archived:only***
[https://sourcegraph.com/search?q=context:global+repo:%5Egithub.com/spatie/+archived:only+media&patternType=literal](https://sourcegraph.com/search?q=context:global+repo:%5Egithub.com/spatie/+archived:only+media&patternType=literal)

***archived:yes***
[https://sourcegraph.com/search?q=context:global+repo:%5Egithub.com/spatie/+archived:yes+media&patternType=literal](https://sourcegraph.com/search?q=context:global+repo:%5Egithub.com/spatie/+archived:yes+media&patternType=literal)

***Problem 10:*** De-duplication of search results.

**GitHub:** I haven’t found a way to do this yet.

**Sourcegraph:** [https://sourcegraph.com/search?q=context:global+bou.ke/monkey+-file:go%5C.mod%24+select:repo+&patternType=literal](https://sourcegraph.com/search?q=context:global+bou.ke/monkey+-file:go%5C.mod%24+select:repo+&patternType=literal)

With this search query, without the “select: repo” keywords, you can see similar search results from multiple files in the same repo: [https://sourcegraph.com/search?q=context:global+bou.ke/monkey+-file:go%5C.mod%24&patternType=literal](https://sourcegraph.com/search?q=context:global+bou.ke/monkey+-file:go%5C.mod%24&patternType=literal) 

Adding the “select: repo” to the search query ensures unique repositories get returned.



## Indexing / Syncing Rate of Code Search Results

Just as GitHub lets you know the last time each code search result was indexed, Sourcegraph does the same as well as shown in the image below. 

Sourcegraph indexes faster than GitHub so you never need to worry about stale results. But you don’t have to take my word for it. Try it yourself on [sourcegraph.com](https://sourcegraph.com)

![Sourcegraph](https://res.cloudinary.com/unicodeveloper/image/upload/v1632217076/image12_c7mfzw.png)

## Conclusion

In addition to searching publicly available code, Sourcegraph makes it possible to [add and search through your private code](https://learn.sourcegraph.com/how-to-add-private-code-repositories-to-sourcegraph) on [sourcegraph.com](https://sourcegraph.com). 

I believe that every programmer should be code search literate. Investing in finding the right resources faster increases a developer’s productivity geometrically & ultimately accelerates their career.