|
1 | 1 | # Exit Codes |
2 | 2 |
|
3 | | -Commitizen handles expected exceptions through `CommitizenException` and returns different exit codes for different situations. They could be useful if you want to ignore specific errors in your pipeline. |
| 3 | +Commitizen handles expected exceptions through `CommitizenException` and returns different exit codes for different situations. This reference is useful when you need to ignore specific errors in your CI/CD pipeline or automation scripts. |
4 | 4 |
|
5 | | -These exit codes can be found in `commitizen/exceptions.py::ExitCode`. |
| 5 | +All exit codes are defined in [commitizen/exceptions.py](https://github.com/commitizen-tools/commitizen/blob/master/commitizen/exceptions.py). |
| 6 | + |
| 7 | +## Exit Code Reference |
6 | 8 |
|
7 | 9 | | Exception | Exit Code | Description | |
8 | 10 | | --------------------------- | --------- | ----------------------------------------------------------------------------------------------------------- | |
9 | | -| ExpectedExit | 0 | Expected exit | |
10 | | -| DryRunExit | 0 | Exit due to passing `--dry-run` option | |
11 | | -| NoCommitizenFoundException | 1 | Using a cz (e.g., `cz_jira`) that cannot be found in your system | |
12 | | -| NotAGitProjectError | 2 | Not in a git project | |
13 | | -| NoCommitsFoundError | 3 | No commit found | |
14 | | -| NoVersionSpecifiedError | 4 | Version can not be found in configuration file | |
15 | | -| NoPatternMapError | 5 | bump / changelog pattern or map can not be found in configuration file | |
16 | | -| BumpCommitFailedError | 6 | Commit error when bumping version | |
17 | | -| BumpTagFailedError | 7 | Tag error when bumping version | |
18 | | -| NoAnswersError | 8 | No user response given | |
19 | | -| CommitError | 9 | git commit error | |
20 | | -| NoCommitBackupError | 10 | Commit back up file cannot be found | |
21 | | -| NothingToCommitError | 11 | Nothing in staging to be committed | |
22 | | -| CustomError | 12 | `CzException` raised | |
23 | | -| NoCommandFoundError | 13 | No command found when running Commitizen cli (e.g., `cz --debug`) | |
24 | | -| InvalidCommitMessageError | 14 | The commit message does not pass `cz check` | |
25 | | -| MissingConfigError | 15 | Configuration missed for `cz_customize` | |
26 | | -| NoRevisionError | 16 | No revision found | |
27 | | -| CurrentVersionNotFoundError | 17 | current version cannot be found in _version_files_ | |
28 | | -| InvalidCommandArgumentError | 18 | The argument provide to command is invalid (e.g. `cz check -commit-msg-file filename --rev-range master..`) | |
29 | | -| InvalidConfigurationError | 19 | An error was found in the Commitizen Configuration, such as duplicates in `change_type_order` | |
30 | | -| NotAllowed | 20 | `--incremental` cannot be combined with a `rev_range` | |
31 | | -| NoneIncrementExit | 21 | The commits found are not eligible to be bumped | |
32 | | -| CharacterSetDecodeError | 22 | The character encoding of the command output could not be determined | |
33 | | -| GitCommandError | 23 | Unexpected failure while calling a git command | |
34 | | -| InvalidManualVersion | 24 | Manually provided version is invalid | |
35 | | -| InitFailedError | 25 | Failed to initialize pre-commit | |
36 | | -| RunHookError | 26 | An error occurred during a hook execution | |
37 | | -| VersionProviderUnknown | 27 | `version_provider` setting is set to an unknown version provider identifier | |
38 | | -| VersionSchemeUnknown | 28 | `version_scheme` setting is set to an unknown version scheme identifier | |
39 | | -| ChangelogFormatUnknown | 29 | `changelog_format` setting is set to an unknown version scheme identifier or could not be guessed | |
| 11 | +| `ExpectedExit` | 0 | Expected exit | |
| 12 | +| `DryRunExit` | 0 | Exit due to passing `--dry-run` option | |
| 13 | +| `NoCommitizenFoundException` | 1 | Using a cz (e.g., `cz_jira`) that cannot be found in your system | |
| 14 | +| `NotAGitProjectError` | 2 | Not in a git project | |
| 15 | +| `NoCommitsFoundError` | 3 | No commits found | |
| 16 | +| `NoVersionSpecifiedError` | 4 | Version is not specified in configuration file | |
| 17 | +| `NoPatternMapError` | 5 | bump / changelog pattern or map can not be found in configuration file | |
| 18 | +| `BumpCommitFailedError` | 6 | Commit failed when bumping version | |
| 19 | +| `BumpTagFailedError` | 7 | Tag failed when bumping version | |
| 20 | +| `NoAnswersError` | 8 | No user response given | |
| 21 | +| `CommitError` | 9 | git commit error | |
| 22 | +| `NoCommitBackupError` | 10 | Commit backup file is not found | |
| 23 | +| `NothingToCommitError` | 11 | Nothing in staging to be committed | |
| 24 | +| `CustomError` | 12 | `CzException` raised | |
| 25 | +| `NoCommandFoundError` | 13 | No command found when running Commitizen cli (e.g., `cz --debug`) | |
| 26 | +| `InvalidCommitMessageError` | 14 | The commit message does not pass `cz check` | |
| 27 | +| `MissingConfigError` | 15 | Configuration is missing for `cz_customize` | |
| 28 | +| `NoRevisionError` | 16 | No revision found | |
| 29 | +| `CurrentVersionNotFoundError`| 17 | Current version cannot be found in `version_files` | |
| 30 | +| `InvalidCommandArgumentError`| 18 | The argument provided to the command is invalid (e.g. `cz check -commit-msg-file filename --rev-range master..`) | |
| 31 | +| `InvalidConfigurationError` | 19 | An error was found in the Commitizen Configuration, such as duplicates in `change_type_order` | |
| 32 | +| `NotAllowed` | 20 | Invalid combination of command line / configuration file options | |
| 33 | +| `NoneIncrementExit` | 21 | The commits found are not eligible to be bumped | |
| 34 | +| `CharacterSetDecodeError` | 22 | The character encoding of the command output could not be determined | |
| 35 | +| `GitCommandError` | 23 | Unexpected failure while calling a git command | |
| 36 | +| `InvalidManualVersion` | 24 | Manually provided version is invalid | |
| 37 | +| `InitFailedError` | 25 | Failed to initialize pre-commit | |
| 38 | +| `RunHookError` | 26 | An error occurred during a hook execution | |
| 39 | +| `VersionProviderUnknown` | 27 | Unknown `version_provider` | |
| 40 | +| `VersionSchemeUnknown` | 28 | Unknown `version_scheme` | |
| 41 | +| `ChangelogFormatUnknown` | 29 | Unknown `changelog_format` or cannot be determined by the file extension | |
| 42 | +| `ConfigFileNotFound` | 30 | The configuration file is not found | |
| 43 | +| `ConfigFileIsEmpty` | 31 | The configuration file is empty | |
| 44 | +| `CommitMessageLengthLimitExceededError`| 32 | The commit message length exceeds the given limit. | |
| 45 | + |
| 46 | +## Ignoring Exit Codes |
| 47 | + |
| 48 | +In some scenarios, you may want Commitizen to continue execution even when certain errors occur. This is particularly useful in CI/CD pipelines where you want to handle specific errors gracefully. |
| 49 | + |
| 50 | +### Using `--no-raise` Flag |
| 51 | + |
| 52 | +The `--no-raise` (or `-nr`) flag allows you to specify exit codes that should not cause Commitizen to exit with an error. You can use either: |
| 53 | + |
| 54 | +- **Exit code numbers**: `21`, `3`, `4` |
| 55 | +- **Exit code names**: `NO_INCREMENT`, `NO_COMMITS_FOUND`, `NO_VERSION_SPECIFIED` |
| 56 | +- **Mixed format**: `21,NO_COMMITS_FOUND,4` |
| 57 | + |
| 58 | +Multiple exit codes can be specified as a comma-separated list. |
| 59 | + |
| 60 | +### Common Use Cases |
| 61 | + |
| 62 | +#### Ignoring No Increment Errors |
| 63 | + |
| 64 | +The most common use case is to ignore `NoneIncrementExit` (exit code 21) when running `cz bump`. This allows the command to succeed even when no commits are eligible for a version bump: |
| 65 | + |
| 66 | +```sh |
| 67 | +cz -nr 21 bump |
| 68 | +``` |
| 69 | + |
| 70 | +Or using the exit code name: |
| 71 | + |
| 72 | +```sh |
| 73 | +cz -nr NO_INCREMENT bump |
| 74 | +``` |
| 75 | + |
| 76 | +This is useful in CI pipelines where you want to run `cz bump` regularly, but don't want the pipeline to fail when there are no version-worthy commits. |
| 77 | + |
| 78 | +#### Ignoring Multiple Exit Codes |
| 79 | + |
| 80 | +You can ignore multiple exit codes at once: |
| 81 | + |
| 82 | +```sh |
| 83 | +cz --no-raise 21,3,4 bump |
| 84 | +``` |
| 85 | + |
| 86 | +This example ignores: |
| 87 | + |
| 88 | +- `21` (`NoneIncrementExit`) - No eligible commits for bump |
| 89 | +- `3` (`NoCommitsFoundError`) - No commits found |
| 90 | +- `4` (`NoVersionSpecifiedError`) - Version not specified |
| 91 | + |
| 92 | +### Finding the Exit Code |
| 93 | + |
| 94 | +If you encounter an error and want to ignore it, you can find the exit code in two ways: |
| 95 | + |
| 96 | +#### Method 1: Check the Exit Code After Running |
| 97 | + |
| 98 | +After running a Commitizen command that fails, check the exit code: |
| 99 | + |
| 100 | +```sh |
| 101 | +cz bump |
| 102 | +echo $? # Prints the exit code (e.g., 21) |
| 103 | +``` |
| 104 | + |
| 105 | +Then use that exit code with `--no-raise`: |
| 106 | + |
| 107 | +```sh |
| 108 | +cz -nr 21 bump |
| 109 | +``` |
| 110 | + |
| 111 | +#### Method 2: Look Up the Exception |
| 112 | + |
| 113 | +1. Check the error message to identify the exception type |
| 114 | +2. Find the corresponding exit code in the table above |
| 115 | +3. Use that exit code with `--no-raise` |
| 116 | + |
| 117 | +For example, if you see `NoneIncrementExit` in the error, look it up in the table to find it's exit code 21, then use: |
| 118 | + |
| 119 | +```sh |
| 120 | +cz -nr 21 bump |
| 121 | +``` |
| 122 | + |
| 123 | +### Best Practices |
| 124 | + |
| 125 | +- **Document your usage**: If you use `--no-raise` in scripts or CI/CD, document why specific exit codes are ignored |
| 126 | +- **Be specific**: Only ignore exit codes you understand and have a reason to ignore |
| 127 | +- **Test thoroughly**: Ensure that ignoring certain exit codes doesn't mask real problems in your workflow |
| 128 | +- **Use exit code names**: When possible, use exit code names (e.g., `NO_INCREMENT`) instead of numbers for better readability |
| 129 | + |
| 130 | +### Example: CI/CD Pipeline |
| 131 | + |
| 132 | +Here's an example of using `--no-raise` in a CI/CD pipeline: |
| 133 | + |
| 134 | +```yaml |
| 135 | +# .github/workflows/release.yml |
| 136 | +- name: Bump version |
| 137 | + run: | |
| 138 | + cz -nr NO_INCREMENT bump || true |
| 139 | + # Continue even if no version bump is needed |
| 140 | +``` |
| 141 | +
|
| 142 | +This ensures the pipeline continues even when there are no commits eligible for a version bump. |
0 commit comments