Coverage for src/tools/git_rb.py: 45.21%

73 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 .git_stat import git_stat 

26from .tools import Tools 

27 

28# Constants 

29HELP_POSITION: int = 23 

30 

31# Git rebase, pylint: disable=duplicate-code 

32def git_rb( 

33 remote: str, 

34 branch: str, 

35 local: 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 # Rebase header 

71 print(' ') 

72 if local: 

73 print(f'{Colors.GREEN} === {Tools.NAME} - Rebasing' 

74 f' local commits' 

75 f' against branch \'{branch}\'' 

76 f' from remote \'{remote}\' ===' 

77 f'{Colors.RESET}') 

78 else: 

79 print(f'{Colors.GREEN} === {Tools.NAME} - Rebasing' 

80 f' over branch \'{branch}\'' 

81 f' from remote \'{remote}\' ===' 

82 f'{Colors.RESET}') 

83 Platform.flush() 

84 

85 # Show git statistics 

86 commits_local, _ = git_stat( 

87 remote=remote, 

88 reference=branch, 

89 compare='HEAD', 

90 stats_only=False, 

91 ) 

92 

93 # Prepare rebase commands 

94 if local: 

95 commands = [ 

96 'git', 

97 'rebase', 

98 '-i', 

99 f'HEAD~{commits_local}', 

100 ] 

101 else: 

102 commands = [ 

103 'git', 

104 'rebase', 

105 'FETCH_HEAD', 

106 ] 

107 

108 # Rebase over remote branch 

109 result = Tools.run_command(commands) 

110 print(' ') 

111 Platform.flush() 

112 

113 # Result 

114 return result 

115 

116# Main, pylint: disable=duplicate-code 

117def main() -> None: 

118 

119 # Variables 

120 group: _ArgumentGroup 

121 result: int 

122 

123 # Configure tool 

124 Tools.NAME = 'git rb' 

125 Tools.DESCRIPTION = 'Git rebase with interactive selection' 

126 

127 # Arguments creation 

128 parser: ArgumentParser = ArgumentParser( 

129 prog=Tools.NAME, 

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

131 add_help=False, 

132 formatter_class=lambda prog: RawTextHelpFormatter( 

133 prog, 

134 max_help_position=HELP_POSITION, 

135 width=min( 

136 120, 

137 get_terminal_size().columns - 2, 

138 ), 

139 ), 

140 ) 

141 

142 # Arguments tool definitions 

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

144 group.add_argument( 

145 '-h', 

146 '--help', 

147 dest='help', 

148 action='store_true', 

149 help='Show this help message', 

150 ) 

151 group.add_argument( 

152 '--local', 

153 dest='local', 

154 action='store_true', 

155 help='Rebase only local commits against remote branch', 

156 ) 

157 group.add_argument( 

158 '--validate', 

159 dest='validate', 

160 action='store_true', 

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

162 ) 

163 group.add_argument( 

164 'remote', 

165 nargs='?', 

166 default=None, 

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

168 ) 

169 group.add_argument( 

170 'branch', 

171 nargs='?', 

172 default=None, 

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

174 ) 

175 

176 # Arguments autocompletion 

177 autocomplete(parser) 

178 

179 # Arguments parser 

180 options: Namespace = parser.parse_args() 

181 

182 # Help informations 

183 if options.help: 

184 print(' ') 

185 parser.print_help() 

186 print(' ') 

187 Platform.flush() 

188 sys_exit(0) 

189 

190 # Instantiate settings 

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

192 

193 # Prepare colors 

194 Colors.prepare() 

195 

196 # Instantiate updates 

197 updates: Updates = Updates( 

198 name=Bundle.PACKAGE, 

199 settings=settings, 

200 ) 

201 

202 # Run tool 

203 result = git_rb( 

204 remote=options.remote, 

205 branch=options.branch, 

206 local=options.local, 

207 ) 

208 

209 # Check for daily updates 

210 if updates.enabled and updates.daily: 

211 updates.check() 

212 

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(' ') 

227 

228 # Result 

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

230 

231# Entrypoint 

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

233 main()