|
| 1 | +--- |
| 2 | +title: Hooks |
| 3 | +description: Details about writing and setting up hooks |
| 4 | +icon: |
| 5 | +date: 2024-08-15 |
| 6 | +comments: true |
| 7 | +--- |
| 8 | +# Hooks |
| 9 | + |
| 10 | +- Each global configuration of `setup_hooks`, `pre_commit_hooks`, and `post_commit_hooks` is a list of commands run in a shell |
| 11 | +- Explanation of the context passed into the environment |
| 12 | +- Run in sequentially |
| 13 | + |
| 14 | +Order of operations |
| 15 | + |
| 16 | +- Run setup hooks |
| 17 | +- Increment version |
| 18 | +- Change files |
| 19 | +- Run pre-commit hooks |
| 20 | +- commit and tag |
| 21 | +- Run post-commit hooks |
| 22 | + |
| 23 | +## Setup Hooks |
| 24 | + |
| 25 | +```toml title="Calling individual commands" |
| 26 | +[tool.bumpversion] |
| 27 | +setup_hooks = [ |
| 28 | + "git config --global user.email \"[email protected]\"", |
| 29 | + "git config --global user.name \"Testing Git\"", |
| 30 | + "git --version", |
| 31 | + "git config --list", |
| 32 | +] |
| 33 | +``` |
| 34 | + |
| 35 | +or |
| 36 | + |
| 37 | +```toml title="Calling a shell script" |
| 38 | +[tool.bumpversion] |
| 39 | +setup_hooks = ["path/to/setup.sh"] |
| 40 | +``` |
| 41 | + |
| 42 | +```bash title="path/to/setup.sh" |
| 43 | +#!/usr/bin/env bash |
| 44 | + |
| 45 | +git config --global user.email "[email protected]" |
| 46 | +git config --global user.name "Testing Git" |
| 47 | +git --version |
| 48 | +git config --list |
| 49 | +``` |
| 50 | +### Environment |
| 51 | + |
| 52 | +- The existing OS environment is available |
| 53 | + |
| 54 | +#### Date and time fields |
| 55 | + |
| 56 | +::: field-list |
| 57 | + |
| 58 | + `BVHOOK_NOW` |
| 59 | + : The ISO-8601-formatted current local time without a time zone reference. |
| 60 | + |
| 61 | + `BVHOOK_UTCNOW` |
| 62 | + : The ISO-8601-formatted current local time in the UTC time zone. |
| 63 | + |
| 64 | +#### Source code management fields |
| 65 | + |
| 66 | +These fields will only have values if the code is in a Git or Mercurial repository. |
| 67 | + |
| 68 | +::: field-list |
| 69 | + |
| 70 | + `BVHOOK_COMMIT_SHA` |
| 71 | + : The latest commit reference. |
| 72 | + |
| 73 | + `BHOOK_DISTANCE_TO_LATEST_TAG` |
| 74 | + : The number of commits since the latest tag. |
| 75 | + |
| 76 | + `BVHOOK_IS_DIRTY` |
| 77 | + : A boolean indicating if the current repository has pending changes. |
| 78 | + |
| 79 | + `BVHOOK_BRANCH_NAME` |
| 80 | + : The current branch name. |
| 81 | + |
| 82 | + `BVHOOK_SHORT_BRANCH_NAME` |
| 83 | + : The current branch name, converted to lowercase, with non-alphanumeric characters removed and truncated to 20 characters. For example, `feature/MY-long_branch-name` would become `featuremylongbranchn`. |
| 84 | + |
| 85 | + |
| 86 | +#### Version fields |
| 87 | + |
| 88 | +::: field-list |
| 89 | + `BVHOOK_CURRENT_VERSION` |
| 90 | + : The current version serialized as a string |
| 91 | + |
| 92 | + `BVHOOK_CURRENT_TAG` |
| 93 | + : The current tag |
| 94 | + |
| 95 | + `BVHOOK_CURRENT_<version component>` |
| 96 | + : Each version component defined by the [version configuration parsing regular expression](configuration/global.md#parse). The default configuration would have `current_major`, `current_minor`, and `current_patch` available. |
0 commit comments