gitversioned.versioning.sources¶
Resolve project version from configured version sources.
This module provides functions to extract and resolve semantic versions from explicit settings, files, Python functions, Git metadata (tags, branches, commits), or archives. It processes these sources in a configurable priority order to find and return a valid PEP 440 version.
VersionResolutionError
¶
Bases: ValueError
Exception raised when version resolution fails for a source.
Indicates that a configured version source (file, function, git, or archive) failed to resolve to a valid semantic version. Used to distinguish resolution failures from general runtime errors.
Example
raise VersionResolutionError("No tag matches version pattern.")
Source code in src/gitversioned/versioning/sources.py
resolve_from_explicit_source(settings, repository)
¶
Resolve version from an explicit configuration version string.
Extracts a static version from settings, checking it against configured regex patterns. Rejects dynamic aliases like 'auto', 'dynamic', or '0.0.0'.
Example
from gitversioned.settings import Settings from gitversioned.utils import GitRepository settings = Settings(version="1.2.3") repo = GitRepository() version, ref = resolve_from_explicit_source(settings, repo)
:param settings: Configuration settings containing the version string. :param repository: Target Git repository. :return: Resolved version and the current Git commit or fallback reference. :raises VersionResolutionError: If the version is unset or is a dynamic alias.
Source code in src/gitversioned/versioning/sources.py
resolve_from_file_source(settings, repository)
¶
Resolve version by parsing a configured version source file.
Reads the path specified in settings, matches its contents against file regex patterns, and extracts the first matching version.
Example
from gitversioned.settings import Settings from gitversioned.utils import GitRepository settings = Settings(version_source_file="setup.cfg") repo = GitRepository() version, ref = resolve_from_file_source(settings, repo)
:param settings: Configuration settings specifying file path and regex. :param repository: Target Git repository. :return: Resolved version and current Git commit or fallback reference. :raises VersionResolutionError: If the file is missing, unreadable, or unmatched.
Source code in src/gitversioned/versioning/sources.py
resolve_from_function_source(settings, repository)
¶
Resolve version by executing a custom Python function.
Dynamically imports and calls a user-defined function in the format 'module:function', passing settings and repository as keyword arguments.
Example
from gitversioned.settings import Settings from gitversioned.utils import GitRepository settings = Settings(version_source_function="my_module:get_version") repo = GitRepository() version, ref = resolve_from_function_source(settings, repo)
:param settings: Configuration settings containing the function path. :param repository: Target Git repository. :return: Reconciled version and associated Git reference returned by the function. :raises VersionResolutionError: If the function path is invalid or execution fails. :raises ValueError: If the function returns an invalid version or reference type.
Source code in src/gitversioned/versioning/sources.py
resolve_from_git_source(type_, settings, repository)
¶
Resolve version from Git metadata (tags, branch name, or commit messages).
Extracts version candidates from the specified Git metadata type using regex patterns and selects the match closest to the HEAD commit.
Example
from gitversioned.settings import Settings from gitversioned.utils import GitRepository settings = Settings() repo = GitRepository() version, ref = resolve_from_git_source("tag", settings, repo)
:param type_: Git metadata category to query ('tag', 'branch', or 'commit'). :param settings: Configuration settings containing the regex patterns. :param repository: Target Git repository. :return: Resolved version and the closest matching Git reference. :raises VersionResolutionError: If Git is unavailable or no patterns match. :raises ValueError: If the metadata type is invalid.
Source code in src/gitversioned/versioning/sources.py
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 | |
resolve_sources(sources, settings, repository)
¶
Resolve project version by checking configured sources in order.
Iterates through the requested sources (e.g., 'file', 'tag', 'branch') and returns the first resolved version and Git reference. If 'auto' is specified, it expands to check all standard sources. Falls back to archive resolution if all listed sources fail.
Example
from gitversioned.settings import Settings from gitversioned.utils import GitRepository settings = Settings() repo = GitRepository() version, ref = resolve_sources(["tag", "file"], settings, repo)
:param sources: Priority list of source types to query. :param settings: Configuration settings instance. :param repository: Git repository wrapper. :return: Resolved version and associated Git reference. :raises VersionResolutionError: If no version is found in any source.
Source code in src/gitversioned/versioning/sources.py
resolve_sources_from_archive(sources, settings)
¶
Resolve version from a git-archive export description file.
Parses the archival export file, extracts Git metadata (tags, branches, commits) via regex, reconstructs a GitReference, and extracts versions matching the specified source types.
Example
from gitversioned.settings import Settings settings = Settings(version_source_archive=".git_archival.txt") version, ref = resolve_sources_from_archive(["tag"], settings)
:param sources: Source types to query from the reconstructed Git metadata. :param settings: Configuration settings. :return: Resolved version and reconstructed Git reference. :raises VersionResolutionError: If the archive file is missing, raw, or unmatched.
Source code in src/gitversioned/versioning/sources.py
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 | |