gitversioned.plugins.hatchling_plugin¶
Hatchling version source plugin for GitVersioned.
This module provides the Hatchling plugin interface for GitVersioned, allowing Hatchling-based projects to resolve their dynamic package version directly from the repository's Git history and environment state. It acts as a bridge between Hatch's custom versioning hook framework and GitVersioned's core resolution and generation logic.
The main plugin implementation is defined in the GitVersionedVersionSource class, which is registered with Hatchling via the hatch_register_version_source entry point.
GitVersionedBuildHook
¶
Bases: BuildHookInterface
Hatchling build hook interface for GitVersioned.
Automatically adds the configured output version file to the build data artifacts list so Hatchling packages it even if it is git-ignored.
Source code in src/gitversioned/plugins/hatchling_plugin.py
get_settings_kwargs()
¶
Extract and prepare the configuration dictionary for GitVersioned
settings.
Source code in src/gitversioned/plugins/hatchling_plugin.py
initialize(version, build_data)
¶
Initialize the build hook, injecting the output version file to build
artifacts.
:param version: The version resolved for the build. :param build_data: The target build data dictionary.
Source code in src/gitversioned/plugins/hatchling_plugin.py
GitVersionedVersionSource
¶
Bases: VersionSourceInterface
Hatchling version source interface for GitVersioned.
This class implements the Hatchling VersionSourceInterface to resolve and manage project versions dynamically. It hooks into the Hatchling build lifecycle, retrieving version information from Git tags, commits, or environment variables and passing it to the package builder.
By extending Hatchling's standard interface, it integrates directly with tools like Hatch build or Hatch publish, enabling zero-configuration version resolution for end users.
.. code-block:: python
from gitversioned.plugins.hatchling_plugin import GitVersionedVersionSource
# Instantiate the plugin with project root and Hatch config
source = GitVersionedVersionSource(root="/path/to/project", config={})
version_info = source.get_version_data()
:cvar PLUGIN_NAME: The registered name of this plugin within the Hatchling build system
Source code in src/gitversioned/plugins/hatchling_plugin.py
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 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 | |
__init__(root, config)
¶
Initialize the GitVersioned version source plugin with project details.
:param root: The project root directory path :param config: The plugin configuration dictionary from Hatchling
Source code in src/gitversioned/plugins/hatchling_plugin.py
get_package_name()
¶
Retrieve and normalize the package name from project metadata.
Extracts the project name from the Hatchling project configuration and replaces hyphens with underscores for Python package compatibility.
.. code-block:: python
pkg_name = source.get_package_name()
:returns: The normalized Python package name
Source code in src/gitversioned/plugins/hatchling_plugin.py
get_project_root()
¶
Resolve the absolute path to the project root directory.
.. code-block:: python
root_path = source.get_project_root()
:returns: The resolved absolute path to the project root
Source code in src/gitversioned/plugins/hatchling_plugin.py
get_settings_kwargs()
¶
Extract and prepare the configuration dictionary for GitVersioned settings.
Gathers the project metadata (root directory, package name, source root) and combines it with Hatch plugin-specific configurations to build keyword arguments.
.. code-block:: python
kwargs = source.get_settings_kwargs()
:returns: A dictionary of configuration options compatible with GitVersioned settings
Source code in src/gitversioned/plugins/hatchling_plugin.py
get_src_root()
¶
Determine the source root directory for the project.
Resolves the directory containing the source package by checking the explicit src_root configuration, Hatchling build target package paths, or falling back to directory layout conventions.
.. code-block:: python
src_root = source.get_src_root()
:returns: The resolved Path to the source root directory
Source code in src/gitversioned/plugins/hatchling_plugin.py
get_version_data()
¶
Compute the project version based on Git state and configuration.
Resolves the version using the Git repository, build environment, and plugin configuration, and optionally writes the resolved version to a generated file if configured.
.. code-block:: python
version_data = source.get_version_data()
version_str = version_data["version"]
:returns: A dictionary containing the resolved version string mapped to the 'version' key :raises ValueError: If the version resolution process fails or cannot find a valid Git state
Source code in src/gitversioned/plugins/hatchling_plugin.py
set_version(version, version_data)
¶
Set the project version manually, updating the version source file.
This handler is invoked when writing a version via Hatch CLI (e.g.,
hatch version <version>), persisting the version string to the
configured file.
.. code-block:: python
source.set_version(version="1.2.3", version_data={})
:param version: The raw version string to set :param version_data: Additional version context provided by Hatchling
Source code in src/gitversioned/plugins/hatchling_plugin.py
hatch_register_build_hook()
¶
Register the GitVersioned build hook plugin with the Hatchling build system.
Provides the entry point hook for Hatchling to locate and instantiate the custom GitVersionedBuildHook build hook implementation.
:returns: The GitVersionedBuildHook class implementing BuildHookInterface
Source code in src/gitversioned/plugins/hatchling_plugin.py
hatch_register_version_source()
¶
Register the GitVersioned version source plugin with the Hatchling build system.
Provides the entry point hook for Hatchling to locate and instantiate the custom GitVersionedVersionSource version source implementation.
.. code-block:: python
from gitversioned.plugins.hatchling_plugin import hatch_register_version_source
plugin_type = hatch_register_version_source()
:returns: The GitVersionedVersionSource class implementing VersionSourceInterface