Skip to content

Commit 049b470

Browse files
committed
Changed the terminology for hooks.
Change pre-bump and post-bump to pre-commit and post-commit to better indicate their order of operations.
1 parent e50e991 commit 049b470

7 files changed

+101
-11
lines changed

bumpversion/config/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
"parts": {},
3636
"files": [],
3737
"setup_hooks": [],
38-
"pre_bump_hooks": [],
39-
"post_bump_hooks": [],
38+
"pre_commit_hooks": [],
39+
"post_commit_hooks": [],
4040
}
4141

4242

bumpversion/config/models.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ class Config(BaseSettings):
101101
parts: Dict[str, VersionComponentSpec]
102102
files: List[FileChange] = Field(default_factory=list)
103103
setup_hooks: List[str] = Field(default_factory=list)
104-
pre_bump_hooks: List[str] = Field(default_factory=list)
105-
post_bump_hooks: List[str] = Field(default_factory=list)
104+
pre_commit_hooks: List[str] = Field(default_factory=list)
105+
post_commit_hooks: List[str] = Field(default_factory=list)
106106
included_paths: List[str] = Field(default_factory=list)
107107
excluded_paths: List[str] = Field(default_factory=list)
108108
model_config = SettingsConfigDict(env_prefix="bumpversion_")

bumpversion/hooks.py

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""Implementation of the hook interface."""
2+
3+
import datetime
4+
import os
5+
import subprocess
6+
from typing import Dict, Optional
7+
8+
from bumpversion.config.models import Config
9+
from bumpversion.ui import get_indented_logger
10+
11+
PREFIX = "BVHOOK_"
12+
13+
logger = get_indented_logger(__name__)
14+
15+
16+
def run_command(script: str, environment: Optional[dict] = None) -> subprocess.CompletedProcess:
17+
"""Runs command-line programs using the shell."""
18+
if not isinstance(script, str):
19+
raise TypeError(f"`script` must be a string, not {type(script)}")
20+
if environment and not isinstance(environment, dict):
21+
raise TypeError(f"`environment` must be a dict, not {type(environment)}")
22+
return subprocess.run(script, env=environment, encoding="utf-8", shell=True, text=True, capture_output=True)
23+
24+
25+
def base_env(config: Config) -> Dict[str, str]:
26+
"""Provide the base environment variables."""
27+
return {
28+
f"{PREFIX}NOW": datetime.datetime.now().isoformat(),
29+
f"{PREFIX}UTCNOW": datetime.datetime.now(datetime.timezone.utc).isoformat(),
30+
**os.environ,
31+
**scm_env(config),
32+
}
33+
34+
35+
def scm_env(config: Config) -> Dict[str, str]:
36+
"""Provide the scm environment variables."""
37+
scm = config.scm_info
38+
return {
39+
f"{PREFIX}COMMIT_SHA": scm.commit_sha or "",
40+
f"{PREFIX}DISTANCE_TO_LATEST_TAG": str(scm.distance_to_latest_tag) or "0",
41+
f"{PREFIX}IS_DIRTY": str(scm.dirty),
42+
f"{PREFIX}BRANCH_NAME": scm.branch_name or "",
43+
f"{PREFIX}SHORT_BRANCH_NAME": scm.short_branch_name or "",
44+
f"{PREFIX}CURRENT_VERSION": scm.current_version or "",
45+
f"{PREFIX}CURRENT_TAG": scm.current_tag or "",
46+
}
47+
48+
49+
def current_version_env(config: Config) -> Dict[str, str]:
50+
"""Provide the current version environment variables."""
51+
version_str = config.current_version
52+
version = config.version_config.parse(version_str)
53+
54+
return {f"{PREFIX}CURRENT_{part.upper()}": version[part].value for part in version}
55+
56+
57+
def setup_hook_env(config: Config) -> Dict[str, str]:
58+
"""Provide the environment dictionary for `setup_hook`s."""
59+
return {**base_env(config), **scm_env(config), **current_version_env(config)}
60+
61+
62+
def run_setup_hooks(config: Config) -> None:
63+
"""Run the setup hooks."""
64+
env = setup_hook_env(config)
65+
if config.setup_hooks:
66+
logger.info("Running setup hooks:")
67+
else:
68+
logger.info("No setup hooks defined")
69+
return
70+
71+
logger.indent()
72+
for script in config.setup_hooks:
73+
logger.debug(f"Running {script!r}")
74+
logger.indent()
75+
result = run_command(script, env)
76+
logger.debug(result.stdout)
77+
logger.debug(result.stderr)
78+
logger.debug(f"Exited with {result.returncode}")
79+
logger.indent()
80+
logger.dedent()
81+
82+
83+
def run_pre_commit_hooks(config: Config) -> None:
84+
"""Run the pre-commit hooks."""
85+
pass
86+
87+
88+
def run_post_commit_hooks(config: Config) -> None:
89+
"""Run the post-commit hooks."""
90+
pass

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ line-length = 119
186186
select = ["E", "W", "F", "I", "N", "B", "BLE", "C", "D", "E", "F", "I", "N", "S", "T", "W", "RUF", "NPY", "PD", "PGH", "ANN", "C90", "PLC", "PLE", "PLW", "TCH"]
187187
ignore = [
188188
"ANN002", "ANN003", "ANN101", "ANN102", "ANN204", "ANN401",
189-
"S101", "S104",
189+
"S101", "S104", "S602",
190190
"D105", "D106", "D107", "D200", "D212",
191191
"PD011",
192192
"PLW1510",

tests/fixtures/basic_cfg_expected.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@
7878
'independent': False,
7979
'optional_value': 'gamma',
8080
'values': ['dev', 'gamma']}},
81-
'post_bump_hooks': [],
82-
'pre_bump_hooks': [],
81+
'post_commit_hooks': [],
82+
'pre_commit_hooks': [],
8383
'regex': False,
8484
'replace': '{new_version}',
8585
'scm_info': {'branch_name': None,

tests/fixtures/basic_cfg_expected.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ parts:
106106
values:
107107
- "dev"
108108
- "gamma"
109-
post_bump_hooks:
109+
post_commit_hooks:
110110

111-
pre_bump_hooks:
111+
pre_commit_hooks:
112112

113113
regex: false
114114
replace: "{new_version}"

tests/fixtures/basic_cfg_expected_full.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@
121121
]
122122
}
123123
},
124-
"post_bump_hooks": [],
125-
"pre_bump_hooks": [],
124+
"post_commit_hooks": [],
125+
"pre_commit_hooks": [],
126126
"regex": false,
127127
"replace": "{new_version}",
128128
"scm_info": {

0 commit comments

Comments
 (0)