-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathtools.py
More file actions
92 lines (66 loc) · 2.69 KB
/
Copy pathtools.py
File metadata and controls
92 lines (66 loc) · 2.69 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
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
import os
import re
from pathlib import Path
from typing import NamedTuple, Optional
from git.repo import Repo
from semantic_version import Version
class GitDescribeVersion(NamedTuple):
version: str
commits: Optional[str] = None
hash: Optional[str] = None
def determine_version_bump(repo_path="."):
try:
repo = Repo(repo_path)
if repo.bare:
raise ValueError("Not a valid Git repository.")
tags = sorted(repo.tags, key=lambda t: t.commit.committed_date, reverse=True)
version_tags = [tag for tag in tags if tag.name.startswith("v")]
if not version_tags:
raise ValueError("No version tags found.")
last_tag = version_tags[0]
commits = repo.iter_commits(f"{last_tag.name}..HEAD")
ignored_types = ["chore", "style", "refactor", "test"]
patch_types = ["fix", "docs", "perf"]
minor_change = False
patch_change = False
for commit in commits:
message = commit.message.strip()
if any(re.match(rf"^{ignored_type}(\([^\)]+\))?:", message) for ignored_type in ignored_types):
continue
if "BREAKING CHANGE:" in message or re.match(r"^feat(\([^\)]+\))?!:", message):
return "major"
if re.match(r"^feat(\([^\)]+\))?:", message):
minor_change = True
if any(re.match(rf"^{patch_types}(\([^\)]+\))?:", message) for ignored_type in ignored_types):
patch_change = True
if minor_change:
return "minor"
if patch_change:
return "patch"
return None
except Exception as e:
raise RuntimeError(f"Error: {e}")
def get_current_version_from_git() -> Version:
repo = Repo(Path.cwd())
git_version = GitDescribeVersion(
*repo.git.describe("--tag", "--long", "--first-parent", "--match", "v[0-9]*").rsplit("-", 2)
)
version = Version(git_version.version[1:])
if git_version.commits is not None and git_version.commits != "0":
next_version = determine_version_bump()
if next_version == "major":
version = version.next_major()
elif next_version == "minor":
version = version.next_minor()
elif next_version == "patch":
version = version.next_patch()
version.prerelease = ("dev", git_version.commits)
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())