@@ -4,15 +4,25 @@ use std::{
44} ;
55
66fn main ( ) {
7- let commit_msg = match get_sha ( ) {
7+ let commit_sha = match get_sha ( ) {
88 Ok ( v) => v,
99 Err ( err) => {
1010 println ! ( "cargo::warning={err:?}" ) ;
11- "(Failed to get version )" . to_owned ( )
11+ "(Failed to get git commit sha )" . to_owned ( )
1212 }
1313 } ;
1414
15- println ! ( "cargo::rustc-env=GIT_HASH_EXPERIENCED={}" , commit_msg) ;
15+ let rev_num = match get_rev ( ) {
16+ Ok ( v) => v,
17+ Err ( err) => {
18+ println ! ( "cargo::warning={err:?}" ) ;
19+ "(Failed to get revision number)" . to_owned ( )
20+ }
21+ } ;
22+
23+ println ! ( "cargo::rustc-env=GIT_HASH_EXPERIENCED={}" , commit_sha) ;
24+ println ! ( "cargo::rustc-env=GIT_REV_COUNT_EXPERIENCED={}" , rev_num)
25+
1626}
1727
1828fn get_sha ( ) -> Result < String , Error > {
@@ -34,24 +44,43 @@ fn get_sha() -> Result<String, Error> {
3444 Ok ( output)
3545}
3646
47+ fn get_rev ( ) -> Result < String , Error > {
48+ let output = Command :: new ( "git" )
49+ . arg ( "rev-list" )
50+ . arg ( "--count" )
51+ . arg ( "HEAD" )
52+ . stdout ( Stdio :: piped ( ) )
53+ . stderr ( Stdio :: piped ( ) )
54+ . stdin ( Stdio :: null ( ) )
55+ . spawn ( ) ?
56+ . wait_with_output ( ) ?;
57+ if !output. status . success ( ) {
58+ return Err ( Error :: BadStatus ( output. status ) ) ;
59+ }
60+ let output = String :: from_utf8 ( output. stdout ) ?. trim ( ) . to_string ( ) ;
61+ Ok ( output)
62+ }
63+
3764#[ derive( Debug ) ]
3865enum Error {
3966 TryFromString ,
4067 BadStatus ( ExitStatus ) ,
4168 Io ( std:: io:: Error ) ,
69+ ParseInt ( std:: num:: ParseIntError ) ,
4270 NoOutput ,
4371}
4472
4573impl std:: fmt:: Display for Error {
4674 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
4775 match self {
48- Self :: TryFromString => write ! ( f, "Invalid UTF-8 in `git rev-parse HEAD ` output" ) ?,
76+ Self :: TryFromString => write ! ( f, "Invalid UTF-8 in `git` output" ) ?,
4977 Self :: BadStatus ( exit_status) => write ! (
5078 f,
51- "`git rev-parse HEAD ` exited with non-zero status {exit_status}"
79+ "`git` exited with non-zero status {exit_status}"
5280 ) ?,
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" ) ?,
81+ Self :: Io ( error) => write ! ( f, "I/O error trying to run `git`: {error}" ) ?,
82+ Self :: NoOutput => write ! ( f, "No output from `git`" ) ?,
83+ Self :: ParseInt ( error) => write ! ( f, "Could not convert to int: {error}" ) ?
5584 }
5685 Ok ( ( ) )
5786 }
@@ -74,3 +103,10 @@ impl From<std::io::Error> for Error {
74103 Self :: Io ( value)
75104 }
76105}
106+
107+
108+ impl From < std:: num:: ParseIntError > for Error {
109+ fn from ( value : std:: num:: ParseIntError ) -> Self {
110+ Self :: ParseInt ( value)
111+ }
112+ }
0 commit comments