-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathtools.py
More file actions
65 lines (51 loc) · 1.61 KB
/
Copy pathtools.py
File metadata and controls
65 lines (51 loc) · 1.61 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
63
64
65
import os
import subprocess
from pathlib import Path
from git.repo import Repo
from semantic_version import Version
def _run_commitizen(repo_path: Path, *args: str) -> str:
result = subprocess.run(
["cz", *args],
cwd=repo_path,
check=True,
capture_output=True,
text=True,
)
return result.stdout.strip()
def get_current_version_from_git() -> Version:
repo = Repo(Path.cwd())
if repo.bare or repo.working_tree_dir is None:
raise ValueError("Not a valid Git repository with a working tree.")
repo_path = Path(repo.working_tree_dir)
current_version = _run_commitizen(repo_path, "version", "--project")
current_tag = _run_commitizen(repo_path, "version", "--project", "--tag")
commit_count = repo.git.rev_list(
"--count",
"--first-parent",
f"{current_tag}..HEAD",
)
if commit_count == "0":
return Version(current_version)
next_version = _run_commitizen(
repo_path,
"--no-raise",
"NO_INCREMENT",
"bump",
"--get-next",
"--devrelease",
commit_count,
"--yes",
)
if next_version:
return Version(next_version)
version = Version(current_version)
version.prerelease = ("dev", commit_count)
return version
def get_version() -> Version:
if "npm_package_version" in os.environ:
return Version(os.environ["npm_package_version"])
if "CZ_PRE_NEW_VERSION" in os.environ:
return Version(os.environ["CZ_PRE_NEW_VERSION"])
return get_current_version_from_git()
if __name__ == "__main__":
print(get_version())