Coverage for src/tools/git_cauthor.py: 44.93%

69 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, Optional 

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# Update commit authorship, pylint: disable=duplicate-code 

31def git_cauthor( 

32 from_commit: Optional[str] = None, 

33 no_verify: bool = False, 

34) -> int: 

35 

36 # Variables 

37 git_hooks: List[str] 

38 name: str 

39 email: str 

40 result: int 

41 

42 # Acquire author from commit 

43 if from_commit: 

44 name = Tools.run_output( 

45 [ 

46 'git', 

47 'log', 

48 '--format=%an', 

49 f'{from_commit}^!', 

50 ], 

51 stderr=False, 

52 ).strip() 

53 email = Tools.run_output( 

54 [ 

55 'git', 

56 'log', 

57 '--format=%ae', 

58 f'{from_commit}^!', 

59 ], 

60 stderr=False, 

61 ).strip() 

62 

63 # Acquire author from user 

64 else: 

65 name = Tools.run_output( 

66 [ 

67 'git', 

68 'config', 

69 '--get', 

70 'user.name', 

71 ], 

72 stderr=False, 

73 ).strip() 

74 email = Tools.run_output( 

75 [ 

76 'git', 

77 'config', 

78 '--get', 

79 'user.email', 

80 ], 

81 stderr=False, 

82 ).strip() 

83 

84 # Validate author 

85 if not name or not email: 

86 print(' ') 

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

88 f'{Colors.RED} Ownership details are incomplete or missing' 

89 f'{Colors.RESET}') 

90 print(' ') 

91 Platform.flush() 

92 return -1 

93 

94 # Authorship header 

95 print(' ') 

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

97 f'{Colors.GREEN} Amending commit with author \'' 

98 f'{Colors.CYAN}{name} <{email}>' 

99 f'{Colors.GREEN}\'' 

100 f'{Colors.RESET}') 

101 print(' ') 

102 Platform.flush() 

103 

104 # Authorship command 

105 commands = [ 

106 'git', 

107 'commit', 

108 '--allow-empty', 

109 '--amend', 

110 f'--author=\'{name} <{email}>\'', 

111 '--no-edit', 

112 '--quiet', 

113 ] 

114 

115 # Configure verify option 

116 if no_verify: 

117 commands += [ 

118 '--no-verify', 

119 ] 

120 

121 # Authorship update 

122 result = Tools.run_command( 

123 commands, 

124 env={ 

125 'GIT_COMMITTER_NAME': name, 

126 'GIT_COMMITTER_EMAIL': email, 

127 }, 

128 ) 

129 Platform.flush() 

130 

131 # Detect hooks 

132 git_hooks = Tools.git_hooks() 

133 if any(git_hook in git_hooks for git_hook in [ 

134 'pre-commit', 

135 'prepare-commit-msg', 

136 'commit-msg', 

137 ]): 

138 print(' ') 

139 

140 # Authorship display 

141 Tools.run_command([ 

142 'git', 

143 'show', 

144 '--pretty=fuller', 

145 '--stat', 

146 ]) 

147 print(' ') 

148 Platform.flush() 

149 

150 # Result 

151 return result 

152 

153# Main, pylint: disable=duplicate-code 

154def main() -> None: 

155 

156 # Variables 

157 group: _ArgumentGroup 

158 result: int 

159 

160 # Configure tool 

161 Tools.NAME = 'git cauthor' 

162 Tools.DESCRIPTION = 'Update commit authorship from current user or a commit' 

163 

164 # Arguments creation 

165 parser: ArgumentParser = ArgumentParser( 

166 prog=Tools.NAME, 

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

168 add_help=False, 

169 formatter_class=lambda prog: RawTextHelpFormatter( 

170 prog, 

171 max_help_position=HELP_POSITION, 

172 width=min( 

173 120, 

174 get_terminal_size().columns - 2, 

175 ), 

176 ), 

177 ) 

178 

179 # Arguments tool definitions 

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

181 group.add_argument( 

182 '-h', 

183 '--help', 

184 dest='help', 

185 action='store_true', 

186 help='Show this help message', 

187 ) 

188 group.add_argument( 

189 '--no-verify', 

190 dest='no_verify', 

191 action='store_true', 

192 help='Disable pre-commit \'pre-push\' hooks', 

193 ) 

194 group.add_argument( 

195 '--validate', 

196 dest='validate', 

197 action='store_true', 

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

199 ) 

200 group.add_argument( 

201 'from_commit', 

202 nargs='?', 

203 default=None, 

204 help='Get author from a commit instead of current user', 

205 ) 

206 

207 # Arguments autocompletion 

208 autocomplete(parser) 

209 

210 # Arguments parser 

211 options: Namespace = parser.parse_args() 

212 

213 # Help informations 

214 if options.help: 

215 print(' ') 

216 parser.print_help() 

217 print(' ') 

218 Platform.flush() 

219 sys_exit(0) 

220 

221 # Instantiate settings 

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

223 

224 # Prepare colors 

225 Colors.prepare() 

226 

227 # Instantiate updates 

228 updates: Updates = Updates( 

229 name=Bundle.PACKAGE, 

230 settings=settings, 

231 ) 

232 

233 # Run tool 

234 result = git_cauthor( 

235 from_commit=options.from_commit, 

236 no_verify=options.no_verify, 

237 ) 

238 

239 # Check for daily updates 

240 if updates.enabled and updates.daily: 

241 updates.check() 

242 

243 # Validate result 

244 if options.validate: 

245 print( 

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

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

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

249 f'{Colors.RESET}', 

250 end='', 

251 ) 

252 questionary_press_any_key_to_continue( 

253 message='', 

254 style=None, 

255 ).ask() 

256 print(' ') 

257 

258 # Result 

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

260 

261# Entrypoint 

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

263 main()