|
| 1 | +# sync-versions.py |
| 2 | + |
| 3 | +Single source of truth for version management across DazzleTools projects. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +`sync-versions.py` reads version components from `_version.py` and propagates them to: |
| 8 | +- The `__version__` string (with git metadata: branch, build count, date, commit hash) |
| 9 | +- CHANGELOG.md compare links |
| 10 | + |
| 11 | +It replaces manual version editing. Git hooks call it automatically on every commit. |
| 12 | + |
| 13 | +## How It Works |
| 14 | + |
| 15 | +### The Version File (`_version.py`) |
| 16 | + |
| 17 | +Every project has a `_version.py` in its package directory. This is the canonical source: |
| 18 | + |
| 19 | +```python |
| 20 | +# Version components - edit these for version bumps |
| 21 | +MAJOR = 0 |
| 22 | +MINOR = 3 |
| 23 | +PATCH = 0 |
| 24 | +PHASE = "" # "" (stable), "alpha", "beta", "rc1" |
| 25 | +PROJECT_PHASE = "" # "prealpha", "alpha", "beta", "stable" |
| 26 | + |
| 27 | +# Auto-updated by git hooks - do not edit manually |
| 28 | +__version__ = "0.3.0_main_12-20260404-a1b2c3d4" |
| 29 | +__app_name__ = "my-project" |
| 30 | +``` |
| 31 | + |
| 32 | +**You edit:** `MAJOR`, `MINOR`, `PATCH`, `PHASE`, `PROJECT_PHASE` |
| 33 | +**Hooks update:** `__version__`, `PIP_VERSION`, `VERSION`, etc. |
| 34 | + |
| 35 | +### The `__version__` String Format |
| 36 | + |
| 37 | +``` |
| 38 | +MAJOR.MINOR.PATCH[-PHASE]_BRANCH_BUILD-YYYYMMDD-COMMITHASH |
| 39 | +``` |
| 40 | + |
| 41 | +Examples: |
| 42 | +- `0.3.0_main_12-20260404-a1b2c3d4` -- stable, 12th build on main |
| 43 | +- `0.3.0-alpha_dev_5-20260401-b2c3d4e5` -- alpha phase, 5th build on dev |
| 44 | + |
| 45 | +### Version Levels |
| 46 | + |
| 47 | +| Level | Scope | Changes when... | Example | |
| 48 | +|-------|-------|-----------------|---------| |
| 49 | +| `PHASE` | Per-MINOR feature set | Feature set matures: `"alpha"` -> `"beta"` -> `""` | `0.3.0-alpha` -> `0.3.0` | |
| 50 | +| `PROJECT_PHASE` | Entire project | Project hits maturity threshold (rare) | `PREALPHA 0.3.0` -> `BETA 0.5.0` | |
| 51 | + |
| 52 | +`PHASE` resets with each MINOR bump. `PROJECT_PHASE` is independent of version numbers. |
| 53 | + |
| 54 | +## Configuration |
| 55 | + |
| 56 | +In `pyproject.toml`: |
| 57 | + |
| 58 | +```toml |
| 59 | +[tool.repokit-common] |
| 60 | +version-source = "my_package/_version.py" |
| 61 | +changelog = "CHANGELOG.md" |
| 62 | +repo-url = "https://github.com/MyOrg/my-project" |
| 63 | +tag-prefix = "v" |
| 64 | +tag-format = "pep440" # or "human" |
| 65 | +private-patterns = ["private/", "local/", ".env"] |
| 66 | +``` |
| 67 | + |
| 68 | +### Tag Format |
| 69 | + |
| 70 | +| Setting | Tag example | PEP 440 | Use when | |
| 71 | +|---------|-------------|---------|----------| |
| 72 | +| `"pep440"` (default) | `v0.3.0a1` | `0.3.0a1` | Publishing to PyPI | |
| 73 | +| `"human"` | `v0.3.0-alpha` | N/A | Human-readable tags, not on PyPI | |
| 74 | + |
| 75 | +For stable releases (no phase), both produce identical tags: `v0.3.0`. |
| 76 | + |
| 77 | +## Usage |
| 78 | + |
| 79 | +### Check if versions are in sync |
| 80 | + |
| 81 | +```bash |
| 82 | +python scripts/sync-versions.py --check |
| 83 | +``` |
| 84 | + |
| 85 | +Reports `[OK]` or `[X]` for each managed file. Returns exit code 1 if out of sync. |
| 86 | + |
| 87 | +### Sync without changing version |
| 88 | + |
| 89 | +```bash |
| 90 | +python scripts/sync-versions.py |
| 91 | +``` |
| 92 | + |
| 93 | +Updates `__version__` with current git metadata (branch, build count, date, hash). Run this after manual edits to `_version.py`. |
| 94 | + |
| 95 | +### Bump version |
| 96 | + |
| 97 | +```bash |
| 98 | +python scripts/sync-versions.py --bump patch # 0.3.0 -> 0.3.1 |
| 99 | +python scripts/sync-versions.py --bump minor # 0.3.0 -> 0.4.0 |
| 100 | +python scripts/sync-versions.py --bump major # 0.3.0 -> 1.0.0 |
| 101 | +``` |
| 102 | + |
| 103 | +### Set version directly |
| 104 | + |
| 105 | +```bash |
| 106 | +python scripts/sync-versions.py --set 1.0.0 |
| 107 | +``` |
| 108 | + |
| 109 | +### Change phase |
| 110 | + |
| 111 | +```bash |
| 112 | +python scripts/sync-versions.py --phase alpha # add -alpha suffix |
| 113 | +python scripts/sync-versions.py --phase beta # add -beta suffix |
| 114 | +python scripts/sync-versions.py --phase none # clear phase (stable) |
| 115 | +``` |
| 116 | + |
| 117 | +### Demote version |
| 118 | + |
| 119 | +```bash |
| 120 | +python scripts/sync-versions.py --demote patch # 0.3.1 -> 0.3.0 |
| 121 | +``` |
| 122 | + |
| 123 | +### Dry run |
| 124 | + |
| 125 | +```bash |
| 126 | +python scripts/sync-versions.py --bump minor --dry-run |
| 127 | +``` |
| 128 | + |
| 129 | +Shows what would change without modifying any files. |
| 130 | + |
| 131 | +### Git hook mode |
| 132 | + |
| 133 | +```bash |
| 134 | +python scripts/sync-versions.py --auto |
| 135 | +``` |
| 136 | + |
| 137 | +Called by the pre-commit hook. Quiet mode, stages modified files, uses today's date. |
| 138 | + |
| 139 | +## PEP 440 Mapping |
| 140 | + |
| 141 | +The `get_pip_version()` function in `_version.py` converts to PEP 440 for PyPI: |
| 142 | + |
| 143 | +| Our format | PEP 440 | Notes | |
| 144 | +|------------|---------|-------| |
| 145 | +| `0.3.0` | `0.3.0` | Stable release | |
| 146 | +| `0.3.0-alpha` | `0.3.0a0` | Alpha pre-release | |
| 147 | +| `0.3.0-beta` | `0.3.0b0` | Beta pre-release | |
| 148 | +| `0.3.0-rc1` | `0.3.0rc1` | Release candidate | |
| 149 | +| `0.3.0` (on dev branch) | `0.3.0.dev5` | Dev build | |
| 150 | + |
| 151 | +## CHANGELOG Management |
| 152 | + |
| 153 | +`sync-versions.py` manages the compare links at the bottom of `CHANGELOG.md`: |
| 154 | + |
| 155 | +```markdown |
| 156 | +[Unreleased]: https://github.com/Org/repo/compare/v0.3.0...HEAD |
| 157 | +[0.3.0]: https://github.com/Org/repo/compare/v0.2.0...v0.3.0 |
| 158 | +[0.2.0]: https://github.com/Org/repo/releases/tag/v0.2.0 |
| 159 | +``` |
| 160 | + |
| 161 | +- `[Unreleased]` always points from the current tag to `HEAD` |
| 162 | +- Each version link compares from the previous tag |
| 163 | +- The first release uses `releases/tag/` format (no prior tag to compare) |
| 164 | + |
| 165 | +The script updates these links automatically. It does **not** modify section headers or content -- you write changelog entries manually. |
| 166 | + |
| 167 | +## Git Hooks Integration |
| 168 | + |
| 169 | +### pre-commit |
| 170 | + |
| 171 | +Runs `sync-versions.py --auto` to update `__version__` with the pending commit's metadata. Also: |
| 172 | +- Blocks private files from public branches |
| 173 | +- Blocks files > 10MB |
| 174 | + |
| 175 | +### post-commit |
| 176 | + |
| 177 | +Runs `sync-versions.py --auto` again to update the commit hash (which isn't known until after the commit). |
| 178 | + |
| 179 | +### pre-push |
| 180 | + |
| 181 | +Does **not** run sync-versions. Instead: |
| 182 | +- Validates Python syntax |
| 183 | +- Runs pytest |
| 184 | +- Checks for debug statements |
| 185 | + |
| 186 | +## Flags Reference |
| 187 | + |
| 188 | +| Flag | Description | |
| 189 | +|------|-------------| |
| 190 | +| `--check` | Verify sync status, exit 1 if out of sync | |
| 191 | +| `--bump PART` | Bump major, minor, or patch before syncing | |
| 192 | +| `--demote PART` | Demote major, minor, or patch | |
| 193 | +| `--set X.Y.Z` | Set version directly | |
| 194 | +| `--phase PHASE` | Set phase (alpha, beta, rc1, none) | |
| 195 | +| `--pre-num N` | Set PRE_RELEASE_NUM explicitly | |
| 196 | +| `--dry-run` | Show changes without modifying files | |
| 197 | +| `--auto` | Git hook mode (quiet, stages files) | |
| 198 | +| `--no-git-ver` | Skip `__version__` string update | |
| 199 | +| `--force`, `-f` | Skip confirmation prompts | |
| 200 | +| `--verbose`, `-v` | Show detailed output | |
0 commit comments