Coverage for src/tools/git_fe.py: 37.93%

87 statements  

« 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 

3 

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 

14 

15# Modules libraries 

16from argcomplete import autocomplete 

17from questionary import press_any_key_to_continue as questionary_press_any_key_to_continue 

18 

19# Components 

20from ..package.bundle import Bundle 

21from ..package.settings import Settings 

22from ..package.updates import Updates 

23from ..prints.colors import Colors 

24from ..system.platform import Platform 

25from .tools import Tools 

26 

27# Constants 

28HELP_POSITION: int = 23 

29 

30# Git fetch, pylint: disable=duplicate-code 

31def git_fe( 

32 remote: str, 

33 branch: str, 

34 tags: bool = False, 

35 reset: bool = False, 

36) -> int: 

37 

38 # Variables 

39 commands: List[str] 

40 result: int 

41 

42 # Select remote 

43 if not remote: 

44 remote = Tools.select_remote() 

45 

46 # Validate remote 

47 if not remote: 

48 print(' ') 

49 print(f'{Colors.YELLOW} {Tools.NAME}:' 

50 f'{Colors.RED} Remote selection missing...' 

51 f'{Colors.RESET}') 

52 print(' ') 

53 Platform.flush() 

54 return -1 

55 

56 # Select branch 

57 if not branch: 

58 branch = Tools.select_branch(remote) 

59 

60 # Validate branch 

61 if not branch: 

62 print(' ') 

63 print(f'{Colors.YELLOW} {Tools.NAME}:' 

64 f'{Colors.RED} Remote branch selection missing...' 

65 f'{Colors.RESET}') 

66 print(' ') 

67 Platform.flush() 

68 return -3 

69 

70 # Cleanup local tags 

71 if tags: 

72 

73 # Get existing tags 

74 tags_list = Tools.run_output([ 

75 'git', 

76 'tag', 

77 ]).strip().splitlines() 

78 

79 # Delete existing tags 

80 for tag_name in tags_list: 

81 Tools.run_command( 

82 [ 

83 'git', 

84 'tag', 

85 '-d', 

86 tag_name, 

87 ], 

88 stdout=False, 

89 ) 

90 

91 # Rebase header 

92 print(' ') 

93 print(f'{Colors.GREEN} === {Tools.NAME} - Fetching branch \'' 

94 f'{Colors.CYAN}{branch}' 

95 f'{Colors.GREEN}\' from remote \'' 

96 f'{Colors.CYAN}{remote}' 

97 f'{Colors.GREEN}\' ===' 

98 f'{Colors.RESET}') 

99 Platform.flush() 

100 

101 # Prepare fetch commands 

102 commands = [ 

103 'git', 

104 'fetch', 

105 ] 

106 if tags: 

107 commands += [ 

108 '--tags', 

109 ] 

110 commands += [ 

111 remote, 

112 branch, 

113 ] 

114 

115 # Fetch from remote branch 

116 print(' ') 

117 result = Tools.run_command(commands) 

118 Platform.flush() 

119 

120 # Reset mode 

121 if reset: 

122 

123 # Prepare reset commands 

124 commands = [ 

125 'git', 

126 'reset', 

127 '--hard', 

128 'FETCH_HEAD', 

129 ] 

130 

131 # Reset to fetched remote 

132 print(' ') 

133 result = Tools.run_command(commands) 

134 Platform.flush() 

135 

136 # Get commit description 

137 tag_describe = Tools.run_output([ 

138 'git', 

139 'describe', 

140 '--always', 

141 ]) 

142 

143 # Commit informations 

144 if reset: 

145 print(' ') 

146 print(f'{Colors.YELLOW} {Tools.NAME}:' 

147 f'{Colors.BOLD} Current HEAD is' 

148 f'{Colors.CYAN} {tag_describe}' 

149 f'{Colors.RESET}') 

150 Platform.flush() 

151 

152 # Footer 

153 print(' ') 

154 Platform.flush() 

155 

156 # Result 

157 return result 

158 

159# Main, pylint: disable=duplicate-code 

160def main() -> None: 

161 

162 # Variables 

163 group: _ArgumentGroup 

164 result: int 

165 

166 # Configure tool 

167 Tools.NAME = 'git fe' 

168 Tools.DESCRIPTION = 'Git fetch and reset sources with interactive selection' 

169 

170 # Arguments creation 

171 parser: ArgumentParser = ArgumentParser( 

172 prog=Tools.NAME, 

173 description=f'{Tools.NAME}: {Tools.DESCRIPTION} ({Bundle.NAME})', 

174 add_help=False, 

175 formatter_class=lambda prog: RawTextHelpFormatter( 

176 prog, 

177 max_help_position=HELP_POSITION, 

178 width=min( 

179 120, 

180 get_terminal_size().columns - 2, 

181 ), 

182 ), 

183 ) 

184 

185 # Arguments tool definitions 

186 group = parser.add_argument_group('tool arguments') 

187 group.add_argument( 

188 '-h', 

189 '--help', 

190 dest='help', 

191 action='store_true', 

192 help='Show this help message', 

193 ) 

194 group.add_argument( 

195 '--reset', 

196 dest='reset', 

197 action='store_true', 

198 help='Reset sources to fetched remote branch', 

199 ) 

200 group.add_argument( 

201 '--tags', 

202 dest='tags', 

203 action='store_true', 

204 help='Fetch and refresh all tags', 

205 ) 

206 group.add_argument( 

207 '--validate', 

208 dest='validate', 

209 action='store_true', 

210 help='Validate result by requesting user input', 

211 ) 

212 group.add_argument( 

213 'remote', 

214 nargs='?', 

215 default=None, 

216 help='Remote repository name (default: auto)', 

217 ) 

218 group.add_argument( 

219 'branch', 

220 nargs='?', 

221 default=None, 

222 help='Branch name (default: auto)', 

223 ) 

224 

225 # Arguments autocompletion 

226 autocomplete(parser) 

227 

228 # Arguments parser 

229 options: Namespace = parser.parse_args() 

230 

231 # Help informations 

232 if options.help: 

233 print(' ') 

234 parser.print_help() 

235 print(' ') 

236 Platform.flush() 

237 sys_exit(0) 

238 

239 # Instantiate settings 

240 settings: Settings = Settings(name=Bundle.NAME) 

241 

242 # Prepare colors 

243 Colors.prepare() 

244 

245 # Instantiate updates 

246 updates: Updates = Updates( 

247 name=Bundle.PACKAGE, 

248 settings=settings, 

249 ) 

250 

251 # Run tool 

252 result = git_fe( 

253 remote=options.remote, 

254 branch=options.branch, 

255 tags=options.tags, 

256 reset=options.reset, 

257 ) 

258 

259 # Check for daily updates 

260 if updates.enabled and updates.daily: 

261 updates.check() 

262 

263 # Validate result 

264 if options.validate: 

265 print( 

266 f'{Colors.YELLOW} {Tools.NAME}:' 

267 f'{Colors.GREEN if result >= 0 else Colors.RED} Press any key to continue...' 

268 f'{Colors.RESET} [Enter]' 

269 f'{Colors.RESET}', 

270 end='', 

271 ) 

272 questionary_press_any_key_to_continue( 

273 message='', 

274 style=None, 

275 ).ask() 

276 print(' ') 

277 

278 # Result 

279 sys_exit(0 if result >= 0 else result) 

280 

281# Entrypoint 

282if __name__ == '__main__': # pragma: no cover 

283 main()