Coverage for src/tools/git_foreach.py: 47.76%
67 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
13from typing import List
15# Modules libraries
16from argcomplete import autocomplete
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
27COMMANDS_DEFAULT: List[str] = [
28 'git',
29 'status',
30 '--short',
31 '--untracked-files',
32]
33HELP_POSITION: int = 23
35# Git foreach, pylint: disable=duplicate-code
36def git_foreach(
37 commands: List[str],
38 dry_run: bool = False,
39 quiet: bool = False,
40 recurse_submodules: bool = True,
41) -> int:
43 # Variables
44 repositories: List[str]
45 result: int = 0
47 # Tool header
48 if not quiet:
49 print(' ')
50 print(f'{Colors.GREEN} === {Tools.NAME} - Execute commands in Git repositories'
51 f'{Colors.RESET}')
52 Platform.flush()
54 # Detect repositories
55 repositories = Tools.git_repositories(submodules=recurse_submodules)
56 if not repositories:
57 if not quiet:
58 print(' ')
59 print(
60 f'{Colors.YELLOW} {Tools.NAME}:'
61 f'{Colors.RED} Could not detect Git repositories under current folder...'
62 f'{Colors.RESET}')
63 print(' ')
64 Platform.flush()
65 return -1
67 # Iterate over repositories
68 for repository in repositories:
70 # Execution header
71 if not quiet:
72 print(' ')
73 print(f'{Colors.BOLD} - Repository:'
74 f'{Colors.CYAN} {repository}'
75 f'{Colors.RESET}')
76 print(' ')
77 Platform.flush()
79 # Show commands
80 if not quiet or dry_run:
81 print(f' + {" ".join(commands)}')
82 Platform.flush()
84 # Run commands
85 if not dry_run:
86 result = Tools.run_command(
87 commands,
88 cwd=repository,
89 )
91 # Root footer
92 if not quiet:
93 print(' ')
94 Platform.flush()
96 # Result
97 return result
99# Main, pylint: disable=duplicate-code
100def main() -> None:
102 # Variables
103 group: _ArgumentGroup
104 result: int
106 # Configure tool
107 Tools.NAME = 'git foreach'
108 Tools.DESCRIPTION = 'Execute commands in each Git repositories under current path'
110 # Arguments creation
111 parser: ArgumentParser = ArgumentParser(
112 prog=Tools.NAME,
113 description=f'{Tools.NAME}: {Tools.DESCRIPTION} ({Bundle.NAME})',
114 add_help=False,
115 formatter_class=lambda prog: RawTextHelpFormatter(
116 prog,
117 max_help_position=HELP_POSITION,
118 width=min(
119 120,
120 get_terminal_size().columns - 2,
121 ),
122 ),
123 )
125 # Arguments tool definitions
126 group = parser.add_argument_group('tool arguments')
127 group.add_argument(
128 '-h',
129 '--help',
130 dest='help',
131 action='store_true',
132 help='Show this help message',
133 )
134 group.add_argument(
135 '-q',
136 '--quiet',
137 dest='quiet',
138 action='store_true',
139 help='Hide repositories context upon execution',
140 )
141 group.add_argument(
142 '-n',
143 '--dry-run',
144 dest='dry_run',
145 action='store_true',
146 help='Show commands usage without execution',
147 )
148 group.add_argument(
149 '-R',
150 '--no-recurse-submodules',
151 dest='no_recurse_submodules',
152 action='store_true',
153 help='Avoid recursing in repositories submodules',
154 )
155 group.add_argument(
156 'commands',
157 nargs='*',
158 default=COMMANDS_DEFAULT,
159 help=f'Command arguments to run (default: {" ".join(COMMANDS_DEFAULT)})',
160 )
162 # Arguments autocompletion
163 autocomplete(parser)
165 # Arguments parser
166 options: Namespace = parser.parse_args()
168 # Help informations
169 if options.help:
170 print(' ')
171 parser.print_help()
172 print(' ')
173 Platform.flush()
174 sys_exit(0)
176 # Instantiate settings
177 settings: Settings = Settings(name=Bundle.NAME)
179 # Prepare colors
180 Colors.prepare()
182 # Instantiate updates
183 updates: Updates = Updates(
184 name=Bundle.PACKAGE,
185 settings=settings,
186 )
188 # Run tool
189 result = git_foreach(
190 commands=options.commands,
191 dry_run=options.dry_run,
192 quiet=options.quiet,
193 recurse_submodules=not options.no_recurse_submodules,
194 )
196 # Check for daily updates
197 if updates.enabled and updates.daily:
198 updates.check()
200 # Result
201 sys_exit(0 if result >= 0 else result)
203# Entrypoint
204if __name__ == '__main__': # pragma: no cover
205 main()