11use crate :: Result ;
22use crate :: helpers:: { CmdOutput , CommandRunner } ;
3+ use anyhow:: { Context , bail} ;
34
45#[ cfg( test) ]
56use mockall:: { automock, predicate:: * } ;
@@ -24,8 +25,8 @@ impl<Cmd: CommandRunner> GitConfigTeamMemberRepo<Cmd> {
2425
2526 fn git_config_error < T > ( output : & CmdOutput ) -> Result < T > {
2627 match output. status_code {
27- Some ( code) => Err ( format ! ( "Git config command exited with status code: {code}" ) . into ( ) ) ,
28- None => Err ( "Git config command terminated by signal" . into ( ) ) ,
28+ Some ( code) => bail ! ( "Git config command exited with status code: {code}" ) ,
29+ None => bail ! ( "Git config command terminated by signal" ) ,
2930 }
3031 }
3132}
@@ -35,23 +36,28 @@ impl<Cmd: CommandRunner> TeamMemberRepo for GitConfigTeamMemberRepo<Cmd> {
3536 let section = Self :: COAUTHORS_SECTION ;
3637 let search_regex = format ! ( "^{section}\\ ." ) ;
3738
38- let output = self . command_runner . execute (
39- "git" ,
40- & [ "config" , "--global" , "--get-regexp" , & search_regex] ,
41- ) ?;
39+ let output = self
40+ . command_runner
41+ . execute (
42+ "git" ,
43+ & [ "config" , "--global" , "--get-regexp" , & search_regex] ,
44+ )
45+ . context ( "Failed to list team members from git config" ) ?;
4246
4347 match output. status_code {
44- Some ( Self :: EXIT_CODE_SUCCESS ) => String :: from_utf8 ( output. stdout ) ?
48+ Some ( Self :: EXIT_CODE_SUCCESS ) => String :: from_utf8 ( output. stdout )
49+ . context ( "Failed to parse git config output as UTF-8" ) ?
4550 . lines ( )
4651 . map ( |x| {
4752 let delimiter = if show_keys {
4853 format ! ( "{section}." )
4954 } else {
5055 " " . to_owned ( )
5156 } ;
52- x. split_once ( & delimiter)
53- . ok_or ( format ! ( "Failed to split string: '{x}'" ) . into ( ) )
54- . map ( |( _, team_member) | team_member. to_owned ( ) )
57+ let Some ( ( _, team_member) ) = x. split_once ( & delimiter) else {
58+ bail ! ( "Failed to split git config line: '{x}'" ) ;
59+ } ;
60+ Ok ( team_member. to_owned ( ) )
5561 } )
5662 . collect ( ) ,
5763
@@ -65,12 +71,16 @@ impl<Cmd: CommandRunner> TeamMemberRepo for GitConfigTeamMemberRepo<Cmd> {
6571
6672 let output = self
6773 . command_runner
68- . execute ( "git" , & [ "config" , "--global" , & full_key] ) ?;
74+ . execute ( "git" , & [ "config" , "--global" , & full_key] )
75+ . with_context ( || format ! ( "Failed to get team member '{key}' from git config" ) ) ?;
6976
7077 match output. status_code {
71- Some ( Self :: EXIT_CODE_SUCCESS ) => {
72- Ok ( Some ( String :: from_utf8 ( output. stdout ) ?. trim ( ) . into ( ) ) )
73- }
78+ Some ( Self :: EXIT_CODE_SUCCESS ) => Ok ( Some (
79+ String :: from_utf8 ( output. stdout )
80+ . context ( "Failed to parse git config output as UTF-8" ) ?
81+ . trim ( )
82+ . into ( ) ,
83+ ) ) ,
7484 Some ( Self :: EXIT_CODE_CONFIG_INVALID_KEY ) => Ok ( None ) ,
7585 _ => Self :: git_config_error ( & output) ,
7686 }
@@ -81,7 +91,8 @@ impl<Cmd: CommandRunner> TeamMemberRepo for GitConfigTeamMemberRepo<Cmd> {
8191
8292 let output = self
8393 . command_runner
84- . execute ( "git" , & [ "config" , "--global" , "--unset-all" , & full_key] ) ?;
94+ . execute ( "git" , & [ "config" , "--global" , "--unset-all" , & full_key] )
95+ . with_context ( || format ! ( "Failed to remove team member '{key}' from git config" ) ) ?;
8596
8697 match output. status_code {
8798 Some ( Self :: EXIT_CODE_SUCCESS ) => Ok ( ( ) ) ,
@@ -94,11 +105,14 @@ impl<Cmd: CommandRunner> TeamMemberRepo for GitConfigTeamMemberRepo<Cmd> {
94105
95106 let output = self
96107 . command_runner
97- . execute ( "git" , & [ "config" , "--global" , & full_key, team_member] ) ?;
108+ . execute ( "git" , & [ "config" , "--global" , & full_key, team_member] )
109+ . with_context ( || format ! ( "Failed to add team member '{key}' to git config" ) ) ?;
98110
99111 match output. status_code {
100112 Some ( Self :: EXIT_CODE_SUCCESS ) => Ok ( ( ) ) ,
101- Some ( Self :: EXIT_CODE_CONFIG_INVALID_KEY ) => Err ( format ! ( "Invalid key: {key}" ) . into ( ) ) ,
113+ Some ( Self :: EXIT_CODE_CONFIG_INVALID_KEY ) => {
114+ bail ! ( "Invalid key: {key}" )
115+ }
102116 _ => Self :: git_config_error ( & output) ,
103117 }
104118 }
0 commit comments