-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_version.py
More file actions
62 lines (51 loc) · 2.01 KB
/
Copy path_version.py
File metadata and controls
62 lines (51 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (C) 2025-2026 Shinji NAKAGAWA
"""Single source of truth for the application version.
Bump ``__version__`` on each release (alongside the matching ``vX.Y.Z`` git
tag). ``get_version()`` returns that number for installed copies, and enriches
it with a short dev suffix (commits-past-tag + commit hash, ``*`` if the tree is
dirty) when running from a git checkout so bug reports can pinpoint the build.
"""
from __future__ import annotations
import re
import subprocess
from pathlib import Path
__version__ = "1.9.1"
_REPO_DIR = Path(__file__).resolve().parent
def _git_describe() -> str | None:
"""Return ``git describe --tags --dirty`` output, or None if unavailable."""
if not (_REPO_DIR / ".git").exists():
return None
try:
out = subprocess.run(
["git", "describe", "--tags", "--always", "--dirty=*"],
cwd=_REPO_DIR,
capture_output=True,
text=True,
timeout=2,
)
except (OSError, subprocess.SubprocessError):
return None
if out.returncode != 0:
return None
described = out.stdout.strip()
return described or None
def get_version() -> str:
"""Return a human-readable version string for display (About dialog, CLI)."""
described = _git_describe()
if described is None:
return __version__
# "v1.7.0-3-g e75192d" (optionally "*"-suffixed when dirty) → ahead of tag.
m = re.match(
r"v?(?P<tag>.+?)-(?P<n>\d+)-g(?P<sha>[0-9a-f]+)(?P<dirty>\*?)$",
described,
)
if m:
return f"{m['tag']} (dev+{m['n']}, g{m['sha']}{m['dirty']})"
# Exact tag, optionally dirty: "v1.7.0" / "v1.7.0*".
m = re.match(r"v?(?P<tag>.+?)(?P<dirty>\*?)$", described)
if m and m["tag"] and not re.fullmatch(r"[0-9a-f]{7,}", m["tag"]):
dirty = " (dirty)" if m["dirty"] else ""
return f"{m['tag']}{dirty}"
# No tags reachable — bare commit hash from "--always".
return f"{__version__} (g{described})"