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("Current version:", version)
BuildEnvironment
¶
Bases: BaseModel
Structured metadata representing the current system and build execution context.
Captures environmental data such as OS details, hardware specs, and CI presence. Utilized to record the provenance of a build artifact for auditing and debugging.
Example
env = BuildEnvironment() print(env.os_system) 'Darwin'
Source code in src/gitversioned/utils/environment.py
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 | |
__repr__()
¶
Return a detailed string representation.
Source code in src/gitversioned/utils/environment.py
__str__()
¶
Return a concise string representation.
Source code in src/gitversioned/utils/environment.py
GitRepository
¶
Interface for querying Git repository status and references.
Provides properties and methods to interact with a Git repository using typed Pydantic models for commits, tags, and branches.
Example¶
.. code-block:: python
from gitversioned.utils.git import GitRepository
repo = GitRepository()
if repo.is_available:
print(repo.head_name)
Source code in src/gitversioned/utils/git.py
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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 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 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 | |
branches
property
¶
Yield all branches in the repository.
:return: Iterator of branch reference objects. :raises NotAGitRepositoryError: If the path is not a valid Git repository.
commit_count
property
¶
Get the total commit count on the current branch.
:return: Total number of commits, or 0 if unavailable.
commits
property
¶
Yield all commits in the repository history.
:return: Iterator of commit reference objects. :raises NotAGitRepositoryError: If the path is not a valid Git repository.
current_branch
property
¶
Get the currently checked-out branch.
:return: Current branch reference, or None if in detached HEAD.
current_commit
property
¶
Get the most recent commit.
:return: Most recent commit reference, or None if empty.
current_commit_or_fallback
property
¶
Get the most recent commit or a generated fallback reference.
:return: Current commit reference, or a dummy reference if unavailable.
dirty_files
property
¶
Get a list of all modified and untracked file paths.
:return: List of paths with uncommitted changes.
head_name
property
¶
Get the branch name or the short commit SHA of HEAD.
:return: Current branch name, or short SHA if detached.
is_available
property
¶
Check if the base path is within a valid Git work tree.
:return: True if the repository path is a valid Git work tree, False otherwise.
is_dirty
property
¶
Check if the repository has uncommitted modifications.
:return: True if dirty changes exist, False otherwise.
last_tag
property
¶
Get the most recent tag.
:return: Most recent tag reference, or None if no tags exist.
remote_origin_url
property
¶
Get the remote origin URL.
:return: Remote origin URL, or empty string if not set.
repository_name
property
¶
Get the name of the Git repository.
Attempts to parse the name from the remote origin URL, falling back to the root directory name.
:return: The repository name.
root_directory
property
¶
Get the root directory of the Git repository.
:return: Absolute path to the Git repository root. :raises NotAGitRepositoryError: If the path is not a valid Git repository.
tags
property
¶
Yield all tags in the repository sorted by creation date.
:return: Iterator of tag reference objects. :raises NotAGitRepositoryError: If the path is not a valid Git repository.
__init__(repository_path=None)
¶
Initialize the GitRepository instance.
:param repository_path: Base directory of the repository, defaults to Path.cwd().
Source code in src/gitversioned/utils/git.py
__repr__()
¶
__str__()
¶
Return a concise string representation.
Source code in src/gitversioned/utils/git.py
filtered_dirty_files(ignore_paths=None, fail_on_unavailable=False)
¶
Filter modified files excluding the specified ignore paths.
Example¶
.. code-block:: python
dirty = repo.filtered_dirty_files(ignore_paths=[Path("tmp")])
:param ignore_paths: List of file/directory paths to exclude from results. :param fail_on_unavailable: If True, raise exception if Git is missing. :return: List of filtered dirty file paths as strings. :raises NotAGitRepositoryError: If repository is missing and fail_on_unavailable is True.
Source code in src/gitversioned/utils/git.py
LoggingSettings
¶
Bases: BaseSettings
Settings configuration for the GitVersioned logging subsystem.
This class defines the configuration schema for logging, loading parameters
from environment variables prefixed with GITVERSIONED__LOGGING__ or via
direct instantiation. It allows customizing the output sink, log level,
format template, OpenTelemetry integration, and thread-safe queueing.
Example
.. code-block:: python
from gitversioned.logging import LoggingSettings, configure_logger
settings = LoggingSettings(
enabled=True,
level="DEBUG",
sink="stdout"
)
configure_logger(settings)
:cvar model_config: Configuration dictionary dictating environment variable prefixes and nested delimiters.
Source code in src/gitversioned/logging.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 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 | |
Settings
¶
Bases: BaseSettings
Unified configuration settings for GitVersioned.
Manages settings loaded from environment variables, configuration files (such as pyproject.toml or setup.cfg), CLI flags, and constructor inputs. Governs how the dynamic version parser resolves git refs, matches tags, and generates target version files.
Example
::
settings = Settings(package_name="my_package")
src_path = settings.resolve_path_from_src("my_package/__init__.py")
:cvar model_config: Custom configuration dictionary settings for Pydantic.
Source code in src/gitversioned/settings.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 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 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 | |
__repr__()
¶
Return a detailed string representation of the settings.
:return: Detailed string representation of settings.
Source code in src/gitversioned/settings.py
__str__()
¶
Return a concise string representation of the settings.
:return: Concise string representation.
Source code in src/gitversioned/settings.py
get_overridden_settings(override_name)
¶
Get a new Settings instance with overrides for the given override name.
This method dumps the root settings, pops the 'overrides' key to prevent infinite recursion, and updates the settings with override-specific config.
:param override_name: Name of the override configuration to load settings for. :returns: A new Settings instance with the overrides applied. :raises ValueError: If the override name does not exist.
Source code in src/gitversioned/settings.py
resolve_path_from_project(path, enforce_existence=True)
¶
Resolve a path relative to the project root directory.
Example
::
path = settings.resolve_path_from_project("setup.cfg")
:param path: The path to resolve. :param enforce_existence: Whether to enforce that the path exists. :return: The resolved absolute Path if it exists (or if not enforcing existence), otherwise None.
Source code in src/gitversioned/settings.py
resolve_path_from_root(path, enforce_existence=True)
¶
Resolve a path relative to the project root or source root.
This method attempts to resolve the given path first from the project root, falling back to the source root if it is not found.
Example
::
path = settings.resolve_path_from_root("version.txt")
:param path: The path to resolve. :param enforce_existence: Whether to enforce that the path exists. :return: The resolved absolute Path if it exists (or if not enforcing existence), otherwise None.
Source code in src/gitversioned/settings.py
resolve_path_from_src(path, enforce_existence=True)
¶
Resolve a path relative to the source root directory.
Example
::
path = settings.resolve_path_from_src("my_package/__init__.py")
:param path: The path to resolve. :param enforce_existence: Whether to enforce that the path exists. :return: The resolved absolute Path if it exists (or if not enforcing existence), otherwise None.
Source code in src/gitversioned/settings.py
settings_customise_sources(settings_cls, init_settings, env_settings, dotenv_settings, file_secret_settings)
classmethod
¶
Customize configuration sources and priority for loading settings.
This method overrides the default pydantic-settings loaders to resolve values in the following order: constructor kwargs, setup.cfg, pyproject.toml, dotenv files, environment variables, and CLI arguments.
:param settings_cls: The BaseSettings subclass being initialized. :param init_settings: Source loading constructor keyword arguments. :param env_settings: Source loading environment variables. :param dotenv_settings: Source loading .env files. :param file_secret_settings: Source loading file secrets. :return: A tuple of settings sources in priority order.
Source code in src/gitversioned/settings.py
configure_logger(settings=None, **default_overrides)
¶
Configure the global Loguru logger based on settings.
This function initializes or updates the active logger handler. If no settings are provided, it loads settings from environment variables. It enables interception of standard library log statements and configures formatting, sinks, filtering, and queue options.
Example
.. code-block:: python
from gitversioned.logging import LoggingSettings, configure_logger
configure_logger(
settings=LoggingSettings(level="INFO"),
clear_loggers=True
)
:param settings: Logging configurations, defaults to None (loads from environment). :param default_overrides: Parameter overrides merged into env settings if settings is None. :returns: None. :raises ImportError: Raised if OpenTelemetry formatting is enabled but the package is not installed.
Source code in src/gitversioned/logging.py
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 | |
resolve_version(settings, repository=None, environment=None)
¶
Resolve the dynamic version from configured sources and apply formatting.
Queries the repository sources to obtain a base version, determines the build type (e.g., release or dev), applies auto-increment increments if configured, and formats the final PEP 440 version.
Example
from gitversioned.settings import Settings version, v_type, ref = resolve_version(Settings())
:param settings: Configuration settings governing version resolution. :param repository: Optional Git repository instance to query. :param environment: Optional build environment parameters. :return: A tuple containing the final normalized Version, the version type, and the GitReference used for resolution.
Source code in src/gitversioned/versioning/entrypoints.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 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 | |
resolve_version_output(settings, repository=None, environment=None)
¶
Resolve the version and format the target output content.
Runs version resolution and then applies the configured output strategies to format the final string (e.g., for writing to a version file).
Example
from gitversioned.settings import Settings content, version, v_type, ref = resolve_version_output(Settings())
:param settings: Configuration settings governing output generation. :param repository: Optional Git repository instance to query. :param environment: Optional build environment parameters. :return: A tuple containing the formatted output content string, the resolved Version, the version type, and the GitReference.
Source code in src/gitversioned/versioning/entrypoints.py
resolve_version_output_to_stream(settings, repository=None, environment=None)
¶
Resolve the version and write formatted content to the configured output file path.
Resolves the dynamic version, formats it using the configured strategy, and writes the formatted content to the specified output file path. Creates parent directories for files if needed.
Example
from gitversioned.settings import Settings settings = Settings() res = resolve_version_output_to_stream(settings)
:param settings: Configuration settings governing output path and formatting. :param repository: Optional Git repository instance to query. :param environment: Optional build environment parameters. :return: A tuple containing the resolved output Path (or None if disabled), the formatted content, the Version, the version type, and the GitReference. :raises ValueError: If the configured output target cannot be written to.