Coverage for src/tools/git_fcp.py: 41.33%
75 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 fetch path and cherry-pick commits, pylint: disable=duplicate-code
30def git_fcp(
31 path: str,
32 commits: int = 1,
33 signoff: bool = False,
34) -> int:
36 # Variables
37 fetch_range: str
38 result: int
40 # Fetch header
41 print(' ')
42 print(f'{Colors.YELLOW} {Tools.NAME}:'
43 f'{Colors.GREEN} Fetching from path '
44 f'{Colors.CYAN}\'{path}\''
45 f'{Colors.RESET}')
46 Platform.flush()
48 # Fetch path
49 print(' ')
50 result = Tools.run_command([
51 'git',
52 'fetch',
53 path,
54 ])
55 Platform.flush()
56 if result != 0:
57 print(' ')
58 print(f'{Colors.YELLOW} {Tools.NAME}:'
59 f'{Colors.RED} Failed to fetch from path'
60 f'{Colors.RESET}')
61 print(' ')
62 Platform.flush()
63 return result
65 # Cherry-pick preparation
66 fetch_range = f'FETCH_HEAD~{commits}..FETCH_HEAD' if commits > 1 else 'FETCH_HEAD'
67 Tools.run_command(
68 [
69 'git',
70 'cherry-pick',
71 '--abort',
72 ],
73 stdout=False,
74 stderr=False,
75 )
77 # Cherry-pick header
78 print(' ')
79 print(f'{Colors.YELLOW} {Tools.NAME}:'
80 f'{Colors.GREEN} Cherry-picking'
81 f'{Colors.CYAN} \'{fetch_range}\''
82 f'{Colors.RESET}')
83 print(' ')
84 Platform.flush()
86 # Cherry-pick fetched commits
87 result = Tools.run_command([
88 'git',
89 'cherry-pick',
90 ] + (['--signoff'] if signoff else []) + [fetch_range])
91 Platform.flush()
92 if result != 0:
93 print(' ')
94 print(f'{Colors.YELLOW} {Tools.NAME}:'
95 f'{Colors.RED} Cherry-pick failed'
96 f'{Colors.RESET}')
97 Platform.flush()
99 # Footer
100 print(' ')
101 Platform.flush()
103 # Result
104 return result
106# Main, pylint: disable=duplicate-code
107def main() -> None:
109 # Variables
110 group: _ArgumentGroup
111 result: int
113 # Configure tool
114 Tools.NAME = 'git fcp'
115 Tools.DESCRIPTION = 'Git fetch path and cherry-pick commits'
117 # Arguments creation
118 parser: ArgumentParser = ArgumentParser(
119 prog=Tools.NAME,
120 description=f'{Tools.NAME}: {Tools.DESCRIPTION} ({Bundle.NAME})',
121 add_help=False,
122 formatter_class=lambda prog: RawTextHelpFormatter(
123 prog,
124 max_help_position=HELP_POSITION,
125 width=min(
126 120,
127 get_terminal_size().columns - 2,
128 ),
129 ),
130 )
132 # Arguments tool definitions
133 group = parser.add_argument_group('tool arguments')
134 group.add_argument(
135 '-h',
136 '--help',
137 dest='help',
138 action='store_true',
139 help='Show this help message',
140 )
141 group.add_argument(
142 '-s',
143 '--signoff',
144 dest='signoff',
145 action='store_true',
146 help='Add a Signed-off-by to the commit message',
147 )
148 group.add_argument(
149 '--validate',
150 dest='validate',
151 action='store_true',
152 help='Validate result by requesting user input',
153 )
154 group.add_argument(
155 'path',
156 nargs='?',
157 default=None,
158 help='Sources folder to fetch from',
159 )
160 group.add_argument(
161 'commits',
162 nargs='?',
163 type=int,
164 default=1,
165 help='Number of commits to cherry-pick (default: %(default)s)',
166 )
168 # Arguments autocompletion
169 autocomplete(parser)
171 # Arguments parser
172 options: Namespace = parser.parse_args()
174 # Help informations
175 if options.help:
176 print(' ')
177 parser.print_help()
178 print(' ')
179 Platform.flush()
180 sys_exit(0)
182 # Validate arguments
183 if options.path is None:
184 print(' ')
185 parser.print_help()
186 print(' ')
187 Platform.flush()
188 sys_exit(2)
190 # Instantiate settings
191 settings: Settings = Settings(name=Bundle.NAME)
193 # Prepare colors
194 Colors.prepare()
196 # Instantiate updates
197 updates: Updates = Updates(
198 name=Bundle.PACKAGE,
199 settings=settings,
200 )
202 # Run tool
203 result = git_fcp(
204 path=options.path,
205 commits=options.commits,
206 signoff=options.signoff,
207 )
209 # Check for daily updates
210 if updates.enabled and updates.daily:
211 updates.check()
213 # Validate result
214 if options.validate:
215 print(
216 f'{Colors.YELLOW} {Tools.NAME}:'
217 f'{Colors.GREEN if result >= 0 else Colors.RED} Press any key to continue...'
218 f'{Colors.RESET} [Enter]'
219 f'{Colors.RESET}',
220 end='',
221 )
222 questionary_press_any_key_to_continue(
223 message='',
224 style=None,
225 ).ask()
226 print(' ')
228 # Result
229 sys_exit(0 if result >= 0 else result)
231# Entrypoint
232if __name__ == '__main__': # pragma: no cover
233 main()