Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

Commit

Permalink
Fix a few typos and small problems on the code search cheat sheet page (
Browse files Browse the repository at this point in the history
#482)

* Fix a few typos and small problems on the page

I was just reading the page to learn about the search syntax, spotted a few problems, and decided to fix them to improve this page.

For the types, I used https://docs.sourcegraph.com/code_search/reference/queries as a source.

* Update markdown for links

Co-authored-by: ltagliaferri <[email protected]>
  • Loading branch information
vdavid and ltagliaferri authored Apr 8, 2022
1 parent 52fda6f commit b7e3b39
Showing 1 changed file with 40 additions and 28 deletions.
68 changes: 40 additions & 28 deletions posts/how-to-search-code-with-sourcegraph-a-cheat-sheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ type: posts

Sourcegraph is a universal code search tool, enabling you to search across both open source and your own private code repositories. Code search can help you onboard onto new codebases, contribute to open source, find bugs and error messages, understand dependency libraries, and more.

This cheat sheet style guide can help you get up to speed with Sourcegraph commands quickly. For more thorough tutorials on using Sourcegraph, refer to our tutorials(https://learn.sourcegraph.com/tags/sourcegraph) and our documentation(https://docs.sourcegraph.com/).
This cheat sheet style guide can help you get up to speed with Sourcegraph commands quickly. For more thorough tutorials on using Sourcegraph, refer to our [tutorials](https://learn.sourcegraph.com/tags/sourcegraph) and our [documentation](https://docs.sourcegraph.com/).

You can use these commands on either Sourcegraph Cloud(https://sourcegraph.com/search) or your own Sourcegraph instance(https://docs.sourcegraph.com/admin/install).
You can use these commands on either [Sourcegraph Cloud](https://sourcegraph.com/search) or your own [Sourcegraph instance](https://docs.sourcegraph.com/admin/install).

## Searching an organization’s repository

Expand Down Expand Up @@ -48,10 +48,10 @@ When searching a repository, command chaining can be used to return more specifi

**Search for a repository that contains a file**

If you are searching for a file in a repository, use `repo.contains.file`.
If you are searching for a file in a repository, use `repo:contains.file`.

<Highlighter
input='repo:repository-path repo.contains.file(file-path)'
input='repo:repository-path repo:contains.file(file-path)'
matcher='file-path'
/>

Expand All @@ -67,15 +67,15 @@ This query returns repositories that contain a `package.json` file and has conte

**Search for a repository that contains some content**

Suppose you are searching for some content in a repository, such as a library. Use `repo.contains.content`.
Suppose you are searching for some content in a repository, such as a library. Use `repo:contains.content`.

<Highlighter
input='repo:repo-path repo.contains.content(your-content)'
input='repo:repo-path repo:contains.content(your-content)'
matcher='your-content'
/>

<Highlighter
input='repo:repo-path repo.contains.content(regular-pattern)'
input='repo:repo-path repo:contains.content(regular-pattern)'
matcher='regular-pattern'
/>

Expand Down Expand Up @@ -128,7 +128,7 @@ after:time-period'
matcher='time-period'
/>

Sometimes the time period can be literal, like `last week`, `last year`, `3 months ago`, `february 10 2021` or have actual dates in the format `dd/mm/yyyy`.
Sometimes the time period can be relative, like `last week`, `last year`, `3 months ago` or absolute, in several formats including `{month} {day} {year}` (example: `february 10 2021`), `dd/mm/yyyy`, and ISO format `yyyy-mm-dd`.

<Highlighter
input='before:last week'
Expand Down Expand Up @@ -158,10 +158,18 @@ Note that `before` and `after` only work in conjunction when combined with `type
The `archived` keyword will bring up those results from repositories that have been archived.

<Highlighter
input='archived:yes/only
archived:yes
archived:only'
matcher='yes/only'
input='archived:yes'
matcher='yes'
/>

<Highlighter
input='archived:no'
matcher='no'
/>

<Highlighter
input='archived:only'
matcher='only'
/>

We can surface only archived repositories within the Sourcegraph organization with the following query.
Expand All @@ -175,13 +183,16 @@ This can help us understand past decisions made within a given codebase.
Use `yes` or `no` with the `case` search query to specify if the search should be case sensitive or not. By default, searches are case insensitive.

<Highlighter
input='case:yes/no
case:yes
case:no'
matcher='yes/no'
input='case:yes'
matcher='yes'
/>

<Highlighter
input='case:no'
matcher='no'
/>

Suppose you would like to check to align the style of a given codebase to help you bring all function calls in Python to be consistent with the PEP 8(https://www.python.org/dev/peps/pep-0008/) guidance. You can use Sourcegraph to understand which functions are using camelCase rather than lowercase names with underscores between words (also called snake_case).
Suppose you would like to check to align the style of a given codebase to help you bring all function calls in Python to be consistent with the [PEP 8](https://www.python.org/dev/peps/pep-0008/) guidance. You can use Sourcegraph to understand which functions are using camelCase rather than lowercase names with underscores between words (also called snake_case).

<SourcegraphSearch query="case:yes lang:python myFunction" />

Expand All @@ -193,20 +204,21 @@ If you would like to find all declared functions that use camelCase, you can try
Types define the scope of code search. A search scope consists of commits, diffs, symbols, repos, paths and files. It is typically used alongside other search commands to further narrow search results.

<Highlighter
input='type:commit|paths|diff|symbol|repo|files'
matcher='commit|paths|diff|symbol|repo|files'
input='type:commit|path|diff|symbol|repo|file'
matcher='commit|path|diff|symbol|repo|file'
/>

Here is an example to show us time-based commits on the Sourcegraph repo.

<SourcegraphSearch query="repo:sourcegraph after:yesterday type:commit" />

A `type` scope can use the following commands, which will restrict search to the following:
* `commit` — commits to a repository
* `diff` — show diffs(https://git-scm.com/docs/git-diff), or changes, within a repository
*`repo` — repositories available to you
* `files` — returns files
*`symbol` — returns files that contain names or keywords in a library.
* `commit` — commits to a repository.
* `path` — restricts terms to matching filenames only (not file contents).
* `diff` — show [diffs](https://git-scm.com/docs/git-diff), or changes, within a repository.
* `repo` — repositories available to you.
* `file` — restricts terms to matching file contents only (not filenames).
* `symbol` — returns files that contain names or keywords in a library.

Searching by type can help you find exactly what you need in a codebase by narrowing down the scope of your search.

Expand Down Expand Up @@ -319,8 +331,8 @@ This can allow you to search across the code that is private to only you. _Pleas
## Further resources

To learn more about how to search effectively with Sourcegraph, you can read through our Sourcegraph search series:
* How To Search with Sourcegraph using Literal Patterns(/how-to-search-code-with-sourcegraph-using-literal-patterns)
* How To Search with Sourcegraph using Regular Expression Patterns(/how-to-search-with-sourcegraph-using-regular-expression-patterns)
* How To Search with Sourcegraph using Structural Patterns(/how-to-search-with-sourcegraph-using-structural-patterns)
* [How To Search with Sourcegraph using Literal Patterns](/how-to-search-code-with-sourcegraph-using-literal-patterns)
* [How To Search with Sourcegraph using Regular Expression Patterns](/how-to-search-with-sourcegraph-using-regular-expression-patterns)
* [How To Search with Sourcegraph using Structural Patterns](/how-to-search-with-sourcegraph-using-structural-patterns)

You can also check out Sourcegraph product documentation(https://docs.sourcegraph.com/) and Sourcegraph tutorials(https://learn.sourcegraph.com/tags/sourcegraph).
You can also check out [Sourcegraph product documentation](https://docs.sourcegraph.com/) and [Sourcegraph tutorials](https://learn.sourcegraph.com/tags/sourcegraph).

0 comments on commit b7e3b39

Please sign in to comment.