gitversioned.utils¶
Utility components and helpers for the gitversioned package.
Provides foundational utilities such as Git repository abstractions, environment metadata gathering, and Pydantic type coercions. Designed for consistent, typed, and testable interfaces across the core application logic.
Example
.. code-block:: python
from gitversioned.utils import BuildEnvironment, GitRepository
repo = GitRepository(".")
env = BuildEnvironment()
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
EnsureList
¶
Bases: list[TypeVarT], Generic[TypeVarT]
A list subclass integrating directly with Pydantic Core Schema.
Preprocesses inputs (such as comma-separated strings and nested iterables) and applies inner type coercion before final schema validation.
Example
from pydantic import BaseModel class MyModel(BaseModel): ... items: EnsureList[int] model = MyModel(items="1, 2, 3") model.items [1, 2, 3]
Source code in src/gitversioned/utils/pydantic.py
__get_pydantic_core_schema__(source_type, handler)
classmethod
¶
Create a schema that hooks a pre-validator into the Pydantic pipeline.
Extracts the inner type constraint and constructs a validator that runs prior to Pydantic's core schema validation.
:param source_type: The original type annotation. :param handler: The Pydantic core schema handler. :return: The constructed Pydantic core schema.
Source code in src/gitversioned/utils/pydantic.py
GitReference
¶
Bases: BaseModel
Pydantic model representing a Git reference (commit, tag, or branch).
Provides the core metadata fields representing a Git reference in a repository with details for tag, branch, or commit types.
Example¶
.. code-block:: python
from gitversioned.utils.git import GitReference
ref = GitReference(short_sha="a1b2c3d", distance_from_head=0)
print(ref.short_sha)
Source code in src/gitversioned/utils/git.py
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 | |
parse_git_references(data)
classmethod
¶
Extract branch and tag metadata from input dictionary ref strings.
This validator parses command output references to identify current branches and tags.
:param data: The input dictionary or raw data to validate. :return: The parsed and normalized dictionary.
Source code in src/gitversioned/utils/git.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
NotAGitRepositoryError
¶
Bases: Exception
Exception raised when a directory is not a valid Git repository.
This error is raised when Git operations are performed on a directory that is not inside a valid Git work tree.
Example¶
.. code-block:: python
try:
root = GitRepository("/tmp").root_directory
except NotAGitRepositoryError:
pass
Source code in src/gitversioned/utils/git.py
coerce_bool(value)
¶
Normalize truthy/falsy strings to actual booleans.
Example
coerce_bool("yes") True coerce_bool("0") False coerce_bool(5) 5
:param value: The value to coerce. :return: The boolean equivalent if recognized, otherwise the original value.
Source code in src/gitversioned/utils/pydantic.py
coerce_list(value, item_pre_coercer=None)
¶
Recursively transform input into a list.
Splits comma-separated strings and applies an optional pre-coercer function to individual items.
Example
coerce_list("a, b, c") ['a', 'b', 'c'] coerce_list("yes, no", coerce_bool) [True, False]
:param value: The value to coerce into a list. :param item_pre_coercer: Optional function to apply to each item. :return: A list of processed items.
Source code in src/gitversioned/utils/pydantic.py
coerce_path(value)
¶
Normalize string paths to Path objects.
Example
isinstance(coerce_path("/tmp/path "), Path) True
:param value: The value to coerce into a path. :return: A Path object if the input is a string, otherwise the original value.
Source code in src/gitversioned/utils/pydantic.py
get_ci_info()
¶
Determine if the current execution is within a recognized Continuous Integration environment.
Queries standard environment variables to identify platforms like GitHub Actions, GitLab CI, and others.
Example
is_ci, provider = get_ci_info() print(f"CI: {is_ci}, Provider: {provider}") CI: True, Provider: GitHub Actions
:return: Tuple indicating CI presence and provider name if found.
Source code in src/gitversioned/utils/environment.py
get_user()
¶
Retrieve the current system or environment user.
Attempts to use standard library OS queries first, falling back to common environment variables.
Example
get_user() 'markkurtz'
:return: The resolved username or "unknown" if undetermined.