version
version
¶
Version and branch information for terok.
This module provides a single source of truth for version and branch information, used by both the CLI (--version) and TUI (title bar).
installed_dist_version(timeout=5.0)
¶
Return the terok version currently installed on disk, or None.
The running process froze its version (terok.__version__) at
import time; a pip/pipx upgrade performed while the TUI is
open only changes what is on disk. Asking a fresh interpreter
reads the current dist-info, so comparing the two detects a stale
running instance. Returns None when the probe fails for any reason
(package not installed, interpreter gone mid-upgrade, timeout).
Source code in src/terok/lib/core/version.py
get_version_info()
¶
Get version and branch information.
This function implements a multi-layered strategy to determine version and branch information across different installation/execution contexts.
DESIGN RATIONALE:¶
terok can be run in several ways, and we want to show the branch name only when it's meaningful to the user:
-
DEVELOPMENT MODE (git checkout): Run directly via
uv run terokfrom a git working directory. -> Show branch name (via live git detection) unless on a tagged release. -
INSTALLED FROM PyPI (official release): Standard
pip install terokfrom PyPI. -> Show version only. No branch info available or meaningful. -
INSTALLED FROM VCS URL:
pip install git+https://...orpipx install git+https://.... -> Show requested revision (branch/tag/commit) from PEP 610 metadata. -
INSTALLED FROM LOCAL PATH / RELEASE TARBALL:
pip install /path/to/terokorpip install terok-X.Y.Z.tar.gz. -> Show version only. Branch info is not available/meaningful.
IMPLEMENTATION:¶
The branch detection uses three strategies with a priority order:
STRATEGY 1 - PEP 610 metadata (for VCS installs): When installed from a VCS URL, pip records PEP 610 metadata in direct_url.json. If present, we use requested_revision (or commit_id) for display, without mutating any source files.
STRATEGY 2 - Live git detection (for development mode): When running from source (detected by presence of pyproject.toml), query git directly for the current branch. Check for tagged releases and suppress the branch name if HEAD is at a vX.Y.Z tag.
VERSION DETECTION
- Primary: Import version from the installed terok package
- Fallback: Read from pyproject.toml (development mode only)
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple[str, str | None]
|
(version_string, branch_name) where branch_name is None for releases or when branch info is not available/meaningful |
Source code in src/terok/lib/core/version.py
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 | |
base_version(version)
¶
Extract the base X.Y.Z segment from a PEP 440 version string.
Strips .post, .dev, +local and any other suffixes so that
only the release triple remains.
Examples::
>>> base_version("0.4.0")
'0.4.0'
>>> base_version("0.4.0.post3.dev0+gabcdef")
'0.4.0'
>>> base_version("1.2.3rc1")
'1.2.3'
Source code in src/terok/lib/core/version.py
short_version(version)
¶
Return a human-friendly short version for display.
Keeps at most four dot-separated segments (X.Y.Z.SUFFIX) and
drops the +local git-hash segment. Anything past the first
suffix — e.g. a version backend's redundant .dev0 tacked onto
a .postN — is dropped. No hard-coded
knowledge of which suffixes are meaningful, just "first thing
after the release triple, if there is one."
Examples::
>>> short_version("0.4.0")
'0.4.0'
>>> short_version("0.7.4.post4.dev0+549a07a")
'0.7.4.post4'
>>> short_version("1.0.0.dev1")
'1.0.0.dev1'
>>> short_version("1.2.3rc1")
'1.2.3rc1'
Source code in src/terok/lib/core/version.py
format_version_string(version, branch)
¶
Format version and branch into a display string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version
|
str
|
The version string (e.g., "0.3.1") |
required |
branch
|
str | None
|
The branch name or None |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted string like "0.3.1" or "0.3.1 [feature-branch]" |