# Code ownership

Code ownership is aimed at helping find the right person and team to contact, for any question, at any time.

## Concepts

**Owner**: An owner is defined as a person in Sourcegraph.

A _person_ can be:

-   a Sourcegraph user which we were able to resolve from the `CODEOWNERS` handle or email, in which case we link to their profile.
-   an unknown user for which we were unable to resolve a profile, in which case we will return the `CODEOWNERS` data we have.

## Code ownership

Code ownership is set via [the `CODEOWNERS` format](/code-ownership/codeowners-format).

## Limitations

-   The feature has not been fully validated to work well on large repositories or large `CODEOWNERS` rulesets. This is a future area of improvement, but please contact us if you run into issues.

## Browsing ownership

The ownership information is available for browsing once ownership data is available through [a `CODEOWNERS` file](#code-ownership).

When displaying a source file, there is an "Ownership" button at the bottom. Clicking it will open the "Ownership" panel. The button appears only in repositories that have a `CODEOWNERS` file.

![File view with the ownership tab selected in the bottom panel](https://storage.googleapis.com/sourcegraph-assets/docs/own/blob-view-panel-v2.png)

## Ownership search

### Searching for files with owners

Code ownership is a first-class citizen in search. Ownership can be either a query input or a search result:

- Results owned by the given owner:\
  [`repo:^github\.com/grafana/grafana$ file:has.owner(grafana/data-sources-plugins) azure count:10`](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/grafana/grafana%24+file:has.owner%28grafana/data-sources-plugins%29+azure+count:10&patternType=keyword)
- Files that have an owner:\
  [`repo:^github\.com/grafana/grafana$ file:has.owner() azure count:10`](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/grafana/grafana%24+file:has.owner%28%29+azure+count:10&patternType=keyword)
- Files without an owner:\
  [`repo:^github\.com/grafana/grafana$ -file:has.owner() azure count:10`](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/grafana/grafana%24+-file:has.owner%28%29+azure+count:10&patternType=keyword)

Write the owner as the complete username, team name, or email address from the `CODEOWNERS` file. The leading `@` is optional. To exclude results owned by the given owner, use `-file:has.owner(...)`.

When performing a search, the `select:file.owners` predicate returns the owners of the search results. For example, to find the owners of Go files in the `pkg/tsdb/azuremonitor/` directory:

[`repo:^github\.com/grafana/grafana$ file:^pkg/tsdb/azuremonitor/ lang:go select:file.owners count:50`](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/grafana/grafana%24+file:%5Epkg/tsdb/azuremonitor/+lang:go+select:file.owners+count:50&patternType=keyword)

### Find commits in a release for a given owner

With `type:commit`, `file:has.owner(...)` matches commits that changed files owned by the given owner. For example, to find the commits between versions `v12.4.13` and `v12.4.14` that changed files owned by the `grafana/grafana-frontend-platform` team:

[`repo:^github\.com/grafana/grafana$@v12.4.14:^v12.4.13 type:commit file:has.owner(grafana/grafana-frontend-platform)`](https://sourcegraph.com/search?q=repo:%5Egithub%5C.com/grafana/grafana%24@v12.4.14:%5Ev12.4.13+type:commit+file:has.owner%28grafana/grafana-frontend-platform%29&patternType=keyword)

## Further reading

In order to learn more please check out our references:

-   [CODEOWNERS format](/code-ownership/codeowners-format) - Guide to using the CODEOWNERS file format to define ownership
