Coverage for src/tools/git_stat.py: 38.10%
84 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-07 23:52 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-07 23:52 +0000
1#!/usr/bin/env python3
2# PYTHON_ARGCOMPLETE_OK
4# Standard libraries
5from argparse import (
6 _ArgumentGroup,
7 ArgumentParser,
8 Namespace,
9 RawTextHelpFormatter,
10)
11from shutil import get_terminal_size
12from sys import exit as sys_exit
14# Modules libraries
15from argcomplete import autocomplete
16from questionary import press_any_key_to_continue as questionary_press_any_key_to_continue
18# Components
19from ..package.bundle import Bundle
20from ..package.settings import Settings
21from ..package.updates import Updates
22from ..prints.colors import Colors
23from ..system.platform import Platform
24from .tools import Tools
26# Constants
27HELP_POSITION: int = 23
29# Git statistics, pylint: disable=duplicate-code
30def git_stat(
31 remote: str,
32 reference: str,
33 compare: str = 'HEAD',
34 stats_only: bool = False,
35) -> tuple[int, int]:
37 # Select remote
38 if not remote:
39 remote = Tools.select_remote()
41 # Validate remote
42 if not remote:
43 print(' ')
44 print(f'{Colors.YELLOW} {Tools.NAME}:'
45 f'{Colors.RED} Remote selection missing...'
46 f'{Colors.RESET}')
47 print(' ')
48 Platform.flush()
49 return -1, -1
51 # Select branch
52 if not reference:
53 reference = Tools.select_branch(remote)
55 # Validate branch
56 if not reference:
57 print(' ')
58 print(f'{Colors.YELLOW} {Tools.NAME}:'
59 f'{Colors.RED} Remote branch selection missing...'
60 f'{Colors.RESET}')
61 print(' ')
62 Platform.flush()
63 return -2, -2
65 # Fetch remote branch
66 print(' ')
67 print(f'{Colors.YELLOW} {Tools.NAME}:'
68 f'{Colors.RESET} git fetch {remote} {reference};'
69 f'{Colors.RESET} git rev-list --left-right {compare}...FETCH_HEAD'
70 f'{Colors.RESET}')
71 print(' ')
72 Platform.flush()
73 if not Tools.git_fetch(remote, reference):
74 print(' ')
75 print(f'{Colors.YELLOW} {Tools.NAME}:'
76 f'{Colors.RED} Remote branch "{reference}" was not found...'
77 f'{Colors.RESET}')
78 print(' ')
79 Platform.flush()
80 return -3, -3
82 # Show differences
83 print(' ')
84 if stats_only:
85 if Tools.run_command([
86 'git',
87 '--no-pager',
88 'diff',
89 '--exit-code',
90 '--stat',
91 'FETCH_HEAD',
92 f'{compare}',
93 ]) != 0:
94 print(' ')
95 Platform.flush()
96 else:
97 if Tools.run_command([
98 'git',
99 '--no-pager',
100 'diff',
101 '--exit-code',
102 f'FETCH_HEAD...{compare}',
103 ]) != 0:
104 print(' ')
105 Platform.flush()
107 # Show comparison stats
108 rev_list = Tools.run_output([
109 'git',
110 'rev-list',
111 '--left-right',
112 f'FETCH_HEAD...{compare}',
113 ])
114 commits_local = sum(1 for line in rev_list.splitlines() if line.startswith('>'))
115 commits_remote = sum(1 for line in rev_list.splitlines() if line.startswith('<'))
116 print(f'{Colors.YELLOW} {Tools.NAME}:'
117 f'{Colors.BOLD} Local commits:'
118 f'{Colors.YELLOW} {commits_local} '
119 f'{Colors.BOLD}| Upstream commits:'
120 f'{Colors.YELLOW} {commits_remote}'
121 f'{Colors.RESET}')
122 print(' ')
123 Platform.flush()
125 # Result
126 return commits_local, commits_remote
128# Main, pylint: disable=duplicate-code
129def main() -> None:
131 # Variables
132 group: _ArgumentGroup
133 commits_remote: int
135 # Configure tool
136 Tools.NAME = 'git stat'
137 Tools.DESCRIPTION = 'Git history with remote comparator'
139 # Arguments creation
140 parser: ArgumentParser = ArgumentParser(
141 prog=Tools.NAME,
142 description=f'{Tools.NAME}: {Tools.DESCRIPTION} ({Bundle.NAME})',
143 add_help=False,
144 formatter_class=lambda prog: RawTextHelpFormatter(
145 prog,
146 max_help_position=HELP_POSITION,
147 width=min(
148 120,
149 get_terminal_size().columns - 2,
150 ),
151 ),
152 )
154 # Arguments tool definitions
155 group = parser.add_argument_group('tool arguments')
156 group.add_argument(
157 '-h',
158 '--help',
159 dest='help',
160 action='store_true',
161 help='Show this help message',
162 )
163 group.add_argument(
164 '-r',
165 '--ref',
166 dest='ref',
167 action='store',
168 default='HEAD',
169 help='Reference to push (default: %(default)s)',
170 )
171 group.add_argument(
172 '--stats-only',
173 dest='stats_only',
174 action='store_true',
175 help='Show only stats of the differences',
176 )
177 group.add_argument(
178 '--validate',
179 dest='validate',
180 action='store_true',
181 help='Validate result by requesting user input',
182 )
183 group.add_argument(
184 'remote',
185 nargs='?',
186 default=None,
187 help='Remote repository name (default: auto)',
188 )
189 group.add_argument(
190 'branch',
191 nargs='?',
192 default=None,
193 help='Branch name (default: auto)',
194 )
196 # Arguments autocompletion
197 autocomplete(parser)
199 # Arguments parser
200 options: Namespace = parser.parse_args()
202 # Help informations
203 if options.help:
204 print(' ')
205 parser.print_help()
206 print(' ')
207 Platform.flush()
208 sys_exit(0)
210 # Instantiate settings
211 settings: Settings = Settings(name=Bundle.NAME)
213 # Prepare colors
214 Colors.prepare()
216 # Instantiate updates
217 updates: Updates = Updates(
218 name=Bundle.PACKAGE,
219 settings=settings,
220 )
222 # Run tool
223 _, commits_remote = git_stat(
224 remote=options.remote,
225 reference=options.branch,
226 compare=options.ref,
227 stats_only=options.stats_only,
228 )
230 # Check for daily updates
231 if updates.enabled and updates.daily:
232 updates.check()
234 # Validate result
235 if options.validate:
236 print(
237 f'{Colors.YELLOW} {Tools.NAME}:'
238 f'{Colors.GREEN if commits_remote >= 0 else Colors.RED} Press any key to continue...'
239 f'{Colors.RESET} [Enter]'
240 f'{Colors.RESET}',
241 end='',
242 )
243 questionary_press_any_key_to_continue(
244 message='',
245 style=None,
246 ).ask()
247 print(' ')
249 # Result
250 sys_exit(0 if commits_remote >= 0 else commits_remote)
252# Entrypoint
253if __name__ == '__main__': # pragma: no cover
254 main()