Coverage for src/features/commitizen.py: 64.52%
62 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-08 13:49 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-08 13:49 +0000
1#!/usr/bin/env python3
3# Standard libraries
4from os import linesep
5from pathlib import Path
6from typing import List, Optional
8# Modules libraries
9from packaging.version import InvalidVersion, Version as PackageVersion
11# Components
12from ..system.platform import Platform
13from ..system.commands import Commands
14from .mise import Mise
16# Commitizen class
17class Commitizen:
19 # Constants
20 BINARY: str = 'cz'
21 CONFIGURATION_EXTENSION: str = '.yaml'
22 CONFIGURATION_FILE: str = '.cz.yaml'
23 MARKER: str = '# register-python-argcomplete cz'
24 PACKAGES: List[str] = ['commitizen']
25 # PACKAGES_MISE: str = 'pipx:commitizen'
26 PACKAGES_MISE: str = 'pipx:git+https://github.com/AdrianDC/commitizen.git'
27 SOURCES: str = 'git+https://github.com/AdrianDC/commitizen.git'
28 TITLE: str = 'commitizen'
29 VERSION_COMPATIBLE: str = '4.19.3+adriandc.20261108'
31 # Internals
32 __flags_configuration: List[str] = []
34 # Check
35 @staticmethod
36 def check(rev_range: str) -> bool:
38 # Check commitizen range
39 return Commands.run(
40 Commitizen.BINARY, Commitizen.__flags_configuration + [
41 '--no-raise',
42 '23',
43 'check',
44 '--rev-range',
45 rev_range,
46 ])
48 # Compatible
49 @staticmethod
50 def compatible() -> bool:
52 # Missing commitizen
53 if not Commitizen.exists():
54 return False
56 # Get commitizen version
57 version: str = Commands.output(Commitizen.BINARY, [
58 'version',
59 ])
61 # Parse commitizen version
62 try:
63 return PackageVersion(version) >= PackageVersion(
64 Commitizen.VERSION_COMPATIBLE)
65 except InvalidVersion:
66 return False
68 # Configure
69 @staticmethod
70 def configure() -> str:
72 # Variables
73 bashrc: Optional[Path] = Platform.bashrc()
75 # Configure commitizen in bashrc
76 if bashrc:
77 with open(bashrc, encoding='utf8', mode='a') as f:
78 f.write(f'{linesep}')
79 f.write(f'# register-python-argcomplete cz{linesep}')
80 f.write(f'eval "$(register-python-argcomplete cz)"{linesep}')
81 return str(bashrc)
83 # Fallback
84 return ''
86 # Configured
87 @staticmethod
88 def configured() -> bool:
90 # Variables
91 bashrc: Optional[Path] = Platform.bashrc()
93 # Missing bashrc
94 if not bashrc:
95 return False
97 # Result
98 return Commands.grep(
99 bashrc,
100 Commitizen.MARKER,
101 )
103 # Dependencies
104 @staticmethod
105 def dependencies() -> bool:
107 # Install commitizen (mise)
108 result_mise: Optional[bool] = Mise.dependencies(
109 Commitizen.BINARY,
110 Commitizen.PACKAGES_MISE,
111 Commitizen.VERSION_COMPATIBLE,
112 )
113 if result_mise is not None: # pragma: no cover
114 return result_mise
116 # Uninstall existing commitizen, pylint: disable=duplicate-code
117 if Commands.exists(Commitizen.BINARY):
118 for package in Commitizen.PACKAGES:
119 Commands.pip([
120 'uninstall',
121 package,
122 ])
123 print(' ')
125 # Install commitizen
126 return Commands.pip([
127 'install',
128 f'{Commitizen.SOURCES}@{Commitizen.VERSION_COMPATIBLE}',
129 ])
131 # Exists
132 @staticmethod
133 def exists() -> bool:
135 # Check if binary exists
136 return Commands.exists(Commitizen.BINARY)
138 # Set configuration
139 @staticmethod
140 def set_configuration(file: str) -> None:
142 # Set configuration flags
143 if file:
144 Commitizen.__flags_configuration = [
145 '--config',
146 file,
147 ]
148 else:
149 Commitizen.__flags_configuration = []