11use crate :: Result ;
22use crate :: helpers:: { CmdOutput , CommandRunner } ;
3+ use anyhow:: { Context , anyhow, 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,10 +36,13 @@ 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 {
4448 Some ( Self :: EXIT_CODE_SUCCESS ) => String :: from_utf8 ( output. stdout ) ?
@@ -50,7 +54,7 @@ impl<Cmd: CommandRunner> TeamMemberRepo for GitConfigTeamMemberRepo<Cmd> {
5054 " " . to_owned ( )
5155 } ;
5256 x. split_once ( & delimiter)
53- . ok_or ( format ! ( "Failed to split string: '{x}'" ) . into ( ) )
57+ . ok_or_else ( || anyhow ! ( "Failed to split string: '{x}'" ) )
5458 . map ( |( _, team_member) | team_member. to_owned ( ) )
5559 } )
5660 . collect ( ) ,
@@ -65,12 +69,16 @@ impl<Cmd: CommandRunner> TeamMemberRepo for GitConfigTeamMemberRepo<Cmd> {
6569
6670 let output = self
6771 . command_runner
68- . execute ( "git" , & [ "config" , "--global" , & full_key] ) ?;
72+ . execute ( "git" , & [ "config" , "--global" , & full_key] )
73+ . with_context ( || format ! ( "Failed to get team member '{key}' from git config" ) ) ?;
6974
7075 match output. status_code {
71- Some ( Self :: EXIT_CODE_SUCCESS ) => {
72- Ok ( Some ( String :: from_utf8 ( output. stdout ) ?. trim ( ) . into ( ) ) )
73- }
76+ Some ( Self :: EXIT_CODE_SUCCESS ) => Ok ( Some (
77+ String :: from_utf8 ( output. stdout )
78+ . context ( "Failed to parse git config output as UTF-8" ) ?
79+ . trim ( )
80+ . into ( ) ,
81+ ) ) ,
7482 Some ( Self :: EXIT_CODE_CONFIG_INVALID_KEY ) => Ok ( None ) ,
7583 _ => Self :: git_config_error ( & output) ,
7684 }
@@ -81,7 +89,8 @@ impl<Cmd: CommandRunner> TeamMemberRepo for GitConfigTeamMemberRepo<Cmd> {
8189
8290 let output = self
8391 . command_runner
84- . execute ( "git" , & [ "config" , "--global" , "--unset-all" , & full_key] ) ?;
92+ . execute ( "git" , & [ "config" , "--global" , "--unset-all" , & full_key] )
93+ . with_context ( || format ! ( "Failed to remove team member '{key}' from git config" ) ) ?;
8594
8695 match output. status_code {
8796 Some ( Self :: EXIT_CODE_SUCCESS ) => Ok ( ( ) ) ,
@@ -94,11 +103,14 @@ impl<Cmd: CommandRunner> TeamMemberRepo for GitConfigTeamMemberRepo<Cmd> {
94103
95104 let output = self
96105 . command_runner
97- . execute ( "git" , & [ "config" , "--global" , & full_key, team_member] ) ?;
106+ . execute ( "git" , & [ "config" , "--global" , & full_key, team_member] )
107+ . with_context ( || format ! ( "Failed to add team member '{key}' to git config" ) ) ?;
98108
99109 match output. status_code {
100110 Some ( Self :: EXIT_CODE_SUCCESS ) => Ok ( ( ) ) ,
101- Some ( Self :: EXIT_CODE_CONFIG_INVALID_KEY ) => Err ( format ! ( "Invalid key: {key}" ) . into ( ) ) ,
111+ Some ( Self :: EXIT_CODE_CONFIG_INVALID_KEY ) => {
112+ bail ! ( "Invalid key: {key}" )
113+ }
102114 _ => Self :: git_config_error ( & output) ,
103115 }
104116 }
0 commit comments