Index
gitversioned ¶
Opinionated PEP 440 Python versioning for Git repos and submodules.
Provides an automated, deterministic system for generating rich version information from Git repository metadata. It enforces CI/User authority and creates version files with deep metadata for auditability, integrating natively with Hatch and Setuptools.
Example: :: from gitversioned import Settings, resolve_version from gitversioned.utils import BuildEnvironment, GitRepository
version, ref = resolve_version(
Settings(), GitRepository(), BuildEnvironment()
)
print(f"Current version: {version}")
LoggingSettings ¶
Bases: BaseSettings
Settings model for configuring the loguru logging infrastructure.
This Pydantic model loads configuration from environment variables prefixed with GITVERSIONED__LOGGING__ and provides typed fields for controlling log output, formatting, and OpenTelemetry integration.
Example: .. code-block:: python
from gitversioned.logging import LoggingSettings, configure_logger
settings = LoggingSettings(enabled=True, level="DEBUG")
configure_logger(settings)
Source code in src/gitversioned/logging.py
model_config = SettingsConfigDict(env_prefix='GITVERSIONED__LOGGING__', env_nested_delimiter='__') class-attribute ¶
Pydantic configuration dict dictating environment variable prefixes.
Settings ¶
Bases: BaseSettings
Configuration for GitVersioned, handling formatting, strictness, and outputs.
This class aggregates and prioritizes configuration from multiple sources, providing a unified state for version resolution across the tool. It is built on top of pydantic-settings to allow validation and type coercion.
Example: ::
settings = Settings(package_name="my_pkg")
print(settings.format_main)
Source code in src/gitversioned/settings.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | |
__repr__() ¶
Return a detailed string representation.
Source code in src/gitversioned/settings.py
__str__() ¶
Return a concise string representation.
Source code in src/gitversioned/settings.py
settings_customise_sources(settings_cls, init_settings, env_settings, dotenv_settings, file_secret_settings) classmethod ¶
Customizes the configuration sources and their priority.
This method overrides the default pydantic-settings source priority to inject our custom SetupCfgSettingsSource and define the specific order of resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings_cls | type[BaseSettings] | The settings class being instantiated. | required |
init_settings | PydanticBaseSettingsSource | The initial settings provided via kwargs. | required |
env_settings | PydanticBaseSettingsSource | Settings loaded from environment variables. | required |
dotenv_settings | PydanticBaseSettingsSource | Settings loaded from a .env file. | required |
file_secret_settings | PydanticBaseSettingsSource | Settings loaded from secret files. | required |
Returns:
| Type | Description |
|---|---|
tuple[PydanticBaseSettingsSource, ...] | A tuple of settings sources in priority order. |
Source code in src/gitversioned/settings.py
configure_logger(settings=None) ¶
Initializes the loguru logger with the provided settings or from the environment.
This function configures the global loguru logger instance based on the provided LoggingSettings. It handles enabling/disabling the logger, managing sinks, and injecting the appropriate formatter (including OpenTelemetry).
Example: .. code-block:: python
from gitversioned.logging import configure_logger, LoggingSettings
configure_logger(LoggingSettings(level="DEBUG"))
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings | LoggingSettings | None | An optional instance of | None |
Returns:
| Type | Description |
|---|---|
None | None |
Raises:
| Type | Description |
|---|---|
ImportError | If OpenTelemetry formatting is explicitly enabled but the package is not installed. |
Source code in src/gitversioned/logging.py
generate_version_py(version, reference, settings, repository, environment) ¶
Writes the resolved version metadata to a python file using templates.
This function utilizes the configured release or development templates to generate a python file containing version information, which can then be included directly within the target package.
Example: >>> path = generate_version_py(version, ref, settings, repo, env)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version | Version | The resolved PEP 440 version object. | required |
reference | GitReference | The resolved Git reference object. | required |
settings | Settings | Configuration rules for resolving the version. | required |
repository | GitRepository | The current git repository state. | required |
environment | BuildEnvironment | Build environment metadata. | required |
Returns:
| Type | Description |
|---|---|
Path | None | The Path object pointing to the written file. |
Source code in src/gitversioned/versioning.py
resolve_and_generate_version(settings, repository, environment) ¶
Main entry point to resolve the version and write the output file if configured.
This function wraps the core version resolution logic and subsequently triggers the generation of the version python file if an output file is specified in the settings. It provides a convenient single call for build hooks and integrations.
Example: >>> version, path = resolve_and_generate_version(settings, repo, env)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings | Settings | Configuration rules for resolving the version. | required |
repository | GitRepository | The current git repository state. | required |
environment | BuildEnvironment | Build environment metadata. | required |
Returns:
| Type | Description |
|---|---|
tuple[Version, Path | None] | A tuple of the resolved Version and output Path (if generated). |
Source code in src/gitversioned/versioning.py
resolve_version(settings, repository, environment) ¶
Computes the PEP 440 version based on configuration and repository state.
This function coordinates the resolution of version sources according to the provided settings, performs auto-increments if necessary, and formats the final version string based on the target build type (e.g., release, dev, alpha).
Example: >>> version, reference = resolve_version(settings, repo, env)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings | Settings | Configuration rules for resolving the version. | required |
repository | GitRepository | The current git repository state. | required |
environment | BuildEnvironment | Build environment metadata. | required |
Returns:
| Type | Description |
|---|---|
tuple[Version, GitReference] | A tuple containing the resolved Version and the Git reference object. |
Raises:
| Type | Description |
|---|---|
ValueError | If an unknown source type or git type is encountered. |
Source code in src/gitversioned/versioning.py
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 | |