Concepts & Resolution¶
gitversioned provides an intelligent engine that calculates PEP 440 compliant version strings dynamically based on the state of your Git repository and project configuration. This document explains the underlying concepts of how gitversioned evaluates sources, determines the build environment, and applies auto-increments.
The Resolution Algorithm¶
When a build starts, gitversioned follows a strict evaluation order to determine the final version string.
- Source Evaluation: Inspect configured sources (tags, branches, commits, files) in priority order until a match is found.
- Environment Detection: Determine if the repository state implies a release, development, or pre-release build.
- Auto-Increment Calculation: If the current working tree is ahead of the matched source, bump the requested version segment (
major,minor, orpatch). - Template Application: Apply the resolved version, Git metadata, and environment variables into the final formatting template to produce the output string and file.
1. Source Evaluation Priority¶
The source_type configuration setting defines an ordered list of places to look for a version string. By default, source_type = ["auto"] expands to: ["file", "function", "tag", "branch", "commit"].
gitversioned iterates through this list. As soon as it extracts a valid version from one of these sources (using the corresponding regex_* configuration), the search stops.
Example Flow¶
If source_type = ["tag", "file"]:
gitversionedfirst checks the repository for Git tags matching theregex_tagpattern.- If it finds matches (e.g.,
v1.2.0), it selects the tag that is "closest" (has the fewest commits) to the currentHEAD. - If no matching tags exist (e.g., a fresh repository or a shallow clone without tags), it falls back to inspecting the file defined by
version_source_file. - Archive Fallback: If all configured sources fail to resolve a version (or if the
.gitdirectory is entirely absent, such as in a GitHub ZIP download),gitversionedwill attempt to parse a version and metadata from the file specified byversion_source_archive(defaulting to.git_archival.txt).
2. Environment Detection (version_type)¶
Once the base version and the corresponding Git reference are established, gitversioned calculates the version_type to dictate the suffix of the final version string.
If version_type is set to "auto" (the default), gitversioned inspects the repository state:
- Clean HEAD: If the current
HEADexactly matches the resolved Git reference (e.g., you checked out thev1.2.0tag directly) and there are no uncommitted changes, theversion_typeis resolved asrelease. This generates a clean PEP 440 version (e.g.,1.2.0). - Dirty or Ahead: If there are uncommitted changes, or if
HEADhas commits beyond the matched reference, theversion_typedefaults todev.
You can forcefully override this mechanism by setting version_type in your configuration to release, dev, pre, alpha, nightly, or post.
3. Auto-Increment Logic¶
When building development or nightly versions, the repository is often ahead of the base version. Re-using the same base version for continuous builds can lead to naming collisions in package registries.
The [tool.gitversioned.auto_increment] setting allows you to bump the base version before applying the suffix.
Mechanics¶
If the repository is ahead of the matched Git reference (distance_from_head > 0), the engine checks the auto-increment target for the active version_type.
Configuration Example:
Scenario:
- Base Version Resolved:
1.2.3 - Current state:
HEADis 5 commits ahead of the tag. Uncommitted changes exist. - Active
version_type:dev(Resolved automatically)
Evaluation:
- Since the distance is
> 0andversion_typeisdev, the target isminor. - The engine splits
1.2.3into its components. - It increments the minor segment and zeroes out all subsequent segments.
major:1minor:2 + 1 = 3patch:0- The new base version becomes
1.3.0.
Finally, the dev template is applied to 1.3.0, generating a string like 1.3.0.dev20260507+abc1234.
4. Templating and Output¶
After all version components are calculated, gitversioned passes a rich context dictionary into the formatting templates.
The context contains:
version: The resolvedpackaging.version.Versionobject.repo: Metadata about the Git repository (e.g.,dirty_files,current_branch).ref: Metadata about the matched reference (e.g.,commit_sha,author_name).config: The fullSettingsobject.env: The build environment (OS variables, build dates).
For details on how to write these templates and customize the output strings, see the Templates & Formatting Guide.