11use std:: {
2- process:: { Command , ExitStatus } ,
2+ process:: { Command , ExitStatus , Stdio } ,
33 string:: FromUtf8Error ,
44} ;
55
@@ -12,19 +12,25 @@ fn main() {
1212 }
1313 } ;
1414
15- println ! ( "cargo:rustc-env=GIT_HASH_EXPERIENCED={}" , commit_msg) ;
15+ println ! ( "cargo:: rustc-env=GIT_HASH_EXPERIENCED={}" , commit_msg) ;
1616}
1717
1818fn get_sha ( ) -> Result < String , Error > {
1919 let output = Command :: new ( "git" )
2020 . arg ( "rev-parse" )
2121 . arg ( "HEAD" )
22+ . stdout ( Stdio :: piped ( ) )
23+ . stderr ( Stdio :: piped ( ) )
24+ . stdin ( Stdio :: null ( ) )
2225 . spawn ( ) ?
2326 . wait_with_output ( ) ?;
2427 if !output. status . success ( ) {
2528 return Err ( Error :: BadStatus ( output. status ) ) ;
2629 }
2730 let output = String :: from_utf8 ( output. stdout ) ?. trim ( ) . to_string ( ) ;
31+ if output. is_empty ( ) {
32+ return Err ( Error :: NoOutput ) ;
33+ }
2834 Ok ( output)
2935}
3036
@@ -33,17 +39,19 @@ enum Error {
3339 TryFromString ,
3440 BadStatus ( ExitStatus ) ,
3541 Io ( std:: io:: Error ) ,
42+ NoOutput ,
3643}
3744
3845impl std:: fmt:: Display for Error {
3946 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
4047 match self {
41- Error :: TryFromString => write ! ( f, "Invalid UTF-8 in `git rev-parse HEAD` output" ) ?,
42- Error :: BadStatus ( exit_status) => write ! (
48+ Self :: TryFromString => write ! ( f, "Invalid UTF-8 in `git rev-parse HEAD` output" ) ?,
49+ Self :: BadStatus ( exit_status) => write ! (
4350 f,
4451 "`git rev-parse HEAD` exited with non-zero status {exit_status}"
4552 ) ?,
46- Error :: Io ( error) => write ! ( f, "I/O error trying to run `git rev-parse HEAD`: {error}" ) ?,
53+ Self :: Io ( error) => write ! ( f, "I/O error trying to run `git rev-parse HEAD`: {error}" ) ?,
54+ Self :: NoOutput => write ! ( f, "No output from git-rev-parse" ) ?,
4755 }
4856 Ok ( ( ) )
4957 }
0 commit comments