-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_version.py
More file actions
97 lines (80 loc) · 3.62 KB
/
Copy pathget_version.py
File metadata and controls
97 lines (80 loc) · 3.62 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
93
94
95
96
import os
from version import Version
import subprocess as sp
"""
Ordre de priorité pour déterminer la prochaine version :
1. Si on est checkout sur un tag Git, utiliser ce tag comme version.
2. Si le nom de la branche Git est une version valide (ex: "1.2.3"), utiliser cette version, avec le commit actuel comme metadata.
3. Sinon, prendre le tag Git le plus récent, et utiliser le commit actuel comme metadata.
pour les cas 2 et 3, ajouter le nom de l'utilisateur Git (si configuré) comme metadata à la suite du commit (séparé par un .)
"""
def get_current_tag(project_dir: str) -> str|None:
os.chdir(project_dir)
try:
tag = sp.check_output(["git", "describe", "--tags", "--exact-match"], stderr=sp.DEVNULL).decode().strip()
return tag
except sp.CalledProcessError:
return None
def get_current_branch(project_dir: str) -> str:
os.chdir(project_dir)
branch = sp.check_output(["git", "branch", "--show-current"], stderr=sp.DEVNULL).decode().strip()
return branch
def get_last_commit(project_dir: str) -> str:
os.chdir(project_dir)
commit = sp.check_output(["git", "rev-parse", "HEAD"], stderr=sp.DEVNULL).decode().strip()
return commit
def get_latest_tag(project_dir: str) -> Version:
os.chdir(project_dir)
tags_list = sp.check_output(["git", "tag"], stderr=sp.DEVNULL).decode().strip().split("\n")
tags = [Version.from_string(t) for t in tags_list if t]
tags.sort(reverse=True)
if not tags:
raise ValueError("No tags found in the repository.")
return tags[0]
def get_git_username(project_dir: str) -> str|None:
os.chdir(project_dir)
username = sp.check_output(["git", "config", "user.name"], stderr=sp.DEVNULL).decode().strip()
if username == "":
return None
return username
def get_version(project_dir: str) -> Version:
# 1. Vérifier si on est sur un tag exact
current_tag = get_current_tag(project_dir)
if current_tag is not None:
return Version.from_string(current_tag)
# 2. Vérifier si le nom de la branche est une version valide
current_branch = get_current_branch(project_dir)
try:
branch_version = Version.from_string(current_branch)
commit = get_last_commit(project_dir)[:7] # On prend les 7 premiers caractères du commit
branch_version.prerelease = f"{commit}"
username = get_git_username(project_dir)
if username:
branch_version.metadata = username
return branch_version
except ValueError:
pass # Ce n'est pas une version valide, continuer
# 3. Prendre le dernier tag et ajouter le commit actuel comme metadata
try:
latest_tag = get_latest_tag(project_dir)
except ValueError as e:
latest_tag = Version(0,1,0)
commit = get_last_commit(project_dir)[:7] # On prend les 7 premiers caractères du commit
latest_tag.prerelease = f"{commit}"
username = get_git_username(project_dir)
if username:
latest_tag.metadata = username.lower().replace(" ", "")
return latest_tag
if __name__ == "__main__":
import argparse
argparser = argparse.ArgumentParser(description="Obtient la prochaine version basée sur la branche Git ou les tags.")
argparser.add_argument("project_dir", type=str, help="Chemin vers le répertoire du projet Git")
args = argparser.parse_args()
project_dir = args.project_dir
version = get_version(project_dir)
str_version = f"{version.major}.{version.minor}.{version.patch}"
if version.prerelease:
str_version += f"+{version.prerelease}"
if version.metadata:
str_version += f".{version.metadata}"
print(str_version)