> For the complete documentation index, see [llms.txt](https://cortex-docs.paloaltonetworks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cortex-docs.paloaltonetworks.com/demisto-sdk-development-guide/demisto-sdk-guide/demisto-sdk-commands/pre-commit.md).

# pre-commit

The `pre-commit` command enhances the content development experience by running a variety of checks and linters. It utilizes the [pre-commit](https://github.com/pre-commit/pre-commit) framework for managing and maintaining multi-language pre-commit hooks. A `.pre-commit-config-template.yaml` file is used to configure the hooks if they are found in the content repository. Otherwise, a default is used.

Since content items run in containers with different Python versions and dependencies, this command matches content items with similar configurations before passing the generated temporary `.pre-commit-config.yaml` file.

{% hint style="info" %}

### Note

An internet connection is required to use `pre-commit`.
{% endhint %}

<details>

<summary>Arguments</summary>

| `-i`, `--input`, `--files` | The paths to run `pre-commit` on. May pass multiple paths.                                                                                               |
| -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--staged-only`            | Whether to run only on staged files.                                                                                                                     |
| `--committed-only`         | Whether to run on committed files only.                                                                                                                  |
| `-g`, `--git-diff`         | Whether to use Git to determine which files to run on.                                                                                                   |
| `--prev-ver`               | The previous version to compare against. If not provided, the previous version will be determined using Git.                                             |
| `-a`, `--all-files`        | Whether to run on all files.                                                                                                                             |
| `--mode`                   | Special mode to run `pre-commit` with.                                                                                                                   |
| `--skip`                   | A list of `pre-commit` hooks to skip.                                                                                                                    |
| `--validate/--no-validate` | Whether to run `demisto-sdk validate`.                                                                                                                   |
| `--format/--no-format`     | Whether to run `demisto-sdk format`.                                                                                                                     |
| `--secrets/--no-secrets`   | Whether to run `demisto-sdk secrets`.                                                                                                                    |
| `--show-diff-on-failure`   | Show `diff` on failure.                                                                                                                                  |
| `--dry-run`                | Whether to run the `pre-commit` hooks in `dry-run` mode, which only creates the configuration file.                                                      |
| `--docker/--no-docker`     | Whether to run Docker-based hooks.                                                                                                                       |
| `--image-ref`              | The Docker image reference to run Docker hooks with. Overrides the Docker image from the YAML file or the native image configuration.                    |
| `--docker-image`           | Override the `docker_image` property in the template file. This is a comma-separated list of: `from-yml`, `native:dev`, `native:ga`, `native:candidate`. |
| `--console-log-threshold`  | Minimum logging threshold for the console logger.                                                                                                        |
| `--file-log-threshold`     | Minimum logging threshold for the file logger.                                                                                                           |
| `--log-file-path`          | Path to save log files to.                                                                                                                               |
| `--template-path`          | A custom path for the pre-defined `pre-commit` template. If not provided, the system uses the default template.                                          |

</details>

<details>

<summary>Examples</summary>

* `demisto-sdk --pre-commit`

  Runs `pre-commit` on all files collected by Git.
* `demisto-sdk --pre-commit -i Packs/hello_world`

  Runs `pre-commit` on all files in the `hello_world` pack.
* `demisto-sdk --pre-commit --no-validate`

  Runs `pre-commit` without the validate step.
* `demisto-sdk --pre-commit --show-diff-on-failure`

  Runs `pre-commit` and shows differences on failures.

</details>

<details>

<summary>Modes</summary>

You can set different arguments for different modes. For example, you might want to exclude some rules from the nightly build. Any key can be set this way.

You can use any mode you want and have multiple modes for each hook as in the following example.

#### Example

To set arguments for a mode:

```programlisting
  - id: sample-hook
    args:nightly: ['--exclude', '123']
    args:mode1: ['--test', '123']
    args: ['This is the default argument']
    otherkey:nightly: hello
    otherkey: world
```

To call the `pre-commit` command:

```programlisting
demisto-sdk pre-commit -a --mode nightly
```

</details>

<details>

<summary>Hooks</summary>

Hooks can be set in the `.pre-commit-config_template.yaml` file. The syntax is similar to the official [pre-commit hooks](https://pre-commit.com/#new-hooks), but allows more keys to be set.

#### Skip key

To skip a certain hook from running, add a `skip` key to the hook configuration:

```programlisting
- id: sample-hook
  skip: true
```

The `skip` hook can be used together with `mode`, to skip a hook when running in a specific mode.

#### Needs key

The `needs` keys allows you to define dependencies between hooks. If a hook with `needs` is skipped, hooks that depend on it are also skipped. In this example, both hooks are skipped:

```programlisting
- id: sample-hook
  skip: true
- id: needs-example
  needs: ["sample-hook"]
```

#### Parallel key

The `parallel` key indicates whether a hook should run in parallel. By default, hooks such as `mypy`, `Ruff`, and `Docker` produce multiple hooks which run in parallel. Default is `True`. To avoid running a specific hook in parallel, you can set it to `false`. When setting the parallel of a hook to `false`, a split hook will run sequentially.

```programlisting
- id: sample-hook
  parallel: false
```

</details>

<details>

<summary>External tools</summary>

* [Ruff](https://github.com/astral-sh/ruff)

  Runs Ruff, the extremely fast Python linter, using the Python version used in the container image set for the content item.

  This step uses the Ruff rules configured in the `pyproject.toml` at the root of the content repo. If `pyproject.toml` does not exist, it uses Ruff's default rules. The rules set under the official `demisto/content` repo [pyproject.toml](https://github.com/demisto/content/blob/master/pyproject.toml) file were handpicked to ensure a high level of code quality, and prevent bugs often found in content.

  This step may modify files using Ruff's `--fix` flag. When files are modified, the step fails. We recommend committing them into the repo.
* Check JSON/YAML/AST(py)

  Validates that the content of the files is formatted correctly.
* [Autopep8](https://github.com/hhatto/autopep8)

  A linter that automatically formats Python code to conform to the [PEP 8 Style Guide](https://peps.python.org/pep-0008).
* [Pycln](https://github.com/hadialqattan/pycln)

  Pycln is a formatter for finding and removing unused import statements.

</details>

<details>

<summary>Docker hooks</summary>

To run a command in a script's relevant container, you can set up a Docker hook.

A Docker hook must be under the `local` repo and its `id` must end with `in-docker`.

#### Example

```programlisting
  - id: simple-in-docker
    name: simple-in-docker
    description: my simple description
    entry: simplelinter
    args: ['run-all-checks']
```

The `entry` key should be the command that should be run (such as `pylint`, `pytest`).

#### env key

Some commands require environment variables to be set. To configure environment variables for Docker hooks you can set the `env` key. An example with `pytest`:

```programlisting
  - id: simple-in-docker
    env:
      DEMISTO_SDK__PYTEST__TESTS_PATH: Tests/scripts
```

#### copy\_files key

Some hooks require several files to exist in the same directory as the code file in order to run properly. To configure this, set the `copy_files` key as follows:

```programlisting
- id: simple-in-docker
  copy_files:
    - Tests/scripts/conftest.py
```

#### config\_file\_arg key

Commands run in Docker often require a specific configuration file per integration or script.  To configure this, you must set the `config_file_arg` key. The configuration file should be in the same directory as the code file.

Example with Ruff:

```programlisting
  - id: simple-in-docker
    config_file_arg:
      arg_name: '--config'
      file_name: 'ruff.toml'
```

</details>

<details>

<summary>SDK Commands</summary>

The following SDK commands are automatically run:

* [validate](/demisto-sdk-development-guide/demisto-sdk-guide/demisto-sdk-commands/validate.md)
* [format](/demisto-sdk-development-guide/demisto-sdk-guide/demisto-sdk-commands/format.md)
* [secrets](/demisto-sdk-development-guide/demisto-sdk-guide/demisto-sdk-commands/secrets.md)

</details>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://cortex-docs.paloaltonetworks.com/demisto-sdk-development-guide/demisto-sdk-guide/demisto-sdk-commands/pre-commit.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
