2020-10-08 20:08:41 -04:00
---
type: reference, dev
stage: none
group: Development
2020-11-26 01:09:20 -05:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2020-10-08 20:08:41 -04:00
---
2018-10-11 07:57:49 -04:00
# Style guides
2020-03-16 17:09:21 -04:00
## Editor/IDE styling standardization
We use [EditorConfig ](https://editorconfig.org/ ) to automatically apply certain styling
standards before files are saved locally. Most editors/IDEs will honor the `.editorconfig`
settings automatically by default. If your editor/IDE does not automatically support `.editorconfig` ,
we suggest investigating to see if a plugin exists. For instance here is the
[plugin for vim ](https://github.com/editorconfig/editorconfig-vim ).
2021-01-22 04:08:53 -05:00
## Pre-push static analysis with Lefthook
2020-01-28 16:08:56 -05:00
2021-01-22 04:08:53 -05:00
[Lefthook ](https://github.com/Arkweid/lefthook ) is a Git hooks manager that allows
custom logic to be executed prior to Git committing or pushing. GitLab comes with
Lefthook configuration (`lefthook.yml`), but it must be installed.
2020-02-03 10:08:45 -05:00
2021-01-22 04:08:53 -05:00
We have a `lefthook.yml` checked in but it is ignored until Lefthook is installed.
2020-01-28 16:08:56 -05:00
2021-01-22 04:08:53 -05:00
### Uninstall Overcommit
We were using Overcommit prior to Lefthook, so you may want to uninstall it first with `overcommit --uninstall` .
### Install Lefthook
1. Install the `lefthook` Ruby gem:
```shell
bundle install
```
1. Install Lefthook managed Git hooks:
```shell
bundle exec lefthook install
```
1. Test Lefthook is working by running the Lefthook `prepare-commit-msg` Git hook:
```shell
bundle exec lefthook run prepare-commit-msg
```
This should return a fully qualified path command with no other output.
2020-10-30 02:08:54 -04:00
2021-01-22 04:08:53 -05:00
### Lefthook configuration
2020-11-02 19:08:51 -05:00
2022-02-17 01:17:38 -05:00
Lefthook is configured with a combination of:
2020-11-10 22:08:57 -05:00
2022-02-17 01:17:38 -05:00
- Project configuration in [`lefthook.yml` ](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lefthook.yml ).
- Any [local configuration ](https://github.com/Arkweid/lefthook/blob/master/docs/full_guide.md#local-config ).
2020-11-10 22:08:57 -05:00
2021-01-22 04:08:53 -05:00
### Disable Lefthook temporarily
2020-12-02 16:09:44 -05:00
2021-01-22 04:08:53 -05:00
To disable Lefthook temporarily, you can set the `LEFTHOOK` environment variable to `0` . For instance:
```shell
LEFTHOOK=0 git push ...
```
### Run Lefthook hooks manually
To run the `pre-push` Git hook, run:
```shell
bundle exec lefthook run pre-push
2020-01-28 16:08:56 -05:00
```
2021-01-22 04:08:53 -05:00
For more information, check out [Lefthook documentation ](https://github.com/Arkweid/lefthook/blob/master/docs/full_guide.md#run-githook-group-directly ).
### Skip Lefthook checks per tag
2020-01-28 16:08:56 -05:00
2021-01-22 04:08:53 -05:00
To skip some checks based on tags when pushing, you can set the `LEFTHOOK_EXCLUDE` environment variable. For instance:
2020-10-30 02:08:54 -04:00
2021-01-22 04:08:53 -05:00
```shell
LEFTHOOK_EXCLUDE=frontend,documentation git push ...
```
2020-10-30 02:08:54 -04:00
2021-06-28 14:08:39 -04:00
As an alternative, you can create `lefthook-local.yml` with this structure:
```yaml
pre-push:
exclude_tags:
- frontend
- documentation
```
2021-01-22 04:08:53 -05:00
For more information, check out [Lefthook documentation ](https://github.com/Arkweid/lefthook/blob/master/docs/full_guide.md#skip-some-tags-on-the-fly ).
2020-02-04 07:09:00 -05:00
2021-06-28 14:08:39 -04:00
### Skip or enable a specific Lefthook check
To skip or enable a check based on its name when pushing, you can add `skip: true`
or `skip: false` to the `lefthook-local.yml` section for that hook. For instance,
you might want to enable the gettext check to detect issues with `locale/gitlab.pot` :
```yaml
pre-push:
commands:
gettext:
skip: false
```
2021-06-29 08:08:48 -04:00
For more information, check out [Lefthook documentation Skipping commands section ](https://github.com/evilmartians/lefthook/blob/master/docs/full_guide.md#skipping-commands ).
2021-06-28 14:08:39 -04:00
2020-01-28 16:08:56 -05:00
## Ruby, Rails, RSpec
Our codebase style is defined and enforced by [RuboCop ](https://github.com/rubocop-hq/rubocop ).
You can check for any offenses locally with `bundle exec rubocop --parallel` .
On the CI, this is automatically checked by the `static-analysis` jobs.
2021-04-21 11:09:35 -04:00
In addition, you can [integrate RuboCop ](../developing_with_solargraph.md ) into
2021-05-18 14:10:54 -04:00
supported IDEs using the [Solargraph ](https://github.com/castwide/solargraph ) gem.
2021-04-21 11:09:35 -04:00
2020-02-06 19:09:12 -05:00
For RuboCop rules that we have not taken a decision on yet, we follow the
2020-01-28 16:08:56 -05:00
[Ruby Style Guide ](https://github.com/rubocop-hq/ruby-style-guide ),
[Rails Style Guide ](https://github.com/rubocop-hq/rails-style-guide ), and
[RSpec Style Guide ](https://github.com/rubocop-hq/rspec-style-guide ) as general
guidelines to write idiomatic Ruby/Rails/RSpec, but reviewers/maintainers should
be tolerant and not too pedantic about style.
Similarly, some RuboCop rules are currently disabled, and for those,
reviewers/maintainers must not ask authors to use one style or the other, as both
are accepted. This isn't an ideal situation since this leaves space for
[bike-shedding ](https://en.wiktionary.org/wiki/bikeshedding ), and ideally we
should enable all RuboCop rules to avoid style-related
2021-08-04 02:09:49 -04:00
discussions/nitpicking/back-and-forth in reviews. There are some styles that
commonly come up in reviews that are not enforced, the
[GitLab Ruby style guide ](../backend/ruby_style_guide.md ) includes a non-exhaustive
list of these topics.
2020-01-28 16:08:56 -05:00
Additionally, we have a dedicated
2020-05-19 23:08:04 -04:00
[newlines style guide ](../newlines_styleguide.md ), as well as dedicated
2020-01-28 16:08:56 -05:00
[test-specific style guides and best practices ](../testing_guide/index.md ).
2020-06-18 05:08:41 -04:00
### Creating new RuboCop cops
Typically it is better for the linting rules to be enforced programmatically as it
reduces the aforementioned [bike-shedding ](https://en.wiktionary.org/wiki/bikeshedding ).
To that end, we encourage creation of new RuboCop rules in the codebase.
2021-07-02 14:08:28 -04:00
We currently maintain Cops across several Ruby code bases, and not all of them are
specific to the GitLab application.
2020-06-18 05:08:41 -04:00
When creating a new cop that could be applied to multiple applications, we encourage you
to add it to our [GitLab Styles ](https://gitlab.com/gitlab-org/gitlab-styles ) gem.
2021-07-02 14:08:28 -04:00
If the Cop targets rules that only apply to the main GitLab application,
it should be added to [GitLab ](https://gitlab.com/gitlab-org/gitlab ) instead.
2020-06-18 05:08:41 -04:00
2020-12-11 13:09:57 -05:00
### Resolving RuboCop exceptions
When the number of RuboCop exceptions exceed the default [`exclude-limit` of 15 ](https://docs.rubocop.org/rubocop/1.2/usage/basic_usage.html#command-line-flags ),
we may want to resolve exceptions over multiple commits. To minimize confusion,
we should track our progress through the exception list.
2022-03-09 19:08:36 -05:00
The preferred way to [generate the initial list or a list for specific RuboCop rules ](../rake_tasks.md#generate-initial-rubocop-todo-list )
is to run the Rake task `rubocop:todo:generate` :
2020-12-11 13:09:57 -05:00
```shell
2022-03-09 19:08:36 -05:00
# Initial list
2021-09-14 08:10:35 -04:00
bundle exec rake rubocop:todo:generate
2022-03-09 19:08:36 -05:00
# List for specific RuboCop rules
bundle exec rake 'rubocop:todo:generate[Gitlab/NamespacedClass,Lint/Syntax]'
2020-12-11 13:09:57 -05:00
```
2022-03-09 19:08:36 -05:00
This Rake task creates or updates the exception list in `.rubocop_todo/` . For
example, the configuration for the RuboCop rule `Gitlab/NamespacedClass` is
located in `.rubocop_todo/gitlab/namespaced_class.yml` .
Make sure to commit any changes in `.rubocop_todo/` after running the Rake task.
2020-12-11 13:09:57 -05:00
2021-02-18 22:10:49 -05:00
### Reveal existing RuboCop exceptions
To reveal existing RuboCop exceptions in the code that have been excluded via `.rubocop_todo.yml` and
2021-12-01 07:16:08 -05:00
`.rubocop_todo/**/*.yml` , set the environment variable `REVEAL_RUBOCOP_TODO` to `1` .
2021-02-18 22:10:49 -05:00
This allows you to reveal existing RuboCop exceptions during your daily work cycle and fix them along the way.
2021-02-22 04:10:46 -05:00
NOTE:
2021-12-01 07:16:08 -05:00
Define permanent `Exclude` s in `.rubocop.yml` instead of `.rubocop_todo/**/*.yml` .
2021-02-22 04:10:46 -05:00
2020-01-28 16:08:56 -05:00
## Database migrations
See the dedicated [Database Migrations Style Guide ](../migration_style_guide.md ).
## JavaScript
See the dedicated [JS Style Guide ](../fe_guide/style/javascript.md ).
## SCSS
See the dedicated [SCSS Style Guide ](../fe_guide/style/scss.md ).
## Go
See the dedicated [Go standards and style guidelines ](../go_guide/index.md ).
## Shell commands (Ruby)
See the dedicated [Guidelines for shell commands in the GitLab codebase ](../shell_commands.md ).
## Shell scripting
See the dedicated [Shell scripting standards and style guidelines ](../shell_scripting_guide/index.md ).
## Markdown
2021-01-29 16:09:34 -05:00
<!-- vale gitlab.Spelling = NO -->
2020-06-26 02:09:03 -04:00
We're following [Ciro Santilli's Markdown Style Guide ](https://cirosantilli.com/markdown-style-guide/ ).
2020-01-28 16:08:56 -05:00
2021-01-29 16:09:34 -05:00
<!-- vale gitlab.Spelling = YES -->
2020-01-28 16:08:56 -05:00
## Documentation
2020-11-12 16:09:19 -05:00
See the dedicated [Documentation Style Guide ](../documentation/styleguide/index.md ).
2020-01-28 16:08:56 -05:00
## Python
See the dedicated [Python Development Guidelines ](../python_guide/index.md ).
## Misc
Code should be written in [US English ](https://en.wikipedia.org/wiki/American_English ).