Skip to content

Commit 3b638e0

Browse files
committed
Added hooks to bump command
1 parent 8446567 commit 3b638e0

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

bumpversion/bump.py

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from pathlib import Path
55
from typing import TYPE_CHECKING, List, MutableMapping, Optional
66

7+
from bumpversion.hooks import run_post_commit_hooks, run_pre_commit_hooks, run_setup_hooks
8+
79
if TYPE_CHECKING: # pragma: no-coverage
810
from bumpversion.files import ConfiguredFile
911
from bumpversion.versioning.models import Version
@@ -75,10 +77,14 @@ def do_bump(
7577
logger.indent()
7678

7779
ctx = get_context(config)
80+
7881
logger.info("Parsing current version '%s'", config.current_version)
7982
logger.indent()
8083
version = config.version_config.parse(config.current_version)
8184
logger.dedent()
85+
86+
run_setup_hooks(config)
87+
8288
next_version = get_next_version(version, config, version_part, new_version)
8389
next_version_str = config.version_config.serialize(next_version, ctx)
8490
logger.info("New version will be '%s'", next_version_str)
@@ -109,7 +115,13 @@ def do_bump(
109115

110116
ctx = get_context(config, version, next_version)
111117
ctx["new_version"] = next_version_str
118+
119+
run_pre_commit_hooks(config)
120+
112121
commit_and_tag(config, config_file, configured_files, ctx, dry_run)
122+
123+
run_post_commit_hooks(config)
124+
113125
logger.info("Done.")
114126

115127

docs/reference/hooks.md

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)